Skip to content

Commit ceff955

Browse files
sec: add NEA1 ciphering engine
1 parent 9463865 commit ceff955

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

lib/security/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
set(SOURCES
1111
ciphering_engine_generic.cpp
12+
ciphering_engine_nea1.cpp
1213
ciphering_engine_nea2.cpp
1314
integrity_engine_generic.cpp
1415
security.cpp
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
*
3+
* \section COPYRIGHT
4+
*
5+
* Copyright 2021-2024 Software Radio Systems Limited
6+
*
7+
* By using this file, you agree to the terms and conditions set
8+
* forth in the LICENSE file which can be found at the top level of
9+
* the distribution.
10+
*
11+
*/
12+
13+
#include "ciphering_engine_nea1.h"
14+
#include "srsran/security/ciphering.h"
15+
16+
using namespace srsran;
17+
using namespace security;
18+
19+
ciphering_engine_nea1::ciphering_engine_nea1(sec_128_key k_128_enc_,
20+
uint8_t bearer_id_,
21+
security_direction direction_) :
22+
k_128_enc(k_128_enc_), bearer_id(bearer_id_), direction(direction_)
23+
{
24+
}
25+
26+
security_result ciphering_engine_nea1::apply_ciphering(byte_buffer buf, size_t offset, uint32_t count)
27+
{
28+
security_result result{.buf = std::move(buf), .count = count};
29+
byte_buffer_view msg{result.buf.value().begin() + offset, result.buf.value().end()};
30+
31+
security_nea1(k_128_enc, count, bearer_id, direction, msg);
32+
33+
return result;
34+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
*
3+
* \section COPYRIGHT
4+
*
5+
* Copyright 2021-2024 Software Radio Systems Limited
6+
*
7+
* By using this file, you agree to the terms and conditions set
8+
* forth in the LICENSE file which can be found at the top level of
9+
* the distribution.
10+
*
11+
*/
12+
13+
#pragma once
14+
15+
#include "srsran/security/ciphering_engine.h"
16+
#include "srsran/security/security.h"
17+
18+
namespace srsran {
19+
namespace security {
20+
21+
class ciphering_engine_nea1 final : public ciphering_engine
22+
{
23+
public:
24+
ciphering_engine_nea1(sec_128_key k_128_enc_, uint8_t bearer_id_, security_direction direction_);
25+
virtual ~ciphering_engine_nea1() = default;
26+
27+
security_result apply_ciphering(byte_buffer buf, size_t offset, uint32_t count) override;
28+
29+
private:
30+
sec_128_key k_128_enc;
31+
uint8_t bearer_id;
32+
security_direction direction;
33+
};
34+
35+
} // namespace security
36+
} // namespace srsran

lib/security/security_engine_impl.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "security_engine_impl.h"
1414
#include "ciphering_engine_generic.h"
15+
#include "ciphering_engine_nea1.h"
1516
#include "ciphering_engine_nea2.h"
1617
#include "integrity_engine_generic.h"
1718

@@ -34,11 +35,16 @@ security_engine_impl::security_engine_impl(security::sec_128_as_config sec_cfg,
3435
}
3536
}
3637
if (ciphering_enabled == security::ciphering_enabled::on) {
37-
if (sec_cfg.cipher_algo == ciphering_algorithm::nea2) {
38-
cipher_eng = std::make_unique<ciphering_engine_nea2>(sec_cfg.k_128_enc, bearer_id, direction);
39-
} else {
40-
cipher_eng =
41-
std::make_unique<ciphering_engine_generic>(sec_cfg.k_128_enc, bearer_id, direction, sec_cfg.cipher_algo);
38+
switch (sec_cfg.cipher_algo) {
39+
case ciphering_algorithm::nea1:
40+
cipher_eng = std::make_unique<ciphering_engine_nea1>(sec_cfg.k_128_enc, bearer_id, direction);
41+
break;
42+
case ciphering_algorithm::nea2:
43+
cipher_eng = std::make_unique<ciphering_engine_nea2>(sec_cfg.k_128_enc, bearer_id, direction);
44+
break;
45+
default:
46+
cipher_eng =
47+
std::make_unique<ciphering_engine_generic>(sec_cfg.k_128_enc, bearer_id, direction, sec_cfg.cipher_algo);
4248
}
4349
}
4450
}

tests/unittests/security/ciphering_engine_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111
#include "lib/security/ciphering_engine_generic.h"
12+
#include "lib/security/ciphering_engine_nea1.h"
1213
#include "lib/security/ciphering_engine_nea2.h"
1314
#include "nea1_test_set.h"
1415
#include "nea2_test_set.h"
@@ -103,6 +104,26 @@ bool trim_tail_to_bitlength(byte_buffer_view buf, uint32_t bitlength)
103104
return true;
104105
}
105106

107+
TEST_P(fxt_nea1, ciphering_engine_nea1)
108+
{
109+
nea_test_set param = GetParam();
110+
111+
// Pack hex strings into srsran types
112+
sec_128_key key = make_sec_128_key(param.key_cstr);
113+
auto dir = static_cast<security_direction>(param.direction);
114+
byte_buffer plaintext = make_byte_buffer(param.plaintext_cstr).value();
115+
byte_buffer ciphertext = make_byte_buffer(param.ciphertext_cstr).value();
116+
117+
// Create ciphering engine
118+
std::unique_ptr<ciphering_engine> nea = std::make_unique<ciphering_engine_nea1>(key, param.bearer, dir);
119+
120+
// Apply ciphering and compare results
121+
security_result result = nea->apply_ciphering(plaintext.deep_copy().value(), 0, param.count);
122+
ASSERT_TRUE(result.buf.has_value());
123+
ASSERT_TRUE(trim_tail_to_bitlength(result.buf.value(), param.length));
124+
EXPECT_EQ(result.buf.value(), ciphertext);
125+
}
126+
106127
TEST_P(fxt_nea2, ciphering_engine_nea2)
107128
{
108129
nea_test_set param = GetParam();

0 commit comments

Comments
 (0)