[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
Tue Apr 4 14:51:52 UTC 2017


Hello Jenkins Builder,

I'd like you to reexamine a change.  Please visit

    https://gerrit.osmocom.org/2180

to look at the new patch set (#4).

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, 131 insertions(+), 0 deletions(-)


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

diff --git a/include/osmocom/gsm/gsm0808_utils.h b/include/osmocom/gsm/gsm0808_utils.h
index 48e737d..5bd27c7 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -55,3 +55,11 @@
 /* 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 e2355f6..3939aed 100644
--- a/include/osmocom/gsm/protocol/gsm_08_08.h
+++ b/include/osmocom/gsm/protocol/gsm_08_08.h
@@ -447,3 +447,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 ef0f943..1915605 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 @@
 
 	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 2ba0c42..323ad5a 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 0dd9e84..5fe9c87 100644
--- a/tests/gsm0808/gsm0808_test.c
+++ b/tests/gsm0808/gsm0808_test.c
@@ -571,6 +571,45 @@
 	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 @@
 	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;

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

Gerrit-MessageType: newpatchset
Gerrit-Change-Id: I8262050a9d9fd3f17462cfbb046c6e034dccc6fb
Gerrit-PatchSet: 4
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: dexter <pmaier at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Jenkins Builder



More information about the gerrit-log mailing list