blob: f790ff5bf8bef2c1e904c3cad200fec90ec06902 [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>
Harald Welted318e612011-12-07 12:16:27 +010025#include <string.h>
Harald Welted82e0eb2011-12-06 21:53:42 +010026
27#include <osmocom/core/utils.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/plugin.h>
30
31#include <osmocom/crypt/auth.h>
32
33static LLIST_HEAD(osmo_auths);
34
35static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM];
36
37/* register a cipher with the core */
38int osmo_auth_register(struct osmo_auth_impl *impl)
39{
40 if (impl->algo >= ARRAY_SIZE(selected_auths))
41 return -ERANGE;
42
43 llist_add_tail(&impl->list, &osmo_auths);
44
45 /* check if we want to select this implementation over others */
46 if (!selected_auths[impl->algo] ||
47 (selected_auths[impl->algo]->priority > impl->priority))
48 selected_auths[impl->algo] = impl;
49
50 return 0;
51}
52
53/* load all available GPRS cipher plugins */
54int osmo_auth_load(const char *path)
55{
56 /* load all plugins available from path */
57 return osmo_plugin_load_all(path);
58}
59
60int osmo_auth_supported(enum osmo_auth_algo algo)
61{
62 if (algo >= ARRAY_SIZE(selected_auths))
63 return -ERANGE;
64
65 if (selected_auths[algo])
66 return 1;
67
68 return 0;
69}
70
71int osmo_auth_gen_vec(struct osmo_auth_vector *vec,
72 struct osmo_sub_auth_data *aud,
73 const uint8_t *_rand)
74{
Harald Welte781bd5d2011-12-06 22:23:52 +010075 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welte4afdd5d2011-12-07 02:38:18 +010076 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +010077
78 if (!impl)
79 return -ENOENT;
80
Harald Welte4afdd5d2011-12-07 02:38:18 +010081 rc = impl->gen_vec(vec, aud, _rand);
82 if (rc < 0)
83 return rc;
84
85 memcpy(vec->rand, _rand, sizeof(vec->rand));
86
87 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +010088}
89
90int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
91 struct osmo_sub_auth_data *aud,
92 const uint8_t *rand_auts, const uint8_t *auts,
93 const uint8_t *_rand)
94{
Harald Welte781bd5d2011-12-06 22:23:52 +010095 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welted82e0eb2011-12-06 21:53:42 +010096
97 if (!impl || !impl->gen_vec_auts)
98 return -ENOENT;
99
100 return impl->gen_vec_auts(vec, aud, rand_auts, auts, _rand);
101}
Harald Weltea5ab1622011-12-07 02:33:11 +0100102
103const struct value_string auth_alg_vals[] = {
104 { OSMO_AUTH_ALG_NONE, "None" },
105 { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" },
106 { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" },
107 { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" },
108 { OSMO_AUTH_ALG_XOR, "XOR" },
109 { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" },
110 { 0, NULL }
111};
112
113const char *osmo_auth_alg_name(enum osmo_auth_algo alg)
114{
115 return get_value_string(auth_alg_vals, alg);
116}
117
118enum osmo_auth_algo osmo_auth_alg_parse(const char *name)
119{
120 return get_string_value(auth_alg_vals, name);
121}