Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file auth_milenage.c |
| 2 | * GSM/GPRS/3G authentication core infrastructure */ |
| 3 | /* |
| 4 | * (C) 2011 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <osmocom/crypt/auth.h> |
Max | aead05d | 2016-04-21 15:57:52 +0200 | [diff] [blame] | 23 | #include <osmocom/core/bits.h> |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 24 | #include "milenage/common.h" |
| 25 | #include "milenage/milenage.h" |
| 26 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 27 | /*! \addtogroup auth |
| 28 | * @{ |
| 29 | */ |
| 30 | |
Harald Welte | 113392a | 2017-08-16 23:37:25 +0200 | [diff] [blame] | 31 | static const uint8_t *gen_opc_if_needed(const struct osmo_sub_auth_data *aud, uint8_t *gen_opc) |
| 32 | { |
| 33 | int rc; |
| 34 | |
| 35 | /* Check if we only know OP and compute OPC if required */ |
| 36 | if (aud->type == OSMO_AUTH_TYPE_UMTS && aud->u.umts.opc_is_op) { |
| 37 | rc = milenage_opc_gen(gen_opc, aud->u.umts.k, aud->u.umts.opc); |
| 38 | if (rc < 0) |
| 39 | return NULL; |
| 40 | return gen_opc; |
| 41 | } else |
| 42 | return aud->u.umts.opc; |
| 43 | } |
| 44 | |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 45 | static int milenage_gen_vec(struct osmo_auth_vector *vec, |
| 46 | struct osmo_sub_auth_data *aud, |
| 47 | const uint8_t *_rand) |
| 48 | { |
| 49 | size_t res_len = sizeof(vec->res); |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 50 | uint64_t next_sqn; |
Harald Welte | 113392a | 2017-08-16 23:37:25 +0200 | [diff] [blame] | 51 | uint8_t gen_opc[16]; |
| 52 | const uint8_t *opc; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 53 | uint8_t sqn[6]; |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 54 | uint64_t ind_mask; |
| 55 | uint64_t seq_1; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 56 | int rc; |
| 57 | |
Harald Welte | 113392a | 2017-08-16 23:37:25 +0200 | [diff] [blame] | 58 | opc = gen_opc_if_needed(aud, gen_opc); |
| 59 | if (!opc) |
| 60 | return -1; |
| 61 | |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 62 | /* Determine next SQN, according to 3GPP TS 33.102: |
| 63 | * SQN consists of SEQ and a lower significant part of IND bits: |
| 64 | * |
| 65 | * |----------SEQ------------| |
| 66 | * |------------------------SQN-----------| |
| 67 | * |-----IND----| |
| 68 | * |
| 69 | * The IND part is used as "slots": e.g. a given HLR client will always |
| 70 | * get the same IND part, called ind here, with incrementing SEQ. In |
| 71 | * the USIM, each IND slot enforces that its SEQ are used in ascending |
| 72 | * order -- as long as that constraint is satisfied, the SQN may jump |
| 73 | * forwards and backwards. For example, for ind_bitlen == 5, asking the |
| 74 | * USIM for SQN = 32, 64, 33 is allowed, because 32 and 64 are |
| 75 | * SEQ || (ind == 0), and though 33 is below 64, it is ind == 1 and |
| 76 | * allowed. Not allowed would be 32, 96, 64, because 64 would go |
| 77 | * backwards after 96, both being ind == 0. |
| 78 | * |
| 79 | * From the last used SQN, we want to increment SEQ + 1, and then pick |
| 80 | * the matching IND part. |
| 81 | * |
| 82 | * IND size is suggested in TS 33.102 as 5 bits. SQN is 48 bits long. |
| 83 | * If ind_bitlen is passed too large here, the algorithms will break |
| 84 | * down. But at which point should we return an error? A sane limit |
| 85 | * seems to be ind_bitlen == 10, but to protect against failure, |
| 86 | * limiting ind_bitlen to 28 is enough, 28 being the number of bits |
| 87 | * suggested for the delta in 33.102, which is discussed to still |
| 88 | * require 2^15 > 32000 authentications to wrap the SQN back to the |
| 89 | * start. |
| 90 | * |
| 91 | * Note that if a caller with ind == 1 generates N vectors, the SQN |
| 92 | * stored after this will reflect SEQ + N. If then another caller with |
| 93 | * ind == 2 generates another N vectors, this will then use SEQ + N |
| 94 | * onwards and end up with SEQ + N + N. In other words, most of each |
| 95 | * SEQ's IND slots will remain unused. When looking at SQN being 48 |
| 96 | * bits wide, after dropping ind_bitlen (say 5) from it, we will still |
| 97 | * have a sequence range of 2^43 = 8.8e12, eight trillion sequences, |
| 98 | * which is large enough to not bother further. With the maximum |
| 99 | * ind_bitlen of 28 enforced below, we still get more than 1 million |
| 100 | * sequences, which is also sufficiently large. |
| 101 | * |
| 102 | * An ind_bitlen of zero may be passed from legacy callers that are not |
| 103 | * aware of the IND extension. For these, below algorithm works out as |
| 104 | * before, simply incrementing SQN by 1. |
| 105 | * |
| 106 | * This is also a mechanism for tools like the osmo-auc-gen to directly |
| 107 | * request a given SQN to be used. With ind_bitlen == 0 the caller can |
| 108 | * be sure that this code will increment SQN by exactly one before |
| 109 | * generating a tuple, thus a caller would simply pass |
| 110 | * { .ind_bitlen = 0, .ind = 0, .sqn = (desired_sqn - 1) } |
| 111 | */ |
| 112 | |
Neels Hofmeyr | 1a02e36 | 2017-10-07 04:44:08 +0200 | [diff] [blame] | 113 | if (aud->u.umts.ind_bitlen > OSMO_MILENAGE_IND_BITLEN_MAX) |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 114 | return -2; |
| 115 | |
| 116 | seq_1 = 1LL << aud->u.umts.ind_bitlen; |
| 117 | ind_mask = ~(seq_1 - 1); |
| 118 | |
| 119 | /* the ind index must not affect the SEQ part */ |
Neels Hofmeyr | e0cb0ee | 2017-03-16 05:30:11 +0100 | [diff] [blame] | 120 | if (aud->u.umts.ind >= seq_1) |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 121 | return -3; |
| 122 | |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 123 | /* keep the incremented SQN local until gsm_milenage() succeeded. */ |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 124 | next_sqn = ((aud->u.umts.sqn + seq_1) & ind_mask) + aud->u.umts.ind; |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 125 | |
| 126 | osmo_store64be_ext(next_sqn, sqn, 6); |
Harald Welte | 113392a | 2017-08-16 23:37:25 +0200 | [diff] [blame] | 127 | milenage_generate(opc, aud->u.umts.amf, aud->u.umts.k, |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 128 | sqn, _rand, |
| 129 | vec->autn, vec->ik, vec->ck, vec->res, &res_len); |
| 130 | vec->res_len = res_len; |
Harald Welte | 113392a | 2017-08-16 23:37:25 +0200 | [diff] [blame] | 131 | rc = gsm_milenage(opc, aud->u.umts.k, _rand, vec->sres, vec->kc); |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 132 | if (rc < 0) |
| 133 | return rc; |
| 134 | |
| 135 | vec->auth_types = OSMO_AUTH_TYPE_UMTS | OSMO_AUTH_TYPE_GSM; |
Neels Hofmeyr | 82c9a0e | 2017-03-13 17:36:17 +0100 | [diff] [blame] | 136 | |
| 137 | /* for storage in the caller's AUC database */ |
| 138 | aud->u.umts.sqn = next_sqn; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int milenage_gen_vec_auts(struct osmo_auth_vector *vec, |
| 144 | struct osmo_sub_auth_data *aud, |
| 145 | const uint8_t *auts, const uint8_t *rand_auts, |
| 146 | const uint8_t *_rand) |
| 147 | { |
| 148 | uint8_t sqn_out[6]; |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 149 | uint8_t gen_opc[16]; |
Harald Welte | 113392a | 2017-08-16 23:37:25 +0200 | [diff] [blame] | 150 | const uint8_t *opc; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 151 | int rc; |
| 152 | |
Harald Welte | 113392a | 2017-08-16 23:37:25 +0200 | [diff] [blame] | 153 | opc = gen_opc_if_needed(aud, gen_opc); |
Harald Welte | a72e47b | 2012-03-21 09:03:16 +0100 | [diff] [blame] | 154 | |
| 155 | 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] | 156 | if (rc < 0) |
| 157 | return rc; |
| 158 | |
Neels Hofmeyr | 2066a42 | 2017-08-26 22:43:50 +0200 | [diff] [blame] | 159 | aud->u.umts.sqn_ms = osmo_load64be_ext(sqn_out, 6) >> 16; |
Neels Hofmeyr | bb6f7b7 | 2017-03-13 17:27:17 +0100 | [diff] [blame] | 160 | /* Update our "largest used SQN" from the USIM -- milenage_gen_vec() |
| 161 | * below will increment SQN. */ |
Neels Hofmeyr | 2066a42 | 2017-08-26 22:43:50 +0200 | [diff] [blame] | 162 | aud->u.umts.sqn = aud->u.umts.sqn_ms; |
Harald Welte | 781bd5d | 2011-12-06 22:23:52 +0100 | [diff] [blame] | 163 | |
| 164 | return milenage_gen_vec(vec, aud, _rand); |
| 165 | } |
| 166 | |
| 167 | static struct osmo_auth_impl milenage_alg = { |
| 168 | .algo = OSMO_AUTH_ALG_MILENAGE, |
| 169 | .name = "MILENAGE (libosmogsm built-in)", |
| 170 | .priority = 1000, |
| 171 | .gen_vec = &milenage_gen_vec, |
| 172 | .gen_vec_auts = &milenage_gen_vec_auts, |
| 173 | }; |
| 174 | |
| 175 | static __attribute__((constructor)) void on_dso_load_milenage(void) |
| 176 | { |
| 177 | osmo_auth_register(&milenage_alg); |
| 178 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 179 | |
| 180 | /*! @} */ |