Skip to content

Commit a0d1e6b

Browse files
committed
[#375] WIP - shows a good example of TLV actually being corrupted and deactivating a bunch of keys. Tests are being worked pre-development
1 parent f4d8552 commit a0d1e6b

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

src/core/crypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ int32_t Crypto_Process_Extended_Procedure_Pdu(TC_t *tc_sdls_processed_frame, uin
837837

838838
// Subtract headers from total frame length
839839
// uint16_t max_tlv = tc_sdls_processed_frame->tc_header.fl - CCSDS_HDR_SIZE - CCSDS_PUS_SIZE - SDLS_TLV_HDR_SIZE;
840-
if (sdls_frame.hdr.pkt_length < TLV_DATA_SIZE) // && (sdls_frame.hdr.pkt_length < max_tlv))
840+
if (sdls_frame.hdr.pkt_length <= TLV_DATA_SIZE) // && (sdls_frame.hdr.pkt_length < max_tlv))
841841
{
842842
for (int x = 13; x < (13 + sdls_frame.hdr.pkt_length); x++)
843843
{

test/unit/ut_ep_key_mgmt.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,122 @@ UTEST(EP_KEY_MGMT, DEACTIVATE_142_PUS_BAD_TLV)
873873
free(buffer_DEACTIVATE_b);
874874
}
875875

876+
/*
877+
** Test EP PDU TLV Values
878+
*/
879+
UTEST(EP_KEY_MGMT, TLV_TESTS)
880+
{
881+
remove("sa_save_file.bin");
882+
uint8_t *ptr_enc_frame = NULL;
883+
// Setup & Initialize CryptoLib
884+
Crypto_Config_CryptoLib(KEY_TYPE_INTERNAL, MC_TYPE_INTERNAL, SA_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT,
885+
IV_INTERNAL, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR,
886+
TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE,
887+
TC_CHECK_FECF_FALSE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE);
888+
889+
GvcidManagedParameters_t TC_0_Managed_Parameters = {
890+
0, 0x0003, 0, TC_NO_FECF, AOS_FHEC_NA, AOS_IZ_NA, 0, TC_HAS_SEGMENT_HDRS, 1024, TC_OCF_NA, 1};
891+
Crypto_Config_Add_Gvcid_Managed_Parameters(TC_0_Managed_Parameters);
892+
893+
GvcidManagedParameters_t TC_1_Managed_Parameters = {
894+
0, 0x0003, 1, TC_NO_FECF, AOS_FHEC_NA, AOS_IZ_NA, 0, TC_HAS_SEGMENT_HDRS, 1024, TC_OCF_NA, 1};
895+
Crypto_Config_Add_Gvcid_Managed_Parameters(TC_1_Managed_Parameters);
896+
897+
Crypto_Init();
898+
SaInterface sa_if = get_sa_interface_inmemory();
899+
crypto_key_t *ekp = NULL;
900+
int status = CRYPTO_LIB_SUCCESS;
901+
902+
// NOTE: Added Transfer Frame header to the plaintext
903+
char *buffer_nist_key_h = "000102030405060708090A0B0C0D0E0F000102030405060708090A0B0C0D0E0F";
904+
char *buffer_nist_iv_h = "b6ac8e4963f49207ffd6374b"; // The last valid IV that was seen by the SA
905+
906+
// These assume a max TLV of 494 as defined by TLV_DATA_SIZE
907+
// 2003001c00ff000000001880d039FFFF197f0b00030002008e1f6d21c4555555555555
908+
// 197f0b00 - pus
909+
// 03 - tag
910+
// 0002 - length
911+
// 008e - value
912+
913+
char *buffer_TLV_OVERRUN_h = "2003001c00ff000000001880d039FFFF197f0b0003FFFF008e1f6d21c4555555555555";
914+
char *buffer_TLV_MAX_h = "2003001c00ff000000001880d03901EE197f0b000301EE008e1f6d21c4555555555555";
915+
char *buffer_TLV_MAX_PLUS_h = "2003001c00ff000000001880d03901EF197f0b000301EF008e1f6d21c4555555555555";
916+
char *buffer_TLV_ONE_h = "2003001c00ff000000001880d0390001197f0b0003000100811f6d21c4555555555555";
917+
char *buffer_TLV_ZERO_h = "2003001c00ff000000001880d0390000197f0b00030000008e1f6d21c4555555555555";
918+
919+
uint8_t *buffer_nist_iv_b, *buffer_nist_key_b, *buffer_TLV_OVERRUN_b, *buffer_TLV_MAX_b,
920+
*buffer_TLV_MAX_PLUS_b, *buffer_TLV_ONE_b, *buffer_TLV_ZERO_b = NULL;
921+
int buffer_nist_iv_len, buffer_nist_key_len, buffer_TLV_OVERRUN_len, buffer_TLV_MAX_len,
922+
buffer_TLV_MAX_PLUS_len, buffer_TLV_ONE_len, buffer_TLV_ZERO_len = 0;
923+
924+
// Setup Processed Frame For Decryption
925+
TC_t tc_nist_processed_frame;
926+
927+
// Expose/setup SAs for testing
928+
SecurityAssociation_t *test_association;
929+
930+
// Deactivate SA 1
931+
sa_if->sa_get_from_spi(1, &test_association);
932+
test_association->sa_state = SA_NONE;
933+
934+
// Activate SA 0
935+
sa_if->sa_get_from_spi(0, &test_association);
936+
test_association->sa_state = SA_OPERATIONAL;
937+
// test_association->ecs_len = 1;
938+
test_association->ecs = CRYPTO_CIPHER_NONE;
939+
test_association->est = 0;
940+
test_association->ast = 0;
941+
test_association->iv_len = 12;
942+
test_association->shsnf_len = 2;
943+
test_association->arsn_len = 2;
944+
test_association->arsnw = 5;
945+
946+
// Insert key into keyring of SA 9
947+
hex_conversion(buffer_nist_key_h, (char **)&buffer_nist_key_b, &buffer_nist_key_len);
948+
ekp = key_if->get_key(142);
949+
memcpy(ekp->value, buffer_nist_key_b, buffer_nist_key_len);
950+
ekp->key_state = KEY_ACTIVE;
951+
952+
// Convert frames that will be processed
953+
hex_conversion(buffer_TLV_OVERRUN_h, (char **)&buffer_TLV_OVERRUN_b, &buffer_TLV_OVERRUN_len);
954+
hex_conversion(buffer_TLV_MAX_h, (char **)&buffer_TLV_MAX_b, &buffer_TLV_MAX_len);
955+
hex_conversion(buffer_TLV_MAX_PLUS_h, (char **)&buffer_TLV_MAX_PLUS_b, &buffer_TLV_MAX_PLUS_len);
956+
hex_conversion(buffer_TLV_ZERO_h, (char **)&buffer_TLV_ZERO_b, &buffer_TLV_ZERO_len);
957+
hex_conversion(buffer_TLV_ONE_h, (char **)&buffer_TLV_ONE_b, &buffer_TLV_ONE_len);
958+
// Convert/Set input IV
959+
hex_conversion(buffer_nist_iv_h, (char **)&buffer_nist_iv_b, &buffer_nist_iv_len);
960+
memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len);
961+
962+
printf(KGRN "Checking for TLV overrun, should fail... \n" RESET);
963+
status = Crypto_TC_ProcessSecurity(buffer_TLV_OVERRUN_b, &buffer_TLV_OVERRUN_len, &tc_nist_processed_frame);
964+
ASSERT_EQ(CRYPTO_LIB_ERR_BAD_TLV_LENGTH, status);
965+
966+
printf(KGRN "Checking for TLV MAX, should pass... \n" RESET);
967+
status = Crypto_TC_ProcessSecurity(buffer_TLV_MAX_b, &buffer_TLV_MAX_len, &tc_nist_processed_frame);
968+
ASSERT_EQ(CRYPTO_LIB_SUCCESS, status);
969+
970+
printf(KGRN "Checking for TLV MAX + 1, should fail... \n" RESET);
971+
status = Crypto_TC_ProcessSecurity(buffer_TLV_MAX_PLUS_b, &buffer_TLV_MAX_PLUS_len, &tc_nist_processed_frame);
972+
ASSERT_EQ(CRYPTO_LIB_ERR_BAD_TLV_LENGTH, status);
973+
974+
printf(KGRN "Checking for TLV length of 1, should pass... \n" RESET);
975+
status = Crypto_TC_ProcessSecurity(buffer_TLV_ONE_b, &buffer_TLV_ONE_len, &tc_nist_processed_frame);
976+
ASSERT_EQ(CRYPTO_LIB_SUCCESS, status);
977+
978+
printf(KGRN "Checking for TLV length of 0, should ????... \n" RESET);
979+
status = Crypto_TC_ProcessSecurity(buffer_TLV_ONE_b, &buffer_TLV_ONE_len, &tc_nist_processed_frame);
980+
ASSERT_EQ(-110000, status);
981+
982+
printf("\n");
983+
Crypto_Shutdown();
984+
free(ptr_enc_frame);
985+
free(buffer_nist_iv_b);
986+
free(buffer_nist_key_b);
987+
free(buffer_TLV_MAX_b);
988+
free(buffer_TLV_MAX_PLUS_b);
989+
free(buffer_TLV_ONE_b);
990+
free(buffer_TLV_ZERO_b);
991+
free(buffer_TLV_OVERRUN_b);
992+
}
993+
876994
UTEST_MAIN();

0 commit comments

Comments
 (0)