Skip to content

Commit cd93f4f

Browse files
sec: apply bitlength in unit tests for NEA2 ciphering engine
1 parent 84d1e63 commit cd93f4f

File tree

1 file changed

+59
-42
lines changed

1 file changed

+59
-42
lines changed

tests/unittests/security/ciphering_engine_test.cpp

Lines changed: 59 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -74,31 +74,43 @@ int arrcmp(uint8_t const* const a, uint8_t const* const b, uint32_t len)
7474
return 0;
7575
}
7676

77+
bool trim_tail_to_bitlength(byte_buffer_view buf, uint32_t bitlength)
78+
{
79+
if ((bitlength + 7) / 8 != buf.length()) {
80+
return false;
81+
}
82+
uint32_t padding = bitlength % 8;
83+
uint8_t mask = 0xff << (8 - padding);
84+
buf[buf.length() - 1] &= mask;
85+
return true;
86+
}
87+
7788
/// 128-NEA2 Test Set 1
7889
/// Ref: TS 33.501 Sec. D.4.4, TS 33.401 Sec. C.1 128-EEA2
7990
TEST_F(ciphering_engine_test, security_nea2_testset1)
8091
{
8192
// Testdata in plain format
82-
const char* key_cstr = "d3c5d592327fb11c4035c6680af8c6d1";
83-
uint32_t count = 0x398a59b4;
84-
uint8_t bearer = 0x15;
85-
uint8_t direction = 1;
86-
// uint32_t length = 253;
93+
const char* key_cstr = "d3c5d592327fb11c4035c6680af8c6d1";
94+
uint32_t count = 0x398a59b4;
95+
uint8_t bearer = 0x15;
96+
uint8_t direction = 1;
97+
uint32_t length = 253;
8798
const char* plaintext_cstr = "981ba6824c1bfb1ab485472029b71d808ce33e2cc3c0b5fc1f3de8a6dc66b1f0";
8899
const char* ciphertext_cstr = "e9fed8a63d155304d71df20bf3e82214b20ed7dad2f233dc3c22d7bdeeed8e78";
89100

90101
// Pack hex strings into srsran types
91-
sec_128_key key = make_sec_128_key(key_cstr);
92-
security_direction dir = static_cast<security_direction>(direction);
93-
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
94-
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
102+
sec_128_key key = make_sec_128_key(key_cstr);
103+
auto dir = static_cast<security_direction>(direction);
104+
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
105+
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
95106

96107
// Create ciphering engine
97108
std::unique_ptr<ciphering_engine> nea = std::make_unique<ciphering_engine_nea2>(key, bearer, dir);
98109

99110
// Apply ciphering and compare results
100111
security_result result = nea->apply_ciphering(plaintext.deep_copy().value(), 0, count);
101112
ASSERT_TRUE(result.buf.has_value());
113+
ASSERT_TRUE(trim_tail_to_bitlength(result.buf.value(), length));
102114
EXPECT_EQ(result.buf.value(), ciphertext);
103115
}
104116

@@ -111,7 +123,7 @@ TEST_F(ciphering_engine_test, security_nea2_testset2)
111123
uint32_t count = 0xc675a64b;
112124
uint8_t bearer = 0x0c;
113125
uint8_t direction = 1;
114-
// uint32_t length = 798;
126+
uint32_t length = 798;
115127
const char* plaintext_cstr =
116128
"7ec61272743bf1614726446a6c38ced166f6ca76eb5430044286346cef130f92922b03450d3a9975e5bd2ea0eb55ad8e1b199e3ec4316020"
117129
"e9a1b285e762795359b7bdfd39bef4b2484583d5afe082aee638bf5fd5a606193901a08f4ab41aab9b134880";
@@ -120,17 +132,18 @@ TEST_F(ciphering_engine_test, security_nea2_testset2)
120132
"4cd97b870976503c0943f2cb5ae8f052c7b7d392239587b8956086bcab18836042e2e6ce42432a17105c53d0";
121133

122134
// Pack hex strings into srsran types
123-
sec_128_key key = make_sec_128_key(key_cstr);
124-
security_direction dir = static_cast<security_direction>(direction);
125-
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
126-
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
135+
sec_128_key key = make_sec_128_key(key_cstr);
136+
auto dir = static_cast<security_direction>(direction);
137+
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
138+
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
127139

128140
// Create ciphering engine
129141
std::unique_ptr<ciphering_engine> nea = std::make_unique<ciphering_engine_nea2>(key, bearer, dir);
130142

