aboutsummaryrefslogtreecommitdiffstats
path: root/src/gsm/tuak/tuak.c
blob: c044a377217e3151b826c1225cc8dafa1f90abe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* (C) 2023 by Harald Welte <laforge@osmocom.org>
 *
 * All Rights Reserved
 *
 * SPDX-License-Identifier: GPL-2.0+
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdint.h>
#include <string.h>
#include <string.h>
#include <errno.h>

#include <osmocom/core/utils.h>

#include "KeccakP-1600-3gpp.h"

/* TUAK authentication algorithm
 * as proposed by 3GPP as an alternative to Milenage
 * algorithm based on SHA-3 (more exactly its KeccakP-1600 permutation)
 * see 3GPP TS 35.231, 232 and 233 */

static unsigned int g_keccak_iterations = 1;
static const char algoname[] = "TUAK1.0";
const uint8_t zero16[16] = { 0, };

void tuak_set_keccak_iterations(unsigned int i)
{
	g_keccak_iterations = i;
}

/* append data from 'input' to 'buf' at 'idx', reversing byte order */
#define PUSH_DATA(buf, idx, input, nbytes)	\
	for (int i = nbytes-1; i >= 0; i--) {	\
		buf[idx++] = input[i];		\
	}

/* like memcpy(), but reversing they order of bytes */
void memcpy_reverse(uint8_t *dst, const uint8_t *src, size_t len)
{
	for (size_t i = 0; i < len; i++)
		dst[i] = src[len-i-1];
}

static void tuak_core(uint8_t buf[200], const uint8_t *opc, uint8_t instance, const uint8_t *_rand,
		      const uint8_t *amf, const uint8_t *sqn, const uint8_t *k, uint8_t k_len_bytes,
		      unsigned int keccac_iterations)
{
	unsigned int idx = 0;

	PUSH_DATA(buf, idx, opc, 32);
	buf[idx++] = instance;
	PUSH_DATA(buf, idx, algoname, strlen(algoname)); /* without trailing NUL */
	PUSH_DATA(buf, idx, _rand, 16);
	PUSH_DATA(buf, idx, amf, 2);
	PUSH_DATA(buf, idx, sqn, 6);
	PUSH_DATA(buf, idx, k, k_len_bytes);
	memset(buf+idx, 0, 32-k_len_bytes); idx += 32-k_len_bytes;
	buf[idx++] = 0x1f;
	memset(buf+idx, 0, 38); idx += 38;
	buf[idx++] = 0x80;
	memset(buf+idx, 0, 64); idx += 64;
	OSMO_ASSERT(idx == 200);

	for (unsigned int i = 0; i < keccac_iterations; i++)
		Keccak_f_64((uint64_t *) buf);
}

/**
 * tuak_f1 - TUAK f1 algorithm
 * @opc: OPc = 256-bit value derived from OP and K
 * @k: K = 128-bit or 256-bit subscriber key
 * @_rand: RAND = 128-bit random challenge
 * @sqn: SQN = 48-bit sequence number
 * @amf: AMF = 16-bit authentication management field
 * @mac_a: Buffer for MAC-A = 64/128/256-bit network authentication code
 * Returns: 0 on success, -1 on failure
 */
int tuak_f1(const uint8_t *opc, const uint8_t *k, uint8_t k_len_bytes, const uint8_t *_rand,
	    const uint8_t *sqn, const uint8_t *amf, uint8_t *mac_a, uint8_t mac_a_len_bytes,
	    unsigned int keccac_iterations)
{
	uint8_t buf[200];
	uint8_t instance = 0x00;

	switch (mac_a_len_bytes) {
	case 8:
		instance |= 0x08;
		break;
	case 16:
		instance |= 0x10;
		break;
	case 32:
		instance |= 0x20;
		break;
	default:
		return -EINVAL;
	}

	switch (k_len_bytes) {
	case 16:
		break;
	case 32:
		instance |= 0x01;
		break;
	default:
		return -EINVAL;
	}

	tuak_core(buf, opc, instance, _rand, amf, sqn, k, k_len_bytes, keccac_iterations);

	memcpy_reverse(mac_a, buf, mac_a_len_bytes);

	return 0;
}

