1 /* GPRS LLC cipher core infrastructure */
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
26 #include <osmocom/core/utils.h>
27 #include <osmocom/core/linuxlist.h>
28 #include <osmocom/core/plugin.h>
30 #include <osmocom/crypt/gprs_cipher.h>
32 static LLIST_HEAD(gprs_ciphers);
34 static struct gprs_cipher_impl *selected_ciphers[_GPRS_ALGO_NUM];
36 /* register a cipher with the core */
37 int gprs_cipher_register(struct gprs_cipher_impl *ciph)
39 if (ciph->algo >= ARRAY_SIZE(selected_ciphers))
42 llist_add_tail(&ciph->list, &gprs_ciphers);
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;
52 /* load all available GPRS cipher plugins */
53 int gprs_cipher_load(const char *path)
55 /* load all plugins available from path */
56 return osmo_plugin_load_all(path);
59 /* function to be called by core code */
60 int gprs_cipher_run(uint8_t *out, uint16_t len, enum gprs_ciph_algo algo,
61 uint64_t kc, uint32_t iv, enum gprs_cipher_direction dir)
63 if (algo >= ARRAY_SIZE(selected_ciphers))
66 if (!selected_ciphers[algo])
69 if (len > GSM0464_CIPH_MAX_BLOCK)
72 /* run the actual cipher from the plugin */
73 return selected_ciphers[algo]->run(out, len, kc, iv, dir);
76 int gprs_cipher_supported(enum gprs_ciph_algo algo)
78 if (algo >= ARRAY_SIZE(selected_ciphers))
81 if (selected_ciphers[algo])
87 /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */
88 uint32_t gprs_cipher_gen_input_ui(uint32_t iov_ui, uint8_t sapi, uint32_t lfn, uint32_t oc)
90 uint32_t sx = ((1<<27) * sapi) + (1<<31);
92 return (iov_ui ^ sx) + lfn + oc;
95 /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */
96 uint32_t gprs_cipher_gen_input_i(uint32_t iov_i, uint32_t lfn, uint32_t oc)
98 return iov_i + lfn + oc;