summaryrefslogtreecommitdiffstats
path: root/src/auc_core.c
blob: 0fd09f86f58d8338f9ce4b619c5cb58107a264ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <errno.h>
#include <string.h>

#include <osmocom/core/linuxlist.h>
#include <osmocom/crypt/auth.h>

#include "auc.h"

#define NBUCKETS 1024
static struct llist_head auc_buckets[NBUCKETS];

int auc_core_init(void *ctx)
{
	int i;

	for (i = 0; i < NBUCKETS; i++)
		INIT_LLIST_HEAD(&auc_buckets[i]);

	return 0;
}

static unsigned int osmo_auc_hashfn(const char *imsi)
{
	int len = strlen(imsi);
	unsigned int res;

	res = atoi(imsi + len - 6);

	return res % NBUCKETS;
}

int auc_add_rec(struct auc_rec *rec)
{
	struct auc_rec *exist;
	unsigned int hash = osmo_auc_hashfn(rec->imsi);
	struct llist_head *list = &auc_buckets[hash];

	/* make sure we don't get duplicates */
	llist_for_each_entry(exist, list, hash_list) {
		if (!strcmp(exist->imsi, rec->imsi))
			return -EEXIST;
	}

	llist_add(&rec->hash_list, list);

	return 0;
}

struct auc_rec *auc_lookup(const char *imsi)
{
	struct auc_rec *exist;
	unsigned int hash = osmo_auc_hashfn(imsi);
	struct llist_head *list = &auc_buckets[hash];

	/* make sure we don't get duplicates */
	llist_for_each_entry(exist, list, hash_list) {
		if (!strcmp(exist->imsi, imsi))
			return exist;
	}

	return NULL;
}

int auc_gen_vecs(struct osmo_auth_vector *vec,
		 const char *imsi, int n_vecs)
{
	struct auc_rec *rec;
	int i;

	rec = auc_lookup(imsi);
	if (!rec)
		return -ENOENT;

	for (i = 0; i < n_vecs; i++) {
		uint8_t rand[16];
		int rc;

		rc = auc_get_rand(rand, sizeof(rand));
		if (rc < 0)
			return rc;
		rc = osmo_auth_gen_vec(&vec[i], &rec->auth, rand);
		if (rc < 0)
			return rc;
	}

	return 0;
}

int auc_gen_vec_auts(struct osmo_auth_vector *vec,
		     const char *imsi, const uint8_t *rand_auts,
		     const uint8_t *auts)
{
	struct auc_rec *rec;
	uint8_t rand[16];
	int rc;

	rec = auc_lookup(imsi);
	if (!rec)
		return -ENOENT;

	rc = auc_get_rand(rand, sizeof(rand));
	if (rc < 0)
		return rc;

	return osmo_auth_gen_vec_auts(vec, &rec->auth, rand_auts, auts, rand);
}