blob: 7f822245e73789699f0672a12801e4304041cfdb [file] [log] [blame]
Harald Weltec918e4e2019-07-12 18:53:55 +08001/* Utility functions from ogslib imported to TTCN-3
2 *
3 * (C) 2019 Harald Welte <laforge@gnumonks.org>
4 * All rights reserved.
5 *
6 * Released under the terms of GNU General Public License, Version 2 or
7 * (at your option) any later version.
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12#include <stdio.h>
13#include <string.h>
14#include <errno.h>
15#include <stdint.h>
16
17#include <Boolean.hh>
18#include <Integer.hh>
19#include <Octetstring.hh>
20#include <Bitstring.hh>
21
22#include "snow-3g.h"
23#include "key_derivation.h"
24
25//#define DEBUG
26
27#ifdef DEBUG
28static __thread char hexd_buff[4096];
29static const char hex_chars[] = "0123456789abcdef";
30
31static const char *_osmo_hexdump_buf(char *out_buf, size_t out_buf_size, const unsigned char *buf, int len, const char *delim,
32 bool delim_after_last)
33{
34 int i;
35 char *cur = out_buf;
36 size_t delim_len;
37
38 if (!out_buf || !out_buf_size)
39 return "";
40
41 delim = delim ? : "";
42 delim_len = strlen(delim);
43
44 for (i = 0; i < len; i++) {
45 const char *delimp = delim;
46 int len_remain = out_buf_size - (cur - out_buf) - 1;
47 if (len_remain < (2 + delim_len)
48 && !(!delim_after_last && i == (len - 1) && len_remain >= 2))
49 break;
50
51 *cur++ = hex_chars[buf[i] >> 4];
52 *cur++ = hex_chars[buf[i] & 0xf];
53
54 if (i == (len - 1) && !delim_after_last)
55 break;
56
57 while (len_remain > 1 && *delimp) {
58 *cur++ = *delimp++;
59 len_remain--;
60 }
61 }
62 *cur = '\0';
63 return out_buf;
64}
65
66static char *_osmo_hexdump(const unsigned char *buf, int len)
67{
68 _osmo_hexdump_buf(hexd_buff, sizeof(hexd_buff), buf, len, "", true);
69 return hexd_buff;
70}
71#endif
72
73namespace LTE__CryptoFunctions {
74
75
76/* f8.
77* Input key: 128 bit Confidentiality Key as OCT16.
78* Input count:32-bit Count, Frame dependent input as INTEGER.
79* Input bearer: 5-bit Bearer identity (in the LSB side) as BIT5.
80* Input is_dlwnlink: Direction of transmission.
81* Input data: length number of bits, input bit stream as OCTETSTRING.
82* Output data: Output bit stream. Assumes data is suitably memory
83* allocated.
84* Encrypts/decrypts blocks of data between 1 and 2^32 bits in length as
85* defined in Section 3.
86*/
87OCTETSTRING f__snow__3g__f8(const OCTETSTRING& key, const INTEGER& count, const INTEGER & bearer,
88 const BOOLEAN& is_downlink, const OCTETSTRING& data)
89{
90 TTCN_Buffer ttcn_buf_data(data);
91 TTCN_Buffer ttcn_buf_key(key);
92 uint32_t direction = (uint32_t)is_downlink;
93
94 snow_3g_f8((u8 *)ttcn_buf_key.get_data(), (u32) count, (u32)bearer, direction,
95 (u8 *)ttcn_buf_data.get_data(), ttcn_buf_data.get_len());
96
97 return OCTETSTRING(ttcn_buf_data.get_len(), ttcn_buf_data.get_data());
98}
99
100/* f9.
101* Input key: 128 bit Integrity Key as OCT16.
102* Input count:32-bit Count, Frame dependent input as UINT32.
103* Input fresh: 32-bit Random number as UINT32.
104* Input is_downlink:1 Direction of transmission.
105* Input data: input bit stream.
106* Output : 32 bit block used as MAC
107* Generates 32-bit MAC using UIA2 algorithm as defined in Section 4.
108*/
109
110OCTETSTRING f__snow__3g__f9(const OCTETSTRING& key, const INTEGER& count, const INTEGER& fresh,
111 const BOOLEAN& is_downlink, const OCTETSTRING& data)
112{
113 TTCN_Buffer ttcn_buf_data(data);
114 TTCN_Buffer ttcn_buf_key(key);
115 uint32_t direction = (uint32_t)is_downlink;
116 uint8_t tmp[4];
117 TTCN_Buffer ttcn_buf_mac;
118
119#ifdef DEBUG
120 printf("F9: key=%s, count=%u, fresh=%u, direction=%u, ",
121 _osmo_hexdump((u8 *)ttcn_buf_key.get_data(), ttcn_buf_key.get_len()), (u32) count,
122 (u32) fresh, direction);
123 printf("data=%s -> ", _osmo_hexdump(ttcn_buf_data.get_data(), ttcn_buf_data.get_len()));
124#endif
125 snow_3g_f9((u8 *)ttcn_buf_key.get_data(), (u32) count, (u32) fresh, direction,
126 (u8 *)ttcn_buf_data.get_data(), ttcn_buf_data.get_len()*8, tmp);
127#ifdef DEBUG
128 printf("%s\n", _osmo_hexdump(tmp, sizeof(tmp)));
129#endif
130
131 return OCTETSTRING(4, tmp);
132}
133
134OCTETSTRING f__kdf__kasme(const OCTETSTRING& ck, const OCTETSTRING& ik, const OCTETSTRING& plmn_id,
135 const OCTETSTRING& sqn, const OCTETSTRING& ak)
136{
137 TTCN_Buffer ttcn_buf_ck(ck);
138 TTCN_Buffer ttcn_buf_ik(ik);
139 TTCN_Buffer ttcn_buf_plmn_id(plmn_id);
140 TTCN_Buffer ttcn_buf_sqn(sqn);
141 TTCN_Buffer ttcn_buf_ak(ak);
142 uint8_t kasme[32];
143
144 hss_auc_kasme(ttcn_buf_ck.get_data(), ttcn_buf_ik.get_data(), ttcn_buf_plmn_id.get_data(),
145 ttcn_buf_sqn.get_data(), ttcn_buf_ak.get_data(), kasme);
146 return OCTETSTRING(sizeof(kasme), kasme);
147}
148
149OCTETSTRING f__kdf__nas__int(const INTEGER& alg_id, const OCTETSTRING &kasme)
150{
151 TTCN_Buffer ttcn_buf_kasme(kasme);
152 uint8_t knas[16];
153
154 mme_kdf_nas(MME_KDF_NAS_INT_ALG, (int)alg_id, (const u8*) ttcn_buf_kasme.get_data(), knas);
155 return OCTETSTRING(sizeof(knas), knas);
156}
157
158OCTETSTRING f__kdf__nas__enc(const INTEGER& alg_id, const OCTETSTRING &kasme)
159{
160 TTCN_Buffer ttcn_buf_kasme(kasme);
161 uint8_t knas[16];
162
163 mme_kdf_nas(MME_KDF_NAS_ENC_ALG, (int)alg_id, (const u8*) ttcn_buf_kasme.get_data(), knas);
164 return OCTETSTRING(sizeof(knas), knas);
165}
166
167
168OCTETSTRING f__kdf__enb(const OCTETSTRING &kasme, const INTEGER &ul_count)
169{
170 TTCN_Buffer ttcn_buf_kasme(kasme);
171 uint8_t kenb[32];
172
173 mme_kdf_enb(ttcn_buf_kasme.get_data(), (int)ul_count, kenb);
174 return OCTETSTRING(sizeof(kenb), kenb);
175}
176
177OCTETSTRING f__kdf__nh(const OCTETSTRING &kasme, const OCTETSTRING &sync_inp)
178{
179 TTCN_Buffer ttcn_buf_kasme(kasme);
180 TTCN_Buffer ttcn_buf_sync_inp(sync_inp);
181 uint8_t kenb[32];
182
183 mme_kdf_nh(ttcn_buf_kasme.get_data(), ttcn_buf_sync_inp.get_data(), kenb);
184 return OCTETSTRING(sizeof(kenb), kenb);
185}
186
Pau Espin Pedrol3be4d922024-01-15 15:21:57 +0100187OCTETSTRING f__kdf__nas__token(const OCTETSTRING &kasme, const INTEGER &ul_count)
188{
189 TTCN_Buffer ttcn_buf_kasme(kasme);
190 uint8_t nas_token[32];
191
192 mme_kdf_nas_token(ttcn_buf_kasme.get_data(), (int)ul_count, nas_token);
193 return OCTETSTRING(sizeof(nas_token), nas_token);
194}
Harald Weltec918e4e2019-07-12 18:53:55 +0800195
196
197} // namespace