Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gprs_cipher.h */ |
| 2 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 3 | #pragma once |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 4 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 5 | #include <osmocom/core/linuxlist.h> |
Max | b897c42 | 2016-06-28 14:03:21 +0200 | [diff] [blame] | 6 | #include <osmocom/core/utils.h> |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 7 | |
Max | 4f16950 | 2016-06-30 10:39:00 +0200 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 10 | #define GSM0464_CIPH_MAX_BLOCK 1523 |
| 11 | |
Max | dda9dff | 2016-06-28 17:05:35 +0200 | [diff] [blame] | 12 | /* 3GPP TS 24.008 ยง 10.5.5.3 */ |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 13 | enum gprs_ciph_algo { |
Max | dda9dff | 2016-06-28 17:05:35 +0200 | [diff] [blame] | 14 | GPRS_ALGO_GEA0 = 0, |
| 15 | GPRS_ALGO_GEA1 = 1, |
| 16 | GPRS_ALGO_GEA2 = 2, |
| 17 | GPRS_ALGO_GEA3 = 3, |
| 18 | GPRS_ALGO_GEA4 = 4, |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 19 | _GPRS_ALGO_NUM |
| 20 | }; |
| 21 | |
Max | c57e5da | 2016-06-27 16:19:12 +0200 | [diff] [blame] | 22 | /* 3GPP TS 04.64 Table A.1: */ |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 23 | enum gprs_cipher_direction { |
Max | c57e5da | 2016-06-27 16:19:12 +0200 | [diff] [blame] | 24 | GPRS_CIPH_MS2SGSN = 0, |
| 25 | GPRS_CIPH_SGSN2MS = 1, |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 26 | }; |
| 27 | |
Max | b897c42 | 2016-06-28 14:03:21 +0200 | [diff] [blame] | 28 | extern const struct value_string gprs_cipher_names[]; |
| 29 | |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 30 | /* An implementation of a GPRS cipher */ |
| 31 | struct gprs_cipher_impl { |
| 32 | struct llist_head list; |
| 33 | enum gprs_ciph_algo algo; |
| 34 | const char *name; |
| 35 | unsigned int priority; |
| 36 | |
| 37 | /* As specified in 04.64 Annex A. Uses Kc, IV and direction |
| 38 | * to generate the 1523 bytes cipher stream that need to be |
| 39 | * XORed wit the plaintext for encrypt / ciphertext for decrypt */ |
Max | bf990bb | 2016-04-21 14:46:30 +0200 | [diff] [blame] | 40 | int (*run)(uint8_t *out, uint16_t len, uint8_t *kc, uint32_t iv, |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 41 | enum gprs_cipher_direction direction); |
| 42 | }; |
| 43 | |
| 44 | /* register a cipher with the core (from a plugin) */ |
| 45 | int gprs_cipher_register(struct gprs_cipher_impl *ciph); |
| 46 | |
| 47 | /* load all available GPRS cipher plugins */ |
| 48 | int gprs_cipher_load(const char *path); |
| 49 | |
| 50 | /* function to be called by core code */ |
| 51 | 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] | 52 | uint8_t *kc, uint32_t iv, enum gprs_cipher_direction dir); |
Harald Welte | b9ce51c | 2010-06-30 19:43:11 +0200 | [diff] [blame] | 53 | |
| 54 | /* Do we have an implementation for this cipher? */ |
| 55 | int gprs_cipher_supported(enum gprs_ciph_algo algo); |
| 56 | |
Max | bf990bb | 2016-04-21 14:46:30 +0200 | [diff] [blame] | 57 | /* Return key length for supported cipher, in bytes */ |
| 58 | unsigned gprs_cipher_key_length(enum gprs_ciph_algo algo); |
| 59 | |
Harald Welte | e335b91 | 2010-06-30 19:50:14 +0200 | [diff] [blame] | 60 | /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */ |
| 61 | uint32_t gprs_cipher_gen_input_ui(uint32_t iov_ui, uint8_t sapi, uint32_t lfn, uint32_t oc); |
| 62 | |
| 63 | /* GSM TS 04.64 / Section A.2.1 : Generation of 'input' */ |
| 64 | uint32_t gprs_cipher_gen_input_i(uint32_t iov_i, uint32_t lfn, uint32_t oc); |