Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 1 | /* 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 Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 26 | #include <osmocom/core/utils.h> |
| 27 | #include <osmocom/core/linuxlist.h> |
| 28 | #include <osmocom/core/plugin.h> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 29 | |
| 30 | #include <osmocom/crypt/gprs_cipher.h> |
| 31 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 32 | /*! \addtogroup crypto |
| 33 | * @{ |
| 34 | */ |
| 35 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 36 | static LLIST_HEAD(gprs_ciphers); |
| 37 | |
| 38 | static struct gprs_cipher_impl *selected_ciphers[_GPRS_ALGO_NUM]; |
| 39 | |
Max | b897c42 | 2016-06-28 14:03:21 +0200 | [diff] [blame] | 40 | const struct value_string gprs_cipher_names[] = { |
| 41 | { GPRS_ALGO_GEA0, "GEA0" }, |
| 42 | { GPRS_ALGO_GEA1, "GEA1" }, |
| 43 | { GPRS_ALGO_GEA2, "GEA2" }, |
| 44 | { GPRS_ALGO_GEA3, "GEA3" }, |
| 45 | { GPRS_ALGO_GEA4, "GEA4" }, |
| 46 | { 0, NULL }, |
| 47 | }; |
| 48 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 49 | /* register a cipher with the core */ |
| 50 | int gprs_cipher_register(struct gprs_cipher_impl *ciph) |
| 51 | { |
Harald Welte | 4876dcf | 2011-07-16 12:03:13 +0200 | [diff] [blame] | 52 | if (ciph->algo >= ARRAY_SIZE(selected_ciphers)) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 53 | return -ERANGE; |
| 54 | |
| 55 | llist_add_tail(&ciph->list, &gprs_ciphers); |
| 56 | |
| 57 | /* check if we want to select this implementation over others */ |
| 58 | if (!selected_ciphers[ciph->algo] || |
| 59 | (selected_ciphers[ciph->algo]->priority > ciph->priority)) |
| 60 | selected_ciphers[ciph->algo] = ciph; |
| 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | /* load all available GPRS cipher plugins */ |
| 66 | int gprs_cipher_load(const char *path) |
| 67 | { |
| 68 | /* load all plugins available from path */ |
Max | bf990bb | 2016-04-21 14:46:30 +0200 | [diff] [blame] | 69 | if (path) |
| 70 | return osmo_plugin_load_all(path); |
| 71 | return 0; |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* function to be called by core code */ |
| 75 | 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] | 76 | uint8_t *kc, uint32_t iv, enum gprs_cipher_direction dir) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 77 | { |
Harald Welte | 4876dcf | 2011-07-16 12:03:13 +0200 | [diff] [blame] | 78 | if (algo >= ARRAY_SIZE(selected_ciphers)) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 79 | return -ERANGE; |
| 80 | |
| 81 | if (!selected_ciphers[algo]) |
| 82 | return -EINVAL; |
| 83 | |
| 84 | if (len > GSM0464_CIPH_MAX_BLOCK) |
| 85 | return -ERANGE; |
| 86 | |
| 87 | /* run the actual cipher from the plugin */ |
| 88 | return selected_ciphers[algo]->run(out, len, kc, iv, dir); |
| 89 | } |
| 90 | |
Max | bf990bb | 2016-04-21 14:46:30 +0200 | [diff] [blame] | 91 | /*! \brief Obtain key lenght for given GPRS cipher |
| 92 | * \param[in] algo Enum representive GPRS cipher |
| 93 | * \returns unsigned integer key length for supported algorithms, |
| 94 | * for GEA0 and unknown ciphers will return 0 |
| 95 | */ |
| 96 | unsigned gprs_cipher_key_length(enum gprs_ciph_algo algo) |
| 97 | { |
| 98 | switch (algo) { |
| 99 | case GPRS_ALGO_GEA0: return 0; |
| 100 | case GPRS_ALGO_GEA1: |
| 101 | case GPRS_ALGO_GEA2: |
| 102 | case GPRS_ALGO_GEA3: return 8; |
| 103 | case GPRS_ALGO_GEA4: return 16; |
| 104 | default: return 0; |
| 105 | } |
| 106 | } |
| 107 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 108 | int gprs_cipher_supported(enum gprs_ciph_algo algo) |
| 109 | { |
Harald Welte | 4876dcf | 2011-07-16 12:03:13 +0200 | [diff] [blame] | 110 | if (algo >= ARRAY_SIZE(selected_ciphers)) |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 111 | return -ERANGE; |
| 112 | |
| 113 | if (selected_ciphers[algo]) |
| 114 | return 1; |
| 115 | |
| 116 | return 0; |
| 117 | } |
Harald Welte | e335b91 | 2010-06-30 19:50:14 +0200 | [diff] [blame] | 118 | |
| 119 | /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */ |
| 120 | uint32_t gprs_cipher_gen_input_ui(uint32_t iov_ui, uint8_t sapi, uint32_t lfn, uint32_t oc) |
| 121 | { |
Harald Welte | 6cfa56b | 2017-01-04 10:06:09 +0100 | [diff] [blame] | 122 | uint32_t sx = ((1<<27) * sapi) + ((uint32_t ) 1<<31); |
Harald Welte | e335b91 | 2010-06-30 19:50:14 +0200 | [diff] [blame] | 123 | |
| 124 | return (iov_ui ^ sx) + lfn + oc; |
| 125 | } |
| 126 | |
| 127 | /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */ |
| 128 | uint32_t gprs_cipher_gen_input_i(uint32_t iov_i, uint32_t lfn, uint32_t oc) |
| 129 | { |
| 130 | return iov_i + lfn + oc; |
| 131 | } |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 132 | /*! @} */ |