blob: c94b02f7cb6349655bbc663cabe5d7d2699b0955 [file] [log] [blame]
Daniel Willmanna981f9d2018-03-16 01:59:42 +07001/*! \file auth_xor.c
2 * GSM/GPRS/3G authentication core infrastructure */
3/*
4 * (C) 2018 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2017 by sysmocom s.f.m.c. GmbH
6 *
7 * All Rights Reserved
8 *
9 * Author: Daniel Willmann <dwillmann@sysmocom.de>
10 *
11 * All Rights Reserved
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
Daniel Willmanna981f9d2018-03-16 01:59:42 +070023 */
24
25#include <string.h>
26#include <stdint.h>
27#include <errno.h>
28
29#include <osmocom/core/bit64gen.h>
30#include <osmocom/crypt/auth.h>
31
32/*! \addtogroup auth
33 * @{
34 */
35
36static void xor(uint8_t *out, const uint8_t *a, const uint8_t *b, size_t len)
37{
38 size_t i;
39
40 for (i = 0; i < len; i++)
41 out[i] = a[i] ^ b[i];
42}
43
44/* 3GPP TS 34.108, section 8.1.2.1 */
45static int xor_gen_vec(struct osmo_auth_vector *vec,
Harald Welte08450c92023-05-30 10:55:37 +020046 struct osmo_sub_auth_data2 *aud,
Daniel Willmanna981f9d2018-03-16 01:59:42 +070047 const uint8_t *_rand)
48{
49 uint8_t xdout[16], cdout[8];
50 uint8_t ak[6], xmac[8];
51 int i;
52
Harald Welte5248c472023-05-30 11:09:17 +020053 OSMO_ASSERT(aud->algo == OSMO_AUTH_ALG_XOR_3G);
54
Daniel Willmanna981f9d2018-03-16 01:59:42 +070055 /* Step 1: xdout = (ki or k) ^ rand */
56 if (aud->type == OSMO_AUTH_TYPE_GSM)
57 xor(xdout, aud->u.gsm.ki, _rand, sizeof(xdout));
58 else if (aud->type == OSMO_AUTH_TYPE_UMTS)
59 xor(xdout, aud->u.umts.k, _rand, sizeof(xdout));
60 else
61 return -ENOTSUP;
62
63 /**
64 * Step 2: res = xdout
65 *
66 * Suggested length for res is 128 bits, i.e. 16 bytes,
67 * but also can be in range: 30 < n < 128 bits.
68 */
69 memcpy(vec->res, xdout, sizeof(xdout));
70 vec->res_len = sizeof(xdout);
71
72 /* ck = xdout[1-15,0] */
73 memcpy(vec->ck, xdout + 1, sizeof(xdout) - 1);
74 vec->ck[15] = xdout[0];
75
76 /* ik = xdout[2-15,0-1] */
77 memcpy(vec->ik, xdout + 2, sizeof(xdout) - 2);
78 memcpy(vec->ik + sizeof(xdout) - 2, xdout, 2);
79
80 /* ak = xdout[3-8] */
81 memcpy(ak, xdout + 3, sizeof(ak));
82
83 /**
84 * 3GPP TS 33.102, clause 6.8.1.2, b
85 * sres = c2(res) = res[0-3] ^ res[4-7] ^ res[8-11] ^ res[12-15]
86 */
87 for (i = 0; i < 4; i++) {
88 vec->sres[i] = vec->res[i] ^ vec->res[i + 4];
89 vec->sres[i] ^= vec->res[i + 8] ^ vec->res[i + 12];
90 }
91
92 /**
93 * 3GPP TS 33.102, clause 6.8.1.2, c
94 * kc = c3(ck, ik) = ck[0-7] ^ ck[8-15] ^ ik[0-7] ^ ik[8-15]
95 * FIXME: do we really have CK/IK for GSM?
96 */
97 osmo_auth_c3(vec->kc, vec->ck, vec->ik);
98
99 /* The further part is UMTS specific */
100 if (aud->type != OSMO_AUTH_TYPE_UMTS) {
101 vec->auth_types = OSMO_AUTH_TYPE_GSM;
102 return 0;
103 }
104
105 /**
106 * Step 3: cdout = sqn[0-5] || amf[0-1]
107 * NOTE (for USIM): sqn[0-5] = autn[0-5] ^ ak[0-5]
108 */
109 osmo_store64be_ext(aud->u.umts.sqn, cdout, 6);
110 memcpy(cdout + 6, aud->u.umts.amf, 2);
111
112 /* Step 4: xmac = xdout[0-8] ^ cdout[0-8] */
113 xor(xmac, xdout, cdout, sizeof(xmac));
114
115 /**
116 * Step 5: autn = sqn ^ ak || amf || mac
117 * NOTE: cdout still contains SQN from step 3
118 */
119 xor(vec->autn, cdout, ak, sizeof(ak));
120 memcpy(vec->autn + 6, aud->u.umts.amf, 2);
121 memcpy(vec->autn + 8, xmac, sizeof(xmac));
122
123 vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM;
124
125 return 0;
126}
127
128/* 3GPP TS 34.108, section 8.1.2.2 */
129static int xor_gen_vec_auts(struct osmo_auth_vector *vec,
Harald Welte08450c92023-05-30 10:55:37 +0200130 struct osmo_sub_auth_data2 *aud,
Daniel Willmanna981f9d2018-03-16 01:59:42 +0700131 const uint8_t *auts,
132 const uint8_t *rand_auts,
133 const uint8_t *_rand)
134{
135 uint8_t xdout[16], cdout[8];
136 uint8_t ak[6], xmac[8];
137 uint8_t sqnms[6];
138
Harald Welte5248c472023-05-30 11:09:17 +0200139 OSMO_ASSERT(aud->algo == OSMO_AUTH_ALG_XOR_3G);
140
Daniel Willmanna981f9d2018-03-16 01:59:42 +0700141 /* Step 1: xdout = (ki or k) ^ rand */
142 if (aud->type == OSMO_AUTH_TYPE_GSM)
143 xor(xdout, aud->u.gsm.ki, _rand, sizeof(xdout));
144 else if (aud->type == OSMO_AUTH_TYPE_UMTS)
145 xor(xdout, aud->u.umts.k, _rand, sizeof(xdout));
146 else
147 return -ENOTSUP;
148
149 /* Step 2: ak = xdout[2-8] */
150 memcpy(ak, xdout + 3, 6);
151
152 /* sqnms = auts[0-5] ^ ak[0-5] */
153 xor(sqnms, auts, ak, sizeof(ak));
154
155 /* cdout = sqnms || amf* (dummy) */
156 memcpy(cdout, sqnms, 6);
157 memset(cdout + 6, 0x00, 2);
158
159 /* xmac = xdout[0-7] ^ cdout[0-7] */
160 xor(xmac, xdout, cdout, 8);
161
162 /* Compare the last 64 bits of received AUTS with the locally-generated MAC-S */
163 if (memcmp(auts + 6, xmac, 8))
164 return -1;
165
166 /* Update the "largest used SQN" from the USIM,
167 * milenage_gen_vec() will increment it. */
168 aud->u.umts.sqn_ms = osmo_load64be_ext(sqnms, 6) >> 16;
169 aud->u.umts.sqn = aud->u.umts.sqn_ms;
170
171 return xor_gen_vec(vec, aud, _rand);
172}
173
174static struct osmo_auth_impl xor_alg = {
Harald Welte9b7c9ae2023-02-21 22:24:15 +0100175 .algo = OSMO_AUTH_ALG_XOR_3G,
176 .name = "XOR-3G (libosmogsm built-in)",
Daniel Willmanna981f9d2018-03-16 01:59:42 +0700177 .priority = 1000,
178 .gen_vec = &xor_gen_vec,
179 .gen_vec_auts = &xor_gen_vec_auts,
180};
181
182static __attribute__((constructor)) void on_dso_load_xor(void)
183{
184 osmo_auth_register(&xor_alg);
185}
186
187/*! @} */