blob: e180762182fa19f09d060f0ca3e8abfd5e03875a [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);
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +010033 uint64_t next_sqn;
Harald Welte781bd5d2011-12-06 22:23:52 +010034 uint8_t sqn[6];
35 int rc;
36
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +010037 /* keep the incremented SQN local until gsm_milenage() succeeded. */
38 next_sqn = aud->u.umts.sqn + 1;
39
40 osmo_store64be_ext(next_sqn, sqn, 6);
Harald Welteaae23622011-12-07 11:35:02 +010041 milenage_generate(aud->u.umts.opc, aud->u.umts.amf, aud->u.umts.k,
Harald Welte781bd5d2011-12-06 22:23:52 +010042 sqn, _rand,
43 vec->autn, vec->ik, vec->ck, vec->res, &res_len);
44 vec->res_len = res_len;
Harald Welteaae23622011-12-07 11:35:02 +010045 rc = gsm_milenage(aud->u.umts.opc, aud->u.umts.k, _rand, vec->sres, vec->kc);
Harald Welte781bd5d2011-12-06 22:23:52 +010046 if (rc < 0)
47 return rc;
48
49 vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM;
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +010050
51 /* for storage in the caller's AUC database */
52 aud->u.umts.sqn = next_sqn;
Harald Welte781bd5d2011-12-06 22:23:52 +010053
54 return 0;
55}
56
57static int milenage_gen_vec_auts(struct osmo_auth_vector *vec,
58 struct osmo_sub_auth_data *aud,
59 const uint8_t *auts, const uint8_t *rand_auts,
60 const uint8_t *_rand)
61{
62 uint8_t sqn_out[6];
Harald Weltea72e47b2012-03-21 09:03:16 +010063 uint8_t gen_opc[16];
64 uint8_t *opc;
Harald Welte781bd5d2011-12-06 22:23:52 +010065 int rc;
66
Harald Weltea72e47b2012-03-21 09:03:16 +010067 /* Check if we only know OP and compute OPC if required */
68 if (aud->type == OSMO_AUTH_TYPE_UMTS && aud->u.umts.opc_is_op) {
69 rc = milenage_opc_gen(gen_opc, aud->u.umts.k,
70 aud->u.umts.opc);
71 if (rc < 0)
72 return rc;
73 opc = gen_opc;
74 } else
75 opc = aud->u.umts.opc;
76
77 rc = milenage_auts(opc, aud->u.umts.k, rand_auts, auts, sqn_out);
Harald Welte781bd5d2011-12-06 22:23:52 +010078 if (rc < 0)
79 return rc;
80
Neels Hofmeyr82c9a0e2017-03-13 17:36:17 +010081 aud->u.umts.sqn = osmo_load64be_ext(sqn_out, 6) >> 16;
Harald Welte781bd5d2011-12-06 22:23:52 +010082
83 return milenage_gen_vec(vec, aud, _rand);
84}
85
86static struct osmo_auth_impl milenage_alg = {
87 .algo = OSMO_AUTH_ALG_MILENAGE,
88 .name = "MILENAGE (libosmogsm built-in)",
89 .priority = 1000,
90 .gen_vec = &milenage_gen_vec,
91 .gen_vec_auts = &milenage_gen_vec_auts,
92};
93
94static __attribute__((constructor)) void on_dso_load_milenage(void)
95{
96 osmo_auth_register(&milenage_alg);
97}