131143
// Apply ciphering and compare results
132144
security_result result = nea->apply_ciphering(plaintext.deep_copy().value(), 0, count);
133145
ASSERT_TRUE(result.buf.has_value());
146+
ASSERT_TRUE(trim_tail_to_bitlength(result.buf.value(), length));
134147
EXPECT_EQ(result.buf.value(), ciphertext);
135148
}
136149

@@ -139,26 +152,27 @@ TEST_F(ciphering_engine_test, security_nea2_testset2)
139152
TEST_F(ciphering_engine_test, security_nea2_testset3)
140153
{
141154
// Testdata in plain format
142-
const char* key_cstr = "0a8b6bd8d9b08b08d64e32d1817777fb";
143-
uint32_t count = 0x544d49cd;
144-
uint8_t bearer = 0x04;
145-
uint8_t direction = 0;
146-
// uint32_t length = 310;
155+
const char* key_cstr = "0a8b6bd8d9b08b08d64e32d1817777fb";
156+
uint32_t count = 0x544d49cd;
157+
uint8_t bearer = 0x04;
158+
uint8_t direction = 0;
159+
uint32_t length = 310;
147160
const char* plaintext_cstr = "fd40a41d370a1f65745095687d47ba1d36d2349e23f644392c8ea9c49d40c13271aff264d0f248";
148161
const char* ciphertext_cstr = "75750d37b4bba2a4dedb34235bd68c6645acdaaca48138a3b0c471e2a7041a576423d2927287f0";
149162

150163
// Pack hex strings into srsran types
151-
sec_128_key key = make_sec_128_key(key_cstr);
152-
security_direction dir = static_cast<security_direction>(direction);
153-
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
154-
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
164+
sec_128_key key = make_sec_128_key(key_cstr);
165+
auto dir = static_cast<security_direction>(direction);
166+
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
167+
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
155168

156169
// Create ciphering engine
157170
std::unique_ptr<ciphering_engine> nea = std::make_unique<ciphering_engine_nea2>(key, bearer, dir);
158171

159172
// Apply ciphering and compare results
160173
security_result result = nea->apply_ciphering(plaintext.deep_copy().value(), 0, count);
161174
ASSERT_TRUE(result.buf.has_value());
175+
ASSERT_TRUE(trim_tail_to_bitlength(result.buf.value(), length));
162176
EXPECT_EQ(result.buf.value(), ciphertext);
163177
}
164178

@@ -167,11 +181,11 @@ TEST_F(ciphering_engine_test, security_nea2_testset3)
167181
TEST_F(ciphering_engine_test, security_nea2_testset4)
168182
{
169183
// Testdata in plain format
170-
const char* key_cstr = "aa1f95aea533bcb32eb63bf52d8f831a";
171-
uint32_t count = 0x72d8c671;
172-
uint8_t bearer = 0x10;
173-
uint8_t direction = 1;
174-
// uint32_t length = 1022;
184+
const char* key_cstr = "aa1f95aea533bcb32eb63bf52d8f831a";
185+
uint32_t count = 0x72d8c671;
186+
uint8_t bearer = 0x10;
187+
uint8_t direction = 1;
188+
uint32_t length = 1022;
175189
const char* plaintext_cstr = "fb1b96c5c8badfb2e8e8edfde78e57f2ad81e74103fc430a534dcc37afcec70e1517bb06f27219dae49022d"
176190
"dc47a068de4c9496a951a6b09edbdc864c7adbd740ac50c022f3082bafd22d78197c5d508b977bca13f32e6"
177191
"52e74ba728576077ce628c535e87dc6077ba07d29068590c8cb5f1088e082cfa0ec961302d69cf3d44";
@@ -180,17 +194,18 @@ TEST_F(ciphering_engine_test, security_nea2_testset4)
180194
"3f2f8f8e0592a9879201be7ff9777a162ab810feb324ba74c4c156e04d39097209653ac33e5a5f2d8864";
181195

182196
// Pack hex strings into srsran types
183-
sec_128_key key = make_sec_128_key(key_cstr);
184-
security_direction dir = static_cast<security_direction>(direction);
185-
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
186-
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
197+
sec_128_key key = make_sec_128_key(key_cstr);
198+
auto dir = static_cast<security_direction>(direction);
199+
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
200+
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
187201

188202
// Create ciphering engine
189203
std::unique_ptr<ciphering_engine> nea = std::make_unique<ciphering_engine_nea2>(key, bearer, dir);
190204

191205
// Apply ciphering and compare results
192206
security_result result = nea->apply_ciphering(plaintext.deep_copy().value(), 0, count);
193207
ASSERT_TRUE(result.buf.has_value());
208+
ASSERT_TRUE(trim_tail_to_bitlength(result.buf.value(), length));
194209
EXPECT_EQ(result.buf.value(), ciphertext);
195210
}
196211

