blob: d0c9f1ca97771ccd6b5aedafbc61f4ef931a4774 [file] [log] [blame]
Harald Welted82e0eb2011-12-06 21:53:42 +01001/* GSM/GPRS/3G authentication core infrastructure */
2
3/* (C) 2010-2011 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <errno.h>
24#include <stdint.h>
25
26#include <osmocom/core/utils.h>
27#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/plugin.h>
29
30#include <osmocom/crypt/auth.h>
31
32static LLIST_HEAD(osmo_auths);
33
34static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM];
35
36/* register a cipher with the core */
37int osmo_auth_register(struct osmo_auth_impl *impl)
38{
39 if (impl->algo >= ARRAY_SIZE(selected_auths))
40 return -ERANGE;
41
42 llist_add_tail(&impl->list, &osmo_auths);
43
44 /* check if we want to select this implementation over others */
45 if (!selected_auths[impl->algo] ||
46 (selected_auths[impl->algo]->priority > impl->priority))
47 selected_auths[impl->algo] = impl;
48
49 return 0;
50}
51
52/* load all available GPRS cipher plugins */
53int osmo_auth_load(const char *path)
54{
55 /* load all plugins available from path */
56 return osmo_plugin_load_all(path);
57}
58
59int osmo_auth_supported(enum osmo_auth_algo algo)
60{
61 if (algo >= ARRAY_SIZE(selected_auths))
62 return -ERANGE;
63
64 if (selected_auths[algo])
65 return 1;
66
67 return 0;
68}
69
70int osmo_auth_gen_vec(struct osmo_auth_vector *vec,
71 struct osmo_sub_auth_data *aud,
72 const uint8_t *_rand)
73{
Harald Welte781bd5d2011-12-06 22:23:52 +010074 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welte4afdd5d2011-12-07 02:38:18 +010075 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +010076
77 if (!impl)
78 return -ENOENT;
79
Harald Welte4afdd5d2011-12-07 02:38:18 +010080 rc = impl->gen_vec(vec, aud, _rand);
81 if (rc < 0)
82 return rc;
83
84 memcpy(vec->rand, _rand, sizeof(vec->rand));
85
86 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +010087}
88
89int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
90 struct osmo_sub_auth_data *aud,
91 const uint8_t *rand_auts, const uint8_t *auts,
92 const uint8_t *_rand)
93{
Harald Welte781bd5d2011-12-06 22:23:52 +010094 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welted82e0eb2011-12-06 21:53:42 +010095
96 if (!impl || !impl->gen_vec_auts)
97 return -ENOENT;
98
99 return impl->gen_vec_auts(vec, aud, rand_auts, auts, _rand);
100}
Harald Weltea5ab1622011-12-07 02:33:11 +0100101
102const struct value_string auth_alg_vals[] = {
103 { OSMO_AUTH_ALG_NONE, "None" },
104 { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" },
105 { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" },
106 { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" },
107 { OSMO_AUTH_ALG_XOR, "XOR" },
108 { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" },
109 { 0, NULL }
110};
111
112const char *osmo_auth_alg_name(enum osmo_auth_algo alg)
113{
114 return get_value_string(auth_alg_vals, alg);
115}
116
117enum osmo_auth_algo osmo_auth_alg_parse(const char *name)
118{
119 return get_string_value(auth_alg_vals, name);
120}