Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 1 | |
| 2 | #include <stdlib.h> |
| 3 | #include <stdio.h> |
| 4 | #include <errno.h> |
| 5 | #include <string.h> |
Neels Hofmeyr | 8e1b598 | 2017-03-13 17:36:36 +0100 | [diff] [blame] | 6 | #include <inttypes.h> |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 7 | |
| 8 | #include <osmocom/crypt/auth.h> |
| 9 | #include <osmocom/core/utils.h> |
| 10 | |
Jacob Erlbeck | 73ae7a9 | 2013-10-08 12:04:42 +0200 | [diff] [blame] | 11 | int milenage_opc_gen(uint8_t *opc, const uint8_t *k, const uint8_t *op); |
| 12 | |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 13 | static void dump_auth_vec(struct osmo_auth_vector *vec) |
| 14 | { |
| 15 | printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand))); |
| 16 | |
| 17 | if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) { |
| 18 | printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn))); |
| 19 | printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik))); |
| 20 | printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck))); |
| 21 | printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len)); |
| 22 | } |
| 23 | |
| 24 | if (vec->auth_types & OSMO_AUTH_TYPE_GSM) { |
| 25 | printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres))); |
| 26 | printf("Kc:\t%s\n", osmo_hexdump(vec->kc, sizeof(vec->kc))); |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | static struct osmo_sub_auth_data test_aud = { |
| 31 | .type = OSMO_AUTH_TYPE_UMTS, |
| 32 | .algo = OSMO_AUTH_ALG_MILENAGE, |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 33 | .u.umts = { |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 34 | .opc = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 35 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
| 36 | .k = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, |
| 37 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }, |
| 38 | .amf = { 0x00, 0x00 }, |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 39 | .sqn = 0x21, |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 40 | .ind_bitlen = 0, |
| 41 | .ind = 0, |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 42 | }, |
| 43 | }; |
| 44 | |
Harald Welte | 042afe7 | 2012-03-21 08:19:47 +0100 | [diff] [blame] | 45 | static int opc_test(const struct osmo_sub_auth_data *aud) |
| 46 | { |
| 47 | int rc; |
| 48 | uint8_t opc[16]; |
| 49 | #if 0 |
| 50 | const uint8_t op[16] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, |
| 51 | 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f }; |
| 52 | #else |
| 53 | const uint8_t op[16] = { 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 }; |
| 54 | #endif |
| 55 | |
Max | eb59f24 | 2016-06-27 15:44:16 +0200 | [diff] [blame] | 56 | printf("MILENAGE supported: %d\n", |
| 57 | osmo_auth_supported(osmo_auth_alg_parse("MILENAGE"))); |
| 58 | |
Harald Welte | 042afe7 | 2012-03-21 08:19:47 +0100 | [diff] [blame] | 59 | rc = milenage_opc_gen(opc, aud->u.umts.k, op); |
| 60 | |
| 61 | printf("OP:\t%s\n", osmo_hexdump(op, sizeof(op))); |
| 62 | printf("OPC:\t%s\n", osmo_hexdump(opc, sizeof(opc))); |
| 63 | return rc; |
| 64 | } |
| 65 | |
Neels Hofmeyr | 6761c3f | 2017-03-14 02:31:41 +0100 | [diff] [blame] | 66 | #define RECALC_AUTS 0 |
| 67 | #if RECALC_AUTS |
| 68 | typedef uint8_t u8; |
| 69 | extern int milenage_f2345(const u8 *opc, const u8 *k, const u8 *_rand, |
| 70 | u8 *res, u8 *ck, u8 *ik, u8 *ak, u8 *akstar); |
| 71 | extern int milenage_f1(const u8 *opc, const u8 *k, const u8 *_rand, |
| 72 | const u8 *sqn, const u8 *amf, u8 *mac_a, u8 *mac_s); |
| 73 | #endif |
| 74 | |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 75 | int main(int argc, char **argv) |
| 76 | { |
| 77 | struct osmo_auth_vector _vec; |
| 78 | struct osmo_auth_vector *vec = &_vec; |
| 79 | uint8_t _rand[16]; |
| 80 | int rc; |
| 81 | |
| 82 | #if 0 |
| 83 | srand(time(NULL)); |
| 84 | *(uint32_t *)&_rand[0] = rand(); |
| 85 | *(uint32_t *)(&_rand[4]) = rand(); |
| 86 | *(uint32_t *)(&_rand[8]) = rand(); |
| 87 | *(uint32_t *)(&_rand[12]) = rand(); |
| 88 | #else |
| 89 | memset(_rand, 0, sizeof(_rand)); |
| 90 | #endif |
| 91 | memset(vec, 0, sizeof(*vec)); |
| 92 | |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 93 | /* ind_bitlen == 0 uses the legacy mode of incrementing SQN by 1. |
| 94 | * sqn == 0x21 == 33, so the SQN used to generate the vector is |
| 95 | * sqn + 1 == 34. */ |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 96 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 97 | if (rc < 0) { |
| 98 | fprintf(stderr, "error generating auth vector\n"); |
| 99 | exit(1); |
| 100 | } |
| 101 | |
| 102 | dump_auth_vec(vec); |
| 103 | |
Neels Hofmeyr | 6761c3f | 2017-03-14 02:31:41 +0100 | [diff] [blame] | 104 | /* The USIM generates an AUTS to tell us it is at SQN == 31: |
| 105 | * |
| 106 | * SQN_MS = 00000000001f |
| 107 | * |
| 108 | * AUTS = Conc(SQN_MS) || MAC-S |
| 109 | * Conc(SQN_MS) = SQN_MS ⊕ f5*[K](RAND) |
| 110 | * MAC-S = f1*[K] (SQN MS || RAND || AMF) |
| 111 | * |
| 112 | * K = 000102030405060708090a0b0c0d0e0f |
| 113 | * RAND = 00000000000000000000000000000000 |
| 114 | * |
| 115 | * f5*--> Conc(SQN_MS) = SQN_MS ^ f5*(K,RAND) |
| 116 | * = 00000000001f ^ 8711a0ec9e09 |
| 117 | * = 8711a0ec9e16 |
| 118 | * AMF = 0000 (TS 33.102 v7.0.0, 6.3.3) |
| 119 | * MAC-S = f1*[K] (SQN MS || RAND || AMF) |
| 120 | * = f1*[K] (00000000001f || 00000000000000000000000000000000 || 0000) |
| 121 | * = 37df17f80b384ee4 |
| 122 | * |
| 123 | * AUTS = 8711a0ec9e16 || 37df17f80b384ee4 |
| 124 | */ |
| 125 | #if RECALC_AUTS |
| 126 | uint8_t ak[6]; |
| 127 | uint8_t akstar[6]; |
| 128 | uint8_t opc[16]; |
| 129 | uint8_t k[16]; |
| 130 | uint8_t rand[16]; |
| 131 | osmo_hexparse("000102030405060708090a0b0c0d0e0f", k, sizeof(k)); |
| 132 | osmo_hexparse("000102030405060708090a0b0c0d0e0f", opc, sizeof(opc)); |
| 133 | osmo_hexparse("00000000000000000000000000000000", rand, sizeof(rand)); |
| 134 | milenage_f2345(opc, k, rand, NULL, NULL, NULL, ak, akstar); |
| 135 | printf("ak = %s\n", osmo_hexdump_nospc(ak, sizeof(ak))); |
| 136 | printf("akstar = %s\n", osmo_hexdump_nospc(akstar, sizeof(akstar))); |
| 137 | |
| 138 | uint8_t sqn_ms[6] = { 0, 0, 0, 0, 0, 31 }; |
| 139 | uint8_t amf[2] = {}; |
| 140 | uint8_t mac_s[8]; |
| 141 | milenage_f1(opc, k, rand, sqn_ms, amf, NULL, mac_s); |
| 142 | printf("mac_s = %s\n", osmo_hexdump_nospc(mac_s, sizeof(mac_s))); |
| 143 | /* verify valid AUTS resulting in SQN 31 with: |
| 144 | osmo-auc-gen -3 -a milenage -k 000102030405060708090a0b0c0d0e0f \ |
| 145 | -o 000102030405060708090a0b0c0d0e0f \ |
| 146 | -r 00000000000000000000000000000000 \ |
| 147 | -A 8711a0ec9e1637df17f80b384ee4 |
| 148 | */ |
| 149 | #endif |
| 150 | |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 151 | const uint8_t auts[14] = { 0x87, 0x11, 0xa0, 0xec, 0x9e, 0x16, 0x37, 0xdf, |
| 152 | 0x17, 0xf8, 0x0b, 0x38, 0x4e, 0xe4 }; |
| 153 | |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 154 | /* Invoking with ind_bitlen == 0, the next SQN after 31 is 32. */ |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 155 | rc = osmo_auth_gen_vec_auts(vec, &test_aud, auts, _rand, _rand); |
| 156 | if (rc < 0) { |
| 157 | printf("AUTS failed\n"); |
| 158 | } else { |
Neels Hofmeyr | 8e1b598 | 2017-03-13 17:36:36 +0100 | [diff] [blame] | 159 | printf("AUTS success: tuple generated with SQN = %" PRIu64 "\n", |
| 160 | test_aud.u.umts.sqn); |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 161 | } |
| 162 | |
Neels Hofmeyr | 45e778d | 2017-03-14 02:53:56 +0100 | [diff] [blame] | 163 | /* Now test SQN incrementing scheme using SEQ and IND parts: |
| 164 | * with ind_bitlen == 5 and ind == 10, the next SQN after 31 is |
| 165 | * 32 + 10 == 42. */ |
| 166 | test_aud.u.umts.ind_bitlen = 5; |
| 167 | test_aud.u.umts.ind = 10; |
| 168 | rc = osmo_auth_gen_vec_auts(vec, &test_aud, auts, _rand, _rand); |
| 169 | if (rc < 0) |
| 170 | printf("AUTS failed\n"); |
| 171 | else |
| 172 | printf("AUTS success: tuple generated with SQN = %" PRIu64 "\n", |
| 173 | test_aud.u.umts.sqn); |
| 174 | |
| 175 | /* And the one after that is 64 + 10 == 74 */ |
| 176 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 177 | if (rc < 0) |
| 178 | printf("generating vector failed\n"); |
| 179 | else |
| 180 | printf("tuple generated with SQN = %" PRIu64 "\n", |
| 181 | test_aud.u.umts.sqn); |
| 182 | |
| 183 | /* And the one after *that* is 96 + 10 == 106 */ |
| 184 | rc = osmo_auth_gen_vec(vec, &test_aud, _rand); |
| 185 | if (rc < 0) |
| 186 | printf("generating vector failed\n"); |
| 187 | else |
| 188 | printf("tuple generated with SQN = %" PRIu64 "\n", |
| 189 | test_aud.u.umts.sqn); |
| 190 | |
Harald Welte | 042afe7 | 2012-03-21 08:19:47 +0100 | [diff] [blame] | 191 | opc_test(&test_aud); |
| 192 | |
Harald Welte | e076ac0 | 2011-12-07 00:10:18 +0100 | [diff] [blame] | 193 | exit(0); |
| 194 | |
| 195 | } |