/**
 * tuak_f1star - TUAK f1* algorithm
 * @opc: OPc = 256-bit value derived from OP and K
 * @k: K = 128-bit or 256-bit subscriber key
 * @_rand: RAND = 128-bit random challenge
 * @sqn: SQN = 48-bit sequence number
 * @amf: AMF = 16-bit authentication management field
 * @mac_s: Buffer for MAC-S = 64/128/256-bit resync authentication code
 * Returns: 0 on success, -1 on failure
 */
int tuak_f1star(const uint8_t *opc, const uint8_t *k, uint8_t k_len_bytes, const uint8_t *_rand,
		const uint8_t *sqn, const uint8_t *amf, uint8_t *mac_s, uint8_t mac_s_len_bytes,
		unsigned int keccac_iterations)
{
	uint8_t buf[200];
	uint8_t instance = 0x80;

	switch (mac_s_len_bytes) {
	case 8:
		instance |= 0x08;
		break;
	case 16:
		instance |= 0x10;
		break;
	case 32:
		instance |= 0x20;
		break;
	default:
		return -EINVAL;
	}

	switch (k_len_bytes) {
	case 16:
		break;
	case 32:
		instance |= 0x01;
		break;
	default:
		return -EINVAL;
	}

	tuak_core(buf, opc, instance, _rand, amf, sqn, k, k_len_bytes, keccac_iterations);

	memcpy_reverse(mac_s, buf, mac_s_len_bytes);

	return 0;
}

/**
 * tuak_f2345 - TUAK f2, f3, f4, f5, algorithms
 * @opc: OPc = 256-bit value derived from OP and K
 * @k: K = 128/256-bit subscriber key
 * @_rand: RAND = 128-bit random challenge
 * @res: Buffer for RES = 32/64/128/256-bit signed response (f2), or %NULL
 * @ck: Buffer for CK = 128/256-bit confidentiality key (f3), or %NULL
 * @ik: Buffer for IK = 128/256-bit integrity key (f4), or %NULL
 * @ak: Buffer for AK = 48-bit anonymity key (f5), or %NULL
 * Returns: 0 on success, -1 on failure
 */
int tuak_f2345(const uint8_t *opc, const uint8_t *k, uint8_t k_len_bytes,
	       const uint8_t *_rand, uint8_t *res, uint8_t res_len_bytes,
	       uint8_t *ck, uint8_t ck_len_bytes,
	       uint8_t *ik, uint8_t ik_len_bytes, uint8_t *ak, unsigned int keccac_iterations)
{
	uint8_t buf[200];
	uint8_t instance = 0x40;

	switch (res_len_bytes) {
	case 4:
		break;
	case 8:
		instance |= 0x08;
		break;
	case 16:
		instance |= 0x10;
		break;
	case 32:
		instance |= 0x20;
		break;
	default:
		return -EINVAL;
	}

	switch (ck_len_bytes) {
	case 16:
		break;
	case 32:
		instance |= 0x04;
		break;
	default:
		return -EINVAL;
	}

	switch (ik_len_bytes) {
	case 16:
		break;
	case 32:
		instance |= 0x02;
		break;
	default:
		return -EINVAL;
	}

	switch (k_len_bytes) {
	case 16:
		break;
	case 32:
		instance |= 0x01;
		break;
	default:
		return -EINVAL;
	}

	tuak_core(buf, opc, instance, _rand, zero16, zero16, k, k_len_bytes, keccac_iterations);

	if (res)
		memcpy_reverse(res, buf, res_len_bytes);

	if (ck)
		memcpy_reverse(ck, buf + 32, ck_len_bytes);

	if (ik)
		memcpy_reverse(ik, buf + 64, ik_len_bytes);

	if (ak)
		memcpy_reverse(ak, buf + 96, 6);

	return 0;
}

/**
 * tuak_f5star - TUAK f5* algorithm
 * @opc: OPc = 256-bit value derived from OP and K
 * @k: K = 128/256-bit subscriber key
 * @_rand: RAND = 128-bit random challenge
 * @ak: Buffer for AK = 48-bit anonymity key (f5)
 * Returns: 0 on success, -1 on failure
 */
