Removing header before decrypting. Continuing to add decryption tests to PDCP NR

master
Pedro Alvarez 6 years ago committed by Andre Puschmann
parent 7e6725f0a2
commit ea3c44e9ac

@ -74,9 +74,9 @@ private:
uint32_t window_size = 0; uint32_t window_size = 0;
// Packing/Unpacking Helper functions // Packing/Unpacking Helper functions
void write_data_header(const unique_byte_buffer_t& sdu, uint32_t sn); void read_data_header(const unique_byte_buffer_t& sdu, uint32_t* rcvd_sn);
void append_mac(const unique_byte_buffer_t& sdu, uint8_t* mac); void write_data_header(const unique_byte_buffer_t& sdu, uint32_t sn);
uint32_t get_rcvd_sn(const unique_byte_buffer_t& pdu); void append_mac(const unique_byte_buffer_t& sdu, uint8_t* mac);
}; };
} // namespace srslte } // namespace srslte

@ -92,8 +92,8 @@ void pdcp_entity_base::integrity_generate(uint8_t* msg, uint32_t msg_len, uint32
count, count,
cfg.bearer_id, cfg.bearer_id,
(cfg.direction == SECURITY_DIRECTION_DOWNLINK ? "Downlink" : "Uplink")); (cfg.direction == SECURITY_DIRECTION_DOWNLINK ? "Downlink" : "Uplink"));
log->debug_hex(msg, msg_len, "Integrity gen input msg:");
log->debug_hex(mac, 4, "MAC (generated)"); log->debug_hex(mac, 4, "MAC (generated)");
log->debug_hex(msg, msg_len, " Message");
} }
bool pdcp_entity_base::integrity_verify(uint8_t* msg, uint32_t msg_len, uint32_t count, uint8_t* mac) bool pdcp_entity_base::integrity_verify(uint8_t* msg, uint32_t msg_len, uint32_t count, uint8_t* mac)
@ -140,7 +140,7 @@ bool pdcp_entity_base::integrity_verify(uint8_t* msg, uint32_t msg_len, uint32_t
count, count,
cfg.bearer_id, cfg.bearer_id,
(cfg.direction == SECURITY_DIRECTION_DOWNLINK ? "Downlink" : "Uplink")); (cfg.direction == SECURITY_DIRECTION_DOWNLINK ? "Downlink" : "Uplink"));
log->debug_hex(msg, msg_len, " Message"); log->debug_hex(msg, msg_len, "Integrity check input msg:");
if (integ_algo != INTEGRITY_ALGORITHM_ID_EIA0) { if (integ_algo != INTEGRITY_ALGORITHM_ID_EIA0) {
for (uint8_t i = 0; i < 4; i++) { for (uint8_t i = 0; i < 4; i++) {
@ -175,6 +175,7 @@ void pdcp_entity_base::cipher_encrypt(uint8_t* msg, uint32_t msg_len, uint32_t c
count, count,
cfg.bearer_id, cfg.bearer_id,
(cfg.direction == SECURITY_DIRECTION_DOWNLINK) ? "Downlink" : "Uplink"); (cfg.direction == SECURITY_DIRECTION_DOWNLINK) ? "Downlink" : "Uplink");
log->debug_hex(msg, msg_len, "Cipher encrypt input msg");
switch (cipher_algo) { switch (cipher_algo) {
case CIPHERING_ALGORITHM_ID_EEA0: case CIPHERING_ALGORITHM_ID_EEA0:
@ -190,12 +191,14 @@ void pdcp_entity_base::cipher_encrypt(uint8_t* msg, uint32_t msg_len, uint32_t c
default: default:
break; break;
} }
log->debug_hex(ct, msg_len, "Cipher encrypt output msg");
} }
void pdcp_entity_base::cipher_decrypt(uint8_t* ct, uint32_t ct_len, uint32_t count, uint8_t* msg) void pdcp_entity_base::cipher_decrypt(uint8_t* ct, uint32_t ct_len, uint32_t count, uint8_t* msg)
{ {
byte_buffer_t msg_tmp; byte_buffer_t msg_tmp;
uint8_t *k_enc; uint8_t *k_enc;
// If control plane use RRC encrytion key. If data use user plane key // If control plane use RRC encrytion key. If data use user plane key
if (is_srb()) { if (is_srb()) {
k_enc = k_rrc_enc; k_enc = k_rrc_enc;
@ -203,10 +206,11 @@ void pdcp_entity_base::cipher_decrypt(uint8_t* ct, uint32_t ct_len, uint32_t cou
k_enc = k_up_enc; k_enc = k_up_enc;
} }
log->debug("Cipher decript input: COUNT: %d, Bearer ID: %d, Direction %s\n", log->debug("Cipher decrypt input: COUNT: %d, Bearer ID: %d, Direction %s\n",
count, count,
cfg.bearer_id, cfg.bearer_id,
(cfg.direction == SECURITY_DIRECTION_DOWNLINK) ? "Downlink" : "Uplink"); (cfg.direction == SECURITY_DIRECTION_DOWNLINK) ? "Downlink" : "Uplink");
log->debug_hex(ct, ct_len, "Cipher decrypt input msg");
switch(cipher_algo) switch(cipher_algo)
{ {
@ -237,6 +241,7 @@ void pdcp_entity_base::cipher_decrypt(uint8_t* ct, uint32_t ct_len, uint32_t cou
default: default:
break; break;
} }
log->debug_hex(msg, ct_len,"Cipher decrypt output msg");
} }
} }

@ -45,6 +45,8 @@ void pdcp_entity_nr::init(srsue::rlc_interface_pdcp* rlc_,
active = true; active = true;
do_integrity = false; do_integrity = false;
do_encryption = false; do_encryption = false;
window_size = 1 << (cfg.sn_len - 1);
} }
// Reestablishment procedure: 38.323 5.2 // Reestablishment procedure: 38.323 5.2
@ -107,18 +109,27 @@ void pdcp_entity_nr::write_pdu(unique_byte_buffer_t pdu)
return; return;
} }
// Extract RCVD_SN from header
uint32_t rcvd_sn;
read_data_header(pdu, &rcvd_sn);
// Calculate RCVD_COUNT // Calculate RCVD_COUNT
uint32_t rcvd_sn = get_rcvd_sn(pdu); printf("%d\n", rcvd_sn);
uint32_t rcvd_hfn, rcvd_count; uint32_t rcvd_hfn, rcvd_count;
if (rcvd_sn < SN(rx_deliv) - window_size) { if ((int64_t)rcvd_sn < (int64_t)SN(rx_deliv) - (int64_t)window_size) {
printf("rcvd_sn %d < SN(rx_deliv) %d(%d) - window_size %d\n", rcvd_sn, SN(rx_deliv), rx_deliv, window_size);
rcvd_hfn = HFN(rx_deliv) + 1; rcvd_hfn = HFN(rx_deliv) + 1;
} else if (rcvd_sn >= SN(rx_deliv) + window_size) { } else if (rcvd_sn >= SN(rx_deliv) + window_size) {
printf("rcvd_sn %d >= SN(rx_deliv) %d(%d) + window_size %d\n", rcvd_sn, SN(rx_deliv), rx_deliv, window_size);
rcvd_hfn = HFN(rx_deliv) - 1; rcvd_hfn = HFN(rx_deliv) - 1;
} else { } else {
rcvd_hfn = HFN(rx_deliv); rcvd_hfn = HFN(rx_deliv);
} }
rcvd_count = COUNT(rcvd_hfn, rcvd_sn); rcvd_count = COUNT(rcvd_hfn, rcvd_sn);
// Decripting
cipher_decrypt(pdu->msg, pdu->N_bytes - 4, rcvd_count, pdu->msg);
// Integrity check // Integrity check
uint8_t mac[4]; uint8_t mac[4];
bool is_valid = integrity_verify(pdu->msg, pdu->N_bytes, rcvd_count, mac); bool is_valid = integrity_verify(pdu->msg, pdu->N_bytes, rcvd_count, mac);
@ -126,8 +137,6 @@ void pdcp_entity_nr::write_pdu(unique_byte_buffer_t pdu)
return; // Invalid packet, drop. return; // Invalid packet, drop.
} }
// Decripting
cipher_decrypt(pdu->msg, pdu->N_bytes, rcvd_count, pdu->msg);
// Check valid rcvd_count // Check valid rcvd_count
if (rcvd_count < rx_deliv /*|| check_received_before() TODO*/) { if (rcvd_count < rx_deliv /*|| check_received_before() TODO*/) {
@ -154,20 +163,23 @@ void pdcp_entity_nr::write_pdu(unique_byte_buffer_t pdu)
// TODO handle reordering timers // TODO handle reordering timers
} }
uint32_t pdcp_entity_nr::get_rcvd_sn(const unique_byte_buffer_t& pdu) void pdcp_entity_nr::read_data_header(const unique_byte_buffer_t& pdu, uint32_t *rcvd_sn)
{ {
uint32_t rcvd_sn = 0; uint16_t rcvd_sn_16 = 0;
switch (cfg.sn_len) { switch (cfg.sn_len) {
case PDCP_SN_LEN_12: case PDCP_SN_LEN_12:
pdu->msg; srslte::uint8_to_uint16(pdu->msg, &rcvd_sn_16);
(*rcvd_sn) = 0x0FFF & rcvd_sn_16;
break; break;
case PDCP_SN_LEN_18: case PDCP_SN_LEN_18:
pdu->msg; // srslte::uint8_to_uint24((uint16_t*)sdu->msg, rcvd_sn);
break; break;
default: default:
log->error("Cannot extract RCVD_SN, invalid SN length configured: %d\n", cfg.sn_len); log->error("Cannot extract RCVD_SN, invalid SN length configured: %d\n", cfg.sn_len);
} }
return rcvd_sn; pdu->msg += cfg.hdr_len_bytes;
pdu->N_bytes -= cfg.hdr_len_bytes;
return;
} }
void pdcp_entity_nr::write_data_header(const srslte::unique_byte_buffer_t& sdu, uint32_t count) void pdcp_entity_nr::write_data_header(const srslte::unique_byte_buffer_t& sdu, uint32_t count)

Loading…
Cancel
Save