blob: 0923a278d4332b644b101844b83cecd31d86e94e [file] [log] [blame]
Neels Hofmeyr00c06972017-01-31 01:19:27 +01001/* (C) 2016 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
2 * All Rights Reserved
3 *
4 * Author: Neels Hofmeyr <nhofmeyr@sysmocom.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <stdio.h>
22#include <string.h>
Neels Hofmeyr6b883f72017-01-31 16:40:28 +010023#include <inttypes.h>
Neels Hofmeyr00c06972017-01-31 01:19:27 +010024
25#include <osmocom/core/application.h>
26#include <osmocom/core/utils.h>
27#include <osmocom/core/logging.h>
28
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010029#include <osmocom/crypt/auth.h>
Neels Hofmeyr00c06972017-01-31 01:19:27 +010030
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010031#include "logging.h"
32#include "auc.h"
33
34#define comment_start() fprintf(stderr, "\n===== %s\n", __func__);
Neels Hofmeyr00c06972017-01-31 01:19:27 +010035#define comment_end() fprintf(stderr, "===== %s: SUCCESS\n\n", __func__);
36
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010037#define VERBOSE_ASSERT(val, expect_op, fmt) \
38 do { \
39 fprintf(stderr, #val " == " fmt "\n", (val)); \
40 OSMO_ASSERT((val) expect_op); \
41 } while (0);
42
Neels Hofmeyr6b883f72017-01-31 16:40:28 +010043char *vec_str(const struct osmo_auth_vector *vec)
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010044{
45 static char buf[1024];
46 char *pos = buf;
47 char *end = buf + sizeof(buf);
48
49#define append(what) \
50 if (pos >= end) \
51 return buf; \
52 pos += snprintf(pos, sizeof(buf) - (pos - buf), \
53 " " #what ": %s\n", \
54 osmo_hexdump_nospc((void*)&vec->what, sizeof(vec->what)))
55
56 append(rand);
57 append(autn);
58 append(ck);
59 append(ik);
60 append(res);
61 append(res_len);
62 append(kc);
63 append(sres);
64 append(auth_types);
65#undef append
66
67 return buf;
68}
69
70#define VEC_IS(vec, expect) do { \
Neels Hofmeyr6b883f72017-01-31 16:40:28 +010071 char *_is = vec_str(vec); \
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010072 fprintf(stderr, "auth vector ==\n%s\n", _is); \
73 if (strcmp(_is, expect)) { \
74 fprintf(stderr, "MISMATCH! expected ==\n%s\n", \
75 expect); \
76 char *a = _is; \
77 char *b = expect; \
78 for (; *a && *b; a++, b++) { \
79 if (*a != *b) { \
80 while (a > _is && *(a-1) != '\n') a--; \
81 fprintf(stderr, "mismatch at %d:\n" \
Neels Hofmeyr6b883f72017-01-31 16:40:28 +010082 "%s", (int)(a - _is), a); \
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010083 break; \
84 } \
85 } \
86 OSMO_ASSERT(false); \
87 } \
88 } while (0)
89
Neels Hofmeyr00c06972017-01-31 01:19:27 +010090uint8_t fake_rand[16] = { 0 };
91
92int rand_get(uint8_t *rand, unsigned int len)
93{
94 OSMO_ASSERT(len <= sizeof(fake_rand));
95 memcpy(rand, fake_rand, len);
96 return len;
97}
98
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010099static void test_gen_vectors_2g_only(void)
100{
101 struct osmo_sub_auth_data aud2g;
102 struct osmo_sub_auth_data aud3g;
103 struct osmo_auth_vector vec;
104 int rc;
105
106 comment_start();
107
108 aud2g = (struct osmo_sub_auth_data){
109 .type = OSMO_AUTH_TYPE_GSM,
110 .algo = OSMO_AUTH_ALG_COMP128v1,
111 };
112
113 osmo_hexparse("EB215756028D60E3275E613320AEC880",
114 aud2g.u.gsm.ki, sizeof(aud2g.u.gsm.ki));
115
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100116 aud3g = (struct osmo_sub_auth_data){ 0 };
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100117
118 osmo_hexparse("39fa2f4e3d523d8619a73b4f65c3e14d",
119 fake_rand, sizeof(fake_rand));
120
121 vec = (struct osmo_auth_vector){ {0} };
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100122 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100123 rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL);
124 VERBOSE_ASSERT(rc, == 1, "%d");
125
126 VEC_IS(&vec,
127 " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n"
128 " autn: 00000000000000000000000000000000\n"
129 " ck: 00000000000000000000000000000000\n"
130 " ik: 00000000000000000000000000000000\n"
131 " res: 00000000000000000000000000000000\n"
132 " res_len: 00\n"
133 " kc: 241a5b16aeb8e400\n"
134 " sres: 429d5b27\n"
135 " auth_types: 01000000\n"
136 );
137
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100138 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100139
140 /* even though vec is not zero-initialized, it should produce the same
141 * result (regardless of the umts sequence nr) */
142 aud3g.u.umts.sqn = 123;
143 rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL);
144 VERBOSE_ASSERT(rc, == 1, "%d");
145
146 VEC_IS(&vec,
147 " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n"
148 " autn: 00000000000000000000000000000000\n"
149 " ck: 00000000000000000000000000000000\n"
150 " ik: 00000000000000000000000000000000\n"
151 " res: 00000000000000000000000000000000\n"
152 " res_len: 00\n"
153 " kc: 241a5b16aeb8e400\n"
154 " sres: 429d5b27\n"
155 " auth_types: 01000000\n"
156 );
157
158 comment_end();
159}
160
161static void test_gen_vectors_2g_plus_3g(void)
162{
163 struct osmo_sub_auth_data aud2g;
164 struct osmo_sub_auth_data aud3g;
165 struct osmo_auth_vector vec;
166 int rc;
167
168 comment_start();
169
170 aud2g = (struct osmo_sub_auth_data){
171 .type = OSMO_AUTH_TYPE_GSM,
172 .algo = OSMO_AUTH_ALG_COMP128v1,
173 };
174
175 osmo_hexparse("EB215756028D60E3275E613320AEC880",
176 aud2g.u.gsm.ki, sizeof(aud2g.u.gsm.ki));
177
178 aud3g = (struct osmo_sub_auth_data){
179 .type = OSMO_AUTH_TYPE_UMTS,
180 .algo = OSMO_AUTH_ALG_MILENAGE,
181 };
182
183 osmo_hexparse("EB215756028D60E3275E613320AEC880",
184 aud3g.u.umts.k, sizeof(aud3g.u.umts.k));
185 osmo_hexparse("FB2A3D1B360F599ABAB99DB8669F8308",
186 aud3g.u.umts.opc, sizeof(aud3g.u.umts.opc));
187
188 osmo_hexparse("39fa2f4e3d523d8619a73b4f65c3e14d",
189 fake_rand, sizeof(fake_rand));
190
191 vec = (struct osmo_auth_vector){ {0} };
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100192 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100193 rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL);
194 VERBOSE_ASSERT(rc, == 1, "%d");
195
196 VEC_IS(&vec,
197 " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n"
198 " autn: 8704f5ba55f30000d2ee44b22c8ea919\n"
199 " ck: f64735036e5871319c679f4742a75ea1\n"
200 " ik: 27497388b6cb044648f396aa155b95ef\n"
201 " res: e229c19e791f2e410000000000000000\n"
202 " res_len: 08\n"
203 " kc: 241a5b16aeb8e400\n"
204 " sres: 429d5b27\n"
205 " auth_types: 03000000\n"
206 );
207
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100208 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100209
210 /* even though vec is not zero-initialized, it should produce the same
211 * result with the same sequence nr */
212 aud3g.u.umts.sqn = 0;
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100213 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100214 rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL);
215 VERBOSE_ASSERT(rc, == 1, "%d");
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100216 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100217
218 VEC_IS(&vec,
219 " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n"
220 " autn: 8704f5ba55f30000d2ee44b22c8ea919\n"
221 " ck: f64735036e5871319c679f4742a75ea1\n"
222 " ik: 27497388b6cb044648f396aa155b95ef\n"
223 " res: e229c19e791f2e410000000000000000\n"
224 " res_len: 08\n"
225 " kc: 241a5b16aeb8e400\n"
226 " sres: 429d5b27\n"
227 " auth_types: 03000000\n"
228 );
229
230 comment_end();
231}
232
Neels Hofmeyr00c06972017-01-31 01:19:27 +0100233static void test_gen_vectors_3g_only(void)
234{
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100235 struct osmo_sub_auth_data aud2g;
236 struct osmo_sub_auth_data aud3g;
237 struct osmo_auth_vector vec;
238 int rc;
239
Neels Hofmeyr00c06972017-01-31 01:19:27 +0100240 comment_start();
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100241
242 aud2g = (struct osmo_sub_auth_data){ 0 };
243
244 aud3g = (struct osmo_sub_auth_data){
245 .type = OSMO_AUTH_TYPE_UMTS,
246 .algo = OSMO_AUTH_ALG_MILENAGE,
247 };
248
249 osmo_hexparse("EB215756028D60E3275E613320AEC880",
250 aud3g.u.umts.k, sizeof(aud3g.u.umts.k));
251 osmo_hexparse("FB2A3D1B360F599ABAB99DB8669F8308",
252 aud3g.u.umts.opc, sizeof(aud3g.u.umts.opc));
253
254 osmo_hexparse("39fa2f4e3d523d8619a73b4f65c3e14d",
255 fake_rand, sizeof(fake_rand));
256
257 vec = (struct osmo_auth_vector){ {0} };
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100258 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100259 rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL);
260 VERBOSE_ASSERT(rc, == 1, "%d");
261
262 VEC_IS(&vec,
263 " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n"
264 " autn: 8704f5ba55f30000d2ee44b22c8ea919\n"
265 " ck: f64735036e5871319c679f4742a75ea1\n"
266 " ik: 27497388b6cb044648f396aa155b95ef\n"
267 " res: e229c19e791f2e410000000000000000\n"
268 " res_len: 08\n"
269 " kc: 059a4f668f6fbe39\n"
270 " sres: 9b36efdf\n"
271 " auth_types: 03000000\n"
272 );
273
274 /* Note: 3GPP TS 33.102 6.8.1.2: c3 function to get GSM auth is
275 * KC[0..7] == CK[0..7] ^ CK[8..15] ^ IK[0..7] ^ IK[8..15]
276 * In [16]: hex( 0xf64735036e587131
277 * ^ 0x9c679f4742a75ea1
278 * ^ 0x27497388b6cb0446
279 * ^ 0x48f396aa155b95ef)
280 * Out[16]: '0x59a4f668f6fbe39L'
281 * hence expecting kc: 059a4f668f6fbe39
282 */
283
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100284 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100285
286 /* even though vec is not zero-initialized, it should produce the same
287 * result with the same sequence nr */
288 aud3g.u.umts.sqn = 0;
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100289 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 0, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100290 rc = auc_compute_vectors(&vec, 1, &aud2g, &aud3g, NULL, NULL);
291 VERBOSE_ASSERT(rc, == 1, "%d");
Neels Hofmeyr6b883f72017-01-31 16:40:28 +0100292 VERBOSE_ASSERT(aud3g.u.umts.sqn, == 1, "%"PRIu64);
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100293
294 VEC_IS(&vec,
295 " rand: 39fa2f4e3d523d8619a73b4f65c3e14d\n"
296 " autn: 8704f5ba55f30000d2ee44b22c8ea919\n"
297 " ck: f64735036e5871319c679f4742a75ea1\n"
298 " ik: 27497388b6cb044648f396aa155b95ef\n"
299 " res: e229c19e791f2e410000000000000000\n"
300 " res_len: 08\n"
301 " kc: 059a4f668f6fbe39\n"
302 " sres: 9b36efdf\n"
303 " auth_types: 03000000\n"
304 );
305
Neels Hofmeyr00c06972017-01-31 01:19:27 +0100306 comment_end();
307}
308
309int main()
310{
311 printf("auc_3g_test.c\n");
312 osmo_init_logging(&hlr_log_info);
313 log_set_print_filename(osmo_stderr_target, 0);
314 log_set_print_timestamp(osmo_stderr_target, 0);
315 log_set_use_color(osmo_stderr_target, 0);
316 log_set_print_category(osmo_stderr_target, 1);
317
Neels Hofmeyr8cde6622017-01-31 02:10:40 +0100318 test_gen_vectors_2g_only();
319 test_gen_vectors_2g_plus_3g();
Neels Hofmeyr00c06972017-01-31 01:19:27 +0100320 test_gen_vectors_3g_only();
321
322 printf("Done\n");
323 return 0;
324}