[PATCH] libosmocore[master]: gsm0808: Add utils for Encryption Information

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

dexter gerrit-no-reply at lists.osmocom.org
Thu Mar 30 16:44:53 UTC 2017


Review at  https://gerrit.osmocom.org/2180

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
---
M include/osmocom/gsm/gsm0808_utils.h
M include/osmocom/gsm/protocol/gsm_08_08.h
M src/gsm/gsm0808_utils.c
M src/gsm/libosmogsm.map
M tests/gsm0808/gsm0808_test.c
5 files changed, 126 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/80/2180/1

diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h
index 6a2259f..2196f94 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -50,3 +50,10 @@
 /* Decode Channel Type element */
 struct gsm0808_channel_type *gsm0808_dec_channel_type(const void *ctx,
 						      struct msgb *msg);
+
+/* Encode Encryption Information element */
+struct msgb *gsm0808_enc_encrypt_info(struct gsm0808_encrypt_info *ei);
+
+/* Decode Encryption Information element */
+struct gsm0808_encrypt_info *gsm0808_dec_encrypt_info(const void *ctx,
+						      struct msgb *msg);
diff --git a/include/osmocom/gsm/protocol/gsm_08_08.h b/include/osmocom/gsm/protocol/gsm_08_08.h
index 981dda5..d52af9f 100644
--- a/include/osmocom/gsm/protocol/gsm_08_08.h
+++ b/include/osmocom/gsm/protocol/gsm_08_08.h
@@ -442,3 +442,13 @@
 	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 54b8a70..7dfe2e9 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -34,6 +34,7 @@
 
 #define CHANNEL_TYPE_ELEMENT_MAXLEN 11
 #define CHANNEL_TYPE_ELEMENT_MINLEN 3
+#define ENCRYPT_INFO_ELEMENT_MINLEN 1
 
 /* Encode AoIP transport address element */
 struct msgb *gsm0808_enc_aoip_trasp_addr(struct sockaddr_storage *ss)
@@ -356,3 +357,72 @@
 
 	return ct;
 }
+
+/* Encode Encryption Information element */
+struct msgb *gsm0808_enc_encrypt_info(struct gsm0808_encrypt_info *ei)
+{
+	struct msgb *msg;
+	unsigned int i;
+	uint8_t perm_algo = 0;
+	uint8_t *ptr;
+
+	OSMO_ASSERT(ei);
+	OSMO_ASSERT(ei->key_len <= ARRAY_SIZE(ei->key));
+	OSMO_ASSERT(ei->perm_algo_len <= ENCRY_INFO_PERM_ALGO_MAXLEN);
+
+	msg =
+	    msgb_alloc(ELEMENT_MSGB_MAXLEN, "Encryption Information Element");
+	if (!msg)
+		return NULL;
+
+	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);
+
+	return msg;
+}
+
+/* Decode Encryption Information element */
+struct gsm0808_encrypt_info *gsm0808_dec_encrypt_info(const void *ctx,
+						      struct msgb *msg)
+{
+	struct gsm0808_encrypt_info *ei;
+	uint8_t perm_algo;
+	unsigned int i;
+	uint8_t *ptr;
+	unsigned int perm_algo_len = 0;
+
+	if (!msg)
+		return NULL;
+
+	/* Malformed element */
+	if (msg->len < ENCRYPT_INFO_ELEMENT_MINLEN)
+		return NULL;
+
+	ei = talloc_zero(ctx, struct gsm0808_encrypt_info);
+	if (!ei)
+		return NULL;
+
+	perm_algo = msgb_pull_u8(msg);
+	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 = msg->len;
+	ptr = msgb_get(msg, msg->len);
+	memcpy(ei->key, ptr, ei->key_len);
+
+	return ei;
+}
diff --git a/src/gsm/libosmogsm.map b/src/gsm/libosmogsm.map
index 60cc742..e64fdd9 100644
--- a/src/gsm/libosmogsm.map
+++ b/src/gsm/libosmogsm.map
@@ -148,6 +148,8 @@
 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 51edce2..e52cba7 100644
--- a/tests/gsm0808/gsm0808_test.c
+++ b/tests/gsm0808/gsm0808_test.c
@@ -619,6 +619,42 @@
 	msgb_free(ct_enc);
 }
 
+
+static void test_gsm0808_enc_dec_encrypt_info(const void *ctx)
+{
+	struct gsm0808_encrypt_info ei;
+	struct msgb *ei_enc;
+	uint8_t ei_enc_expected[] =
+	    { 0x03, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x23, 0x42 };
+	struct gsm0808_encrypt_info *ei_dec;
+
+	memset(&ei, 0, sizeof(ei));
+	ei.perm_algo[0] = GSM0808_ALG_ID_A5_0;
+	ei.perm_algo[1] = GSM0808_ALG_ID_A5_1;
+	ei.perm_algo_len = 2;
+	ei.key[0] = 0xaa;
+	ei.key[1] = 0xbb;
+	ei.key[2] = 0xcc;
+	ei.key[3] = 0xdd;
+	ei.key[4] = 0xee;
+	ei.key[5] = 0xff;
+	ei.key[6] = 0x23;
+	ei.key[7] = 0x42;
+	ei.key_len = 8;
+
+	ei_enc = gsm0808_enc_encrypt_info(&ei);
+	OSMO_ASSERT(ei_enc);
+	OSMO_ASSERT(memcmp(ei_enc_expected, ei_enc->data, ei_enc->len) == 0);
+
+	ei_dec = gsm0808_dec_encrypt_info(ctx, ei_enc);
+	OSMO_ASSERT(ei_dec);
+	OSMO_ASSERT(ei_enc->len == 0);
+	OSMO_ASSERT(memcmp(&ei, ei_dec, sizeof(ei)) == 0);
+
+	talloc_free(ei_dec);
+	msgb_free(ei_enc);
+}
+
 int main(int argc, char **argv)
 {
 	void *ctx;
@@ -649,6 +685,7 @@
 	test_gsm0808_enc_dec_speech_codec_ext_with_cfg(ctx);
 	test_gsm0808_enc_dec_speech_codec_list(ctx);
 	test_gsm0808_enc_dec_channel_type(ctx);
+	test_gsm0808_enc_dec_encrypt_info(ctx);
 
 	printf("Done\n");
 

-- 
To view, visit https://gerrit.osmocom.org/2180
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8262050a9d9fd3f17462cfbb046c6e034dccc6fb
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: dexter <pmaier at sysmocom.de>



More information about the gerrit-log mailing list