From ea3c44e9ac695df2c7d8fee3c8f46d5b7de64301 Mon Sep 17 00:00:00 2001 From: Pedro Alvarez Date: Tue, 16 Jul 2019 17:54:54 +0100 Subject: [PATCH] Removing header before decrypting. Continuing to add decryption tests to PDCP NR --- lib/include/srslte/upper/pdcp_entity_nr.h | 6 ++--- lib/src/upper/pdcp_entity_base.cc | 11 ++++++--- lib/src/upper/pdcp_entity_nr.cc | 30 ++++++++++++++++------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/lib/include/srslte/upper/pdcp_entity_nr.h b/lib/include/srslte/upper/pdcp_entity_nr.h index c182270e2..373591b34 100644 --- a/lib/include/srslte/upper/pdcp_entity_nr.h +++ b/lib/include/srslte/upper/pdcp_entity_nr.h @@ -74,9 +74,9 @@ private: uint32_t window_size = 0; // Packing/Unpacking Helper functions - void write_data_header(const unique_byte_buffer_t& sdu, uint32_t sn); - void append_mac(const unique_byte_buffer_t& sdu, uint8_t* mac); - uint32_t get_rcvd_sn(const unique_byte_buffer_t& pdu); + void read_data_header(const unique_byte_buffer_t& sdu, uint32_t* rcvd_sn); + void write_data_header(const unique_byte_buffer_t& sdu, uint32_t sn); + void append_mac(const unique_byte_buffer_t& sdu, uint8_t* mac); }; } // namespace srslte diff --git a/lib/src/upper/pdcp_entity_base.cc b/lib/src/upper/pdcp_entity_base.cc index 2915d214a..c5d158df2 100644 --- a/lib/src/upper/pdcp_entity_base.cc +++ b/lib/src/upper/pdcp_entity_base.cc @@ -92,8 +92,8 @@ void pdcp_entity_base::integrity_generate(uint8_t* msg, uint32_t msg_len, uint32 count, cfg.bearer_id, (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(msg, msg_len, " Message"); } 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, cfg.bearer_id, (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) { 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, cfg.bearer_id, (cfg.direction == SECURITY_DIRECTION_DOWNLINK) ? "Downlink" : "Uplink"); + log->debug_hex(msg, msg_len, "Cipher encrypt input msg"); switch (cipher_algo) { 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: 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) { byte_buffer_t msg_tmp; uint8_t *k_enc; + // If control plane use RRC encrytion key. If data use user plane key if (is_srb()) { 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; } - 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, cfg.bearer_id, (cfg.direction == SECURITY_DIRECTION_DOWNLINK) ? "Downlink" : "Uplink"); + log->debug_hex(ct, ct_len, "Cipher decrypt input msg"); switch(cipher_algo) { @@ -237,6 +241,7 @@ void pdcp_entity_base::cipher_decrypt(uint8_t* ct, uint32_t ct_len, uint32_t cou default: break; } + log->debug_hex(msg, ct_len,"Cipher decrypt output msg"); } } diff --git a/lib/src/upper/pdcp_entity_nr.cc b/lib/src/upper/pdcp_entity_nr.cc index 684b2c4d5..500d86b9e 100644 --- a/lib/src/upper/pdcp_entity_nr.cc +++ b/lib/src/upper/pdcp_entity_nr.cc @@ -45,6 +45,8 @@ void pdcp_entity_nr::init(srsue::rlc_interface_pdcp* rlc_, active = true; do_integrity = false; do_encryption = false; + + window_size = 1 << (cfg.sn_len - 1); } // Reestablishment procedure: 38.323 5.2 @@ -107,18 +109,27 @@ void pdcp_entity_nr::write_pdu(unique_byte_buffer_t pdu) return; } + // Extract RCVD_SN from header + uint32_t rcvd_sn; + read_data_header(pdu, &rcvd_sn); + // Calculate RCVD_COUNT - uint32_t rcvd_sn = get_rcvd_sn(pdu); + printf("%d\n", rcvd_sn); 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; } 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; } else { rcvd_hfn = HFN(rx_deliv); } rcvd_count = COUNT(rcvd_hfn, rcvd_sn); + // Decripting + cipher_decrypt(pdu->msg, pdu->N_bytes - 4, rcvd_count, pdu->msg); + // Integrity check uint8_t mac[4]; 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. } - // Decripting - cipher_decrypt(pdu->msg, pdu->N_bytes, rcvd_count, pdu->msg); // Check valid rcvd_count 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 } -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) { case PDCP_SN_LEN_12: - pdu->msg; + srslte::uint8_to_uint16(pdu->msg, &rcvd_sn_16); + (*rcvd_sn) = 0x0FFF & rcvd_sn_16; break; case PDCP_SN_LEN_18: - pdu->msg; + // srslte::uint8_to_uint24((uint16_t*)sdu->msg, rcvd_sn); break; default: 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)