blob: 1635ac6b2f20ddcacc9c912ddd7ff06d454e1509 [file] [log] [blame]
Harald Welte781bd5d2011-12-06 22:23:52 +01001/* GSM/GPRS/3G authentication core infrastructure */
2
3/* (C) 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#include <osmocom/crypt/auth.h>
Maxaead05d2016-04-21 15:57:52 +020024#include <osmocom/core/bits.h>
Harald Welte781bd5d2011-12-06 22:23:52 +010025#include "milenage/common.h"
26#include "milenage/milenage.h"
27
Harald Welte781bd5d2011-12-06 22:23:52 +010028static int milenage_gen_vec(struct osmo_auth_vector *vec,
29 struct osmo_sub_auth_data *aud,
30 const uint8_t *_rand)
31{
32 size_t res_len = sizeof(vec->res);
33 uint8_t sqn[6];
34 int rc;
35
Maxaead05d2016-04-21 15:57:52 +020036 osmo_store64be_ext(aud->u.umts.sqn, sqn, 6);
Harald Welteaae23622011-12-07 11:35:02 +010037 milenage_generate(aud->u.umts.opc, aud->u.umts.amf, aud->u.umts.k,
Harald Welte781bd5d2011-12-06 22:23:52 +010038 sqn, _rand,
39 vec->autn, vec->ik, vec->ck, vec->res, &res_len);
40 vec->res_len = res_len;
Harald Welteaae23622011-12-07 11:35:02 +010041 rc = gsm_milenage(aud->u.umts.opc, aud->u.umts.k, _rand, vec->sres, vec->kc);
Harald Welte781bd5d2011-12-06 22:23:52 +010042 if (rc < 0)
43 return rc;
44
45 vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM;
Harald Welteaae23622011-12-07 11:35:02 +010046 aud->u.umts.sqn++;
Harald Welte781bd5d2011-12-06 22:23:52 +010047
48 return 0;
49}
50
51static int milenage_gen_vec_auts(struct osmo_auth_vector *vec,
52 struct osmo_sub_auth_data *aud,
53 const uint8_t *auts, const uint8_t *rand_auts,
54 const uint8_t *_rand)
55{
56 uint8_t sqn_out[6];
Harald Weltea72e47b2012-03-21 09:03:16 +010057 uint8_t gen_opc[16];
58 uint8_t *opc;
Harald Welte781bd5d2011-12-06 22:23:52 +010059 int rc;
60
Harald Weltea72e47b2012-03-21 09:03:16 +010061 /* Check if we only know OP and compute OPC if required */
62 if (aud->type == OSMO_AUTH_TYPE_UMTS && aud->u.umts.opc_is_op) {
63 rc = milenage_opc_gen(gen_opc, aud->u.umts.k,
64 aud->u.umts.opc);
65 if (rc < 0)
66 return rc;
67 opc = gen_opc;
68 } else
69 opc = aud->u.umts.opc;
70
71 rc = milenage_auts(opc, aud->u.umts.k, rand_auts, auts, sqn_out);
Harald Welte781bd5d2011-12-06 22:23:52 +010072 if (rc < 0)
73 return rc;
74
Maxaead05d2016-04-21 15:57:52 +020075 aud->u.umts.sqn = 1 + (osmo_load64be_ext(sqn_out, 6) >> 16);
Harald Welte781bd5d2011-12-06 22:23:52 +010076
77 return milenage_gen_vec(vec, aud, _rand);
78}
79
80static struct osmo_auth_impl milenage_alg = {
81 .algo = OSMO_AUTH_ALG_MILENAGE,
82 .name = "MILENAGE (libosmogsm built-in)",
83 .priority = 1000,
84 .gen_vec = &milenage_gen_vec,
85 .gen_vec_auts = &milenage_gen_vec_auts,
86};
87
88static __attribute__((constructor)) void on_dso_load_milenage(void)
89{
90 osmo_auth_register(&milenage_alg);
91}