blob: 3552568220de3e01bc29aed6f3bd90a5ea588889 [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
53 /* Step 1: xdout = (ki or k) ^ rand */
54 if (aud->type == OSMO_AUTH_TYPE_GSM)
55 xor(xdout, aud->u.gsm.ki, _rand, sizeof(xdout));
56 else if (aud->type == OSMO_AUTH_TYPE_UMTS)
57 xor(xdout, aud->u.umts.k, _rand, sizeof(xdout));
58 else
59 return -ENOTSUP;
60
61 /**
62 * Step 2: res = xdout
63 *
64 * Suggested length for res is 128 bits, i.e. 16 bytes,
65 * but also can be in range: 30 < n < 128 bits.
66 */
67 memcpy(vec->res, xdout, sizeof(xdout));
68 vec->res_len = sizeof(xdout);
69
70 /* ck = xdout[1-15,0] */
71 memcpy(vec->ck, xdout + 1, sizeof(xdout) - 1);
72 vec->ck[15] = xdout[0];
73
74 /* ik = xdout[2-15,0-1] */
75 memcpy(vec->ik, xdout + 2, sizeof(xdout) - 2);
76 memcpy(vec->ik + sizeof(xdout) - 2, xdout, 2);
77
78 /* ak = xdout[3-8] */
79 memcpy(ak, xdout + 3, sizeof(ak));
80
81 /**
82 * 3GPP TS 33.102, clause 6.8.1.2, b
83 * sres = c2(res) = res[0-3] ^ res[4-7] ^ res[8-11] ^ res[12-15]
84 */
85 for (i = 0; i < 4; i++) {
86 vec->sres[i] = vec->res[i] ^ vec->res[i + 4];
87 vec->sres[i] ^= vec->res[i + 8] ^ vec->res[i + 12];
88 }
89
90 /**
91 * 3GPP TS 33.102, clause 6.8.1.2, c
92 * kc = c3(ck, ik) = ck[0-7] ^ ck[8-15] ^ ik[0-7] ^ ik[8-15]
93 * FIXME: do we really have CK/IK for GSM?
94 */
95 osmo_auth_c3(vec->kc, vec->ck, vec->ik);
96
97 /* The further part is UMTS specific */
98 if (aud->type != OSMO_AUTH_TYPE_UMTS) {
99 vec->auth_types = OSMO_AUTH_TYPE_GSM;
100 return 0;
101 }
102
103 /**
104 * Step 3: cdout = sqn[0-5] || amf[0-1]
105 * NOTE (for USIM): sqn[0-5] = autn[0-5] ^ ak[0-5]
106 */
107 osmo_store64be_ext(aud->u.umts.sqn, cdout, 6);
108 memcpy(cdout + 6, aud->u.umts.amf, 2);
109
110 /* Step 4: xmac = xdout[0-8] ^ cdout[0-8] */
111 xor(xmac, xdout, cdout, sizeof(xmac));
112
113 /**
114 * Step 5: autn = sqn ^ ak || amf || mac
115 * NOTE: cdout still contains SQN from step 3
116 */
117 xor(vec->autn, cdout, ak, sizeof(ak));
118 memcpy(vec->autn + 6, aud->u.umts.amf, 2);
119 memcpy(vec->autn + 8, xmac, sizeof(xmac));
120
121 vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM;
122
123 return 0;
124}
125
126/* 3GPP TS 34.108, section 8.1.2.2 */
127static int xor_gen_vec_auts(struct osmo_auth_vector *vec,
Harald Welte08450c92023-05-30 10:55:37 +0200128 struct osmo_sub_auth_data2 *aud,
Daniel Willmanna981f9d2018-03-16 01:59:42 +0700129 const uint8_t *auts,
130 const uint8_t *rand_auts,
131 const uint8_t *_rand)
132{
133 uint8_t xdout[16], cdout[8];
134 uint8_t ak[6], xmac[8];
135 uint8_t sqnms[6];
136
137 /* Step 1: xdout = (ki or k) ^ rand */
138 if (aud->type == OSMO_AUTH_TYPE_GSM)
139 xor(xdout, aud->u.gsm.ki, _rand, sizeof(xdout));
140 else if (aud->type == OSMO_AUTH_TYPE_UMTS)
141 xor(xdout, aud->u.umts.k, _rand, sizeof(xdout));
142 else
143 return -ENOTSUP;
144
145 /* Step 2: ak = xdout[2-8] */
146 memcpy(ak, xdout + 3, 6);
147
148 /* sqnms = auts[0-5] ^ ak[0-5] */
149 xor(sqnms, auts, ak, sizeof(ak));
150
151 /* cdout = sqnms || amf* (dummy) */
152 memcpy(cdout, sqnms, 6);
153 memset(cdout + 6, 0x00, 2);
154
155 /* xmac = xdout[0-7] ^ cdout[0-7] */
156 xor(xmac, xdout, cdout, 8);
157
158 /* Compare the last 64 bits of received AUTS with the locally-generated MAC-S */
159 if (memcmp(auts + 6, xmac, 8))
160 return -1;
161
162 /* Update the "largest used SQN" from the USIM,
163 * milenage_gen_vec() will increment it. */
164 aud->u.umts.sqn_ms = osmo_load64be_ext(sqnms, 6) >> 16;
165 aud->u.umts.sqn = aud->u.umts.sqn_ms;
166
167 return xor_gen_vec(vec, aud, _rand);
168}
169
170static struct osmo_auth_impl xor_alg = {
Harald Welte9b7c9ae2023-02-21 22:24:15 +0100171 .algo = OSMO_AUTH_ALG_XOR_3G,
172 .name = "XOR-3G (libosmogsm built-in)",
Daniel Willmanna981f9d2018-03-16 01:59:42 +0700173 .priority = 1000,
174 .gen_vec = &xor_gen_vec,
175 .gen_vec_auts = &xor_gen_vec_auts,
176};
177
178static __attribute__((constructor)) void on_dso_load_xor(void)
179{
180 osmo_auth_register(&xor_alg);
181}
182
183/*! @} */