blob: 9ca5d93b4cc9b6d9e2632ec452d18ca69c081b5d [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
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020046 * \returns 0 on success, or a negative error code on failure
Harald Welte007a71e2012-07-18 19:47:56 +020047 *
48 * This function is called by an authentication implementation plugin to
49 * register itself with the authentication core.
50 */
Harald Welted82e0eb2011-12-06 21:53:42 +010051int osmo_auth_register(struct osmo_auth_impl *impl)
52{
53 if (impl->algo >= ARRAY_SIZE(selected_auths))
54 return -ERANGE;
55
56 llist_add_tail(&impl->list, &osmo_auths);
57
58 /* check if we want to select this implementation over others */
59 if (!selected_auths[impl->algo] ||
60 (selected_auths[impl->algo]->priority > impl->priority))
61 selected_auths[impl->algo] = impl;
62
63 return 0;
64}
65
Harald Welte007a71e2012-07-18 19:47:56 +020066/*! \brief Load all available authentication plugins from the given path
67 * \param[in] path Path name of the directory containing the plugins
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020068 * \returns number of plugins loaded in case of success, negative in case of error
Harald Welte007a71e2012-07-18 19:47:56 +020069 *
70 * This function will load all plugins contained in the specified path.
71 */
Harald Welted82e0eb2011-12-06 21:53:42 +010072int osmo_auth_load(const char *path)
73{
74 /* load all plugins available from path */
75 return osmo_plugin_load_all(path);
76}
77
Harald Welte007a71e2012-07-18 19:47:56 +020078/*! \brief Determine if a given authentication algorithm is supported
79 * \param[in] algo Algorithm which should be checked
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +020080 * \returns 1 if algo is supported, 0 if not, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +020081 *
82 * This function is used by an application to determine at runtime if a
83 * given authentication algorithm is supported or not.
84 */
Harald Welted82e0eb2011-12-06 21:53:42 +010085int osmo_auth_supported(enum osmo_auth_algo algo)
86{
87 if (algo >= ARRAY_SIZE(selected_auths))
88 return -ERANGE;
89
90 if (selected_auths[algo])
91 return 1;
92
93 return 0;
94}
95
Harald Weltecd9cb902016-04-20 10:39:00 +020096/* C5 function to derive UMTS IK from GSM Kc */
97static inline void c5_function(uint8_t *ik, const uint8_t *kc)
98{
99 unsigned int i;
100
101 for (i = 0; i < 4; i++)
102 ik[i] = kc[i] ^ kc[i+4];
103 memcpy(ik+4, kc, 8);
104 for (i = 12; i < 16; i++)
105 ik[i] = ik[i-12];
106}
107
108/* C4 function to derive UMTS CK from GSM Kc */
Maxceae1232016-06-27 18:12:49 +0200109void osmo_c4(uint8_t *ck, const uint8_t *kc)
Harald Weltecd9cb902016-04-20 10:39:00 +0200110{
111 memcpy(ck, kc, 8);
112 memcpy(ck+8, kc, 8);
113}
114
115/*! \brief Generate 3G CK + IK from 2G authentication vector
116 * \param vec Authentication Vector to be modified
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200117 * \returns 1 if the vector was changed, 0 otherwise
Harald Weltecd9cb902016-04-20 10:39:00 +0200118 *
119 * This function performs the C5 and C4 functions to derive the UMTS key
120 * material from the GSM key material in the supplied vector, _if_ the input
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200121 * vector doesn't yet have UMTS authentication capability.
122 */
Harald Weltecd9cb902016-04-20 10:39:00 +0200123int osmo_auth_3g_from_2g(struct osmo_auth_vector *vec)
124{
125 if ((vec->auth_types & OSMO_AUTH_TYPE_GSM) &&
126 !(vec->auth_types & OSMO_AUTH_TYPE_UMTS)) {
127 c5_function(vec->ik, vec->kc);
Maxceae1232016-06-27 18:12:49 +0200128 osmo_c4(vec->ck, vec->kc);
Harald Weltecd9cb902016-04-20 10:39:00 +0200129 /* We cannot actually set OSMO_AUTH_TYPE_UMTS as we have no
130 * AUTN and no RES, and thus can only perform GSM
131 * authentication with this tuple.
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200132 */
Harald Weltecd9cb902016-04-20 10:39:00 +0200133 return 1;
134 }
135
136 return 0;
137}
138
Harald Welte007a71e2012-07-18 19:47:56 +0200139/*! \brief Generate authentication vector
140 * \param[out] vec Generated authentication vector
141 * \param[in] aud Subscriber-specific key material
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100142 * \param[in] _rand Random challenge to be used
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200143 * \returns 0 on success, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +0200144 *
145 * This function performs the core cryptographic function of the AUC,
146 * computing authentication triples/quintuples based on the permanent
147 * subscriber data and a random value. The result is what is forwarded
148 * by the AUC via HLR and VLR to the MSC which will then be able to
149 * invoke authentication with the MS
150 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100151int osmo_auth_gen_vec(struct osmo_auth_vector *vec,
152 struct osmo_sub_auth_data *aud,
153 const uint8_t *_rand)
154{
Harald Welte781bd5d2011-12-06 22:23:52 +0100155 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Harald Welte4afdd5d2011-12-07 02:38:18 +0100156 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +0100157
158 if (!impl)
159 return -ENOENT;
160
Harald Welte4afdd5d2011-12-07 02:38:18 +0100161 rc = impl->gen_vec(vec, aud, _rand);
162 if (rc < 0)
163 return rc;
164
165 memcpy(vec->rand, _rand, sizeof(vec->rand));
166
167 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +0100168}
169
Harald Welte007a71e2012-07-18 19:47:56 +0200170/*! \brief Generate authentication vector and re-sync sequence
171 * \param[out] vec Generated authentication vector
172 * \param[in] aud Subscriber-specific key material
Harald Welte007a71e2012-07-18 19:47:56 +0200173 * \param[in] auts AUTS value sent by the SIM/MS
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100174 * \param[in] rand_auts RAND value sent by the SIM/MS
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +0100175 * \param[in] _rand Random challenge to be used to generate vector
Neels Hofmeyrc7bf8d02016-05-08 22:23:40 +0200176 * \returns 0 on success, negative error on failure
Harald Welte007a71e2012-07-18 19:47:56 +0200177 *
178 * This function performs a special variant of the core cryptographic
179 * function of the AUC: computing authentication triples/quintuples
180 * based on the permanent subscriber data, a random value as well as the
181 * AUTS and RAND values returned by the SIM/MS. This special variant is
182 * needed if the sequence numbers between MS and AUC have for some
Thorsten Alteholza81055d2017-03-02 22:13:48 +0100183 * reason become different.
Harald Welte007a71e2012-07-18 19:47:56 +0200184 */
Harald Welted82e0eb2011-12-06 21:53:42 +0100185int osmo_auth_gen_vec_auts(struct osmo_auth_vector *vec,
186 struct osmo_sub_auth_data *aud,
Neels Hofmeyr03ab9a62017-02-03 05:00:24 +0100187 const uint8_t *auts, const uint8_t *rand_auts,
Harald Welted82e0eb2011-12-06 21:53:42 +0100188 const uint8_t *_rand)
189{
Harald Welte781bd5d2011-12-06 22:23:52 +0100190 struct osmo_auth_impl *impl = selected_auths[aud->algo];
Neels Hofmeyr3b8cb392017-02-21 19:54:36 +0100191 int rc;
Harald Welted82e0eb2011-12-06 21:53:42 +0100192
193 if (!impl || !impl->gen_vec_auts)
194 return -ENOENT;
195
Neels Hofmeyr3b8cb392017-02-21 19:54:36 +0100196 rc = impl->gen_vec_auts(vec, aud, auts, rand_auts, _rand);
197 if (rc < 0)
198 return rc;
199
200 memcpy(vec->rand, _rand, sizeof(vec->rand));
201
202 return 0;
Harald Welted82e0eb2011-12-06 21:53:42 +0100203}
Harald Weltea5ab1622011-12-07 02:33:11 +0100204
Harald Welte1c72bfb2012-04-04 22:05:24 +0200205static const struct value_string auth_alg_vals[] = {
Harald Weltea5ab1622011-12-07 02:33:11 +0100206 { OSMO_AUTH_ALG_NONE, "None" },
207 { OSMO_AUTH_ALG_COMP128v1, "COMP128v1" },
208 { OSMO_AUTH_ALG_COMP128v2, "COMP128v2" },
209 { OSMO_AUTH_ALG_COMP128v3, "COMP128v3" },
210 { OSMO_AUTH_ALG_XOR, "XOR" },
211 { OSMO_AUTH_ALG_MILENAGE, "MILENAGE" },
212 { 0, NULL }
213};
214
Harald Welte007a71e2012-07-18 19:47:56 +0200215/*! \brief Get human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100216const char *osmo_auth_alg_name(enum osmo_auth_algo alg)
217{
218 return get_value_string(auth_alg_vals, alg);
219}
220
Harald Welte007a71e2012-07-18 19:47:56 +0200221/*! \brief Parse human-readable name of authentication algorithm */
Harald Weltea5ab1622011-12-07 02:33:11 +0100222enum osmo_auth_algo osmo_auth_alg_parse(const char *name)
223{
224 return get_string_value(auth_alg_vals, name);
225}
Harald Welte007a71e2012-07-18 19:47:56 +0200226
227/*! @} */