blob: 97e581db9f80610cad2c31b9d63821731d121eb5 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_cipher_core.c
2 * GPRS LLC cipher core infrastructure */
3/*
4 * (C) 2010 by Harald Welte <laforge@gnumonks.org>
Harald Welteb9ce51c2010-06-30 19:43:11 +02005 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Harald Welteb9ce51c2010-06-30 19:43:11 +020010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
Harald Welteb9ce51c2010-06-30 19:43:11 +020020 */
21
Max38b17232017-12-01 19:06:21 +010022#include "config.h"
23
Harald Welteb9ce51c2010-06-30 19:43:11 +020024#include <errno.h>
25#include <stdint.h>
26
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/utils.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/plugin.h>
Harald Welteb9ce51c2010-06-30 19:43:11 +020030
31#include <osmocom/crypt/gprs_cipher.h>
32
Harald Welte96e2a002017-06-12 21:44:18 +020033/*! \addtogroup crypto
34 * @{
35 */
36
Harald Welteb9ce51c2010-06-30 19:43:11 +020037static LLIST_HEAD(gprs_ciphers);
38
39static struct gprs_cipher_impl *selected_ciphers[_GPRS_ALGO_NUM];
40
Maxb897c422016-06-28 14:03:21 +020041const struct value_string gprs_cipher_names[] = {
42 { GPRS_ALGO_GEA0, "GEA0" },
43 { GPRS_ALGO_GEA1, "GEA1" },
44 { GPRS_ALGO_GEA2, "GEA2" },
45 { GPRS_ALGO_GEA3, "GEA3" },
46 { GPRS_ALGO_GEA4, "GEA4" },
47 { 0, NULL },
48};
49
Harald Welteb9ce51c2010-06-30 19:43:11 +020050/* register a cipher with the core */
51int gprs_cipher_register(struct gprs_cipher_impl *ciph)
52{
Harald Welte4876dcf2011-07-16 12:03:13 +020053 if (ciph->algo >= ARRAY_SIZE(selected_ciphers))
Harald Welteb9ce51c2010-06-30 19:43:11 +020054 return -ERANGE;
55
56 llist_add_tail(&ciph->list, &gprs_ciphers);
57
58 /* check if we want to select this implementation over others */
59 if (!selected_ciphers[ciph->algo] ||
60 (selected_ciphers[ciph->algo]->priority > ciph->priority))
61 selected_ciphers[ciph->algo] = ciph;
62
63 return 0;
64}
65
66/* load all available GPRS cipher plugins */
67int gprs_cipher_load(const char *path)
68{
Max38b17232017-12-01 19:06:21 +010069#if !defined(EMBEDDED)
Harald Welteb9ce51c2010-06-30 19:43:11 +020070 /* load all plugins available from path */
Maxbf990bb2016-04-21 14:46:30 +020071 if (path)
72 return osmo_plugin_load_all(path);
Max38b17232017-12-01 19:06:21 +010073#endif
Maxbf990bb2016-04-21 14:46:30 +020074 return 0;
Harald Welteb9ce51c2010-06-30 19:43:11 +020075}
76
77/* function to be called by core code */
78int gprs_cipher_run(uint8_t *out, uint16_t len, enum gprs_ciph_algo algo,
Maxbf990bb2016-04-21 14:46:30 +020079 uint8_t *kc, uint32_t iv, enum gprs_cipher_direction dir)
Harald Welteb9ce51c2010-06-30 19:43:11 +020080{
Harald Welte4876dcf2011-07-16 12:03:13 +020081 if (algo >= ARRAY_SIZE(selected_ciphers))
Harald Welteb9ce51c2010-06-30 19:43:11 +020082 return -ERANGE;
83
84 if (!selected_ciphers[algo])
85 return -EINVAL;
86
87 if (len > GSM0464_CIPH_MAX_BLOCK)
88 return -ERANGE;
89
90 /* run the actual cipher from the plugin */
91 return selected_ciphers[algo]->run(out, len, kc, iv, dir);
92}
93
Neels Hofmeyr87e45502017-06-20 00:17:59 +020094/*! Obtain key lenght for given GPRS cipher
Maxbf990bb2016-04-21 14:46:30 +020095 * \param[in] algo Enum representive GPRS cipher
96 * \returns unsigned integer key length for supported algorithms,
97 * for GEA0 and unknown ciphers will return 0
98 */
99unsigned gprs_cipher_key_length(enum gprs_ciph_algo algo)
100{
101 switch (algo) {
102 case GPRS_ALGO_GEA0: return 0;
103 case GPRS_ALGO_GEA1:
104 case GPRS_ALGO_GEA2:
105 case GPRS_ALGO_GEA3: return 8;
106 case GPRS_ALGO_GEA4: return 16;
107 default: return 0;
108 }
109}
110
Harald Welteb9ce51c2010-06-30 19:43:11 +0200111int gprs_cipher_supported(enum gprs_ciph_algo algo)
112{
Harald Welte4876dcf2011-07-16 12:03:13 +0200113 if (algo >= ARRAY_SIZE(selected_ciphers))
Harald Welteb9ce51c2010-06-30 19:43:11 +0200114 return -ERANGE;
115
116 if (selected_ciphers[algo])
117 return 1;
118
119 return 0;
120}
Harald Weltee335b912010-06-30 19:50:14 +0200121
122/* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */
123uint32_t gprs_cipher_gen_input_ui(uint32_t iov_ui, uint8_t sapi, uint32_t lfn, uint32_t oc)
124{
Harald Welte6cfa56b2017-01-04 10:06:09 +0100125 uint32_t sx = ((1<<27) * sapi) + ((uint32_t ) 1<<31);
Harald Weltee335b912010-06-30 19:50:14 +0200126
127 return (iov_ui ^ sx) + lfn + oc;
128}
129
130/* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */
131uint32_t gprs_cipher_gen_input_i(uint32_t iov_i, uint32_t lfn, uint32_t oc)
132{
133 return iov_i + lfn + oc;
134}
Harald Welte96e2a002017-06-12 21:44:18 +0200135/*! @} */