@@ -203,7 +218,7 @@ TEST_F(ciphering_engine_test, security_nea2_testset5)
203218
uint32_t count = 0xc675a64b;
204219
uint8_t bearer = 0x0c;
205220
uint8_t direction = 1;
206-
// uint32_t length = 1245;
221+
uint32_t length = 1245;
207222
const char* plaintext_cstr =
208223
"8daa17b1ae050529c6827f28c0ef6a1242e93f8b314fb18a77f790ae049fedd612267fecaefc450174d76d9f9aa7755a30cd90a9a5874bf4"
209224
"8eaf70eea3a62a250a8b6bd8d9b08b08d64e32d1817777fb544d49cd49720e219dbf8bbed33904e1fd40a41d370a1f65745095687d47ba1d"
@@ -214,17 +229,18 @@ TEST_F(ciphering_engine_test, security_nea2_testset5)
214229
"f1fc0ec684ddb21349747622e209295d27ff3f95623371d49b147c0af486171f22cd04b1cbeb2658223e6938";
215230

216231
// Pack hex strings into srsran types
217-
sec_128_key key = make_sec_128_key(key_cstr);
218-
security_direction dir = static_cast<security_direction>(direction);
219-
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
220-
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
232+
sec_128_key key = make_sec_128_key(key_cstr);
233+
auto dir = static_cast<security_direction>(direction);
234+
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
235+
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
221236

222237
// Create ciphering engine
223238
std::unique_ptr<ciphering_engine> nea = std::make_unique<ciphering_engine_nea2>(key, bearer, dir);
224239

225240
// Apply ciphering and compare results
226241
security_result result = nea->apply_ciphering(plaintext.deep_copy().value(), 0, count);
227242
ASSERT_TRUE(result.buf.has_value());
243+
ASSERT_TRUE(trim_tail_to_bitlength(result.buf.value(), length));
228244
EXPECT_EQ(result.buf.value(), ciphertext);
229245
}
230246

@@ -237,7 +253,7 @@ TEST_F(ciphering_engine_test, security_nea2_testset6)
237253
uint32_t count = 0xaca4f50f;
238254
uint8_t bearer = 0x0b;
239255
uint8_t direction = 0;
240-
// uint32_t length = 3861;
256+
uint32_t length = 3861;
241257
const char* plaintext_cstr =
242258
"40981ba6824c1bfb4286b299783daf442c099f7ab0f58d5c8e46b104f08f01b41ab485472029b71d36bd1a3d90dc3a41b46d51672ac4c966"
243259
"3a2be063da4bc8d2808ce33e2cccbfc634e1b259060876a0fbb5a437ebcc8d31c19e4454318745e3fa16bb11adae248879fe52db2543e53c"
@@ -260,17 +276,18 @@ TEST_F(ciphering_engine_test, security_nea2_testset6)
260276
"c41b863da9e142e90020cfd074d6927b7ab3b6725d1a6f3f98b9c9daa8982aff067828";
261277

262278
// Pack hex strings into srsran types
263-
sec_128_key key = make_sec_128_key(key_cstr);
264-
security_direction dir = static_cast<security_direction>(direction);
265-
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
266-
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
279+
sec_128_key key = make_sec_128_key(key_cstr);
280+
auto dir = static_cast<security_direction>(direction);
281+
byte_buffer plaintext = make_byte_buffer(plaintext_cstr).value();
282+
byte_buffer ciphertext = make_byte_buffer(ciphertext_cstr).value();
267283

268284
// Create ciphering engine
269285
std::unique_ptr<ciphering_engine> nea = std::make_unique<ciphering_engine_nea2>(key, bearer, dir);
270286

271287
// Apply ciphering and compare results
272288
security_result result = nea->apply_ciphering(plaintext.deep_copy().value(), 0, count);
273289
ASSERT_TRUE(result.buf.has_value());
290+
ASSERT_TRUE(trim_tail_to_bitlength(result.buf.value(), length));
274291
EXPECT_EQ(result.buf.value(), ciphertext);
275292
}
276293

0 commit comments

Comments
 (0)