Skip to content

Commit 8e8fadb

Browse files
sec: extend and add NIA test vectors and integrity engine test
This commit adds modified custom versions of the test set(s) expanded to next full byte.
1 parent 4bfd12b commit 8e8fadb

File tree

7 files changed

+1096
-4
lines changed

7 files changed

+1096
-4
lines changed

tests/unittests/security/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ add_executable(security_test security_test.cpp)
1212
target_link_libraries(security_test srsran_security srsran_support srslog gtest gtest_main)
1313
gtest_discover_tests(security_test)
1414

15+
add_executable(integrity_engine_test integrity_engine_test)
16+
target_include_directories(integrity_engine_test PRIVATE ${CMAKE_SOURCE_DIR})
17+
target_link_libraries(integrity_engine_test srsran_security srsran_support srslog gtest gtest_main)
18+
gtest_discover_tests(integrity_engine_test)
19+
1520
add_executable(ciphering_engine_test ciphering_engine_test.cpp)
1621
target_include_directories(ciphering_engine_test PRIVATE ${CMAKE_SOURCE_DIR})
1722
target_link_libraries(ciphering_engine_test srsran_security srsran_support srslog gtest gtest_main)
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
/*
2+
*
3+
* Copyright 2021-2024 Software Radio Systems Limited
4+
*
5+
* By using this file, you agree to the terms and conditions set
6+
* forth in the LICENSE file which can be found at the top level of
7+
* the distribution.
8+
*
9+
*/
10+
11+
#include "lib/security/integrity_engine_generic.h"
12+
#include "nia1_test_set.h"
13+
#include "nia2_test_set.h"
14+
#include "nia3_test_set.h"
15+
#include "srsran/security/integrity_engine.h"
16+
#include "srsran/security/security.h"
17+
#include "srsran/srslog/srslog.h"
18+
#include <gtest/gtest.h>
19+
20+
using namespace srsran;
21+
using namespace srsran::security;
22+
23+
/// Fixture class for integrity engine tests
24+
class fxt_base : public testing::TestWithParam<nia_test_set>
25+
{
26+
protected:
27+
void SetUp() override
28+
{
29+
// init test's logger
30+
srslog::init();
31+
logger.set_level(srslog::basic_levels::debug);
32+
logger.set_hex_dump_max_size(3000);
33+
34+
// init SEC logger
35+
srslog::fetch_basic_logger("SEC", false).set_level(srslog::basic_levels::debug);
36+
srslog::fetch_basic_logger("SEC", false).set_hex_dump_max_size(-1);
37+
38+
logger.info("Created fixture for integrity engine test");
39+
}
40+
41+
void TearDown() override
42+
{
43+
// flush logger after each test
44+
srslog::flush();
45+
}
46+
47+
srslog::basic_logger& logger = srslog::fetch_basic_logger("TEST", false);
48+
};
49+
50+
/// Fixture class for integrity engine tests with NIA1
51+
class fxt_nia1 : public fxt_base
52+
{};
53+
54+
/// Fixture class for integrity engine tests with NIA2
55+
class fxt_nia2 : public fxt_base
56+
{};
57+
58+
/// Fixture class for integrity engine tests with NIA3
59+
class fxt_nia3 : public fxt_base
60+
{};
61+
62+
/// Converts a hex string (e.g. 01FA02) to a sec_as_key.
63+
sec_key make_sec_key(const std::string& hex_str)
64+
{
65+
byte_buffer key_buf = make_byte_buffer(hex_str).value();
66+
sec_key key = {};
67+
std::copy(key_buf.begin(), key_buf.end(), key.begin());
68+
return key;
69+
}
70+
71+
/// Converts a hex string (e.g. 01FA02) to a sec_128_as_key.
72+
sec_128_key make_sec_128_key(const std::string& hex_str)
73+
{
74+
byte_buffer key_buf = make_byte_buffer(hex_str).value();
75+
sec_128_key key = {};
76+
std::copy(key_buf.begin(), key_buf.end(), key.begin());
77+
return key;
78+
}
79+
80+
/// Compares two byte arrays
81+
int arrcmp(uint8_t const* const a, uint8_t const* const b, uint32_t len)
82+
{
83+
uint32_t i = 0;
84+
85+
for (i = 0; i < len; i++) {
86+
if (a[i] != b[i]) {
87+
return a[i] - b[i];
88+
}
89+
}
90+
return 0;
91+
}
92+
93+
bool trim_tail_to_bitlength(byte_buffer_view buf, uint32_t bitlength)
94+
{
95+
if ((bitlength + 7) / 8 != buf.length()) {
96+
return false;
97+
}
98+
uint32_t padding = bitlength % 8;
99+
if (padding > 0) {
100+
uint8_t mask = 0xff << (8 - padding);
101+
buf[buf.length() - 1] &= mask;
102+
}
103+
return true;
104+
}
105+
106+
TEST_P(fxt_nia1, integrity_engine_generic_nia1)
107+
{
108+
nia_test_set param = GetParam();
109+
110+
// Pack hex strings into srsran types
111+
sec_128_key key = make_sec_128_key(param.ik_cstr);
112+
auto dir = static_cast<security_direction>(param.direction);
113+
byte_buffer message = make_byte_buffer(param.message_cstr).value();
114+
byte_buffer mact_buf = make_byte_buffer(param.mact_cstr).value();
115+
byte_buffer prot_buf = message.deep_copy().value();
116+
ASSERT_TRUE(prot_buf.append(mact_buf));
117+
118+
// Create integrity engine
119+
std::unique_ptr<integrity_engine> nia =
120+
std::make_unique<integrity_engine_generic>(key, param.bearer, dir, integrity_algorithm::nia1);
121+
122+
// Apply integrity and compare results
123+
security_result result = nia->protect_integrity(message.deep_copy().value(), param.count_i);
124+
ASSERT_TRUE(result.buf.has_value());
125+
logger.info(result.buf.value().begin(), result.buf.value().end(), "result:");
126+
logger.info(prot_buf.begin(), prot_buf.end(), "exp:");
127+
EXPECT_EQ(result.buf.value(), prot_buf);
128+
}
129+
130+
TEST_P(fxt_nia2, integrity_engine_generic_nia2)
131+
{
132+
nia_test_set param = GetParam();
133+
134+
// Pack hex strings into srsran types
135+
sec_128_key key = make_sec_128_key(param.ik_cstr);
136+
auto dir = static_cast<security_direction>(param.direction);
137+
byte_buffer message = make_byte_buffer(param.message_cstr).value();
138+
byte_buffer mact_buf = make_byte_buffer(param.mact_cstr).value();
139+
byte_buffer prot_buf = message.deep_copy().value();
140+
ASSERT_TRUE(prot_buf.append(mact_buf));
141+
142+
// Create integrity engine
143+
std::unique_ptr<integrity_engine> nia =
144+
std::make_unique<integrity_engine_generic>(key, param.bearer, dir, integrity_algorithm::nia2);
145+
146+
// Apply integrity and compare results
147+
security_result result = nia->protect_integrity(message.deep_copy().value(), param.count_i);
148+
ASSERT_TRUE(result.buf.has_value());
149+
logger.info(result.buf.value().begin(), result.buf.value().end(), "result:");
150+
logger.info(prot_buf.begin(), prot_buf.end(), "exp:");
151+
EXPECT_EQ(result.buf.value(), prot_buf);
152+
}
153+
154+
TEST_P(fxt_nia3, integrity_engine_generic_nia3)
155+
{
156+
nia_test_set param = GetParam();
157+
158+
// Pack hex strings into srsran types
159+
sec_128_key key = make_sec_128_key(param.ik_cstr);
160+
auto dir = static_cast<security_direction>(param.direction);
161+
byte_buffer message = make_byte_buffer(param.message_cstr).value();
162+
byte_buffer mact_buf = make_byte_buffer(param.mact_cstr).value();
163+
byte_buffer prot_buf = message.deep_copy().value();
164+
ASSERT_TRUE(prot_buf.append(mact_buf));
165+
166+
// Create integrity engine
167+
std::unique_ptr<integrity_engine> nia =
168+
std::make_unique<integrity_engine_generic>(key, param.bearer, dir, integrity_algorithm::nia3);
169+
170+
// Apply integrity and compare results
171+
security_result result = nia->protect_integrity(message.deep_copy().value(), param.count_i);
172+
ASSERT_TRUE(result.buf.has_value());
173+
logger.info(result.buf.value().begin(), result.buf.value().end(), "result:");
174+
logger.info(prot_buf.begin(), prot_buf.end(), "exp:");
175+
EXPECT_EQ(result.buf.value(), prot_buf);
176+
}
177+
178+
//////////////////////////////////////////////////////////
179+
// Finally, instantiate all testcases for each test set //
180+
//////////////////////////////////////////////////////////
181+
std::string test_param_info_to_string(const ::testing::TestParamInfo<nia_test_set>& info)
182+
{
183+
fmt::memory_buffer buffer;
184+
fmt::format_to(buffer, "{}", info.param.name);
185+
return fmt::to_string(buffer);
186+
}
187+
188+
INSTANTIATE_TEST_SUITE_P(nia1,
189+
fxt_nia1,
190+
::testing::ValuesIn(nia1_test_set.begin(), nia1_test_set.end()),
191+
test_param_info_to_string);
192+
193+
INSTANTIATE_TEST_SUITE_P(nia2,
194+
fxt_nia2,
195+
::testing::ValuesIn(nia2_test_set.begin(), nia2_test_set.end()),
196+
test_param_info_to_string);
197+
198+
INSTANTIATE_TEST_SUITE_P(nia3,
199+
fxt_nia3,
200+
::testing::ValuesIn(nia3_test_set.begin(), nia3_test_set.end()),
201+
test_param_info_to_string);
202+
203+
int main(int argc, char** argv)
204+
{
205+
srslog::init();
206+
::testing::InitGoogleTest(&argc, argv);
207+
return RUN_ALL_TESTS();
208+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
*
3+
* Copyright 2021-2024 Software Radio Systems Limited
4+
*
5+
* By using this file, you agree to the terms and conditions set
6+
* forth in the LICENSE file which can be found at the top level of
7+
* the distribution.
8+
*
9+
*/
10+
11+
#pragma once
12+
13+
#include "nia_test_set.h"
14+
#include <vector>
15+
16+
/// 128-NIA1 Test Set
17+
/// Test sets named with suffix "_mod" are modified custom versions of the test set expanded to next full byte.
18+
/// Ref: TS 33.501 Sec. D.4.3, TS 33.401 Sec. C.4 128-EIA1
19+
std::vector<nia_test_set> nia1_test_set = {
20+
nia_test_set{
21+
.name = "128_NIA1_Test_Set_1",
22+
.count_i = 0x38a6f056,
23+
.bearer = 0x1f,
24+
.direction = 0,
25+
.ik_cstr = "2bd6459f82c5b300952c49104881ff48",
26+
.length = 88,
27+
.message_cstr = "3332346263393861373479",
28+
.mact_cstr = "731f1165",
29+
},
30+
nia_test_set{
31+
.name = "128_NIA1_Test_Set_2_mod",
32+
.count_i = 0x36af6144,
33+
.bearer = 0x18,
34+
.direction = 1,
35+
.ik_cstr = "7e5e94431e11d73828d739cc6ced4573",
36+
.length = 256,
37+
.message_cstr = "b3d3c9170a4e1632f60f861013d22d84b726b6a278d802d1eeaf1321ba5929dc",
38+
.mact_cstr = "666f3e58",
39+
},
40+
nia_test_set{
41+
.name = "128_NIA1_Test_Set_3_mod",
42+
.count_i = 0xc7590ea9,
43+
.bearer = 0x17,
44+
.direction = 0,
45+
.ik_cstr = "d3419be821087acd02123a9248033359",
46+
.length = 512,
47+
.message_cstr = "bbb057038809496bcff86d6fbc8ce5b135a06b166054f2d565be8ace75dc851e0bcdd8f07141c495872fb5d8c0c66a"
48+
"8b6da556663e4e461205d84580bee5bc7e",
49+
.mact_cstr = "27986aa7",
50+
},
51+
nia_test_set{
52+
.name = "128_NIA1_Test_Set_4",
53+
.count_i = 0x36af6144,
54+
.bearer = 0x0f,
55+
.direction = 1,
56+
.ik_cstr = "83fd23a244a74cf358da3019f1722635",
57+
.length = 768,
58+
.message_cstr =
59+
"35c68716633c66fb750c266865d53c11ea05b1e9fa49c8398d48e1efa5909d3947902837f5ae96d5a05bc8d61ca8dbef1b13a4b4ab"
60+
"fe4fb1006045b674bb54729304c382be53a5af05556176f6eaa2ef1d05e4b083181ee674cda5a485f74d7a",
61+
.mact_cstr = "bba74492",
62+
},
63+
nia_test_set{
64+
.name = "128_NIA1_Test_Set_5_mod",
65+
.count_i = 0x36af6144,
66+
.bearer = 0x18,
67+
.direction = 0,
68+
.ik_cstr = "6832a65cff4473621ebdd4ba26a921fe",
69+
.length = 384,
70+
.message_cstr =
71+
"d3c53839626820717765667620323837636240981ba6824c1bfb1ab485472029b71d808ce33e2cc3c0b5fc1f3de8a6dc",
72+
.mact_cstr = "bd09fa41",
73+
},
74+
nia_test_set{
75+
.name = "128_NIA1_Test_Set_6_mod",
76+
.count_i = 0x7827fab2,
77+
.bearer = 0x05,
78+
.direction = 1,
79+
.ik_cstr = "5d0a80d8134ae19677824b671e838af4",
80+
.length = 2560,
81+
.message_cstr = "70dedf2dc42c5cbd3a96f8a0b11418b3608d5733604a2cd36aabc70ce3193bb5153be2d3c06dfdb2d16e9c357158be"
82+
"6a41d6b861e491db3fbfeb518efcf048d7d58953730ff30c9ec470ffcd663dc34201c36addc0111c35b38afee7cfdb"
83+
"582e3731f8b4baa8d1a89c06e81199a9716227be344efcb436ddd0f096c064c3b5e2c399993fc77394f9e09720a811"
84+
"850ef23b2ee05d9e6173609d86e1c0c18ea51a012a00bb413b9cb8188a703cd6bae31cc67b34b1b00019e6a2b2a690"
85+
"f02671fe7c9ef8dec0094e533763478d58d2c5f5b827a0148c5948a96931acf84f465a64e62ce74007e991e37ea823"
86+
"fa0fb21923b79905b733b631e6c7d6860a3831ac351a9c730c52ff72d9d308eedbab21fde143a0ea17e23edc1f74cb"
87+
"b3638a2033aaa15464eaa733385dbbeb6fd73509b857e6a419dca1d8907af977fbac4dfa35ec",
88+
.mact_cstr = "d7f391d8",
89+
},
90+
nia_test_set{
91+
.name = "128_NIA1_Test_Set_7",
92+
.count_i = 0x296f393c,
93+
.bearer = 0x0b,
94+
.direction = 1,
95+
.ik_cstr = "b3120ffdb2cf6af4e73eaf2ef4ebec69",
96+
.length = 16448,
97+
.message_cstr =
98+
"00000000000000000101010101010101e0958045f3a0bba4e3968346f0a3b8a7c02a018ae640765226b987c913e6cbf083570016cf"
99+
"83efbc61c082513e21561a427c009d28c298eface78ed6d56c2d4505ad032e9c04dc60e73a81696da665c6c48603a57b45ab332215"
100+
"85e68ee3169187fb0239528632dd656c807ea3248b7b46d002b2b5c7458eb85b9ce95879e0340859055e3b0abbc3eace8719caa802"
101+
"65c97205d5dc4bcc902fe1839629ed71328a0f0449f588557e6898860e042aecd84b2404c212c9222da5bf8a89ef6797870cf50771"
102+
"a60f66a2ee62853657addf04cdde07fa414e11f12b4d81b9b4e8ac538ea30666688d881f6c348421992f31b94f8806ed8fccff4c91"
103+
"23b89642527ad613b109bf75167485f1268bf884b4cd23d29a0934925703d634098f7767f1be7491e708a8bb949a3873708aef4a36"
104+
"239e50cc08235cd5ed6bbe578668a17b58c1171d0b90e813a9e4f58a89d719b11042d6360b1b0f52deb730a58d58faf46315954b0a"
105+
"872691475977dc88c0d733feff54600a0cc1d0300aaaeb94572c6e95b01ae90de04f1dce47f87e8fa7bebf77e1dbc20d6ba85cb914"
106+
"3d518b285dfa04b698bf0cf7819f20fa7a288eb0703d995c59940c7c66de57a9b70f82379b70e2031e450fcfd2181326fcd28d8823"
107+
"baaa80df6e0f443559647539fd8907c0ffd9d79c130ed81c9afd9b7e848c9fed38443d5d380e53fbdb8ac8c3d3f06876054f122461"
108+
"107de92fea09c6f6923a188d53afe54a10f60e6e9d5a03d996b5fbc820f8a637116a27ad04b444a0932dd60fbd12671c11e1c0ec73"
109+
"e789879faa3d42c64d20cd1252742a3768c25a901585888ecee1e612d9936b403b0775949a66cdfd99a29b1345baa8d9d5400c9102"
110+
"4b0a607363b013ce5de9ae869d3b8d95b0570b3c2d391422d32450cbcfae96652286e96dec1214a9346527980a8192eac1c39a3aaf"
111+
"6f15351da6be764df89772ec0407d06e4415befae7c92580df9bf507497c8f2995160d4e218daacb02944abf83340ce8be1686a960"
112+
"faf90e2d90c55cc6475babc3171a80a363174954955d7101dab16ae8179167e21444b443a9eaaa7c91de36d118c39d389f8dd4469a"
113+
"846c9a262bf7fa18487a79e8de11699e0b8fdf557cb48719d453ba713056109b93a218c89675ac195fb4fb06639b3797144955b3c9"
114+
"327d1aec003d42ecd0ea98abf19ffb4af3561a67e77c35bf15c59c2412da881db02b1bfbcebfac5152bc99bc3f1d15f771001b7029"
115+
"fedb028f8b852bc4407eb83f891c9ca733254fdd1e9edb56919ce9fea21c174072521c18319a54b5d4efbebddf1d8b69b1cbf25f48"
116+
"9fcc981372547cf41d008ef0bca1926f934b735e090b3b251eb33a36f82ed9b29cf4cb944188fa0e1e38dd778f7d1c9d987b28d132"
117+
"dfb9731fa4f4b416935be49de30516af3578581f2f13f561c0663361941eab249a4bc123f8d15cd711a956a1bf20fe6eb78aea2373"
118+
"361da0426c79a530c3bb1de0c99722ef1fde39ac2b00a0a8ee7c800a08bc2264f89f4effe627ac2f0531fb554f6d21d74c590a70ad"
119+
"faa390bdfbb3d68e46215cab187d2368d5a71f5ebec081cd3b20c082dbe4cd2faca28773795d6b0c10204b659a939ef29bbe108824"
120+
"3624429927a7eb576dd3a00ea5e01af5d47583b2272c0c161a806521a16ff9b0a722c0cf26b025d5836e2258a4f7d4773ac801e426"
121+
"3bc294f43def7fa8703f3a4197463525887652b0b2a4a2a7cf87f00914871e25039113c7e1618da34064b57a43c463249fb8d05e0f"
122+
"26f4a6d84972e7a9054824145f91295cdbe39a6f920facc659712b46a54ba295bbe6a90154e91b33985a2bcd420ad5c67ec9ad8eb7"
123+
"ac6864db272a516bc94c2839b0a8169a6bf58e1a0c2ada8c883b7bf497a49171268ed15ddd2969384e7ff4bf4aab2ec9ecc6529cf6"
124+
"29e2df0f08a77a65afa12aa9b505df8b287ef6cc91493d1caa39076e28ef1ea028f5118de61ae02bb6aefc3343a050292f199f4018"
125+
"57b2bead5e6ee2a1f191022f9278016f047791a9d18da7d2a6d27f2e0e51c2f6ea30e8ac49a0604f4c13542e85b68381b9fdcfa0ce"
126+
"4b2d341354852d360245c536b612af71f3e77c9095ae2dbde504b265733dabfe10a20fc7d6d32c21ccc72b8b3444ae663d65922d17"
127+
"f82caa2b865cd88913d291a65899026ea1328439723c198c36b0c3c8d085bfaf8a320fde334b4a4919b44c2b95f6e8ecf73393f7f0"
128+
"d2a40e60b1d406526b022ddc331810b1a5f7c347bd53ed1f105d6a0d30aba477e178889ab2ec55d558deab2630204336962b4db5b6"
129+
"63b6902b89e85b31bc6af50fc50accb3fb9b57b663297031378db47896d7fbaf6c600add2c67f936db037986db856eb49cf2db3f7d"
130+
"a6d23650e438f1884041b013119e4c2ae5af37cccdfb68660738b58b3c59d1c0248437472aba1f35ca1fb90cd714aa9f635534f49e"
131+
"7c5bba81c2b6b36fdee21ca27e347f793d2ce944edb23c8c9b914be10335e350feb5070394b7a4a15c0ca120283568b7bfc254fe83"
132+
"8b137a2147ce7c113a3a4d65499d9e86b87dbcc7f03bbd3a3ab1aa243ece5ba9bcf25f82836cfe473b2d83e7a7201cd0b96a72451e"
133+
"863f6c3ba664a6d073d1f7b5ed990865d978bd3815d06094fc9a2aba5221c22d5ab996389e3721e3af5f05beddc2875e0dfaeb3902"
134+
"1ee27a41187cbb45ef40c3e73bc03989f9a30d12c54ba7d2141da8a875493e65776ef35f97debc2286cc4af9b4623eee902f840c52"
135+
"f1b8ad658939aef71f3f72b9ec1de21588bd35484ea44436343ff95ead6ab1d8afb1b2a303df1b71e53c4aea6b2e3e9372be0d1bc9"
136+
"9798b0ce3cc10d2a596d565dba82f88ce4cff3b33d5d24e9c0831124bf1ad54b792532983dd6c3a8b7d0",
137+
.mact_cstr = "abf3e651",
138+
},
139+
};

0 commit comments

Comments
 (0)