blob: a3c4dc274b497de4657574080b546ad719f986f9 [file] [log] [blame]
Harald Welte915e0ef2011-12-07 02:38:42 +01001/* GSM/GPRS/3G authentication testing tool */
2
3/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23
24#include <stdlib.h>
25#include <stdio.h>
26#include <errno.h>
27#include <string.h>
28#include <getopt.h>
29
30#include <osmocom/crypt/auth.h>
31#include <osmocom/core/utils.h>
32
33static void dump_auth_vec(struct osmo_auth_vector *vec)
34{
35 printf("RAND:\t%s\n", osmo_hexdump(vec->rand, sizeof(vec->rand)));
36
37 if (vec->auth_types & OSMO_AUTH_TYPE_UMTS) {
38 printf("AUTN:\t%s\n", osmo_hexdump(vec->autn, sizeof(vec->autn)));
39 printf("IK:\t%s\n", osmo_hexdump(vec->ik, sizeof(vec->ik)));
40 printf("CK:\t%s\n", osmo_hexdump(vec->ck, sizeof(vec->ck)));
41 printf("RES:\t%s\n", osmo_hexdump(vec->res, vec->res_len));
42 }
43
44 if (vec->auth_types & OSMO_AUTH_TYPE_GSM) {
45 printf("SRES:\t%s\n", osmo_hexdump(vec->sres, sizeof(vec->sres)));
46 printf("Kc:\t%s\n", osmo_hexdump(vec->kc, sizeof(vec->kc)));
47 }
48}
49
50static struct osmo_sub_auth_data test_aud = {
51 .type = OSMO_AUTH_TYPE_NONE,
52 .algo = OSMO_AUTH_ALG_NONE,
53};
54
Harald Welte5fb795e2012-03-21 08:51:48 +010055static void help()
56{
57 printf( "-2 --2g\tUse 2G (GSM) authentication\n"
58 "-3 --3g\tUse 3G (UMTS) authentication\n"
59 "-a --algorithm\tSpecify name of the algorithm\n"
60 "-k --key\tSpecify Ki / K\n"
61 "-o --opc\tSpecify OPC (only for 3G)\n"
Harald Weltea72e47b2012-03-21 09:03:16 +010062 "-O --op\tSpecify OP (only for 3G)\n"
Harald Welte5fb795e2012-03-21 08:51:48 +010063 "-a --amf\tSpecify AMF (only for 3G)\n"
64 "-s --sqn\tSpecify SQN (only for 3G)\n"
65 "-r --rand\tSpecify random value\n");
66}
67
Harald Welte915e0ef2011-12-07 02:38:42 +010068int main(int argc, char **argv)
69{
70 struct osmo_auth_vector _vec;
71 struct osmo_auth_vector *vec = &_vec;
72 uint8_t _rand[16];
73 int rc, option_index;
74 int rand_is_set = 0;
75
76 printf("osmo-auc-gen (C) 2011 by Harald Welte\n");
77 printf("This is FREE SOFTWARE with ABSOLUTELY NO WARRANTY\n\n");
78
79 while (1) {
80 int c;
81 unsigned long ul;
82 static struct option long_options[] = {
83 { "2g", 0, 0, '2' },
84 { "3g", 0, 0, '3' },
85 { "algorithm", 1, 0, 'a' },
86 { "key", 1, 0, 'k' },
87 { "opc", 1, 0, 'o' },
Harald Weltea72e47b2012-03-21 09:03:16 +010088 { "op", 1, 0, 'O' },
Harald Welte915e0ef2011-12-07 02:38:42 +010089 { "amf", 1, 0, 'f' },
90 { "sqn", 1, 0, 's' },
91 { "rand", 1, 0, 'r' },
Harald Welte5fb795e2012-03-21 08:51:48 +010092 { "help", 0, 0, 'h' },
Harald Welte915e0ef2011-12-07 02:38:42 +010093 { 0, 0, 0, 0 }
94 };
95
96 rc = 0;
97
Harald Welte5fb795e2012-03-21 08:51:48 +010098 c = getopt_long(argc, argv, "23a:k:o:f:s:r:h", long_options,
Harald Welte915e0ef2011-12-07 02:38:42 +010099 &option_index);
100
101 if (c == -1)
102 break;
103
104 switch (c) {
105 case '2':
106 test_aud.type = OSMO_AUTH_TYPE_GSM;
107 break;
108 case '3':
109 test_aud.type = OSMO_AUTH_TYPE_UMTS;
110 break;
111 case 'a':
112 rc = osmo_auth_alg_parse(optarg);
113 if (rc < 0)
114 break;
115 test_aud.algo = rc;
116 break;
117 case 'k':
118 switch (test_aud.type) {
119 case OSMO_AUTH_TYPE_GSM:
Harald Welteaae23622011-12-07 11:35:02 +0100120 rc = osmo_hexparse(optarg, test_aud.u.gsm.ki,
121 sizeof(test_aud.u.gsm.ki));
Harald Welte915e0ef2011-12-07 02:38:42 +0100122 break;
123 case OSMO_AUTH_TYPE_UMTS:
Harald Welteaae23622011-12-07 11:35:02 +0100124 rc = osmo_hexparse(optarg, test_aud.u.umts.k,
125 sizeof(test_aud.u.umts.k));
Harald Welte915e0ef2011-12-07 02:38:42 +0100126 break;
127 default:
128 fprintf(stderr, "please specify 2g/3g first!\n");
129 }
130 break;
131 case 'o':
132 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
133 fprintf(stderr, "Only UMTS has OPC\n");
134 exit(2);
135 }
Harald Welteaae23622011-12-07 11:35:02 +0100136 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
137 sizeof(test_aud.u.umts.opc));
Harald Weltea72e47b2012-03-21 09:03:16 +0100138 test_aud.u.umts.opc_is_op = 0;
139 break;
140 case 'O':
141 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
142 fprintf(stderr, "Only UMTS has OP\n");
143 exit(2);
144 }
145 rc = osmo_hexparse(optarg, test_aud.u.umts.opc,
146 sizeof(test_aud.u.umts.opc));
147 test_aud.u.umts.opc_is_op = 1;
Harald Welte915e0ef2011-12-07 02:38:42 +0100148 break;
149 case 'f':
150 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
151 fprintf(stderr, "Only UMTS has AMF\n");
152 exit(2);
153 }
Harald Welteaae23622011-12-07 11:35:02 +0100154 rc = osmo_hexparse(optarg, test_aud.u.umts.amf,
155 sizeof(test_aud.u.umts.amf));
Harald Welte915e0ef2011-12-07 02:38:42 +0100156 break;
157 case 's':
158 if (test_aud.type != OSMO_AUTH_TYPE_UMTS) {
159 fprintf(stderr, "Only UMTS has SQN\n");
160 exit(2);
161 }
162 ul = strtoul(optarg, 0, 10);
Harald Welteaae23622011-12-07 11:35:02 +0100163 test_aud.u.umts.sqn = ul;
Harald Welte915e0ef2011-12-07 02:38:42 +0100164 break;
165 case 'r':
166 rc = osmo_hexparse(optarg, _rand, sizeof(_rand));
167 rand_is_set = 1;
168 break;
Harald Welte5fb795e2012-03-21 08:51:48 +0100169 case 'h':
170 help();
171 exit(0);
172 default:
173 help();
174 exit(1);
Harald Welte915e0ef2011-12-07 02:38:42 +0100175 }
176
177 if (rc < 0) {
178 fprintf(stderr, "Error parsing argument of option `%c'\n", c);
179 exit(2);
180 }
181 }
182
183 if (!rand_is_set) {
184 printf("WARNING: We're using really weak random numbers!\n\n");
185 srand(time(NULL));
186 *(uint32_t *)&_rand[0] = rand();
187 *(uint32_t *)(&_rand[4]) = rand();
188 *(uint32_t *)(&_rand[8]) = rand();
189 *(uint32_t *)(&_rand[12]) = rand();
190 }
191
Harald Welte5fb795e2012-03-21 08:51:48 +0100192 if (test_aud.type == OSMO_AUTH_TYPE_NONE ||
193 test_aud.algo == OSMO_AUTH_ALG_NONE) {
194 help();
195 exit(2);
196 }
197
Harald Welte915e0ef2011-12-07 02:38:42 +0100198 memset(vec, 0, sizeof(*vec));
199
200 rc = osmo_auth_gen_vec(vec, &test_aud, _rand);
201 if (rc < 0) {
202 fprintf(stderr, "error generating auth vector\n");
203 exit(1);
204 }
205
206 dump_auth_vec(vec);
207#if 0
208 const uint8_t auts[14] = { 0x87, 0x11, 0xa0, 0xec, 0x9e, 0x16, 0x37, 0xdf,
209 0x17, 0xf8, 0x0b, 0x38, 0x4e, 0xe4 };
210
211 rc = osmo_auth_gen_vec_auts(vec, &test_aud, auts, _rand, _rand);
212 if (rc < 0) {
213 printf("AUTS failed\n");
214 } else {
Harald Welteaae23622011-12-07 11:35:02 +0100215 printf("AUTS success: SEQ.MS = %lu\n", test_aud.u.umts.sqn);
Harald Welte915e0ef2011-12-07 02:38:42 +0100216 }
217#endif
218 exit(0);
219
220}