Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 1 | /* 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> |
Max | aead05d | 2016-04-21 15:57:52 +0200 | [diff] [blame] | 24 | #include <osmocom/core/bits.h> |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 25 | #include "milenage/common.h" |
| 26 | #include "milenage/milenage.h" |
| 27 | |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 28 | static 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 | |
Max | aead05d | 2016-04-21 15:57:52 +0200 | [diff] [blame] | 36 | osmo_store64be_ext(aud->u.umts.sqn, sqn, 6); |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 37 | milenage_generate(aud->u.umts.opc, aud->u.umts.amf, aud->u.umts.k, |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 38 | sqn, _rand, |
| 39 | vec->autn, vec->ik, vec->ck, vec->res, &res_len); |
| 40 | vec->res_len = res_len; |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 41 | rc = gsm_milenage(aud->u.umts.opc, aud->u.umts.k, _rand, vec->sres, vec->kc); |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 42 | if (rc < 0) |
| 43 | return rc; |
| 44 | |
| 45 | vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM; |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 46 | aud->u.umts.sqn++; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 47 | |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | static 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 Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 57 | uint8_t gen_opc[16]; |
| 58 | uint8_t *opc; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 59 | int rc; |
| 60 | |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 61 | /* 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 Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 72 | if (rc < 0) |
| 73 | return rc; |
| 74 | |
Max | aead05d | 2016-04-21 15:57:52 +0200 | [diff] [blame] | 75 | aud->u.umts.sqn = 1 + (osmo_load64be_ext(sqn_out, 6) >> 16); |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 76 | |
| 77 | return milenage_gen_vec(vec, aud, _rand); |
| 78 | } |
| 79 | |
| 80 | static 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 | |
| 88 | static __attribute__((constructor)) void on_dso_load_milenage(void) |
| 89 | { |
| 90 | osmo_auth_register(&milenage_alg); |
| 91 | } |