Change in ...osmo-ttcn3-hacks[master]: TTCN-3 native function wrapper for SNOW-3G

2019-08-18 Thread laforge
laforge has submitted this change and it was merged. ( 
https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/15202 )

Change subject: TTCN-3 native function wrapper for SNOW-3G
..

TTCN-3 native function wrapper for SNOW-3G

Change-Id: I7fca69ea8b4aed48d5a64885b762ab85be71ef03
---
A mme/LTE_CryptoFunctionDefs.cc
A mme/LTE_CryptoFunctions.ttcn
A mme/key_derivation.c
A mme/key_derivation.h
M mme/regen_makefile.sh
5 files changed, 557 insertions(+), 2 deletions(-)

Approvals:
  pespin: Looks good to me, approved
  Jenkins Builder: Verified



diff --git a/mme/LTE_CryptoFunctionDefs.cc b/mme/LTE_CryptoFunctionDefs.cc
new file mode 100644
index 000..da2d521
--- /dev/null
+++ b/mme/LTE_CryptoFunctionDefs.cc
@@ -0,0 +1,189 @@
+/* Utility functions from ogslib imported to TTCN-3
+ *
+ * (C) 2019 Harald Welte 
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include "snow-3g.h"
+#include "key_derivation.h"
+
+//#define DEBUG
+
+#ifdef DEBUG
+static __thread char hexd_buff[4096];
+static const char hex_chars[] = "0123456789abcdef";
+
+static const char *_osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const 
unsigned char *buf, int len, const char *delim,
+bool delim_after_last)
+{
+   int i;
+   char *cur = out_buf;
+   size_t delim_len;
+
+   if (!out_buf || !out_buf_size)
+   return "";
+
+   delim = delim ? : "";
+   delim_len = strlen(delim);
+
+   for (i = 0; i < len; i++) {
+   const char *delimp = delim;
+   int len_remain = out_buf_size - (cur - out_buf) - 1;
+   if (len_remain < (2 + delim_len)
+   && !(!delim_after_last && i == (len - 1) && len_remain >= 
2))
+   break;
+
+   *cur++ = hex_chars[buf[i] >> 4];
+   *cur++ = hex_chars[buf[i] & 0xf];
+
+   if (i == (len - 1) && !delim_after_last)
+   break;
+
+   while (len_remain > 1 && *delimp) {
+   *cur++ = *delimp++;
+   len_remain--;
+   }
+   }
+   *cur = '\0';
+   return out_buf;
+}
+
+static char *_osmo_hexdump(const unsigned char *buf, int len)
+{
+   _osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, "", true);
+   return hexd_buff;
+}
+#endif
+
+namespace LTE__CryptoFunctions {
+
+
+/* f8.
+* Input key: 128 bit Confidentiality Key as OCT16.
+* Input count:32-bit Count, Frame dependent input as INTEGER.
+* Input bearer: 5-bit Bearer identity (in the LSB side) as BIT5.
+* Input is_dlwnlink: Direction of transmission.
+* Input data: length number of bits, input bit stream as OCTETSTRING.
+* Output data: Output bit stream. Assumes data is suitably memory
+* allocated.
+* Encrypts/decrypts blocks of data between 1 and 2^32 bits in length as
+* defined in Section 3.
+*/
+OCTETSTRING f__snow__3g__f8(const OCTETSTRING& key, const INTEGER& count, 
const INTEGER & bearer,
+   const BOOLEAN& is_downlink, const OCTETSTRING& data)
+{
+   TTCN_Buffer ttcn_buf_data(data);
+   TTCN_Buffer ttcn_buf_key(key);
+   uint32_t direction = (uint32_t)is_downlink;
+
+   snow_3g_f8((u8 *)ttcn_buf_key.get_data(), (u32) count, (u32)bearer, 
direction,
+  (u8 *)ttcn_buf_data.get_data(), ttcn_buf_data.get_len());
+
+   return OCTETSTRING(ttcn_buf_data.get_len(), ttcn_buf_data.get_data());
+}
+
+/* f9.
+* Input key: 128 bit Integrity Key as OCT16.
+* Input count:32-bit Count, Frame dependent input as UINT32.
+* Input fresh: 32-bit Random number as UINT32.
+* Input is_downlink:1 Direction of transmission.
+* Input data: input bit stream.
+* Output : 32 bit block used as MAC
+* Generates 32-bit MAC using UIA2 algorithm as defined in Section 4.
+*/
+
+OCTETSTRING f__snow__3g__f9(const OCTETSTRING& key, const INTEGER& count, 
const INTEGER& fresh,
+   const BOOLEAN& is_downlink, const OCTETSTRING& data)
+{
+   TTCN_Buffer ttcn_buf_data(data);
+   TTCN_Buffer ttcn_buf_key(key);
+   uint32_t direction = (uint32_t)is_downlink;
+   uint8_t tmp[4];
+   TTCN_Buffer ttcn_buf_mac;
+
+#ifdef DEBUG
+   printf("F9: key=%s, count=%u, fresh=%u, direction=%u, ",
+   _osmo_hexdump((u8 *)ttcn_buf_key.get_data(), 
ttcn_buf_key.get_len()), (u32) count,
+   (u32) fresh, direction);
+   printf("data=%s -> ", _osmo_hexdump(ttcn_buf_data.get_data(), 
ttcn_buf_data.get_len()));
+#endif
+   snow_3g_f9((u8 *)ttcn_buf_key.get_data(), (u32) count, (u32) fresh, 
direction,
+  (u8 *)ttcn_buf_data.get_data(), ttcn_buf_data.get_len()*8, 
tmp);
+#ifdef DEBUG
+   

Change in ...osmo-ttcn3-hacks[master]: TTCN-3 native function wrapper for SNOW-3G

2019-08-14 Thread pespin
pespin has posted comments on this change. ( 
https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/15202 )

Change subject: TTCN-3 native function wrapper for SNOW-3G
..


Patch Set 1: Code-Review+2


--
To view, visit https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/15202
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: osmo-ttcn3-hacks
Gerrit-Branch: master
Gerrit-Change-Id: I7fca69ea8b4aed48d5a64885b762ab85be71ef03
Gerrit-Change-Number: 15202
Gerrit-PatchSet: 1
Gerrit-Owner: laforge 
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: pespin 
Gerrit-Comment-Date: Wed, 14 Aug 2019 11:32:16 +
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment


Change in ...osmo-ttcn3-hacks[master]: TTCN-3 native function wrapper for SNOW-3G

2019-08-14 Thread laforge
laforge has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/osmo-ttcn3-hacks/+/15202


Change subject: TTCN-3 native function wrapper for SNOW-3G
..

TTCN-3 native function wrapper for SNOW-3G

Change-Id: I7fca69ea8b4aed48d5a64885b762ab85be71ef03
---
A mme/LTE_CryptoFunctionDefs.cc
A mme/LTE_CryptoFunctions.ttcn
A mme/key_derivation.c
A mme/key_derivation.h
M mme/regen_makefile.sh
5 files changed, 557 insertions(+), 2 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/osmo-ttcn3-hacks 
refs/changes/02/15202/1

diff --git a/mme/LTE_CryptoFunctionDefs.cc b/mme/LTE_CryptoFunctionDefs.cc
new file mode 100644
index 000..da2d521
--- /dev/null
+++ b/mme/LTE_CryptoFunctionDefs.cc
@@ -0,0 +1,189 @@
+/* Utility functions from ogslib imported to TTCN-3
+ *
+ * (C) 2019 Harald Welte 
+ * All rights reserved.
+ *
+ * Released under the terms of GNU General Public License, Version 2 or
+ * (at your option) any later version.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+#include 
+#include 
+
+#include "snow-3g.h"
+#include "key_derivation.h"
+
+//#define DEBUG
+
+#ifdef DEBUG
+static __thread char hexd_buff[4096];
+static const char hex_chars[] = "0123456789abcdef";
+
+static const char *_osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const 
unsigned char *buf, int len, const char *delim,
+bool delim_after_last)
+{
+   int i;
+   char *cur = out_buf;
+   size_t delim_len;
+
+   if (!out_buf || !out_buf_size)
+   return "";
+
+   delim = delim ? : "";
+   delim_len = strlen(delim);
+
+   for (i = 0; i < len; i++) {
+   const char *delimp = delim;
+   int len_remain = out_buf_size - (cur - out_buf) - 1;
+   if (len_remain < (2 + delim_len)
+   && !(!delim_after_last && i == (len - 1) && len_remain >= 
2))
+   break;
+
+   *cur++ = hex_chars[buf[i] >> 4];
+   *cur++ = hex_chars[buf[i] & 0xf];
+
+   if (i == (len - 1) && !delim_after_last)
+   break;
+
+   while (len_remain > 1 && *delimp) {
+   *cur++ = *delimp++;
+   len_remain--;
+   }
+   }
+   *cur = '\0';
+   return out_buf;
+}
+
+static char *_osmo_hexdump(const unsigned char *buf, int len)
+{
+   _osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, "", true);
+   return hexd_buff;
+}
+#endif
+
+namespace LTE__CryptoFunctions {
+
+
+/* f8.
+* Input key: 128 bit Confidentiality Key as OCT16.
+* Input count:32-bit Count, Frame dependent input as INTEGER.
+* Input bearer: 5-bit Bearer identity (in the LSB side) as BIT5.
+* Input is_dlwnlink: Direction of transmission.
+* Input data: length number of bits, input bit stream as OCTETSTRING.
+* Output data: Output bit stream. Assumes data is suitably memory
+* allocated.
+* Encrypts/decrypts blocks of data between 1 and 2^32 bits in length as
+* defined in Section 3.
+*/
+OCTETSTRING f__snow__3g__f8(const OCTETSTRING& key, const INTEGER& count, 
const INTEGER & bearer,
+   const BOOLEAN& is_downlink, const OCTETSTRING& data)
+{
+   TTCN_Buffer ttcn_buf_data(data);
+   TTCN_Buffer ttcn_buf_key(key);
+   uint32_t direction = (uint32_t)is_downlink;
+
+   snow_3g_f8((u8 *)ttcn_buf_key.get_data(), (u32) count, (u32)bearer, 
direction,
+  (u8 *)ttcn_buf_data.get_data(), ttcn_buf_data.get_len());
+
+   return OCTETSTRING(ttcn_buf_data.get_len(), ttcn_buf_data.get_data());
+}
+
+/* f9.
+* Input key: 128 bit Integrity Key as OCT16.
+* Input count:32-bit Count, Frame dependent input as UINT32.
+* Input fresh: 32-bit Random number as UINT32.
+* Input is_downlink:1 Direction of transmission.
+* Input data: input bit stream.
+* Output : 32 bit block used as MAC
+* Generates 32-bit MAC using UIA2 algorithm as defined in Section 4.
+*/
+
+OCTETSTRING f__snow__3g__f9(const OCTETSTRING& key, const INTEGER& count, 
const INTEGER& fresh,
+   const BOOLEAN& is_downlink, const OCTETSTRING& data)
+{
+   TTCN_Buffer ttcn_buf_data(data);
+   TTCN_Buffer ttcn_buf_key(key);
+   uint32_t direction = (uint32_t)is_downlink;
+   uint8_t tmp[4];
+   TTCN_Buffer ttcn_buf_mac;
+
+#ifdef DEBUG
+   printf("F9: key=%s, count=%u, fresh=%u, direction=%u, ",
+   _osmo_hexdump((u8 *)ttcn_buf_key.get_data(), 
ttcn_buf_key.get_len()), (u32) count,
+   (u32) fresh, direction);
+   printf("data=%s -> ", _osmo_hexdump(ttcn_buf_data.get_data(), 
ttcn_buf_data.get_len()));
+#endif
+   snow_3g_f9((u8 *)ttcn_buf_key.get_data(), (u32) count, (u32) fresh, 
direction,
+  (u8 *)ttcn_buf_data.get_data(), ttcn_buf_data.get_len()*8, 
tmp);
+#ifdef DEBUG
+