blob: 086add55d321d5d32eb74d5905680421057d23db [file] [log] [blame]
Harald Welte86021062021-03-19 13:19:18 +01001#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <stdbool.h>
5#include <getopt.h>
6
7#include <osmocom/core/utils.h>
8#include <osmocom/core/bit64gen.h>
9
10/* Utility program for implementing the SIM-side procedures of 3GPP Authentication and Key Agreement
11 * as specified by 3GPP TS 33.102 Section 6.3.3
12 *
13 * (C) 2021 by Harald Welte <laforge@gnumonks.org>
14 * Milenage library code used from libosmocore, which inherited it from wpa_supplicant
15 */
16
17/* FIXME: libosmogsm implements those, but doesn't declare them */
18int milenage_f1(const uint8_t *opc, const uint8_t *k, const uint8_t *_rand,
19 const uint8_t *sqn, const uint8_t *amf, uint8_t *mac_a, uint8_t *mac_s);
20int milenage_f2345(const uint8_t *opc, const uint8_t *k, const uint8_t *_rand,
21 uint8_t *res, uint8_t *ck, uint8_t *ik, uint8_t *ak, uint8_t *akstar);
Harald Welte9fb5d802021-03-22 11:53:00 +010022int milenage_opc_gen(uint8_t *opc, const uint8_t *k, const uint8_t *op);
Harald Welte86021062021-03-19 13:19:18 +010023
24static int milenage_check(const uint8_t *opc, const uint8_t *k, const uint8_t *sqn, const uint8_t *_rand,
Vadim Yanitskiyc3c914a2021-03-21 22:36:39 +010025 const uint8_t *autn, uint8_t *ck, uint8_t *ik, uint8_t *res, size_t *res_len,
Harald Welte86021062021-03-19 13:19:18 +010026 uint8_t *auts)
27{
28 int i;
29 uint8_t xmac[8], ak[6], rx_sqn_bin[6];
30 unsigned long long rx_sqn;
31 const uint8_t *amf;
32
33 printf("=== Static SIM parameters:\n");
34 printf("Milenage SIM K: %s\n", osmo_hexdump_nospc(k, 16));
35 printf("Milenage SIM OPc: %s\n", osmo_hexdump_nospc(opc, 16));
36 printf("Milenage SIM SQN: %s\n", osmo_hexdump_nospc(sqn, 6));
37 printf("\n");
38
39 printf("=== Authentication Tuple as received from Network:\n");
40 printf("Milenage Input RAND: %s\n", osmo_hexdump_nospc(_rand, 16));
41 printf("Milenage Input AUTN: %s\n", osmo_hexdump_nospc(autn, 16));
42 printf("\tAUTN(+)AK: %s\n", osmo_hexdump_nospc(autn, 6));
43 printf("\tAMF: %s\n", osmo_hexdump_nospc(autn+6, 2));
44 printf("\tMAC: %s\n", osmo_hexdump_nospc(autn+8, 8));
45 printf("\n");
46
47 if (milenage_f2345(opc, k, _rand, res, ck, ik, ak, NULL))
48 return -1;
49
50 *res_len = 8;
51 printf("Milenage f2-Computed RES: %s\n", osmo_hexdump_nospc(res, *res_len));
52 printf("Milenage f3-Computed CK: %s\n", osmo_hexdump_nospc(ck, 16));
53 printf("Milenage f4-Computed IK: %s\n", osmo_hexdump_nospc(ik, 16));
54 printf("Milenage f5-Computed AK: %s\n", osmo_hexdump_nospc(ak, 6));
55
56 /* AUTN = (SQN ^ AK) || AMF || MAC */
57 for (i = 0; i < 6; i++)
58 rx_sqn_bin[i] = autn[i] ^ ak[i];
59 rx_sqn = osmo_load64be_ext(rx_sqn_bin, 6);
60 printf("Milenage Computed SQN: %s (%llu)\n", osmo_hexdump_nospc(rx_sqn_bin, 6), rx_sqn);
61
62 if (memcmp(rx_sqn_bin, sqn, 6) <= 0) {
63 printf("Milenage: RX-SQN differs from SIM SQN: Re-Sync!\n");
64 uint8_t auts_amf[2] = { 0x00, 0x00 }; /* TS 33.102 v7.0.0, 6.3.3 */
65 if (milenage_f2345(opc, k, _rand, NULL, NULL, NULL, NULL, ak))
66 return -1;
67 printf("Milenage Computed AK*: %s", osmo_hexdump_nospc(ak, 6));
68 for (i = 0; i < 6; i++)
69 auts[i] = sqn[i] ^ ak[i];
70 if (milenage_f1(opc, k, _rand, sqn, auts_amf, NULL, auts + 6))
71 return -1;
72 printf("Milenage AUTS: %s\n", osmo_hexdump_nospc(auts, 14));
73 return -2;
74 }
75
76 amf = autn + 6;
77 if (milenage_f1(opc, k, _rand, rx_sqn_bin, amf, xmac, NULL))
78 return -1;
79
80 printf("Milenage f1-Computed XMAC: %s\n", osmo_hexdump_nospc(xmac, 8));
81
82 if (memcmp(xmac, autn + 8, 8) != 0) {
83 fprintf(stderr, "Milenage: MAC mismatch!\n");
84 return -1;
85 }
86
87 return 0;
88}
89
90
91static void help()
92{
93 printf( "Static SIM card parameters:\n"
94 "-k --key\tSpecify Ki / K\n"
95 "-o --opc\tSpecify OPC\n"
96 "-O --op\tSpecify OP\n"
97 "-f --amf\tSpecify AMF\n"
98 "-s --sqn\tSpecify SQN\n"
99 "\n"
100 "Authentication Tuple by network:\n"
101 //"-i --ind\tSpecify IND slot for new SQN after AUTS\n"
102 //"-l --ind-len\tSpecify IND bit length (default=5)\n"
103 "-r --rand\tSpecify RAND random value\n"
104 "-A --autn\tSpecify AUTN authentication nonce\n"
105 );
106}
107
108static uint8_t g_k[16];
109static uint8_t g_opc[16];
110static uint8_t g_rand[16];
111static uint8_t g_autn[16];
112static uint8_t g_amf[16];
113static unsigned long long g_sqn;
114
115
116static int handle_options(int argc, char **argv)
117{
118 int rc, option_index;
119 bool rand_is_set = false;
120 bool autn_is_set = false;
121 bool sqn_is_set = false;
122 bool k_is_set = false;
123 bool opc_is_set = false;
124 bool amf_is_set = false;
125 bool opc_is_op = false;
126
127 while (1) {
128 int c;
129 static struct option long_options[] = {
130 { "key", 1, 0, 'k' },
131 { "opc", 1, 0, 'o' },
132 { "op", 1, 0, 'O' },
133 { "amf", 1, 0, 'f' },
134 { "sqn", 1, 0, 's' },
135 { "rand", 1, 0, 'r' },
136 { "autn", 1, 0, 'A' },
137 { "help", 0, 0, 'h' },
138 { 0, 0, 0, 0 }
139 };
140
141 rc = 0;
142
143 c = getopt_long(argc, argv, "k:o:O:f:s:r:A:h", long_options, &option_index);
144
145 if (c == -1)
146 break;
147
148 switch (c) {
149 case 'k':
150 rc = osmo_hexparse(optarg, g_k, sizeof(g_k));
151 k_is_set = true;
152 break;
153 case 'o':
154 rc = osmo_hexparse(optarg, g_opc, sizeof(g_opc));
155 opc_is_op = false;
156 opc_is_set = true;
157 break;
158 case 'O':
159 rc = osmo_hexparse(optarg, g_opc, sizeof(g_opc));
160 opc_is_op = true;
161 opc_is_set = true;
162 break;
163 case 'A':
164 rc = osmo_hexparse(optarg, g_autn, sizeof(g_autn));
165 autn_is_set = true;
166 break;
167 case 'f':
168 rc = osmo_hexparse(optarg, g_amf, sizeof(g_amf));
169 amf_is_set = true;
170 break;
171 case 's':
172 g_sqn = strtoull(optarg, 0, 10);
173 sqn_is_set = true;
174 break;
175 case 'r':
176 rc = osmo_hexparse(optarg, g_rand, sizeof(g_rand));
177 rand_is_set = true;
178 break;
179 case 'h':
180 help();
181 exit(0);
182 default:
183 help();
184 exit(1);
185 }
186
187 if (rc < 0) {
188 help();
189 fprintf(stderr, "\nError parsing argument of option `%c'\n", c);
190 exit(2);
191 }
192 }
193
194 if (!k_is_set || !opc_is_set || !autn_is_set || !rand_is_set) {
195 fprintf(stderr, "Error: K, OP[c], AUTN and RAND are mandatory arguments\n");
196 fprintf(stderr, "\n");
197 help();
198 exit(2);
199 }
200
201 if (!sqn_is_set)
202 printf("Warning: You may want to specify SQN\n");
203
204 if (!amf_is_set)
205 printf("Warning: You may want to specify AMF\n");
206
207 if (opc_is_op) {
Harald Welte9fb5d802021-03-22 11:53:00 +0100208 uint8_t op[16];
209 memcpy(op, g_opc, 16);
210 rc = milenage_opc_gen(g_opc, g_k, op);
211 OSMO_ASSERT(rc == 0);
Harald Welte86021062021-03-19 13:19:18 +0100212 }
213
214 return 0;
215}
216
217
218
219int main(int argc, char **argv)
220{
221 printf("osmo-aka-check (C) 2021 by Harald Welte\n");
222 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
223
224 handle_options(argc, argv);
225
226 printf("\n");
227
228 uint8_t ck[16];
229 uint8_t ik[16];
230 uint8_t res[16];
231 size_t res_len;
232 uint8_t auts[14];
233 uint8_t sqn_bin[6];
234 int rc;
235
236 osmo_store64be_ext(g_sqn, sqn_bin, 6);
237
238 rc = milenage_check(g_opc, g_k, sqn_bin, g_rand, g_autn, ck, ik, res, &res_len, auts);
239
240 if (rc < 0) {
241 fprintf(stderr, "Authentication FAILED!\n");
242 exit(1);
243 } else {
244 printf("Authentication SUCCEEDED\n");
245 exit(0);
246 }
247}