1
/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU Affero General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Affero General Public License for more details.
15
* You should have received a copy of the GNU Affero General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include <osmocom/core/utils.h>
24
#include <osmocom/crypt/auth.h>
29
#define hexb(buf) osmo_hexdump_nospc((void*)buf, sizeof(buf))
30
#define hex(buf,sz) osmo_hexdump_nospc((void*)buf, sz)
32
/* compute given number of vectors using either aud2g or aud2g or a combination
33
* of both. Handles re-synchronization if rand_auts and auts are set */
34
int auc_compute_vectors(struct osmo_auth_vector *vec, unsigned int num_vec,
35
struct osmo_sub_auth_data *aud2g,
36
struct osmo_sub_auth_data *aud3g,
37
const uint8_t *rand_auts, const uint8_t *auts)
41
struct osmo_auth_vector vtmp;
44
/* no need to iterate the log categories all the time */
45
int dbg = log_check_level(DAUC, LOGL_DEBUG);
46
#define DBGP(args ...) if (dbg) DEBUGP(DAUC, ##args)
47
#define DBGVB(member) DBGP("vector [%u]: " #member " = %s\n", \
48
i, hexb(vec[i].member))
49
#define DBGVV(fmt, member) DBGP("vector [%u]: " #member " = " fmt "\n", \
52
if (aud2g && (aud2g->algo == OSMO_AUTH_ALG_NONE
53
|| aud2g->type == OSMO_AUTH_TYPE_NONE))
55
if (aud3g && (aud3g->algo == OSMO_AUTH_ALG_NONE
56
|| aud3g->type == OSMO_AUTH_TYPE_NONE))
59
if (!aud2g && !aud3g) {
60
LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
61
" with neither 2G nor 3G auth data available\n");
65
if (aud2g && aud2g->type != OSMO_AUTH_TYPE_GSM) {
66
LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
67
" with non-2G auth data passed for aud2g arg\n");
71
if (aud3g && aud3g->type != OSMO_AUTH_TYPE_UMTS) {
72
LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
73
" with non-3G auth data passed for aud3g arg\n");
77
if ((rand_auts != NULL) != (auts != NULL)) {
78
LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() with only one"
79
" of AUTS and AUTS_RAND given, need both or neither\n");
84
LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() with AUTS called"
85
" but no 3G auth data passed\n");
89
DBGP("Computing %d auth vector%s: %s%s\n",
90
num_vec, num_vec == 1 ? "" : "s",
91
aud3g? (aud2g? "3G + separate 2G"
92
: "3G only (2G derived from 3G keys)")
94
auts? ", with AUTS resync" : "");
96
DBGP("3G: k = %s\n", hexb(aud3g->u.umts.k));
98
aud3g->u.umts.opc_is_op? "OP" : "opc",
99
hexb(aud3g->u.umts.opc));
100
DBGP("3G: for sqn ind %u, previous sqn was %" PRIu64 "\n",
101
aud3g->u.umts.ind, aud3g->u.umts.sqn);
104
DBGP("2G: ki = %s\n", hexb(aud2g->u.gsm.ki));
106
for (i = 0; i < num_vec; i++) {
107
rc = rand_get(rand, sizeof(rand));
108
if (rc != sizeof(rand)) {
109
LOGP(DAUC, LOGL_ERROR, "Unable to read %zu random "
110
"bytes: rc=%d\n", sizeof(rand), rc);
113
DBGP("vector [%u]: rand = %s\n", i, hexb(rand));
116
/* 3G or 3G + 2G case */
118
/* Do AUTS only for the first vector or we would use
119
* the same SQN for each following key. */
120
if ((i == 0) && auts) {
121
DBGP("vector [%u]: resync: auts = %s\n",
123
DBGP("vector [%u]: resync: rand_auts = %s\n",
124
i, hex(rand_auts, 16));
126
rc = osmo_auth_gen_vec_auts(vec+i, aud3g, auts,
129
rc = osmo_auth_gen_vec(vec+i, aud3g, rand);
132
LOGP(DAUC, LOGL_ERROR, "Error in 3G vector "
133
"generation: [%u]: rc = %d\n", i, rc);
136
DBGP("vector [%u]: sqn = %" PRIu64 "\n",
137
i, aud3g->u.umts.sqn);
143
DBGVV("%u", res_len);
146
/* use the 2G tokens from 3G keys */
149
DBGVV("0x%x", auth_types);
152
/* calculate 2G separately */
154
DBGP("vector [%u]: deriving 2G from 3G\n", i);
156
rc = osmo_auth_gen_vec(&vtmp, aud2g, rand);
158
LOGP(DAUC, LOGL_ERROR, "Error in 2G vector"
159
"generation: [%u]: rc = %d\n", i, rc);
162
memcpy(&vec[i].kc, vtmp.kc, sizeof(vec[i].kc));
163
memcpy(&vec[i].sres, vtmp.sres, sizeof(vec[i].sres));
164
vec[i].auth_types |= OSMO_AUTH_TYPE_GSM;
167
rc = osmo_auth_gen_vec(vec+i, aud2g, rand);
169
LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
170
"generation: [%u]: rc = %d\n", i, rc);
177
DBGVV("0x%x", auth_types);