blob: 8ceafad59b1c413fe195539ab59a45576402cddf [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 */
58 rc = osmo_auth_gen_vec(vec+i, aud2g, rand);
59 if (rc < 0) {
60 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
61 "generation: %d\n", rc);
62 goto out;
63 }
64 } else if (aud3g) {
65 /* 3G or 3G + 2G case */
66 if (rand_auts && auts)
67 rc = osmo_auth_gen_vec_auts(vec+i, aud3g,
68 rand_auts,
69 auts, rand);
70 else
71 rc = osmo_auth_gen_vec(vec+i, aud3g, rand);
72 if (rc < 0) {
73 LOGP(DAUC, LOGL_ERROR, "Error in 3G vector "
74 "generation: %d\n", rc);
75 goto out;
76 }
77 }
78 if (aud2g && aud3g) {
79 /* separate 2G + 3G case: patch 2G into 3G */
80 struct osmo_auth_vector vtmp;
81 rc = osmo_auth_gen_vec(&vtmp, aud2g, rand);
82 if (rc < 0) {
83 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
84 "generation: %d\n", rc);
85 goto out;
86 }
87 memcpy(&vec[i].kc, vtmp.kc, sizeof(vec[i].kc));
88 memcpy(&vec[i].sres, vtmp.sres, sizeof(vec[i].sres));
89 vec[i].auth_types |= OSMO_AUTH_TYPE_GSM;
90 }
91 }
92out:
93 return i;
94}