blob: 3f3db34470fff6f67e70b4cebef78e502c356806 [file] [log] [blame]
Harald Weltee72cf552016-04-28 07:18:49 +02001/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
2 *
3 * All Rights Reserved
4 *
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.
9 *
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.
14 *
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/>.
17 *
18 */
19
20#include <string.h>
21
22#include <osmocom/core/utils.h>
23#include <osmocom/crypt/auth.h>
24
25#include "logging.h"
26#include "rand.h"
27
28/* compute given number of vectors using either aud2g or aud2g or a combination
Neels Hofmeyr5b581ac2017-01-19 15:54:01 +010029 * of both. Handles re-synchronization if rand_auts and auts are set */
Harald Weltee72cf552016-04-28 07:18:49 +020030int auc_compute_vectors(struct osmo_auth_vector *vec, unsigned int num_vec,
31 struct osmo_sub_auth_data *aud2g,
32 struct osmo_sub_auth_data *aud3g,
33 const uint8_t *rand_auts, const uint8_t *auts)
34{
35 unsigned int i;
36 uint8_t rand[16];
37 int rc;
38
39 if (aud2g->algo == OSMO_AUTH_ALG_NONE)
40 aud2g = NULL;
41 if (aud3g->algo == OSMO_AUTH_ALG_NONE)
42 aud3g = NULL;
43
44 if (!aud2g && !aud3g)
45 return -1;
46
47 /* compute quintuples */
48 for (i = 0; i < num_vec; i++) {
49 rc = rand_get(rand, sizeof(rand));
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010050 DEBUGP(DAUC, "rand %s\n", osmo_hexdump_nospc(rand, sizeof(rand)));
Harald Weltee72cf552016-04-28 07:18:49 +020051 if (rc != sizeof(rand)) {
52 LOGP(DAUC, LOGL_ERROR, "Unable to read %zu random "
53 "bytes: rc=%d\n", sizeof(rand), rc);
54 goto out;
55 }
56
57 if (aud2g && !aud3g) {
58 /* 2G only case: output directly to vec */
Neels Hofmeyr4307ad92016-12-16 16:11:11 +010059 DEBUGP(DAUC, "compute vector [%u]/%u: 2G only\n",
60 i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +020061 rc = osmo_auth_gen_vec(vec+i, aud2g, rand);
62 if (rc < 0) {
63 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
64 "generation: %d\n", rc);
65 goto out;
66 }
67 } else if (aud3g) {
68 /* 3G or 3G + 2G case */
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010069 if (!aud2g)
70 DEBUGP(DAUC, "compute vector [%u]/%u: 3G only\n",
71 i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +020072 if (rand_auts && auts)
73 rc = osmo_auth_gen_vec_auts(vec+i, aud3g,
Neels Hofmeyr912a3032017-02-03 06:02:28 +010074 auts, rand_auts,
75 rand);
Harald Weltee72cf552016-04-28 07:18:49 +020076 else
77 rc = osmo_auth_gen_vec(vec+i, aud3g, rand);
78 if (rc < 0) {
79 LOGP(DAUC, LOGL_ERROR, "Error in 3G vector "
80 "generation: %d\n", rc);
81 goto out;
82 }
83 }
84 if (aud2g && aud3g) {
85 /* separate 2G + 3G case: patch 2G into 3G */
86 struct osmo_auth_vector vtmp;
Neels Hofmeyr4307ad92016-12-16 16:11:11 +010087 DEBUGP(DAUC, "compute vector [%u]/%u:"
88 " separate 2G + 3G\n", i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +020089 rc = osmo_auth_gen_vec(&vtmp, aud2g, rand);
90 if (rc < 0) {
91 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
92 "generation: %d\n", rc);
93 goto out;
94 }
95 memcpy(&vec[i].kc, vtmp.kc, sizeof(vec[i].kc));
96 memcpy(&vec[i].sres, vtmp.sres, sizeof(vec[i].sres));
97 vec[i].auth_types |= OSMO_AUTH_TYPE_GSM;
98 }
99 }
100out:
101 return i;
102}