treewide: remove FSF address
[osmocom-bb.git] / src / shared / libosmocore / src / gsm / auth_milenage.c
blobcac1a0569ddb1fa4301808aa3cee20b76d105b58
1 /* GSM/GPRS/3G authentication core infrastructure */
3 /* (C) 2011 by Harald Welte <laforge@gnumonks.org>
5 * All Rights Reserved
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.
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.
19 #include <osmocom/crypt/auth.h>
20 #include "milenage/common.h"
21 #include "milenage/milenage.h"
23 static void sqn_u64_to_48bit(uint8_t *sqn, const uint64_t sqn64)
25 sqn[5] = (sqn64 >> 0) & 0xff;
26 sqn[4] = (sqn64 >> 8) & 0xff;
27 sqn[3] = (sqn64 >> 16) & 0xff;
28 sqn[2] = (sqn64 >> 24) & 0xff;
29 sqn[1] = (sqn64 >> 32) & 0xff;
30 sqn[0] = (sqn64 >> 40) & 0xff;
33 static uint64_t sqn_48bit_to_u64(const uint8_t *sqn)
35 uint64_t sqn64;
37 sqn64 = sqn[0];
38 sqn64 <<= 8;
39 sqn64 |= sqn[1];
40 sqn64 <<= 8;
41 sqn64 |= sqn[2];
42 sqn64 <<= 8;
43 sqn64 |= sqn[3];
44 sqn64 <<= 8;
45 sqn64 |= sqn[4];
46 sqn64 <<= 8;
47 sqn64 |= sqn[5];
49 return sqn64;
53 static int milenage_gen_vec(struct osmo_auth_vector *vec,
54 struct osmo_sub_auth_data *aud,
55 const uint8_t *_rand)
57 size_t res_len = sizeof(vec->res);
58 uint8_t sqn[6];
59 int rc;
61 sqn_u64_to_48bit(sqn, aud->u.umts.sqn);
62 milenage_generate(aud->u.umts.opc, aud->u.umts.amf, aud->u.umts.k,
63 sqn, _rand,
64 vec->autn, vec->ik, vec->ck, vec->res, &res_len);
65 vec->res_len = res_len;
66 rc = gsm_milenage(aud->u.umts.opc, aud->u.umts.k, _rand, vec->sres, vec->kc);
67 if (rc < 0)
68 return rc;
70 vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM;
71 aud->u.umts.sqn++;
73 return 0;
76 static int milenage_gen_vec_auts(struct osmo_auth_vector *vec,
77 struct osmo_sub_auth_data *aud,
78 const uint8_t *auts, const uint8_t *rand_auts,
79 const uint8_t *_rand)
81 uint8_t sqn_out[6];
82 uint8_t gen_opc[16];
83 uint8_t *opc;
84 int rc;
86 /* Check if we only know OP and compute OPC if required */
87 if (aud->type == OSMO_AUTH_TYPE_UMTS && aud->u.umts.opc_is_op) {
88 rc = milenage_opc_gen(gen_opc, aud->u.umts.k,
89 aud->u.umts.opc);
90 if (rc < 0)
91 return rc;
92 opc = gen_opc;
93 } else
94 opc = aud->u.umts.opc;
96 rc = milenage_auts(opc, aud->u.umts.k, rand_auts, auts, sqn_out);
97 if (rc < 0)
98 return rc;
100 aud->u.umts.sqn = sqn_48bit_to_u64(sqn_out) + 1;
102 return milenage_gen_vec(vec, aud, _rand);
105 static struct osmo_auth_impl milenage_alg = {
106 .algo = OSMO_AUTH_ALG_MILENAGE,
107 .name = "MILENAGE (libosmogsm built-in)",
108 .priority = 1000,
109 .gen_vec = &milenage_gen_vec,
110 .gen_vec_auts = &milenage_gen_vec_auts,
113 static __attribute__((constructor)) void on_dso_load_milenage(void)
115 osmo_auth_register(&milenage_alg);