Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gprs_cipher_core.c |
| 2 | * GPRS LLC cipher core infrastructure */ |
| 3 | /* |
| 4 | * (C) 2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <errno.h> |
| 25 | #include <stdint.h> |
| 26 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 27 | #include <osmocom/core/utils.h> |
| 28 | #include <osmocom/core/linuxlist.h> |
| 29 | #include <osmocom/core/plugin.h> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 30 | |
| 31 | #include <osmocom/crypt/gprs_cipher.h> |
| 32 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 33 | /*! \addtogroup crypto |
| 34 | * @{ |
| 35 | */ |
| 36 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 37 | static LLIST_HEAD(gprs_ciphers); |
| 38 | |
| 39 | static struct gprs_cipher_impl *selected_ciphers[_GPRS_ALGO_NUM]; |
| 40 | |
Max | b897c42 | 2016-06-28 14:03:21 +0200 | [diff] [blame] | 41 | const 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 Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 50 | /* register a cipher with the core */ |
| 51 | int gprs_cipher_register(struct gprs_cipher_impl *ciph) |
| 52 | { |
Harald Welte | 4876dcf | 2011-07-16 12:03:13 +0200 | [diff] [blame] | 53 | if (ciph->algo >= ARRAY_SIZE(selected_ciphers)) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 54 | 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 */ |
| 67 | int gprs_cipher_load(const char *path) |
| 68 | { |
| 69 | /* load all plugins available from path */ |
Max | bf990bb | 2016-04-21 14:46:30 +0200 | [diff] [blame] | 70 | if (path) |
| 71 | return osmo_plugin_load_all(path); |
| 72 | return 0; |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | /* function to be called by core code */ |
| 76 | int gprs_cipher_run(uint8_t *out, uint16_t len, enum gprs_ciph_algo algo, |
Max | bf990bb | 2016-04-21 14:46:30 +0200 | [diff] [blame] | 77 | uint8_t *kc, uint32_t iv, enum gprs_cipher_direction dir) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 78 | { |
Harald Welte | 4876dcf | 2011-07-16 12:03:13 +0200 | [diff] [blame] | 79 | if (algo >= ARRAY_SIZE(selected_ciphers)) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 80 | return -ERANGE; |
| 81 | |
| 82 | if (!selected_ciphers[algo]) |
| 83 | return -EINVAL; |
| 84 | |
| 85 | if (len > GSM0464_CIPH_MAX_BLOCK) |
| 86 | return -ERANGE; |
| 87 | |
| 88 | /* run the actual cipher from the plugin */ |
| 89 | return selected_ciphers[algo]->run(out, len, kc, iv, dir); |
| 90 | } |
| 91 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 92 | /*! Obtain key lenght for given GPRS cipher |
Max | bf990bb | 2016-04-21 14:46:30 +0200 | [diff] [blame] | 93 | * \param[in] algo Enum representive GPRS cipher |
| 94 | * \returns unsigned integer key length for supported algorithms, |
| 95 | * for GEA0 and unknown ciphers will return 0 |
| 96 | */ |
| 97 | unsigned gprs_cipher_key_length(enum gprs_ciph_algo algo) |
| 98 | { |
| 99 | switch (algo) { |
| 100 | case GPRS_ALGO_GEA0: return 0; |
| 101 | case GPRS_ALGO_GEA1: |
| 102 | case GPRS_ALGO_GEA2: |
| 103 | case GPRS_ALGO_GEA3: return 8; |
| 104 | case GPRS_ALGO_GEA4: return 16; |
| 105 | default: return 0; |
| 106 | } |
| 107 | } |
| 108 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 109 | int gprs_cipher_supported(enum gprs_ciph_algo algo) |
| 110 | { |
Harald Welte | 4876dcf | 2011-07-16 12:03:13 +0200 | [diff] [blame] | 111 | if (algo >= ARRAY_SIZE(selected_ciphers)) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 112 | return -ERANGE; |
| 113 | |
| 114 | if (selected_ciphers[algo]) |
| 115 | return 1; |
| 116 | |
| 117 | return 0; |
| 118 | } |
Harald Welte | e335b91 | 2010-06-30 19:50:14 +0200 | [diff] [blame] | 119 | |
| 120 | /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */ |
| 121 | uint32_t gprs_cipher_gen_input_ui(uint32_t iov_ui, uint8_t sapi, uint32_t lfn, uint32_t oc) |
| 122 | { |
Harald Welte | 6cfa56b | 2017-01-04 10:06:09 +0100 | [diff] [blame] | 123 | uint32_t sx = ((1<<27) * sapi) + ((uint32_t ) 1<<31); |
Harald Welte | e335b91 | 2010-06-30 19:50:14 +0200 | [diff] [blame] | 124 | |
| 125 | return (iov_ui ^ sx) + lfn + oc; |
| 126 | } |
| 127 | |
| 128 | /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */ |
| 129 | uint32_t gprs_cipher_gen_input_i(uint32_t iov_i, uint32_t lfn, uint32_t oc) |
| 130 | { |
| 131 | return iov_i + lfn + oc; |
| 132 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 133 | /*! @} */ |