int tuak_f5star(const uint8_t *opc, const uint8_t *k, uint8_t k_len_bytes,
		const uint8_t *_rand, uint8_t *ak, unsigned int keccac_iterations)
{
	uint8_t buf[200];
	uint8_t instance = 0xc0;

	switch (k_len_bytes) {
	case 16:
		break;
	case 32:
		instance += 1;
		break;
	default:
		return -EINVAL;
	}

	tuak_core(buf, opc, instance, _rand, zero16, zero16, k, k_len_bytes, keccac_iterations);

	memcpy_reverse(ak, buf + 96, 6);

	return 0;
}

/**
 * tuak_generate - Generate AKA AUTN,IK,CK,RES
 * @opc: OPc = 256-bit operator variant algorithm configuration field (encr.)
 * @amf: AMF = 16-bit authentication management field
 * @k: K = 128/256-bit subscriber key
 * @sqn: SQN = 48-bit sequence number
 * @_rand: RAND = 128-bit random challenge
 * @autn: Buffer for AUTN = 128-bit authentication token
 * @ik: Buffer for IK = 128/256-bit integrity key (f4), or %NULL
 * @ck: Buffer for CK = 128/256-bit confidentiality key (f3), or %NULL
 * @res: Buffer for RES = 32/64/128-bit signed response (f2), or %NULL
 * @res_len: Max length for res; set to used length or 0 on failure
 */
void tuak_generate(const uint8_t *opc, const uint8_t *amf, const uint8_t *k, uint8_t k_len_bytes,
		   const uint8_t *sqn, const uint8_t *_rand, uint8_t *autn, uint8_t *ik,
		   uint8_t *ck, uint8_t *res, size_t *res_len)
{
	int i;
	uint8_t mac_a[8], ak[6];

	if (*res_len < 4) {
		*res_len = 0;
		return;
	}
	if (tuak_f1(opc, k, k_len_bytes, _rand, sqn, amf, mac_a, sizeof(mac_a), g_keccak_iterations) ||
	    tuak_f2345(opc, k, k_len_bytes, _rand, res, *res_len, ck, 16, ik, 16, ak, g_keccak_iterations)) {
		*res_len = 0;
		return;
	}

	/* AUTN = (SQN ^ AK) || AMF || MAC */
	for (i = 0; i < 6; i++)
		autn[i] = sqn[i] ^ ak[i];
	memcpy(autn + 6, amf, 2);
	memcpy(autn + 8, mac_a, 8);
}


/**
 * tuak_auts - Milenage AUTS validation
 * @opc: OPc = 256-bit operator variant algorithm configuration field (encr.)
 * @k: K = 128/256-bit subscriber key
 * @_rand: RAND = 128-bit random challenge
 * @auts: AUTS = 112-bit authentication token from client
 * @sqn: Buffer for SQN = 48-bit sequence number
 * Returns: 0 = success (sqn filled), -1 on failure
 */
int tuak_auts(const uint8_t *opc, const uint8_t *k, uint8_t k_len_bytes,
	      const uint8_t *_rand, const uint8_t *auts, uint8_t *sqn)
{
	uint8_t amf[2] = { 0x00, 0x00 }; /* TS 33.102 v7.0.0, 6.3.3 */
	uint8_t ak[6], mac_s[8];
	int i;

	if (tuak_f5star(opc, k, k_len_bytes, _rand, ak, g_keccak_iterations))
		return -1;
	for (i = 0; i < 6; i++)
		sqn[i] = auts[i] ^ ak[i];
	if (tuak_f1star(opc, k, k_len_bytes, _rand, sqn, amf, mac_s, 8, g_keccak_iterations) ||
	    memcmp(mac_s, auts + 6, 8) != 0)
		return -1;
	return 0;
}

int tuak_opc_gen(uint8_t *opc, const uint8_t *k, uint8_t k_len_bytes, const uint8_t *op)
{
	uint8_t buf[200];
	uint8_t instance;

	switch (k_len_bytes) {
	case 16:
		instance = 0x00;
		break;
	case 32:
		instance = 0x01;
		break;
	default:
		return -EINVAL;
	}

	tuak_core(buf, op, instance, zero16, zero16, zero16, k, k_len_bytes, g_keccak_iterations);

	memcpy_reverse(opc, buf, 32);

	return 0;
}