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 | |
| 65 | sqn_u64_to_48bit(sqn, aud->umts.sqn); |
| 66 | milenage_generate(aud->umts.opc, aud->umts.amf, aud->umts.k, |
| 67 | sqn, _rand, |
| 68 | vec->autn, vec->ik, vec->ck, vec->res, &res_len); |
| 69 | vec->res_len = res_len; |
| 70 | rc = gsm_milenage(aud->umts.opc, aud->umts.k, _rand, vec->sres, vec->kc); |
| 71 | if (rc < 0) |
| 72 | return rc; |
| 73 | |
| 74 | vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM; |
| 75 | memcpy(vec->rand, _rand, sizeof(vec->rand)); |
| 76 | aud->umts.sqn++; |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int milenage_gen_vec_auts(struct osmo_auth_vector *vec, |
| 82 | struct osmo_sub_auth_data *aud, |
| 83 | const uint8_t *auts, const uint8_t *rand_auts, |
| 84 | const uint8_t *_rand) |
| 85 | { |
| 86 | uint8_t sqn_out[6]; |
| 87 | int rc; |
| 88 | |
| 89 | rc = milenage_auts(aud->umts.opc, aud->umts.k, |
| 90 | rand_auts, auts, sqn_out); |
| 91 | if (rc < 0) |
| 92 | return rc; |
| 93 | |
| 94 | aud->umts.sqn = sqn_48bit_to_u64(sqn_out) + 1; |
| 95 | |
| 96 | return milenage_gen_vec(vec, aud, _rand); |
| 97 | } |
| 98 | |
| 99 | static struct osmo_auth_impl milenage_alg = { |
| 100 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 101 | .name = "MILENAGE (libosmogsm built-in)", |
| 102 | .priority = 1000, |
| 103 | .gen_vec = &milenage_gen_vec, |
| 104 | .gen_vec_auts = &milenage_gen_vec_auts, |
| 105 | }; |
| 106 | |
| 107 | static __attribute__((constructor)) void on_dso_load_milenage(void) |
| 108 | { |
| 109 | osmo_auth_register(&milenage_alg); |
| 110 | } |