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> |
| 24 | #include "milenage/common.h" |
| 25 | #include "milenage/milenage.h" |
| 26 | |
| 27 | static void sqn_u64_to_48bit(uint8_t *sqn, const uint64_t sqn64) |
| 28 | { |
| 29 | sqn[5] = (sqn64 >> 0) & 0xff; |
| 30 | sqn[4] = (sqn64 >> 8) & 0xff; |
| 31 | sqn[3] = (sqn64 >> 16) & 0xff; |
| 32 | sqn[2] = (sqn64 >> 24) & 0xff; |
| 33 | sqn[1] = (sqn64 >> 32) & 0xff; |
| 34 | sqn[0] = (sqn64 >> 40) & 0xff; |
| 35 | } |
| 36 | |
| 37 | static uint64_t sqn_48bit_to_u64(const uint8_t *sqn) |
| 38 | { |
| 39 | uint64_t sqn64; |
| 40 | |
| 41 | sqn64 = sqn[0]; |
| 42 | sqn64 <<= 8; |
| 43 | sqn64 |= sqn[1]; |
| 44 | sqn64 <<= 8; |
| 45 | sqn64 |= sqn[2]; |
| 46 | sqn64 <<= 8; |
| 47 | sqn64 |= sqn[3]; |
| 48 | sqn64 <<= 8; |
| 49 | sqn64 |= sqn[4]; |
| 50 | sqn64 <<= 8; |
| 51 | sqn64 |= sqn[5]; |
| 52 | |
| 53 | return sqn64; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | static int milenage_gen_vec(struct osmo_auth_vector *vec, |
| 58 | struct osmo_sub_auth_data *aud, |
| 59 | const uint8_t *_rand) |
| 60 | { |
| 61 | size_t res_len = sizeof(vec->res); |
| 62 | uint8_t sqn[6]; |
| 63 | int rc; |
| 64 | |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 65 | sqn_u64_to_48bit(sqn, aud->u.umts.sqn); |
| 66 | 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] | 67 | sqn, _rand, |
| 68 | vec->autn, vec->ik, vec->ck, vec->res, &res_len); |
| 69 | vec->res_len = res_len; |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 70 | 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] | 71 | if (rc < 0) |
| 72 | return rc; |
| 73 | |
| 74 | vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM; |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 75 | aud->u.umts.sqn++; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 76 | |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int milenage_gen_vec_auts(struct osmo_auth_vector *vec, |
| 81 | struct osmo_sub_auth_data *aud, |
| 82 | const uint8_t *auts, const uint8_t *rand_auts, |
| 83 | const uint8_t *_rand) |
| 84 | { |
| 85 | uint8_t sqn_out[6]; |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 86 | uint8_t gen_opc[16]; |
| 87 | uint8_t *opc; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 88 | int rc; |
| 89 | |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 90 | /* Check if we only know OP and compute OPC if required */ |
| 91 | if (aud->type == OSMO_AUTH_TYPE_UMTS && aud->u.umts.opc_is_op) { |
| 92 | rc = milenage_opc_gen(gen_opc, aud->u.umts.k, |
| 93 | aud->u.umts.opc); |
| 94 | if (rc < 0) |
| 95 | return rc; |
| 96 | opc = gen_opc; |
| 97 | } else |
| 98 | opc = aud->u.umts.opc; |
| 99 | |
| 100 | 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] | 101 | if (rc < 0) |
| 102 | return rc; |
| 103 | |
Harald Welte | aae2362 | 2011-12-07 11:35:02 +0100 | [diff] [blame] | 104 | aud->u.umts.sqn = sqn_48bit_to_u64(sqn_out) + 1; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 105 | |
| 106 | return milenage_gen_vec(vec, aud, _rand); |
| 107 | } |
| 108 | |
| 109 | static struct osmo_auth_impl milenage_alg = { |
| 110 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 111 | .name = "MILENAGE (libosmogsm built-in)", |
| 112 | .priority = 1000, |
| 113 | .gen_vec = &milenage_gen_vec, |
| 114 | .gen_vec_auts = &milenage_gen_vec_auts, |
| 115 | }; |
| 116 | |
| 117 | static __attribute__((constructor)) void on_dso_load_milenage(void) |
| 118 | { |
| 119 | osmo_auth_register(&milenage_alg); |
| 120 | } |