From 14e76b9958b6cb93944a991ffaa5125f601f3d20 Mon Sep 17 00:00:00 2001 From: Philipp Maier Date: Tue, 28 Mar 2017 18:36:52 +0200 Subject: gsm0808: Add utils for Encryption Information The planned support for true A over IP requires the encoding of the an Encryption Information element (see also BSS_MAP_MSG_CIPHER_MODE_CMD). This commt adds encoding/decoding functionality and tests for the element mentioned above, however, it is not yet actively used. Change-Id: I8262050a9d9fd3f17462cfbb046c6e034dccc6fb --- include/osmocom/gsm/gsm0808_utils.h | 8 ++++ include/osmocom/gsm/protocol/gsm_08_08.h | 10 +++++ src/gsm/gsm0808_utils.c | 71 ++++++++++++++++++++++++++++++++ src/gsm/libosmogsm.map | 2 + tests/gsm0808/gsm0808_test.c | 40 ++++++++++++++++++ 5 files changed, 131 insertions(+) diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h index 48e737d1..5bd27c76 100644 --- a/include/osmocom/gsm/gsm0808_utils.h +++ b/include/osmocom/gsm/gsm0808_utils.h @@ -55,3 +55,11 @@ uint8_t gsm0808_enc_channel_type(struct msgb *msg, /* Decode Channel Type element */ int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct, const uint8_t *elem, uint8_t len); + +/* Encode Encryption Information element */ +uint8_t gsm0808_enc_encrypt_info(struct msgb *msg, + const struct gsm0808_encrypt_info *ei); + +/* Decode Encryption Information element */ +int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei, + const uint8_t *elem, uint8_t len); diff --git a/include/osmocom/gsm/protocol/gsm_08_08.h b/include/osmocom/gsm/protocol/gsm_08_08.h index e2355f68..3939aedd 100644 --- a/include/osmocom/gsm/protocol/gsm_08_08.h +++ b/include/osmocom/gsm/protocol/gsm_08_08.h @@ -447,3 +447,13 @@ struct gsm0808_channel_type { uint8_t perm_spch[CH_TYPE_PERM_SPCH_MAXLEN]; unsigned int perm_spch_len; }; + +/* 3GPP TS 48.008 3.2.2.10 Encryption Information */ +#define ENCRY_INFO_KEY_MAXLEN 252 +#define ENCRY_INFO_PERM_ALGO_MAXLEN 8 +struct gsm0808_encrypt_info { + uint8_t perm_algo[ENCRY_INFO_PERM_ALGO_MAXLEN]; + unsigned int perm_algo_len; + uint8_t key[ENCRY_INFO_KEY_MAXLEN]; + unsigned int key_len; +}; diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c index ef0f9431..19156050 100644 --- a/src/gsm/gsm0808_utils.c +++ b/src/gsm/gsm0808_utils.c @@ -33,6 +33,7 @@ #define CHANNEL_TYPE_ELEMENT_MAXLEN 11 #define CHANNEL_TYPE_ELEMENT_MINLEN 3 +#define ENCRYPT_INFO_ELEMENT_MINLEN 1 /* Encode AoIP transport address element */ uint8_t gsm0808_enc_aoip_trasp_addr(struct msgb *msg, @@ -379,3 +380,73 @@ int gsm0808_dec_channel_type(struct gsm0808_channel_type *ct, return (int)(elem - old_elem); } + +/* Encode Encryption Information element */ +uint8_t gsm0808_enc_encrypt_info(struct msgb *msg, + const struct gsm0808_encrypt_info *ei) +{ + unsigned int i; + uint8_t perm_algo = 0; + uint8_t *ptr; + uint8_t *old_tail; + uint8_t *tlv_len; + + OSMO_ASSERT(msg); + OSMO_ASSERT(ei); + OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key)); + OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN); + + msgb_put_u8(msg, GSM0808_IE_ENCRYPTION_INFORMATION); + tlv_len = msgb_put(msg, 1); + old_tail = msg->tail; + + for (i = 0; i < ei->perm_algo_len; i++) { + /* Note: gsm_08_08.h defines the permitted algorithms + * as an enum which ranges from 0x01 to 0x08 */ + OSMO_ASSERT(ei->perm_algo[i] != 0); + OSMO_ASSERT(ei->perm_algo[i] <= ENCRY_INFO_PERM_ALGO_MAXLEN); + perm_algo |= (1 << (ei->perm_algo[i] - 1)); + } + + msgb_put_u8(msg, perm_algo); + ptr = msgb_put(msg, ei->key_len); + memcpy(ptr, ei->key, ei->key_len); + + *tlv_len = (uint8_t) (msg->tail - old_tail); + return *tlv_len + 2; +} + +/* Decode Encryption Information element */ +int gsm0808_dec_encrypt_info(struct gsm0808_encrypt_info *ei, + const uint8_t *elem, uint8_t len) +{ + uint8_t perm_algo; + unsigned int i; + unsigned int perm_algo_len = 0; + const uint8_t *old_elem = elem; + + OSMO_ASSERT(ei); + if (!elem) + return -EINVAL; + if (len <= 0) + return -EINVAL; + + memset(ei, 0, sizeof(*ei)); + + perm_algo = *elem; + elem++; + + for (i = 0; i < ENCRY_INFO_PERM_ALGO_MAXLEN; i++) { + if (perm_algo & (1 << i)) { + ei->perm_algo[perm_algo_len] = i + 1; + perm_algo_len++; + } + } + ei->perm_algo_len = perm_algo_len; + + ei->key_len = len - 1; + memcpy(ei->key, elem, ei->key_len); + elem+=ei->key_len; + + return (int)(elem - old_elem); +} diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map index 2ba0c427..323ad5ad 100644 --- a/src/gsm/libosmogsm.map +++ b/src/gsm/libosmogsm.map @@ -148,6 +148,8 @@ gsm0808_enc_speech_codec_list; gsm0808_dec_speech_codec_list; gsm0808_enc_channel_type; gsm0808_dec_channel_type; +gsm0808_enc_encrypt_info; +gsm0808_dec_encrypt_info; gsm0858_rsl_ul_meas_enc; diff --git a/tests/gsm0808/gsm0808_test.c b/tests/gsm0808/gsm0808_test.c index 0dd9e846..5fe9c876 100644 --- a/tests/gsm0808/gsm0808_test.c +++ b/tests/gsm0808/gsm0808_test.c @@ -571,6 +571,45 @@ static void test_gsm0808_enc_dec_channel_type() msgb_free(msg); } +static void test_gsm0808_enc_dec_encrypt_info() +{ + struct gsm0808_encrypt_info enc_ei; + struct gsm0808_encrypt_info dec_ei; + struct msgb *msg; + uint8_t ei_enc_expected[] = + { GSM0808_IE_ENCRYPTION_INFORMATION, 0x09, 0x03, 0xaa, 0xbb, + 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42 + }; + uint8_t rc_enc; + int rc_dec; + + memset(&enc_ei, 0, sizeof(enc_ei)); + enc_ei.perm_algo[0] = GSM0808_ALG_ID_A5_0; + enc_ei.perm_algo[1] = GSM0808_ALG_ID_A5_1; + enc_ei.perm_algo_len = 2; + enc_ei.key[0] = 0xaa; + enc_ei.key[1] = 0xbb; + enc_ei.key[2] = 0xcc; + enc_ei.key[3] = 0xdd; + enc_ei.key[4] = 0xee; + enc_ei.key[5] = 0xff; + enc_ei.key[6] = 0x23; + enc_ei.key[7] = 0x42; + enc_ei.key_len = 8; + + msg = msgb_alloc(1024, "output buffer"); + rc_enc = gsm0808_enc_encrypt_info(msg, &enc_ei); + OSMO_ASSERT(rc_enc == 11); + OSMO_ASSERT(memcmp(ei_enc_expected, msg->data, msg->len) == 0); + + rc_dec = gsm0808_dec_encrypt_info(&dec_ei, msg->data + 2, msg->len - 2); + OSMO_ASSERT(rc_dec == 9); + + OSMO_ASSERT(memcmp(&enc_ei, &dec_ei, sizeof(enc_ei)) == 0); + + msgb_free(msg); +} + int main(int argc, char **argv) { printf("Testing generation of GSM0808 messages\n"); @@ -597,6 +636,7 @@ int main(int argc, char **argv) test_gsm0808_enc_dec_speech_codec_ext_with_cfg(); test_gsm0808_enc_dec_speech_codec_list(); test_gsm0808_enc_dec_channel_type(); + test_gsm0808_enc_dec_encrypt_info(); printf("Done\n"); return EXIT_SUCCESS; -- cgit v1.2.3