blob: c2a13a58936fa8d63c9f00ced0a6c3a2e9e848f2 [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 Welted82e0eb2011-12-06 21:53:42 +010075
76 if (!impl)
77 return -ENOENT;
78
79 return impl->gen_vec(vec, aud, _rand);
80}
81
82int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
83 struct osmo_sub_auth_data *aud,
84 const uint8_t *rand_auts, const uint8_t *auts,
85 const uint8_t *_rand)
86{
Harald Welte781bd5d2011-12-06 22:23:52 +010087 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welted82e0eb2011-12-06 21:53:42 +010088
89 if (!impl || !impl->gen_vec_auts)
90 return -ENOENT;
91
92 return impl->gen_vec_auts(vec, aud, rand_auts, auts, _rand);
93}
Harald Weltea5ab1622011-12-07 02:33:11 +010094
95const struct value_string auth_alg_vals[] = {
96 { OSMO_AUTH_ALG_NONE, "None" },
97 { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" },
98 { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" },
99 { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" },
100 { OSMO_AUTH_ALG_XOR, "XOR" },
101 { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" },
102 { 0, NULL }
103};
104
105const char *osmo_auth_alg_name(enum osmo_auth_algo alg)
106{
107 return get_value_string(auth_alg_vals, alg);
108}
109
110enum osmo_auth_algo osmo_auth_alg_parse(const char *name)
111{
112 return get_string_value(auth_alg_vals, name);
113}