diff --git a/src/core/crypto.c b/src/core/crypto.c index 613c02fc..48c18103 100644 --- a/src/core/crypto.c +++ b/src/core/crypto.c @@ -837,7 +837,7 @@ int32_t Crypto_Process_Extended_Procedure_Pdu(TC_t *tc_sdls_processed_frame, uin // Subtract headers from total frame length // uint16_t max_tlv = tc_sdls_processed_frame->tc_header.fl - CCSDS_HDR_SIZE - CCSDS_PUS_SIZE - SDLS_TLV_HDR_SIZE; - if (sdls_frame.hdr.pkt_length < TLV_DATA_SIZE) // && (sdls_frame.hdr.pkt_length < max_tlv)) + if (sdls_frame.hdr.pkt_length <= TLV_DATA_SIZE) // && (sdls_frame.hdr.pkt_length < max_tlv)) { for (int x = 13; x < (13 + sdls_frame.hdr.pkt_length); x++) { diff --git a/test/unit/ut_ep_key_mgmt.c b/test/unit/ut_ep_key_mgmt.c index fee64c54..8be35a09 100644 --- a/test/unit/ut_ep_key_mgmt.c +++ b/test/unit/ut_ep_key_mgmt.c @@ -873,4 +873,122 @@ UTEST(EP_KEY_MGMT, DEACTIVATE_142_PUS_BAD_TLV) free(buffer_DEACTIVATE_b); } +/* +** Test EP PDU TLV Values +*/ +UTEST(EP_KEY_MGMT, TLV_TESTS) +{ + remove("sa_save_file.bin"); + uint8_t *ptr_enc_frame = NULL; + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(KEY_TYPE_INTERNAL, MC_TYPE_INTERNAL, SA_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, + IV_INTERNAL, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_FALSE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + GvcidManagedParameters_t TC_0_Managed_Parameters = { + 0, 0x0003, 0, TC_NO_FECF, AOS_FHEC_NA, AOS_IZ_NA, 0, TC_HAS_SEGMENT_HDRS, 1024, TC_OCF_NA, 1}; + Crypto_Config_Add_Gvcid_Managed_Parameters(TC_0_Managed_Parameters); + + GvcidManagedParameters_t TC_1_Managed_Parameters = { + 0, 0x0003, 1, TC_NO_FECF, AOS_FHEC_NA, AOS_IZ_NA, 0, TC_HAS_SEGMENT_HDRS, 1024, TC_OCF_NA, 1}; + Crypto_Config_Add_Gvcid_Managed_Parameters(TC_1_Managed_Parameters); + + Crypto_Init(); + SaInterface sa_if = get_sa_interface_inmemory(); + crypto_key_t *ekp = NULL; + int status = CRYPTO_LIB_SUCCESS; + + // NOTE: Added Transfer Frame header to the plaintext + char *buffer_nist_key_h = "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F"; + char *buffer_nist_iv_h = "b6ac8e4963f49207ffd6374b"; // The last valid IV that was seen by the SA + + // These assume a max TLV of 494 as defined by TLV_DATA_SIZE + // 2003001c00ff000000001880d039FFFF197f0b00030002008e1f6d21c4555555555555 + // 197f0b00 - pus + // 03 - tag + // 0002 - length + // 008e - value + + char *buffer_TLV_OVERRUN_h = "2003001c00ff000000001880d039FFFF197f0b0003FFFF008e1f6d21c4555555555555"; + char *buffer_TLV_MAX_h = "2003001c00ff000000001880d03901EE197f0b000301EE008e1f6d21c4555555555555"; + char *buffer_TLV_MAX_PLUS_h = "2003001c00ff000000001880d03901EF197f0b000301EF008e1f6d21c4555555555555"; + char *buffer_TLV_ONE_h = "2003001c00ff000000001880d0390001197f0b0003000100811f6d21c4555555555555"; + char *buffer_TLV_ZERO_h = "2003001c00ff000000001880d0390000197f0b00030000008e1f6d21c4555555555555"; + + uint8_t *buffer_nist_iv_b, *buffer_nist_key_b, *buffer_TLV_OVERRUN_b, *buffer_TLV_MAX_b, + *buffer_TLV_MAX_PLUS_b, *buffer_TLV_ONE_b, *buffer_TLV_ZERO_b = NULL; + int buffer_nist_iv_len, buffer_nist_key_len, buffer_TLV_OVERRUN_len, buffer_TLV_MAX_len, + buffer_TLV_MAX_PLUS_len, buffer_TLV_ONE_len, buffer_TLV_ZERO_len = 0; + + // Setup Processed Frame For Decryption + TC_t tc_nist_processed_frame; + + // Expose/setup SAs for testing + SecurityAssociation_t *test_association; + + // Deactivate SA 1 + sa_if->sa_get_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + + // Activate SA 0 + sa_if->sa_get_from_spi(0, &test_association); + test_association->sa_state = SA_OPERATIONAL; + // test_association->ecs_len = 1; + test_association->ecs = CRYPTO_CIPHER_NONE; + test_association->est = 0; + test_association->ast = 0; + test_association->iv_len = 12; + test_association->shsnf_len = 2; + test_association->arsn_len = 2; + test_association->arsnw = 5; + + // Insert key into keyring of SA 9 + hex_conversion(buffer_nist_key_h, (char **)&buffer_nist_key_b, &buffer_nist_key_len); + ekp = key_if->get_key(142); + memcpy(ekp->value, buffer_nist_key_b, buffer_nist_key_len); + ekp->key_state = KEY_ACTIVE; + + // Convert frames that will be processed + hex_conversion(buffer_TLV_OVERRUN_h, (char **)&buffer_TLV_OVERRUN_b, &buffer_TLV_OVERRUN_len); + hex_conversion(buffer_TLV_MAX_h, (char **)&buffer_TLV_MAX_b, &buffer_TLV_MAX_len); + hex_conversion(buffer_TLV_MAX_PLUS_h, (char **)&buffer_TLV_MAX_PLUS_b, &buffer_TLV_MAX_PLUS_len); + hex_conversion(buffer_TLV_ZERO_h, (char **)&buffer_TLV_ZERO_b, &buffer_TLV_ZERO_len); + hex_conversion(buffer_TLV_ONE_h, (char **)&buffer_TLV_ONE_b, &buffer_TLV_ONE_len); + // Convert/Set input IV + hex_conversion(buffer_nist_iv_h, (char **)&buffer_nist_iv_b, &buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); + + printf(KGRN "Checking for TLV overrun, should fail... \n" RESET); + status = Crypto_TC_ProcessSecurity(buffer_TLV_OVERRUN_b, &buffer_TLV_OVERRUN_len, &tc_nist_processed_frame); + ASSERT_EQ(CRYPTO_LIB_ERR_BAD_TLV_LENGTH, status); + + printf(KGRN "Checking for TLV MAX, should pass... \n" RESET); + status = Crypto_TC_ProcessSecurity(buffer_TLV_MAX_b, &buffer_TLV_MAX_len, &tc_nist_processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf(KGRN "Checking for TLV MAX + 1, should fail... \n" RESET); + status = Crypto_TC_ProcessSecurity(buffer_TLV_MAX_PLUS_b, &buffer_TLV_MAX_PLUS_len, &tc_nist_processed_frame); + ASSERT_EQ(CRYPTO_LIB_ERR_BAD_TLV_LENGTH, status); + + printf(KGRN "Checking for TLV length of 1, should pass... \n" RESET); + status = Crypto_TC_ProcessSecurity(buffer_TLV_ONE_b, &buffer_TLV_ONE_len, &tc_nist_processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf(KGRN "Checking for TLV length of 0, should ????... \n" RESET); + status = Crypto_TC_ProcessSecurity(buffer_TLV_ONE_b, &buffer_TLV_ONE_len, &tc_nist_processed_frame); + ASSERT_EQ(-110000, status); + + printf("\n"); + Crypto_Shutdown(); + free(ptr_enc_frame); + free(buffer_nist_iv_b); + free(buffer_nist_key_b); + free(buffer_TLV_MAX_b); + free(buffer_TLV_MAX_PLUS_b); + free(buffer_TLV_ONE_b); + free(buffer_TLV_ZERO_b); + free(buffer_TLV_OVERRUN_b); +} + UTEST_MAIN(); \ No newline at end of file