blob: 7aada36b3322619684f8feff9d051e6528374efe [file] [log] [blame]
Harald Weltee076ac02011-12-07 00:10:18 +01001
2#include <stdlib.h>
3#include <stdio.h>
4#include <errno.h>
5#include <string.h>
Neels Hofmeyr8e1b5982017-03-13 17:36:36 +01006#include <inttypes.h>
Harald Weltee076ac02011-12-07 00:10:18 +01007
8#include <osmocom/crypt/auth.h>
9#include <osmocom/core/utils.h>
10
Jacob Erlbeck73ae7a92013-10-08 12:04:42 +020011int milenage_opc_gen(uint8_t *opc, const uint8_t *k, const uint8_t *op);
12
Harald Weltee076ac02011-12-07 00:10:18 +010013static void dump_auth_vec(struct osmo_auth_vector *vec)
14{
15 printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand)));
16
17 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
18 printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn)));
19 printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik)));
20 printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck)));
21 printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len));
22 }
23
24 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
25 printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres)));
26 printf("Kc:\t%s\n", osmo_hexdump(vec->kc, sizeof(vec->kc)));
27 }
28}
29
30static struct osmo_sub_auth_data test_aud = {
31 .type = OSMO_AUTH_TYPE_UMTS,
32 .algo = OSMO_AUTH_ALG_MILENAGE,
Harald Welteaae23622011-12-07 11:35:02 +010033 .u.umts = {
Harald Weltee076ac02011-12-07 00:10:18 +010034 .opc = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
35 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
36 .k = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
37 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
38 .amf = { 0x00, 0x00 },
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +010039 .sqn = 0x21,
Neels Hofmeyrbb6f7b72017-03-13 17:27:17 +010040 .ind_bitlen = 0,
41 .ind = 0,
Harald Weltee076ac02011-12-07 00:10:18 +010042 },
43};
44
Harald Welte042afe72012-03-21 08:19:47 +010045static int opc_test(const struct osmo_sub_auth_data *aud)
46{
47 int rc;
48 uint8_t opc[16];
49#if 0
50 const uint8_t op[16] = { 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
51 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f };
52#else
53 const uint8_t op[16] = { 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0 };
54#endif
55
Maxeb59f242016-06-27 15:44:16 +020056 printf("MILENAGE supported: %d\n",
57 osmo_auth_supported(osmo_auth_alg_parse("MILENAGE")));
58
Harald Welte042afe72012-03-21 08:19:47 +010059 rc = milenage_opc_gen(opc, aud->u.umts.k, op);
60
61 printf("OP:\t%s\n", osmo_hexdump(op, sizeof(op)));
62 printf("OPC:\t%s\n", osmo_hexdump(opc, sizeof(opc)));
63 return rc;
64}
65
Harald Weltee076ac02011-12-07 00:10:18 +010066int main(int argc, char **argv)
67{
68 struct osmo_auth_vector _vec;
69 struct osmo_auth_vector *vec = &_vec;
70 uint8_t _rand[16];
71 int rc;
72
73#if 0
74 srand(time(NULL));
75 *(uint32_t *)&_rand[0] = rand();
76 *(uint32_t *)(&_rand[4]) = rand();
77 *(uint32_t *)(&_rand[8]) = rand();
78 *(uint32_t *)(&_rand[12]) = rand();
79#else
80 memset(_rand, 0, sizeof(_rand));
81#endif
82 memset(vec, 0, sizeof(*vec));
83
Neels Hofmeyrbb6f7b72017-03-13 17:27:17 +010084 /* ind_bitlen == 0 uses the legacy mode of incrementing SQN by 1.
85 * sqn == 0x21 == 33, so the SQN used to generate the vector is
86 * sqn + 1 == 34. */
Harald Weltee076ac02011-12-07 00:10:18 +010087 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
88 if (rc < 0) {
89 fprintf(stderr, "error generating auth vector\n");
90 exit(1);
91 }
92
93 dump_auth_vec(vec);
94
95 const uint8_t auts[14] = { 0x87, 0x11, 0xa0, 0xec, 0x9e, 0x16, 0x37, 0xdf,
96 0x17, 0xf8, 0x0b, 0x38, 0x4e, 0xe4 };
97
Neels Hofmeyrbb6f7b72017-03-13 17:27:17 +010098 /* Invoking with ind_bitlen == 0, the next SQN after 31 is 32. */
Harald Weltee076ac02011-12-07 00:10:18 +010099 rc = osmo_auth_gen_vec_auts(vec, &test_aud, auts, _rand, _rand);
100 if (rc < 0) {
101 printf("AUTS failed\n");
102 } else {
Neels Hofmeyr8e1b5982017-03-13 17:36:36 +0100103 printf("AUTS success: tuple generated with SQN = %" PRIu64 "\n",
104 test_aud.u.umts.sqn);
Harald Weltee076ac02011-12-07 00:10:18 +0100105 }
106
Harald Welte042afe72012-03-21 08:19:47 +0100107 opc_test(&test_aud);
108
Harald Weltee076ac02011-12-07 00:10:18 +0100109 exit(0);
110
111}