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