blob: e4fac04bcb21fb9764f5f37ae579d903a10fd438 [file] [log] [blame]
Harald Welteb9ce51c2010-06-30 19:43:11 +02001/* GPRS LLC cipher core infrastructure */
2
3/* (C) 2010 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
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/utils.h>
27#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/plugin.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020029
30#include <osmocom/crypt/gprs_cipher.h>
31
32static LLIST_HEAD(gprs_ciphers);
33
34static struct gprs_cipher_impl *selected_ciphers[_GPRS_ALGO_NUM];
35
36/* register a cipher with the core */
37int gprs_cipher_register(struct gprs_cipher_impl *ciph)
38{
Harald Welte4876dcf2011-07-16 12:03:13 +020039 if (ciph->algo >= ARRAY_SIZE(selected_ciphers))
Harald Welteb9ce51c2010-06-30 19:43:11 +020040 return -ERANGE;
41
42 llist_add_tail(&ciph->list, &gprs_ciphers);
43
44 /* check if we want to select this implementation over others */
45 if (!selected_ciphers[ciph->algo] ||
46 (selected_ciphers[ciph->algo]->priority > ciph->priority))
47 selected_ciphers[ciph->algo] = ciph;
48
49 return 0;
50}
51
52/* load all available GPRS cipher plugins */
53int gprs_cipher_load(const char *path)
54{
55 /* load all plugins available from path */
Maxbf990bb2016-04-21 14:46:30 +020056 if (path)
57 return osmo_plugin_load_all(path);
58 return 0;
Harald Welteb9ce51c2010-06-30 19:43:11 +020059}
60
61/* function to be called by core code */
62int gprs_cipher_run(uint8_t *out, uint16_t len, enum gprs_ciph_algo algo,
Maxbf990bb2016-04-21 14:46:30 +020063 uint8_t *kc, uint32_t iv, enum gprs_cipher_direction dir)
Harald Welteb9ce51c2010-06-30 19:43:11 +020064{
Harald Welte4876dcf2011-07-16 12:03:13 +020065 if (algo >= ARRAY_SIZE(selected_ciphers))
Harald Welteb9ce51c2010-06-30 19:43:11 +020066 return -ERANGE;
67
68 if (!selected_ciphers[algo])
69 return -EINVAL;
70
71 if (len > GSM0464_CIPH_MAX_BLOCK)
72 return -ERANGE;
73
74 /* run the actual cipher from the plugin */
75 return selected_ciphers[algo]->run(out, len, kc, iv, dir);
76}
77
Maxbf990bb2016-04-21 14:46:30 +020078/*! \brief Obtain key lenght for given GPRS cipher
79 * \param[in] algo Enum representive GPRS cipher
80 * \returns unsigned integer key length for supported algorithms,
81 * for GEA0 and unknown ciphers will return 0
82 */
83unsigned gprs_cipher_key_length(enum gprs_ciph_algo algo)
84{
85 switch (algo) {
86 case GPRS_ALGO_GEA0: return 0;
87 case GPRS_ALGO_GEA1:
88 case GPRS_ALGO_GEA2:
89 case GPRS_ALGO_GEA3: return 8;
90 case GPRS_ALGO_GEA4: return 16;
91 default: return 0;
92 }
93}
94
Harald Welteb9ce51c2010-06-30 19:43:11 +020095int gprs_cipher_supported(enum gprs_ciph_algo algo)
96{
Harald Welte4876dcf2011-07-16 12:03:13 +020097 if (algo >= ARRAY_SIZE(selected_ciphers))
Harald Welteb9ce51c2010-06-30 19:43:11 +020098 return -ERANGE;
99
100 if (selected_ciphers[algo])
101 return 1;
102
103 return 0;
104}
Harald Weltee335b912010-06-30 19:50:14 +0200105
106/* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */
107uint32_t gprs_cipher_gen_input_ui(uint32_t iov_ui, uint8_t sapi, uint32_t lfn, uint32_t oc)
108{
109 uint32_t sx = ((1<<27) * sapi) + (1<<31);
110
111 return (iov_ui ^ sx) + lfn + oc;
112}
113
114/* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */
115uint32_t gprs_cipher_gen_input_i(uint32_t iov_i, uint32_t lfn, uint32_t oc)
116{
117 return iov_i + lfn + oc;
118}