blob: 012b39968cbaa154ca9820531a35adf456474ebf [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file milenage.c
2 * 3GPP AKA - Milenage algorithm (3GPP TS 35.205, .206, .207, .208) */
Harald Welte781bd5d2011-12-06 22:23:52 +01003/*
Harald Welte781bd5d2011-12-06 22:23:52 +01004 * Copyright (c) 2006-2007 <j@w1.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Alternatively, this software may be distributed under the terms of BSD
11 * license.
12 *
13 * See README and COPYING for more details.
14 *
15 * This file implements an example authentication algorithm defined for 3GPP
16 * AKA. This can be used to implement a simple HLR/AuC into hlr_auc_gw to allow
17 * EAP-AKA to be tested properly with real USIM cards.
18 *
19 * This implementations assumes that the r1..r5 and c1..c5 constants defined in
20 * TS 35.206 are used, i.e., r1=64, r2=0, r3=32, r4=64, r5=96, c1=00..00,
21 * c2=00..01, c3=00..02, c4=00..04, c5=00..08. The block cipher is assumed to
22 * be AES (Rijndael).
23 */
24
25#include "includes.h"
26
27#include "common.h"
28#include "aes_wrap.h"
29#include "milenage.h"
30
31
32/**
33 * milenage_f1 - Milenage f1 and f1* algorithms
34 * @opc: OPc = 128-bit value derived from OP and K
35 * @k: K = 128-bit subscriber key
36 * @_rand: RAND = 128-bit random challenge
37 * @sqn: SQN = 48-bit sequence number
38 * @amf: AMF = 16-bit authentication management field
39 * @mac_a: Buffer for MAC-A = 64-bit network authentication code, or %NULL
40 * @mac_s: Buffer for MAC-S = 64-bit resync authentication code, or %NULL
41 * Returns: 0 on success, -1 on failure
42 */
43int milenage_f1(const u8 *opc, const u8 *k, const u8 *_rand,
44 const u8 *sqn, const u8 *amf, u8 *mac_a, u8 *mac_s)
45{
46 u8 tmp1[16], tmp2[16], tmp3[16];
47 int i;
48
49 /* tmp1 = TEMP = E_K(RAND XOR OP_C) */
50 for (i = 0; i < 16; i++)
51 tmp1[i] = _rand[i] ^ opc[i];
52 if (aes_128_encrypt_block(k, tmp1, tmp1))
53 return -1;
54
55 /* tmp2 = IN1 = SQN || AMF || SQN || AMF */
56 os_memcpy(tmp2, sqn, 6);
57 os_memcpy(tmp2 + 6, amf, 2);
58 os_memcpy(tmp2 + 8, tmp2, 8);
59
60 /* OUT1 = E_K(TEMP XOR rot(IN1 XOR OP_C, r1) XOR c1) XOR OP_C */
61
62 /* rotate (tmp2 XOR OP_C) by r1 (= 0x40 = 8 bytes) */
63 for (i = 0; i < 16; i++)
64 tmp3[(i + 8) % 16] = tmp2[i] ^ opc[i];
65 /* XOR with TEMP = E_K(RAND XOR OP_C) */
66 for (i = 0; i < 16; i++)
67 tmp3[i] ^= tmp1[i];
68 /* XOR with c1 (= ..00, i.e., NOP) */
69
70 /* f1 || f1* = E_K(tmp3) XOR OP_c */
71 if (aes_128_encrypt_block(k, tmp3, tmp1))
72 return -1;
73 for (i = 0; i < 16; i++)
74 tmp1[i] ^= opc[i];
75 if (mac_a)
76 os_memcpy(mac_a, tmp1, 8); /* f1 */
77 if (mac_s)
78 os_memcpy(mac_s, tmp1 + 8, 8); /* f1* */
79 return 0;
80}
81
82
83/**
84 * milenage_f2345 - Milenage f2, f3, f4, f5, f5* algorithms
85 * @opc: OPc = 128-bit value derived from OP and K
86 * @k: K = 128-bit subscriber key
87 * @_rand: RAND = 128-bit random challenge
88 * @res: Buffer for RES = 64-bit signed response (f2), or %NULL
89 * @ck: Buffer for CK = 128-bit confidentiality key (f3), or %NULL
90 * @ik: Buffer for IK = 128-bit integrity key (f4), or %NULL
91 * @ak: Buffer for AK = 48-bit anonymity key (f5), or %NULL
92 * @akstar: Buffer for AK = 48-bit anonymity key (f5*), or %NULL
93 * Returns: 0 on success, -1 on failure
94 */
95int milenage_f2345(const u8 *opc, const u8 *k, const u8 *_rand,
96 u8 *res, u8 *ck, u8 *ik, u8 *ak, u8 *akstar)
97{
98 u8 tmp1[16], tmp2[16], tmp3[16];
99 int i;
100
101 /* tmp2 = TEMP = E_K(RAND XOR OP_C) */
102 for (i = 0; i < 16; i++)
103 tmp1[i] = _rand[i] ^ opc[i];
104 if (aes_128_encrypt_block(k, tmp1, tmp2))
105 return -1;
106
107 /* OUT2 = E_K(rot(TEMP XOR OP_C, r2) XOR c2) XOR OP_C */
108 /* OUT3 = E_K(rot(TEMP XOR OP_C, r3) XOR c3) XOR OP_C */
109 /* OUT4 = E_K(rot(TEMP XOR OP_C, r4) XOR c4) XOR OP_C */
110 /* OUT5 = E_K(rot(TEMP XOR OP_C, r5) XOR c5) XOR OP_C */
111
112 /* f2 and f5 */
113 /* rotate by r2 (= 0, i.e., NOP) */
114 for (i = 0; i < 16; i++)
115 tmp1[i] = tmp2[i] ^ opc[i];
116 tmp1[15] ^= 1; /* XOR c2 (= ..01) */
117 /* f5 || f2 = E_K(tmp1) XOR OP_c */
118 if (aes_128_encrypt_block(k, tmp1, tmp3))
119 return -1;
120 for (i = 0; i < 16; i++)
121 tmp3[i] ^= opc[i];
122 if (res)
123 os_memcpy(res, tmp3 + 8, 8); /* f2 */
124 if (ak)
125 os_memcpy(ak, tmp3, 6); /* f5 */
126
127 /* f3 */
128 if (ck) {
129 /* rotate by r3 = 0x20 = 4 bytes */
130 for (i = 0; i < 16; i++)
131 tmp1[(i + 12) % 16] = tmp2[i] ^ opc[i];
132 tmp1[15] ^= 2; /* XOR c3 (= ..02) */
133 if (aes_128_encrypt_block(k, tmp1, ck))
134 return -1;
135 for (i = 0; i < 16; i++)
136 ck[i] ^= opc[i];
137 }
138
139 /* f4 */
140 if (ik) {
141 /* rotate by r4 = 0x40 = 8 bytes */
142 for (i = 0; i < 16; i++)
143 tmp1[(i + 8) % 16] = tmp2[i] ^ opc[i];
144 tmp1[15] ^= 4; /* XOR c4 (= ..04) */
145 if (aes_128_encrypt_block(k, tmp1, ik))
146 return -1;
147 for (i = 0; i < 16; i++)
148 ik[i] ^= opc[i];
149 }
150
151 /* f5* */
152 if (akstar) {
153 /* rotate by r5 = 0x60 = 12 bytes */
154 for (i = 0; i < 16; i++)
155 tmp1[(i + 4) % 16] = tmp2[i] ^ opc[i];
156 tmp1[15] ^= 8; /* XOR c5 (= ..08) */
157 if (aes_128_encrypt_block(k, tmp1, tmp1))
158 return -1;
159 for (i = 0; i < 6; i++)
160 akstar[i] = tmp1[i] ^ opc[i];
161 }
162
163 return 0;
164}
165
166
167/**
168 * milenage_generate - Generate AKA AUTN,IK,CK,RES
169 * @opc: OPc = 128-bit operator variant algorithm configuration field (encr.)
170 * @amf: AMF = 16-bit authentication management field
171 * @k: K = 128-bit subscriber key
172 * @sqn: SQN = 48-bit sequence number
173 * @_rand: RAND = 128-bit random challenge
174 * @autn: Buffer for AUTN = 128-bit authentication token
175 * @ik: Buffer for IK = 128-bit integrity key (f4), or %NULL
176 * @ck: Buffer for CK = 128-bit confidentiality key (f3), or %NULL
177 * @res: Buffer for RES = 64-bit signed response (f2), or %NULL
178 * @res_len: Max length for res; set to used length or 0 on failure
179 */
180void milenage_generate(const u8 *opc, const u8 *amf, const u8 *k,
181 const u8 *sqn, const u8 *_rand, u8 *autn, u8 *ik,
182 u8 *ck, u8 *res, size_t *res_len)
183{
184 int i;
185 u8 mac_a[8], ak[6];
186
187 if (*res_len < 8) {
188 *res_len = 0;
189 return;
190 }
191 if (milenage_f1(opc, k, _rand, sqn, amf, mac_a, NULL) ||
192 milenage_f2345(opc, k, _rand, res, ck, ik, ak, NULL)) {
193 *res_len = 0;
194 return;
195 }
196 *res_len = 8;
197
198 /* AUTN = (SQN ^ AK) || AMF || MAC */
199 for (i = 0; i < 6; i++)
200 autn[i] = sqn[i] ^ ak[i];
201 os_memcpy(autn + 6, amf, 2);
202 os_memcpy(autn + 8, mac_a, 8);
203}
204
205
206/**
207 * milenage_auts - Milenage AUTS validation
208 * @opc: OPc = 128-bit operator variant algorithm configuration field (encr.)
209 * @k: K = 128-bit subscriber key
210 * @_rand: RAND = 128-bit random challenge
211 * @auts: AUTS = 112-bit authentication token from client
212 * @sqn: Buffer for SQN = 48-bit sequence number
213 * Returns: 0 = success (sqn filled), -1 on failure
214 */
215int milenage_auts(const u8 *opc, const u8 *k, const u8 *_rand, const u8 *auts,
216 u8 *sqn)
217{
218 u8 amf[2] = { 0x00, 0x00 }; /* TS 33.102 v7.0.0, 6.3.3 */
219 u8 ak[6], mac_s[8];
220 int i;
221
222 if (milenage_f2345(opc, k, _rand, NULL, NULL, NULL, NULL, ak))
223 return -1;
224 for (i = 0; i < 6; i++)
225 sqn[i] = auts[i] ^ ak[i];
226 if (milenage_f1(opc, k, _rand, sqn, amf, NULL, mac_s) ||
227 memcmp(mac_s, auts + 6, 8) != 0)
228 return -1;
229 return 0;
230}
231
232
233/**
234 * gsm_milenage - Generate GSM-Milenage (3GPP TS 55.205) authentication triplet
235 * @opc: OPc = 128-bit operator variant algorithm configuration field (encr.)
236 * @k: K = 128-bit subscriber key
237 * @_rand: RAND = 128-bit random challenge
238 * @sres: Buffer for SRES = 32-bit SRES
239 * @kc: Buffer for Kc = 64-bit Kc
240 * Returns: 0 on success, -1 on failure
241 */
242int gsm_milenage(const u8 *opc, const u8 *k, const u8 *_rand, u8 *sres, u8 *kc)
243{
244 u8 res[8], ck[16], ik[16];
245 int i;
246
247 if (milenage_f2345(opc, k, _rand, res, ck, ik, NULL, NULL))
248 return -1;
249
250 for (i = 0; i < 8; i++)
251 kc[i] = ck[i] ^ ck[i + 8] ^ ik[i] ^ ik[i + 8];
252
253#ifdef GSM_MILENAGE_ALT_SRES
254 os_memcpy(sres, res, 4);
255#else /* GSM_MILENAGE_ALT_SRES */
256 for (i = 0; i < 4; i++)
257 sres[i] = res[i] ^ res[i + 4];
258#endif /* GSM_MILENAGE_ALT_SRES */
259 return 0;
260}
261
262
263/**
264 * milenage_generate - Generate AKA AUTN,IK,CK,RES
265 * @opc: OPc = 128-bit operator variant algorithm configuration field (encr.)
266 * @k: K = 128-bit subscriber key
267 * @sqn: SQN = 48-bit sequence number
268 * @_rand: RAND = 128-bit random challenge
269 * @autn: AUTN = 128-bit authentication token
270 * @ik: Buffer for IK = 128-bit integrity key (f4), or %NULL
271 * @ck: Buffer for CK = 128-bit confidentiality key (f3), or %NULL
272 * @res: Buffer for RES = 64-bit signed response (f2), or %NULL
273 * @res_len: Variable that will be set to RES length
274 * @auts: 112-bit buffer for AUTS
275 * Returns: 0 on success, -1 on failure, or -2 on synchronization failure
276 */
277int milenage_check(const u8 *opc, const u8 *k, const u8 *sqn, const u8 *_rand,
278 const u8 *autn, u8 *ik, u8 *ck, u8 *res, size_t *res_len,
279 u8 *auts)
280{
281 int i;
282 u8 mac_a[8], ak[6], rx_sqn[6];
283 const u8 *amf;
284
285 wpa_hexdump(MSG_DEBUG, "Milenage: AUTN", autn, 16);
286 wpa_hexdump(MSG_DEBUG, "Milenage: RAND", _rand, 16);
287
288 if (milenage_f2345(opc, k, _rand, res, ck, ik, ak, NULL))
289 return -1;
290
291 *res_len = 8;
292 wpa_hexdump_key(MSG_DEBUG, "Milenage: RES", res, *res_len);
293 wpa_hexdump_key(MSG_DEBUG, "Milenage: CK", ck, 16);
294 wpa_hexdump_key(MSG_DEBUG, "Milenage: IK", ik, 16);
295 wpa_hexdump_key(MSG_DEBUG, "Milenage: AK", ak, 6);
296
297 /* AUTN = (SQN ^ AK) || AMF || MAC */
298 for (i = 0; i < 6; i++)
299 rx_sqn[i] = autn[i] ^ ak[i];
300 wpa_hexdump(MSG_DEBUG, "Milenage: SQN", rx_sqn, 6);
301
302 if (os_memcmp(rx_sqn, sqn, 6) <= 0) {
303 u8 auts_amf[2] = { 0x00, 0x00 }; /* TS 33.102 v7.0.0, 6.3.3 */
304 if (milenage_f2345(opc, k, _rand, NULL, NULL, NULL, NULL, ak))
305 return -1;
306 wpa_hexdump_key(MSG_DEBUG, "Milenage: AK*", ak, 6);
307 for (i = 0; i < 6; i++)
308 auts[i] = sqn[i] ^ ak[i];
309 if (milenage_f1(opc, k, _rand, sqn, auts_amf, NULL, auts + 6))
310 return -1;
311 wpa_hexdump(MSG_DEBUG, "Milenage: AUTS", auts, 14);
312 return -2;
313 }
314
315 amf = autn + 6;
316 wpa_hexdump(MSG_DEBUG, "Milenage: AMF", amf, 2);
317 if (milenage_f1(opc, k, _rand, rx_sqn, amf, mac_a, NULL))
318 return -1;
319
320 wpa_hexdump(MSG_DEBUG, "Milenage: MAC_A", mac_a, 8);
321
322 if (os_memcmp(mac_a, autn + 8, 8) != 0) {
323 wpa_printf(MSG_DEBUG, "Milenage: MAC mismatch");
324 wpa_hexdump(MSG_DEBUG, "Milenage: Received MAC_A",
325 autn + 8, 8);
326 return -1;
327 }
328
329 return 0;
330}
Harald Welte042afe72012-03-21 08:19:47 +0100331
332int milenage_opc_gen(u8 *opc, const u8 *k, const u8 *op)
333{
334 int i;
335
336 /* Encrypt OP using K */
337 if (aes_128_encrypt_block(k, op, opc))
338 return -1;
339
340 /* XOR the resulting Ek(OP) with OP */
341 for (i = 0; i < 16; i++)
342 opc[i] = opc[i] ^ op[i];
343
344 return 0;
345}