blob: b78cdd50d1b270f980a1ae4560dcfffd04c47f26 [file] [log] [blame]
Harald Welted82e0eb2011-12-06 21:53:42 +01001/* GSM/GPRS/3G authentication core infrastructure */
2
Harald Welte007a71e2012-07-18 19:47:56 +02003/* (C) 2010-2012 by Harald Welte <laforge@gnumonks.org>
Harald Welted82e0eb2011-12-06 21:53:42 +01004 *
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
Harald Welte007a71e2012-07-18 19:47:56 +020033/*! \addtogroup auth
34 * @{
35 */
36
37/* \file auth_core.c
38 */
39
Harald Welted82e0eb2011-12-06 21:53:42 +010040static LLIST_HEAD(osmo_auths);
41
42static struct osmo_auth_impl *selected_auths[_OSMO_AUTH_ALG_NUM];
43
Harald Welte007a71e2012-07-18 19:47:56 +020044/*! \brief Register an authentication algorithm implementation with the core
45 * \param[in] impl Structure describing implementation and it's callbacks
46 *
47 * This function is called by an authentication implementation plugin to
48 * register itself with the authentication core.
49 */
Harald Welted82e0eb2011-12-06 21:53:42 +010050int osmo_auth_register(struct osmo_auth_impl *impl)
51{
52 if (impl->algo >= ARRAY_SIZE(selected_auths))
53 return -ERANGE;
54
55 llist_add_tail(&impl->list, &osmo_auths);
56
57 /* check if we want to select this implementation over others */
58 if (!selected_auths[impl->algo] ||
59 (selected_auths[impl->algo]->priority > impl->priority))
60 selected_auths[impl->algo] = impl;
61
62 return 0;
63}
64
Harald Welte007a71e2012-07-18 19:47:56 +020065/*! \brief Load all available authentication plugins from the given path
66 * \param[in] path Path name of the directory containing the plugins
67 *
68 * This function will load all plugins contained in the specified path.
69 */
Harald Welted82e0eb2011-12-06 21:53:42 +010070int osmo_auth_load(const char *path)
71{
72 /* load all plugins available from path */
73 return osmo_plugin_load_all(path);
74}
75
Harald Welte007a71e2012-07-18 19:47:56 +020076/*! \brief Determine if a given authentication algorithm is supported
77 * \param[in] algo Algorithm which should be checked
78 *
79 * This function is used by an application to determine at runtime if a
80 * given authentication algorithm is supported or not.
81 */
Harald Welted82e0eb2011-12-06 21:53:42 +010082int osmo_auth_supported(enum osmo_auth_algo algo)
83{
84 if (algo >= ARRAY_SIZE(selected_auths))
85 return -ERANGE;
86
87 if (selected_auths[algo])
88 return 1;
89
90 return 0;
91}
92
Harald Weltecd9cb902016-04-20 10:39:00 +020093/* C5 function to derive UMTS IK from GSM Kc */
94static inline void c5_function(uint8_t *ik, const uint8_t *kc)
95{
96 unsigned int i;
97
98 for (i = 0; i < 4; i++)
99 ik[i] = kc[i] ^ kc[i+4];
100 memcpy(ik+4, kc, 8);
101 for (i = 12; i < 16; i++)
102 ik[i] = ik[i-12];
103}
104
105/* C4 function to derive UMTS CK from GSM Kc */
106static inline void c4_function(uint8_t *ck, const uint8_t *kc)
107{
108 memcpy(ck, kc, 8);
109 memcpy(ck+8, kc, 8);
110}
111
112/*! \brief Generate 3G CK + IK from 2G authentication vector
113 * \param vec Authentication Vector to be modified
114 *
115 * This function performs the C5 and C4 functions to derive the UMTS key
116 * material from the GSM key material in the supplied vector, _if_ the input
117 * vector doesn't yet have UMTS authentication capability */
118int osmo_auth_3g_from_2g(struct osmo_auth_vector *vec)
119{
120 if ((vec->auth_types & OSMO_AUTH_TYPE_GSM) &&
121 !(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) {
122 c5_function(vec->ik, vec->kc);
123 c4_function(vec->ck, vec->kc);
124 /* We cannot actually set OSMO_AUTH_TYPE_UMTS as we have no
125 * AUTN and no RES, and thus can only perform GSM
126 * authentication with this tuple.
127 * */
128 return 1;
129 }
130
131 return 0;
132}
133
Harald Welte007a71e2012-07-18 19:47:56 +0200134/*! \brief Generate authentication vector
135 * \param[out] vec Generated authentication vector
136 * \param[in] aud Subscriber-specific key material
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100137 * \param[in] _rand Random challenge to be used
Harald Welte007a71e2012-07-18 19:47:56 +0200138 *
139 * This function performs the core cryptographic function of the AUC,
140 * computing authentication triples/quintuples based on the permanent
141 * subscriber data and a random value. The result is what is forwarded
142 * by the AUC via HLR and VLR to the MSC which will then be able to
143 * invoke authentication with the MS
144 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100145int osmo_auth_gen_vec(struct osmo_auth_vector *vec,
146 struct osmo_sub_auth_data *aud,
147 const uint8_t *_rand)
148{
Harald Welte781bd5d2011-12-06 22:23:52 +0100149 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welte4afdd5d2011-12-07 02:38:18 +0100150 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +0100151
152 if (!impl)
153 return -ENOENT;
154
Harald Welte4afdd5d2011-12-07 02:38:18 +0100155 rc = impl->gen_vec(vec, aud, _rand);
156 if (rc < 0)
157 return rc;
158
159 memcpy(vec->rand, _rand, sizeof(vec->rand));
160
161 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +0100162}
163
Harald Welte007a71e2012-07-18 19:47:56 +0200164/*! \brief Generate authentication vector and re-sync sequence
165 * \param[out] vec Generated authentication vector
166 * \param[in] aud Subscriber-specific key material
167 * \param[in] rand_auts RAND value sent by the SIM/MS
168 * \param[in] auts AUTS value sent by the SIM/MS
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100169 * \param[in] _rand Random challenge to be used to generate vector
Harald Welte007a71e2012-07-18 19:47:56 +0200170 *
171 * This function performs a special variant of the core cryptographic
172 * function of the AUC: computing authentication triples/quintuples
173 * based on the permanent subscriber data, a random value as well as the
174 * AUTS and RAND values returned by the SIM/MS. This special variant is
175 * needed if the sequence numbers between MS and AUC have for some
176 * reason become diffrent.
177 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100178int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
179 struct osmo_sub_auth_data *aud,
180 const uint8_t *rand_auts, const uint8_t *auts,
181 const uint8_t *_rand)
182{
Harald Welte781bd5d2011-12-06 22:23:52 +0100183 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welted82e0eb2011-12-06 21:53:42 +0100184
185 if (!impl || !impl->gen_vec_auts)
186 return -ENOENT;
187
188 return impl->gen_vec_auts(vec, aud, rand_auts, auts, _rand);
189}
Harald Weltea5ab1622011-12-07 02:33:11 +0100190
Harald Welte1c72bfb2012-04-04 22:05:24 +0200191static const struct value_string auth_alg_vals[] = {
Harald Weltea5ab1622011-12-07 02:33:11 +0100192 { OSMO_AUTH_ALG_NONE, "None" },
193 { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" },
194 { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" },
195 { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" },
196 { OSMO_AUTH_ALG_XOR, "XOR" },
197 { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" },
198 { 0, NULL }
199};
200
Harald Welte007a71e2012-07-18 19:47:56 +0200201/*! \brief Get human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100202const char *osmo_auth_alg_name(enum osmo_auth_algo alg)
203{
204 return get_value_string(auth_alg_vals, alg);
205}
206
Harald Welte007a71e2012-07-18 19:47:56 +0200207/*! \brief Parse human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100208enum osmo_auth_algo osmo_auth_alg_parse(const char *name)
209{
210 return get_string_value(auth_alg_vals, name);
211}
Harald Welte007a71e2012-07-18 19:47:56 +0200212
213/*! @} */