blob: 33d8eda80d37e0c5786badaf4a95cb1e72839206 [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
29 * of both. Handles re-synchrnization if rand_auts and auts are set */
30int 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));
50 if (rc != sizeof(rand)) {
51 LOGP(DAUC, LOGL_ERROR, "Unable to read %zu random "
52 "bytes: rc=%d\n", sizeof(rand), rc);
53 goto out;
54 }
55
56 if (aud2g && !aud3g) {
57 /* 2G only case: output directly to vec */
Neels Hofmeyr4307ad92016-12-16 16:11:11 +010058 DEBUGP(DAUC, "compute vector [%u]/%u: 2G only\n",
59 i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +020060 rc = osmo_auth_gen_vec(vec+i, aud2g, rand);
61 if (rc < 0) {
62 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
63 "generation: %d\n", rc);
64 goto out;
65 }
66 } else if (aud3g) {
67 /* 3G or 3G + 2G case */
Neels Hofmeyr4307ad92016-12-16 16:11:11 +010068 DEBUGP(DAUC, "compute vector [%u]/%u: 3G or 3G + 2G\n",
69 i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +020070 if (rand_auts && auts)
71 rc = osmo_auth_gen_vec_auts(vec+i, aud3g,
72 rand_auts,
73 auts, rand);
74 else
75 rc = osmo_auth_gen_vec(vec+i, aud3g, rand);
76 if (rc < 0) {
77 LOGP(DAUC, LOGL_ERROR, "Error in 3G vector "
78 "generation: %d\n", rc);
79 goto out;
80 }
81 }
82 if (aud2g && aud3g) {
83 /* separate 2G + 3G case: patch 2G into 3G */
84 struct osmo_auth_vector vtmp;
Neels Hofmeyr4307ad92016-12-16 16:11:11 +010085 DEBUGP(DAUC, "compute vector [%u]/%u:"
86 " separate 2G + 3G\n", i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +020087 rc = osmo_auth_gen_vec(&vtmp, aud2g, rand);
88 if (rc < 0) {
89 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
90 "generation: %d\n", rc);
91 goto out;
92 }
93 memcpy(&vec[i].kc, vtmp.kc, sizeof(vec[i].kc));
94 memcpy(&vec[i].sres, vtmp.sres, sizeof(vec[i].sres));
95 vec[i].auth_types |= OSMO_AUTH_TYPE_GSM;
96 }
97 }
98out:
99 return i;
100}