blob: a72dda846fec347d9bf4d53e2faf7301280498fa [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
26#include <osmocore/utils.h>
27#include <osmocore/linuxlist.h>
28#include <osmocore/plugin.h>
29
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{
39 if (ciph->algo > ARRAY_SIZE(selected_ciphers))
40 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 */
56 return plugin_load_all(path);
57}
58
59/* function to be called by core code */
60int 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)
62{
63 if (algo > ARRAY_SIZE(selected_ciphers))
64 return -ERANGE;
65
66 if (!selected_ciphers[algo])
67 return -EINVAL;
68
69 if (len > GSM0464_CIPH_MAX_BLOCK)
70 return -ERANGE;
71
72 /* run the actual cipher from the plugin */
73 return selected_ciphers[algo]->run(out, len, kc, iv, dir);
74}
75
76int gprs_cipher_supported(enum gprs_ciph_algo algo)
77{
78 if (algo > ARRAY_SIZE(selected_ciphers))
79 return -ERANGE;
80
81 if (selected_ciphers[algo])
82 return 1;
83
84 return 0;
85}