blob: a30793117f1290269fa1093ab76208650cb6782d [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
Neels Hofmeyr569d3222017-02-21 22:57:11 +010039 if (aud2g && (aud2g->algo == OSMO_AUTH_ALG_NONE
40 || aud2g->type == OSMO_AUTH_TYPE_NONE))
Harald Weltee72cf552016-04-28 07:18:49 +020041 aud2g = NULL;
Neels Hofmeyr569d3222017-02-21 22:57:11 +010042 if (aud3g && (aud3g->algo == OSMO_AUTH_ALG_NONE
43 || aud3g->type == OSMO_AUTH_TYPE_NONE))
Harald Weltee72cf552016-04-28 07:18:49 +020044 aud3g = NULL;
45
Neels Hofmeyr569d3222017-02-21 22:57:11 +010046 if (!aud2g && !aud3g) {
47 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
48 " with neither 2G nor 3G auth data available\n");
Harald Weltee72cf552016-04-28 07:18:49 +020049 return -1;
Neels Hofmeyr569d3222017-02-21 22:57:11 +010050 }
51
52 if (aud2g && aud2g->type != OSMO_AUTH_TYPE_GSM) {
53 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
54 " with non-2G auth data passed for aud2g arg\n");
55 return -1;
56 }
57
58 if (aud3g && aud3g->type != OSMO_AUTH_TYPE_UMTS) {
59 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() called"
60 " with non-3G auth data passed for aud3g arg\n");
61 return -1;
62 }
63
64 if ((rand_auts != NULL) != (auts != NULL)) {
65 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() with only one"
66 " of AUTS and AUTS_RAND given, need both or neither\n");
67 return -1;
68 }
69
70 if (auts && !aud3g) {
71 LOGP(DAUC, LOGL_ERROR, "auc_compute_vectors() with AUTS called"
72 " but no 3G auth data passed\n");
73 return -1;
74 }
Harald Weltee72cf552016-04-28 07:18:49 +020075
76 /* compute quintuples */
77 for (i = 0; i < num_vec; i++) {
78 rc = rand_get(rand, sizeof(rand));
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010079 DEBUGP(DAUC, "rand %s\n", osmo_hexdump_nospc(rand, sizeof(rand)));
Harald Weltee72cf552016-04-28 07:18:49 +020080 if (rc != sizeof(rand)) {
81 LOGP(DAUC, LOGL_ERROR, "Unable to read %zu random "
82 "bytes: rc=%d\n", sizeof(rand), rc);
83 goto out;
84 }
85
86 if (aud2g && !aud3g) {
87 /* 2G only case: output directly to vec */
Neels Hofmeyr4307ad92016-12-16 16:11:11 +010088 DEBUGP(DAUC, "compute vector [%u]/%u: 2G only\n",
89 i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +020090 rc = osmo_auth_gen_vec(vec+i, aud2g, rand);
91 if (rc < 0) {
92 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
93 "generation: %d\n", rc);
94 goto out;
95 }
96 } else if (aud3g) {
97 /* 3G or 3G + 2G case */
Neels Hofmeyr8cde6622017-01-31 02:10:40 +010098 if (!aud2g)
99 DEBUGP(DAUC, "compute vector [%u]/%u: 3G only\n",
100 i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +0200101 if (rand_auts && auts)
102 rc = osmo_auth_gen_vec_auts(vec+i, aud3g,
Neels Hofmeyr912a3032017-02-03 06:02:28 +0100103 auts, rand_auts,
104 rand);
Harald Weltee72cf552016-04-28 07:18:49 +0200105 else
106 rc = osmo_auth_gen_vec(vec+i, aud3g, rand);
107 if (rc < 0) {
108 LOGP(DAUC, LOGL_ERROR, "Error in 3G vector "
109 "generation: %d\n", rc);
110 goto out;
111 }
112 }
113 if (aud2g && aud3g) {
114 /* separate 2G + 3G case: patch 2G into 3G */
115 struct osmo_auth_vector vtmp;
Neels Hofmeyr4307ad92016-12-16 16:11:11 +0100116 DEBUGP(DAUC, "compute vector [%u]/%u:"
117 " separate 2G + 3G\n", i, num_vec);
Harald Weltee72cf552016-04-28 07:18:49 +0200118 rc = osmo_auth_gen_vec(&vtmp, aud2g, rand);
119 if (rc < 0) {
120 LOGP(DAUC, LOGL_ERROR, "Error in 2G vector "
121 "generation: %d\n", rc);
122 goto out;
123 }
124 memcpy(&vec[i].kc, vtmp.kc, sizeof(vec[i].kc));
125 memcpy(&vec[i].sres, vtmp.sres, sizeof(vec[i].sres));
126 vec[i].auth_types |= OSMO_AUTH_TYPE_GSM;
127 }
128 }
129out:
130 return i;
131}