Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 1 | /* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de> |
| 2 | * All Rights Reserved |
| 3 | * |
| 4 | * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU Affero General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU Affero General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 23 | #include <inttypes.h> |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 24 | |
| 25 | #include <osmocom/core/application.h> |
| 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/core/logging.h> |
| 28 | |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 29 | #include <osmocom/crypt/auth.h> |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 30 | |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 31 | #include "logging.h" |
| 32 | #include "auc.h" |
| 33 | |
| 34 | #define comment_start() fprintf(stderr, "\n===== %s\n", __func__); |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 35 | #define comment_end() fprintf(stderr, "===== %s: SUCCESS\n\n", __func__); |
| 36 | |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 37 | #define VERBOSE_ASSERT(val, expect_op, fmt) \ |
| 38 | do { \ |
| 39 | fprintf(stderr, #val " == " fmt "\n", (val)); \ |
| 40 | OSMO_ASSERT((val) expect_op); \ |
| 41 | } while (0); |
| 42 | |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 43 | char *vec_str(const struct osmo_auth_vector *vec) |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 44 | { |
| 45 | static char buf[1024]; |
| 46 | char *pos = buf; |
| 47 | char *end = buf + sizeof(buf); |
| 48 | |
| 49 | #define append(what) \ |
| 50 | if (pos >= end) \ |
| 51 | return buf; \ |
| 52 | pos += snprintf(pos, sizeof(buf) - (pos - buf), \ |
| 53 | " " #what ": %s\n", \ |
| 54 | osmo_hexdump_nospc((void*)&vec->what, sizeof(vec->what))) |
| 55 | |
| 56 | append(rand); |
| 57 | append(autn); |
| 58 | append(ck); |
| 59 | append(ik); |
| 60 | append(res); |
| 61 | append(res_len); |
| 62 | append(kc); |
| 63 | append(sres); |
| 64 | append(auth_types); |
| 65 | #undef append |
| 66 | |
| 67 | return buf; |
| 68 | } |
| 69 | |
| 70 | #define VEC_IS(vec, expect) do { \ |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 71 | char *_is = vec_str(vec); \ |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 72 | fprintf(stderr, "auth vector ==\n%s\n", _is); \ |
| 73 | if (strcmp(_is, expect)) { \ |
| 74 | fprintf(stderr, "MISMATCH! expected ==\n%s\n", \ |
| 75 | expect); \ |
| 76 | char *a = _is; \ |
| 77 | char *b = expect; \ |
| 78 | for (; *a && *b; a++, b++) { \ |
| 79 | if (*a != *b) { \ |
| 80 | while (a > _is && *(a-1) != '\n') a--; \ |
| 81 | fprintf(stderr, "mismatch at %d:\n" \ |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 82 | "%s", (int)(a - _is), a); \ |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 83 | break; \ |
| 84 | } \ |
| 85 | } \ |
| 86 | OSMO_ASSERT(false); \ |
| 87 | } \ |
| 88 | } while (0) |
| 89 | |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 90 | uint8_t fake_rand[16] = { 0 }; |
Neels Hofmeyr | 3aa3c10 | 2017-02-21 22:48:35 +0100 | [diff] [blame^] | 91 | bool fake_rand_fixed = true; |
| 92 | |
| 93 | void next_rand(const char *hexstr, bool fixed) |
| 94 | { |
| 95 | osmo_hexparse(hexstr, fake_rand, sizeof(fake_rand)); |
| 96 | fake_rand_fixed = fixed; |
| 97 | } |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 98 | |
| 99 | int rand_get(uint8_t *rand, unsigned int len) |
| 100 | { |
Neels Hofmeyr | 3aa3c10 | 2017-02-21 22:48:35 +0100 | [diff] [blame^] | 101 | int i; |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 102 | OSMO_ASSERT(len <= sizeof(fake_rand)); |
| 103 | memcpy(rand, fake_rand, len); |
Neels Hofmeyr | 3aa3c10 | 2017-02-21 22:48:35 +0100 | [diff] [blame^] | 104 | if (!fake_rand_fixed) { |
| 105 | for (i = 0; i < len; i++) |
| 106 | fake_rand[i] += 0x11; |
| 107 | } |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 108 | return len; |
| 109 | } |
| 110 | |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 111 | static void test_gen_vectors_2g_only(void) |
| 112 | { |
| 113 | struct osmo_sub_auth_data aud2g; |
| 114 | struct osmo_sub_auth_data aud3g; |
| 115 | struct osmo_auth_vector vec; |
| 116 | int rc; |
| 117 | |
| 118 | comment_start(); |
| 119 | |
| 120 | aud2g = (struct osmo_sub_auth_data){ |
| 121 | .type = OSMO_AUTH_TYPE_GSM, |
| 122 | .algo = OSMO_AUTH_ALG_COMP128v1, |
| 123 | }; |
| 124 | |
| 125 | osmo_hexparse("EB215756028D60E3275E613320AEC880", |
| 126 | aud2g.u.gsm.ki, sizeof(aud2g.u.gsm.ki)); |
| 127 | |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 128 | aud3g = (struct osmo_sub_auth_data){ 0 }; |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 129 | |
Neels Hofmeyr | 3aa3c10 | 2017-02-21 22:48:35 +0100 | [diff] [blame^] | 130 | next_rand("39fa2f4e3d523d8619a73b4f65c3e14d", true); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 131 | |
| 132 | vec = (struct osmo_auth_vector){ {0} }; |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 133 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 134 | rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL); |
| 135 | VERBOSE_ASSERT(rc, == 1, "%d"); |
| 136 | |
| 137 | VEC_IS(&vec, |
| 138 | " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n" |
| 139 | " autn: 00000000000000000000000000000000\n" |
| 140 | " ck: 00000000000000000000000000000000\n" |
| 141 | " ik: 00000000000000000000000000000000\n" |
| 142 | " res: 00000000000000000000000000000000\n" |
| 143 | " res_len: 00\n" |
| 144 | " kc: 241a5b16aeb8e400\n" |
| 145 | " sres: 429d5b27\n" |
| 146 | " auth_types: 01000000\n" |
| 147 | ); |
| 148 | |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 149 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 150 | |
| 151 | /* even though vec is not zero-initialized, it should produce the same |
| 152 | * result (regardless of the umts sequence nr) */ |
| 153 | aud3g.u.umts.sqn = 123; |
| 154 | rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL); |
| 155 | VERBOSE_ASSERT(rc, == 1, "%d"); |
| 156 | |
| 157 | VEC_IS(&vec, |
| 158 | " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n" |
| 159 | " autn: 00000000000000000000000000000000\n" |
| 160 | " ck: 00000000000000000000000000000000\n" |
| 161 | " ik: 00000000000000000000000000000000\n" |
| 162 | " res: 00000000000000000000000000000000\n" |
| 163 | " res_len: 00\n" |
| 164 | " kc: 241a5b16aeb8e400\n" |
| 165 | " sres: 429d5b27\n" |
| 166 | " auth_types: 01000000\n" |
| 167 | ); |
| 168 | |
| 169 | comment_end(); |
| 170 | } |
| 171 | |
| 172 | static void test_gen_vectors_2g_plus_3g(void) |
| 173 | { |
| 174 | struct osmo_sub_auth_data aud2g; |
| 175 | struct osmo_sub_auth_data aud3g; |
| 176 | struct osmo_auth_vector vec; |
| 177 | int rc; |
| 178 | |
| 179 | comment_start(); |
| 180 | |
| 181 | aud2g = (struct osmo_sub_auth_data){ |
| 182 | .type = OSMO_AUTH_TYPE_GSM, |
| 183 | .algo = OSMO_AUTH_ALG_COMP128v1, |
| 184 | }; |
| 185 | |
| 186 | osmo_hexparse("EB215756028D60E3275E613320AEC880", |
| 187 | aud2g.u.gsm.ki, sizeof(aud2g.u.gsm.ki)); |
| 188 | |
| 189 | aud3g = (struct osmo_sub_auth_data){ |
| 190 | .type = OSMO_AUTH_TYPE_UMTS, |
| 191 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 192 | }; |
| 193 | |
| 194 | osmo_hexparse("EB215756028D60E3275E613320AEC880", |
| 195 | aud3g.u.umts.k, sizeof(aud3g.u.umts.k)); |
| 196 | osmo_hexparse("FB2A3D1B360F599ABAB99DB8669F8308", |
| 197 | aud3g.u.umts.opc, sizeof(aud3g.u.umts.opc)); |
Neels Hofmeyr | 3aa3c10 | 2017-02-21 22:48:35 +0100 | [diff] [blame^] | 198 | next_rand("39fa2f4e3d523d8619a73b4f65c3e14d", true); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 199 | |
| 200 | vec = (struct osmo_auth_vector){ {0} }; |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 201 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 202 | rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL); |
| 203 | VERBOSE_ASSERT(rc, == 1, "%d"); |
| 204 | |
| 205 | VEC_IS(&vec, |
| 206 | " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n" |
| 207 | " autn: 8704f5ba55f30000d2ee44b22c8ea919\n" |
| 208 | " ck: f64735036e5871319c679f4742a75ea1\n" |
| 209 | " ik: 27497388b6cb044648f396aa155b95ef\n" |
| 210 | " res: e229c19e791f2e410000000000000000\n" |
| 211 | " res_len: 08\n" |
| 212 | " kc: 241a5b16aeb8e400\n" |
| 213 | " sres: 429d5b27\n" |
| 214 | " auth_types: 03000000\n" |
| 215 | ); |
| 216 | |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 217 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 218 | |
| 219 | /* even though vec is not zero-initialized, it should produce the same |
| 220 | * result with the same sequence nr */ |
| 221 | aud3g.u.umts.sqn = 0; |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 222 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 223 | rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL); |
| 224 | VERBOSE_ASSERT(rc, == 1, "%d"); |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 225 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 226 | |
| 227 | VEC_IS(&vec, |
| 228 | " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n" |
| 229 | " autn: 8704f5ba55f30000d2ee44b22c8ea919\n" |
| 230 | " ck: f64735036e5871319c679f4742a75ea1\n" |
| 231 | " ik: 27497388b6cb044648f396aa155b95ef\n" |
| 232 | " res: e229c19e791f2e410000000000000000\n" |
| 233 | " res_len: 08\n" |
| 234 | " kc: 241a5b16aeb8e400\n" |
| 235 | " sres: 429d5b27\n" |
| 236 | " auth_types: 03000000\n" |
| 237 | ); |
| 238 | |
| 239 | comment_end(); |
| 240 | } |
| 241 | |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 242 | static void test_gen_vectors_3g_only(void) |
| 243 | { |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 244 | struct osmo_sub_auth_data aud2g; |
| 245 | struct osmo_sub_auth_data aud3g; |
| 246 | struct osmo_auth_vector vec; |
Neels Hofmeyr | ec9036b | 2017-02-21 21:56:11 +0100 | [diff] [blame] | 247 | uint8_t auts[14]; |
| 248 | uint8_t rand_auts[16]; |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 249 | int rc; |
| 250 | |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 251 | comment_start(); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 252 | |
| 253 | aud2g = (struct osmo_sub_auth_data){ 0 }; |
| 254 | |
| 255 | aud3g = (struct osmo_sub_auth_data){ |
| 256 | .type = OSMO_AUTH_TYPE_UMTS, |
| 257 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 258 | }; |
| 259 | |
| 260 | osmo_hexparse("EB215756028D60E3275E613320AEC880", |
| 261 | aud3g.u.umts.k, sizeof(aud3g.u.umts.k)); |
| 262 | osmo_hexparse("FB2A3D1B360F599ABAB99DB8669F8308", |
| 263 | aud3g.u.umts.opc, sizeof(aud3g.u.umts.opc)); |
Neels Hofmeyr | 3aa3c10 | 2017-02-21 22:48:35 +0100 | [diff] [blame^] | 264 | next_rand("39fa2f4e3d523d8619a73b4f65c3e14d", true); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 265 | |
| 266 | vec = (struct osmo_auth_vector){ {0} }; |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 267 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 268 | rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL); |
| 269 | VERBOSE_ASSERT(rc, == 1, "%d"); |
| 270 | |
| 271 | VEC_IS(&vec, |
| 272 | " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n" |
| 273 | " autn: 8704f5ba55f30000d2ee44b22c8ea919\n" |
| 274 | " ck: f64735036e5871319c679f4742a75ea1\n" |
| 275 | " ik: 27497388b6cb044648f396aa155b95ef\n" |
| 276 | " res: e229c19e791f2e410000000000000000\n" |
| 277 | " res_len: 08\n" |
| 278 | " kc: 059a4f668f6fbe39\n" |
| 279 | " sres: 9b36efdf\n" |
| 280 | " auth_types: 03000000\n" |
| 281 | ); |
| 282 | |
| 283 | /* Note: 3GPP TS 33.102 6.8.1.2: c3 function to get GSM auth is |
| 284 | * KC[0..7] == CK[0..7] ^ CK[8..15] ^ IK[0..7] ^ IK[8..15] |
| 285 | * In [16]: hex( 0xf64735036e587131 |
| 286 | * ^ 0x9c679f4742a75ea1 |
| 287 | * ^ 0x27497388b6cb0446 |
| 288 | * ^ 0x48f396aa155b95ef) |
| 289 | * Out[16]: '0x59a4f668f6fbe39L' |
| 290 | * hence expecting kc: 059a4f668f6fbe39 |
| 291 | */ |
| 292 | |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 293 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 294 | |
| 295 | /* even though vec is not zero-initialized, it should produce the same |
| 296 | * result with the same sequence nr */ |
| 297 | aud3g.u.umts.sqn = 0; |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 298 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 299 | rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL); |
| 300 | VERBOSE_ASSERT(rc, == 1, "%d"); |
Neels Hofmeyr | 6b883f7 | 2017-01-31 16:40:28 +0100 | [diff] [blame] | 301 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64); |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 302 | |
| 303 | VEC_IS(&vec, |
| 304 | " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n" |
| 305 | " autn: 8704f5ba55f30000d2ee44b22c8ea919\n" |
| 306 | " ck: f64735036e5871319c679f4742a75ea1\n" |
| 307 | " ik: 27497388b6cb044648f396aa155b95ef\n" |
| 308 | " res: e229c19e791f2e410000000000000000\n" |
| 309 | " res_len: 08\n" |
| 310 | " kc: 059a4f668f6fbe39\n" |
| 311 | " sres: 9b36efdf\n" |
| 312 | " auth_types: 03000000\n" |
| 313 | ); |
| 314 | |
Neels Hofmeyr | ec9036b | 2017-02-21 21:56:11 +0100 | [diff] [blame] | 315 | |
| 316 | fprintf(stderr, "- test AUTS resync\n"); |
| 317 | vec = (struct osmo_auth_vector){}; |
| 318 | aud3g.u.umts.sqn = 0; |
| 319 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64); |
| 320 | |
| 321 | /* The AUTN sent was 8704f5ba55f30000d2ee44b22c8ea919 |
| 322 | * with the first 6 bytes being SQN ^ AK. |
| 323 | * K = EB215756028D60E3275E613320AEC880 |
| 324 | * OPC = FB2A3D1B360F599ABAB99DB8669F8308 |
| 325 | * RAND = 39fa2f4e3d523d8619a73b4f65c3e14d |
| 326 | * --milenage-f5--> |
| 327 | * AK = 8704f5ba55f3 |
| 328 | * |
| 329 | * The first six bytes are 8704f5ba55f3, |
| 330 | * and 8704f5ba55f3 ^ AK = 0. |
| 331 | * --> SQN = 0. |
| 332 | * |
| 333 | * Say the USIM doesn't like that, let's say it is at SQN 23. |
| 334 | * SQN_MS = 000000000017 |
| 335 | * |
| 336 | * AUTS = Conc(SQN_MS) || MAC-S |
| 337 | * Conc(SQN_MS) = SQN_MS ⊕ f5*[K](RAND) |
| 338 | * MAC-S = f1*[K] (SQN MS || RAND || AMF) |
| 339 | * |
| 340 | * f5*--> Conc(SQN_MS) = 000000000017 ^ 979498b1f73a |
| 341 | * = 979498b1f72d |
| 342 | * AMF = 0000 (TS 33.102 v7.0.0, 6.3.3) |
| 343 | * |
| 344 | * MAC-S = f1*[K] (000000000017 || 39fa2f4e3d523d8619a73b4f65c3e14d || 0000) |
| 345 | * = 3e28c59fa2e72f9c |
| 346 | * |
| 347 | * AUTS = 979498b1f72d || 3e28c59fa2e72f9c |
| 348 | * |
| 349 | * verify valid AUTS resulting in SQN 23 with: |
| 350 | * osmo-auc-gen -3 -a milenage -k EB215756028D60E3275E613320AEC880 \ |
| 351 | * -o FB2A3D1B360F599ABAB99DB8669F8308 \ |
| 352 | * -r 39fa2f4e3d523d8619a73b4f65c3e14d \ |
| 353 | * -A 979498b1f72d3e28c59fa2e72f9c |
| 354 | */ |
| 355 | |
| 356 | /* AUTS response by USIM */ |
| 357 | osmo_hexparse("979498b1f72d3e28c59fa2e72f9c", |
| 358 | auts, sizeof(auts)); |
| 359 | /* RAND sent to USIM, which AUTS was generated from */ |
| 360 | osmo_hexparse("39fa2f4e3d523d8619a73b4f65c3e14d", |
| 361 | rand_auts, sizeof(rand_auts)); |
| 362 | /* new RAND token for the next key */ |
Neels Hofmeyr | 3aa3c10 | 2017-02-21 22:48:35 +0100 | [diff] [blame^] | 363 | next_rand("897210a0f7de278f0b8213098e098a3f", true); |
Neels Hofmeyr | ec9036b | 2017-02-21 21:56:11 +0100 | [diff] [blame] | 364 | rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, rand_auts, auts); |
| 365 | VERBOSE_ASSERT(rc, == 1, "%d"); |
| 366 | /* The USIM's last sqn was 23, the calculated vector was 24, hence the |
| 367 | * next one stored should be 25. */ |
| 368 | VERBOSE_ASSERT(aud3g.u.umts.sqn, == 25, "%"PRIu64); |
| 369 | |
| 370 | VEC_IS(&vec, |
| 371 | " rand: 897210a0f7de278f0b8213098e098a3f\n" |
| 372 | " autn: c6b9790dad4b00000cf322869ea6a481\n" |
| 373 | " ck: e9922bd036718ed9e40bd1d02c3b81a5\n" |
| 374 | " ik: f19c20ca863137f8892326d959ec5e01\n" |
| 375 | " res: 9af5a557902d2db80000000000000000\n" |
| 376 | " res_len: 08\n" |
| 377 | " kc: 7526fc13c5976685\n" |
| 378 | " sres: 0ad888ef\n" |
| 379 | " auth_types: 03000000\n" |
| 380 | ); |
| 381 | |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 382 | comment_end(); |
| 383 | } |
| 384 | |
Neels Hofmeyr | 569d322 | 2017-02-21 22:57:11 +0100 | [diff] [blame] | 385 | void test_gen_vectors_bad_args() |
| 386 | { |
| 387 | struct osmo_auth_vector vec; |
| 388 | uint8_t auts[14]; |
| 389 | uint8_t rand_auts[16]; |
| 390 | int rc; |
| 391 | int i; |
| 392 | |
| 393 | struct osmo_sub_auth_data aud2g = { |
| 394 | .type = OSMO_AUTH_TYPE_GSM, |
| 395 | .algo = OSMO_AUTH_ALG_COMP128v1, |
| 396 | }; |
| 397 | |
| 398 | struct osmo_sub_auth_data aud3g = { |
| 399 | .type = OSMO_AUTH_TYPE_UMTS, |
| 400 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 401 | }; |
| 402 | |
| 403 | struct osmo_sub_auth_data aud2g_noalg = { |
| 404 | .type = OSMO_AUTH_TYPE_GSM, |
| 405 | .algo = OSMO_AUTH_ALG_NONE, |
| 406 | }; |
| 407 | |
| 408 | struct osmo_sub_auth_data aud3g_noalg = { |
| 409 | .type = OSMO_AUTH_TYPE_UMTS, |
| 410 | .algo = OSMO_AUTH_ALG_NONE, |
| 411 | }; |
| 412 | |
| 413 | struct osmo_sub_auth_data aud_notype = { |
| 414 | .type = OSMO_AUTH_TYPE_NONE, |
| 415 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 416 | }; |
| 417 | |
| 418 | struct osmo_sub_auth_data no_aud = { |
| 419 | .type = OSMO_AUTH_TYPE_NONE, |
| 420 | .algo = OSMO_AUTH_ALG_NONE, |
| 421 | }; |
| 422 | |
| 423 | struct { |
| 424 | struct osmo_sub_auth_data *aud2g; |
| 425 | struct osmo_sub_auth_data *aud3g; |
| 426 | uint8_t *rand_auts; |
| 427 | uint8_t *auts; |
| 428 | const char *label; |
| 429 | } tests[] = { |
| 430 | { NULL, NULL, NULL, NULL, "no auth data (a)"}, |
| 431 | { NULL, &aud3g_noalg, NULL, NULL, "no auth data (b)"}, |
| 432 | { NULL, &aud_notype, NULL, NULL, "no auth data (c)"}, |
| 433 | { NULL, &no_aud, NULL, NULL, "no auth data (d)"}, |
| 434 | { &aud2g_noalg, NULL, NULL, NULL, "no auth data (e)"}, |
| 435 | { &aud2g_noalg, &aud3g_noalg, NULL, NULL, "no auth data (f)"}, |
| 436 | { &aud2g_noalg, &aud_notype, NULL, NULL, "no auth data (g)"}, |
| 437 | { &aud2g_noalg, &no_aud, NULL, NULL, "no auth data (h)"}, |
| 438 | { &aud_notype, NULL, NULL, NULL, "no auth data (i)"}, |
| 439 | { &aud_notype, &aud3g_noalg, NULL, NULL, "no auth data (j)"}, |
| 440 | { &aud_notype, &aud_notype, NULL, NULL, "no auth data (k)"}, |
| 441 | { &aud_notype, &no_aud, NULL, NULL, "no auth data (l)"}, |
| 442 | { &no_aud, NULL, NULL, NULL, "no auth data (m)"}, |
| 443 | { &no_aud, &aud3g_noalg, NULL, NULL, "no auth data (n)"}, |
| 444 | { &no_aud, &aud_notype, NULL, NULL, "no auth data (o)"}, |
| 445 | { &no_aud, &no_aud, NULL, NULL, "no auth data (p)"}, |
| 446 | { &aud3g, NULL, NULL, NULL, "wrong auth data type (a)"}, |
| 447 | { &aud3g, &aud3g_noalg, NULL, NULL, "wrong auth data type (b)"}, |
| 448 | { &aud3g, &aud_notype, NULL, NULL, "wrong auth data type (c)"}, |
| 449 | { &aud3g, &no_aud, NULL, NULL, "wrong auth data type (d)"}, |
| 450 | { NULL, &aud2g, NULL, NULL, "wrong auth data type (e)"}, |
| 451 | { &aud3g_noalg, &aud2g, NULL, NULL, "wrong auth data type (f)"}, |
| 452 | { &aud_notype, &aud2g, NULL, NULL, "wrong auth data type (g)"}, |
| 453 | { &no_aud, &aud2g, NULL, NULL, "wrong auth data type (h)"}, |
| 454 | { &aud3g, &aud2g, NULL, NULL, "wrong auth data type (i)"}, |
| 455 | { &aud3g, &aud3g, NULL, NULL, "wrong auth data type (j)"}, |
| 456 | { &aud2g, &aud2g, NULL, NULL, "wrong auth data type (k)"}, |
| 457 | { &aud2g, NULL, rand_auts, auts, "AUTS for 2G-only (a)"}, |
| 458 | { &aud2g, &aud3g_noalg, rand_auts, auts, "AUTS for 2G-only (b)"}, |
| 459 | { &aud2g, &aud_notype, rand_auts, auts, "AUTS for 2G-only (c)"}, |
| 460 | { &aud2g, &no_aud, rand_auts, auts, "AUTS for 2G-only (d)"}, |
| 461 | { NULL, &aud3g, NULL, auts, "incomplete AUTS (a)"}, |
| 462 | { NULL, &aud3g, rand_auts, NULL, "incomplete AUTS (b)"}, |
| 463 | { &aud2g, &aud3g, NULL, auts, "incomplete AUTS (c)"}, |
| 464 | { &aud2g, &aud3g, rand_auts, NULL, "incomplete AUTS (d)"}, |
| 465 | }; |
| 466 | |
| 467 | comment_start(); |
| 468 | |
| 469 | for (i = 0; i < ARRAY_SIZE(tests); i++) { |
| 470 | fprintf(stderr, "\n- %s\n", tests[i].label); |
| 471 | rc = auc_compute_vectors(&vec, 1, |
| 472 | tests[i].aud2g, |
| 473 | tests[i].aud3g, |
| 474 | tests[i].rand_auts, |
| 475 | tests[i].auts); |
| 476 | VERBOSE_ASSERT(rc, < 0, "%d"); |
| 477 | } |
| 478 | |
| 479 | comment_end(); |
| 480 | } |
| 481 | |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 482 | int main() |
| 483 | { |
| 484 | printf("auc_3g_test.c\n"); |
| 485 | osmo_init_logging(&hlr_log_info); |
| 486 | log_set_print_filename(osmo_stderr_target, 0); |
| 487 | log_set_print_timestamp(osmo_stderr_target, 0); |
| 488 | log_set_use_color(osmo_stderr_target, 0); |
| 489 | log_set_print_category(osmo_stderr_target, 1); |
| 490 | |
Neels Hofmeyr | 8cde662 | 2017-01-31 02:10:40 +0100 | [diff] [blame] | 491 | test_gen_vectors_2g_only(); |
| 492 | test_gen_vectors_2g_plus_3g(); |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 493 | test_gen_vectors_3g_only(); |
Neels Hofmeyr | 569d322 | 2017-02-21 22:57:11 +0100 | [diff] [blame] | 494 | test_gen_vectors_bad_args(); |
Neels Hofmeyr | 00c0697 | 2017-01-31 01:19:27 +0100 | [diff] [blame] | 495 | |
| 496 | printf("Done\n"); |
| 497 | return 0; |
| 498 | } |