From 1197d21992822fa804b93f9ca31cd4e0ed3a4c38 Mon Sep 17 00:00:00 2001 From: "D. Cody Cutright" Date: Tue, 14 Dec 2021 13:51:02 -0500 Subject: [PATCH 01/18] WIP on TC_Process header/field parsing --- fsw/public_inc/crypto_config_structs.h | 2 + fsw/public_inc/crypto_error.h | 2 + fsw/public_inc/crypto_structs.h | 4 +- fsw/src/crypto.c | 184 ++++++++++++++----------- 4 files changed, 112 insertions(+), 80 deletions(-) diff --git a/fsw/public_inc/crypto_config_structs.h b/fsw/public_inc/crypto_config_structs.h index 9e8c6cf2..e6474ff8 100644 --- a/fsw/public_inc/crypto_config_structs.h +++ b/fsw/public_inc/crypto_config_structs.h @@ -36,6 +36,7 @@ typedef enum { TC_NO_PUS_HDR, TC_HAS_PUS_HDR } TcPusHdrPresent; typedef enum { TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_SA_STATE_TRUE } TcIgnoreSaState; typedef enum { TC_IGNORE_ANTI_REPLAY_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE } TcIgnoreAntiReplay; typedef enum { TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_UNIQUE_SA_PER_MAP_ID_TRUE } TcUniqueSaPerMapId; +typedef enum { TC_CHECK_FECF_FALSE, TC_CHECK_FECF_TRUE } TcCheckFecfBool; /* ** Main Crypto Configuration Block @@ -49,6 +50,7 @@ typedef struct TcIgnoreSaState ignore_sa_state; //TODO - add logic that uses this configuration TcIgnoreAntiReplay ignore_anti_replay; TcUniqueSaPerMapId unique_sa_per_mapid; + TcCheckFecfBool crypto_check_fecf; uint8 vcid_bitmask; } CryptoConfig_t; #define CRYPTO_CONFIG_SIZE (sizeof(CryptoConfig_t)) diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index 9432242b..dfb3a2dd 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -38,5 +38,7 @@ #define CRYPTO_LIB_ERR_NULL_BUFFER (-9) #define CRYPTO_LIB_ERR_UT_BYTE_MISMATCH (-10) #define CRYPTO_LIB_ERR_NO_CONFIG (-11) +#define CRYPTO_LIB_ERR_INVALID_FECF (-12) +#define CRYPTO_LIB_ERR_BAD_ANTIREPLAY_WINDOW (-13) #endif //_crypto_error_h_ diff --git a/fsw/public_inc/crypto_structs.h b/fsw/public_inc/crypto_structs.h index 61c07012..0a554349 100644 --- a/fsw/public_inc/crypto_structs.h +++ b/fsw/public_inc/crypto_structs.h @@ -258,8 +258,8 @@ typedef struct uint8 sh:TC_SH_SIZE; // Segment Header uint16 spi; // Security Parameter Index uint8 iv[IV_SIZE]; // Initialization Vector for encryption - //uint8 sn[TC_SN_SIZE]; // Sequence Number for anti-replay - //uint8 pad[TC_PAD_SIZE]; // Count of the used fill Bytes + uint8 sn[TC_SN_SIZE]; // Sequence Number for anti-replay + uint8 pad[TC_PAD_SIZE]; // Count of the used fill Bytes } TC_FrameSecurityHeader_t; #define TC_FRAME_SECHEADER_SIZE (sizeof(TC_FrameSecurityHeader_t)) diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index 5f9bbab3..be6f0933 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -1066,7 +1066,7 @@ int32 Crypto_increment(uint8 *num, int length) static int32 Crypto_window(uint8 *actual, uint8 *expected, int length, int window) { - int status = OS_ERROR; + int status = CRYPTO_LIB_ERR_BAD_ANTIREPLAY_WINDOW; int result = 0; uint8 temp[length]; @@ -1085,7 +1085,7 @@ static int32 Crypto_window(uint8 *actual, uint8 *expected, int length, int windo } if (result == length) { - status = OS_SUCCESS; + status = CRYPTO_LIB_SUCCESS; break; } Crypto_increment(&temp[0], length); @@ -2831,6 +2831,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro gcry_cipher_hd_t tmp_hd; gcry_error_t gcry_error = GPG_ERR_NO_ERROR; SecurityAssociation_t* sa_ptr = NULL; + uint8 sa_service_type = -1; if(crypto_config == NULL) { @@ -2867,11 +2868,12 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro if(status != OS_SUCCESS) {return status;} //Unable to get necessary Managed Parameters for TC TF -- return with error. - // Security Header + // Segment Header if(current_managed_parameters->has_segmentation_hdr==TC_HAS_SEGMENT_HDRS){ tc_sdls_processed_frame->tc_sec_header.sh = (uint8)ingest[byte_idx]; byte_idx++; } + // Security Header tc_sdls_processed_frame->tc_sec_header.spi = ((uint8)ingest[byte_idx] << 8) | (uint8)ingest[byte_idx+1]; byte_idx+=2; #ifdef TC_DEBUG @@ -2879,97 +2881,123 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro OS_printf("spi = %d \n", tc_sdls_processed_frame->tc_sec_header.spi); #endif - // Checks - if (crypto_config->has_pus_hdr==TC_HAS_PUS_HDR && ((uint8)ingest[18] == 0x0B) && ((uint8)ingest[19] == 0x00) && (((uint8)ingest[20] & 0xF0) == 0x40)) - { - // User packet check only used for ESA Testing! + status = sadb_routine->sadb_get_sa_from_spi(tc_sdls_processed_frame->tc_sec_header.spi,&sa_ptr); + // If no valid SPI, return + if(status != CRYPTO_LIB_SUCCESS){ + return status; + } + + // Determine SA Service Type + if ((sa_ptr->est == 0) && (sa_ptr->ast == 0)) + { + sa_service_type = SA_PLAINTEXT; + } + else if ((sa_ptr->est == 0) && (sa_ptr->ast == 1)) + { + sa_service_type = SA_AUTHENTICATION; + } + else if ((sa_ptr->est == 1) && (sa_ptr->ast == 0)) + { + sa_service_type = SA_ENCRYPTION; + } + else if ((sa_ptr->est == 1) && (sa_ptr->ast == 1)) + { + sa_service_type = SA_AUTHENTICATED_ENCRYPTION; } else - { // Update last spi used - report.lspiu = tc_sdls_processed_frame->tc_sec_header.spi; + { + // Probably unnecessary check + // Leaving for now as it would be cleaner in SA to have an association enum returned I believe + OS_printf(KRED "Error: SA Service Type is not defined! \n" RESET); + status = OS_ERROR; + return status; + } - // Verify - if (tc_sdls_processed_frame->tc_header.scid != current_managed_parameters->scid) + #ifdef TC_DEBUG + switch(sa_service_type) { - OS_printf(KRED "Error: SCID incorrect! \n" RESET); - status = OS_ERROR; - } - else - { - switch (report.lspiu) - { // Invalid SPIs fall through to trigger flag in FSR - case 0x0000: - case 0xFFFF: - status = OS_ERROR; - report.ispif = 1; - OS_printf(KRED "Error: SPI invalid! \n" RESET); - break; - default: - break; - } + case SA_PLAINTEXT: + OS_printf(KBLU "Processing a TC - CLEAR!\n" RESET); + break; + case SA_AUTHENTICATION: + OS_printf(KBLU "Processing a TC - AUTHENTICATED!\n" RESET); + break; + case SA_ENCRYPTION: + OS_printf(KBLU "Processing a TC - ENCRYPTED!\n" RESET); + break; + case SA_AUTHENTICATED_ENCRYPTION: + OS_printf(KBLU "Processing a TC - AUTHENTICATED ENCRYPTION!\n" RESET); + break; } + #endif - if ((report.lspiu > NUM_SA) && (status == OS_SUCCESS)) - { - report.ispif = 1; - OS_printf(KRED "Error: SPI value greater than NUM_SA! \n" RESET); - status = OS_ERROR; - } - if (status == OS_SUCCESS) - { - if(sadb_routine->sadb_get_sa_from_spi(report.lspiu,&sa_ptr) != OS_SUCCESS){ - //TODO - Error handling - status = OS_ERROR; //Error -- unable to get SA from SPI. - } - } - if (status == OS_SUCCESS) + // TODO: Calculate lengths when needed + uint8 fecf_len = FECF_SIZE; + if(current_managed_parameters->has_fecf==TC_NO_FECF) { fecf_len = 0; } + + uint8 segment_hdr_len = SEGMENT_HDR_SIZE; + if(current_managed_parameters->has_segmentation_hdr==TC_NO_SEGMENT_HDRS) { segment_hdr_len = 0; } + + // Check FECF + if(current_managed_parameters->has_fecf==TC_HAS_FECF) + { + if(crypto_config->crypto_check_fecf == TC_CHECK_FECF_TRUE) { - if (sa_ptr->gvcid_tc_blk.mapid != TYPE_TC) - { - OS_printf(KRED "Error: SA invalid type! \n" RESET); - status = OS_ERROR; + uint16 received_fecf = (tc_sdls_processed_frame->tc_header.fl-1 & 0xFF00) || (tc_sdls_processed_frame->tc_header.fl & 0x00FF); + // Calculate our own + uint16 calculated_fecf = Crypto_Calc_FECF(ingest, len_ingest-2); + // Compare + if (received_fecf != calculated_fecf) + { + status = CRYPTO_LIB_ERR_INVALID_FECF; + return status; } } - // TODO: I don't think this is needed. - //if (status == OS_SUCCESS) - //{ - // if (sa_ptr->gvcid_tc_blk.vcid != tc_sdls_processed_frame->tc_header.vcid) - // { - // OS_printf(KRED "Error: VCID not mapped to provided SPI! \n" RESET); - // status = OS_ERROR; - // } - //} - if (status == OS_SUCCESS) + } + + // Parse the security header + tc_sdls_processed_frame->tc_sec_header.spi = (uint16)ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len]; + // Get SA via SPI + status = sadb_routine->sadb_get_sa_from_spi(tc_sdls_processed_frame->tc_sec_header.spi, &sa_ptr); + if(status != CRYPTO_LIB_SUCCESS){ return status; } + // Parse IV + memcpy(&(tc_sdls_processed_frame->tc_sec_header.iv)+(IV_SIZE-sa_ptr->shivf_len), ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN], sa_ptr->shivf_len); + // Parse Sequence Number + memcpy(&(tc_sdls_processed_frame->tc_sec_header.sn)+(TC_SN_SIZE-sa_ptr->shsnf_len), ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len], sa_ptr->shsnf_len); + // Parse pad length + memcpy(&(tc_sdls_processed_frame->tc_sec_header.pad)+(TC_PAD_SIZE-sa_ptr->shplf_len), ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len] , sa_ptr->shplf_len); + + // Check MAC, if applicable + if((sa_service_type == SA_AUTHENTICATION) || + (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) + { + if (crypto_config->ignore_anti_replay==TC_IGNORE_ANTI_REPLAY_FALSE ) { - if (sa_ptr->sa_state != SA_OPERATIONAL) + // If sequence number field is greater than zero, use as arsn + if(sa_ptr->shsnf_len > 0) { - OS_printf(KRED "Error: SA state not operational! \n" RESET); - status = OS_ERROR; + // Check Sequence Number is in ARCW + status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.sn, sa_ptr->arc, sa_ptr->shsnf_len, + sa_ptr->arcw[sa_ptr->arcw_len-1]); + if (status != CRYPTO_LIB_SUCCESS) { return status; } + // TODO: Update SA ARC through SADB_Routine function call } - } - if (status != OS_SUCCESS) - { - report.af = 1; - if (log_summary.rs > 0) + else { - Crypto_increment((uint8*)&log_summary.num_se, 4); - log_summary.rs--; - log.blk[log_count].emt = SPI_INVALID_EID; - log.blk[log_count].emv[0] = 0x4E; - log.blk[log_count].emv[1] = 0x41; - log.blk[log_count].emv[2] = 0x53; - log.blk[log_count].emv[3] = 0x41; - log.blk[log_count++].em_len = 4; + // Check IV is in ARCW + status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, + sa_ptr->arcw[sa_ptr->arcw_len-1]); + if (status != CRYPTO_LIB_SUCCESS) { return status; } + // TODO: Update SA IV through SADB_Routine function call } - *len_ingest = 0; - return status; + } } - if(sadb_routine->sadb_get_sa_from_spi(tc_sdls_processed_frame->tc_sec_header.spi,&sa_ptr) != OS_SUCCESS){ - //TODO - Error handling - status = OS_ERROR; //Error -- unable to get SA from SPI. - return status; - } + + // Decrypt, if applicable + + // Extended PDU processing, if applicable + if((sa_ptr->est == 1) && (sa_ptr->ast == 0)) { // Encryption Only From 072ffb47385b999dab008d1db5403853b9f4359f Mon Sep 17 00:00:00 2001 From: "D. Cody Cutright" Date: Tue, 14 Dec 2021 17:02:05 -0500 Subject: [PATCH 02/18] WIP on tc_process mac --- fsw/crypto_util/app/et_dt_validation.c | 16 +- fsw/public_inc/crypto_error.h | 2 + fsw/src/crypto.c | 879 ++----------------------- 3 files changed, 78 insertions(+), 819 deletions(-) diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index ee4be7ba..979b32d8 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -959,6 +959,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) { // Setup & Initialize CryptoLib uint16 enc_frame_len = 0; + int32 status; Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); @@ -971,7 +972,8 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) // Create a MAC'd frame by adding our headers and a fecf // | Header | SPI | iv | plaintext | mac |fecf| - char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c4e599eff39be8327e6950f03a329209d5776cb8"; + char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c40099eff39be8327e6950f03a329209d5776cb8"; // modified + // char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c4e599eff39be8327e6950f03a329209d5776cb8"; // original uint8 *buffer_nist_iv_b, *buffer_nist_pt_b, *buffer_nist_key_b, *buffer_cyber_chef_mac_b , *buffer_nist_mac_frame_b, *buffer_nist_cp_b = NULL; int buffer_nist_iv_len, buffer_nist_pt_len, buffer_nist_key_len, buffer_cyber_chef_mac_len , buffer_nist_mac_frame_len, buffer_nist_cp_len = 0; @@ -991,7 +993,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) test_association->ast = 1; test_association->est = 0; test_association->arc_len = 0; - test_association->abm_len = 20; + test_association->abm_len = 1024; memset(test_association->abm, 0xFF, (test_association->abm_len*sizeof(unsigned char))); test_association->shivf_len = 12; test_association->stmacf_len = 16; @@ -1012,18 +1014,19 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) // Convert mac frame hex_conversion(buffer_nist_mac_frame_h, &buffer_nist_mac_frame_b, &buffer_nist_mac_frame_len); - Crypto_TC_ProcessSecurity(buffer_nist_mac_frame_b, &buffer_nist_mac_frame_len, tc_nist_processed_frame); + status = Crypto_TC_ProcessSecurity(buffer_nist_mac_frame_b, &buffer_nist_mac_frame_len, tc_nist_processed_frame); + printf("TC_Process returned status %d\n", status); // Note: For comparison, interested in the TF payload (exclude headers and FECF if present) // Calc payload index: total length - pt length #ifdef DEBUG printf("Expected MAC: "); - for (int i=0; itc_pdu_len; i++) + for (int i=0; itc_pdu_len; i++) + for (int i=0; istmacf_len; i++) { printf("%02x ", tc_nist_processed_frame->tc_sec_trailer.mac[i]); } @@ -1032,7 +1035,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) Crypto_Shutdown(); // Verify the MAC - for (int i=0; i < tc_nist_processed_frame->tc_pdu_len; i++) + for (int i=0; i < test_association->stmacf_len; i++) { ASSERT_EQ(tc_nist_processed_frame->tc_sec_trailer.mac[i], buffer_cyber_chef_mac_b[i]); } @@ -1041,6 +1044,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) { ASSERT_EQ(tc_nist_processed_frame->tc_pdu[i], buffer_nist_pt_b[i]); } + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); free(buffer_nist_iv_b); free(buffer_nist_key_b); free(buffer_cyber_chef_mac_b); diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index dfb3a2dd..3abd60a6 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -40,5 +40,7 @@ #define CRYPTO_LIB_ERR_NO_CONFIG (-11) #define CRYPTO_LIB_ERR_INVALID_FECF (-12) #define CRYPTO_LIB_ERR_BAD_ANTIREPLAY_WINDOW (-13) +#define CRYPTO_LIB_ERR_LIBGCRYPT_ERROR (-14) +#define CRYPTO_LIB_ERR_AUTHENTICATION_ERROR (-15) #endif //_crypto_error_h_ diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index be6f0933..0e8884a2 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -2832,6 +2832,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro gcry_error_t gcry_error = GPG_ERR_NO_ERROR; SecurityAssociation_t* sa_ptr = NULL; uint8 sa_service_type = -1; + uint8* aad; if(crypto_config == NULL) { @@ -2943,9 +2944,9 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro { if(crypto_config->crypto_check_fecf == TC_CHECK_FECF_TRUE) { - uint16 received_fecf = (tc_sdls_processed_frame->tc_header.fl-1 & 0xFF00) || (tc_sdls_processed_frame->tc_header.fl & 0x00FF); + uint16 received_fecf = (tc_sdls_processed_frame->tc_header.fl-1 & 0xFF00) | (tc_sdls_processed_frame->tc_header.fl & 0x00FF); // Calculate our own - uint16 calculated_fecf = Crypto_Calc_FECF(ingest, len_ingest-2); + uint16 calculated_fecf = Crypto_Calc_FECF(ingest, *len_ingest-2); // Compare if (received_fecf != calculated_fecf) { @@ -2956,21 +2957,24 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro } // Parse the security header - tc_sdls_processed_frame->tc_sec_header.spi = (uint16)ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len]; + tc_sdls_processed_frame->tc_sec_header.spi = (uint16)((uint8)ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len] | (uint8)ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + 1]); // Get SA via SPI status = sadb_routine->sadb_get_sa_from_spi(tc_sdls_processed_frame->tc_sec_header.spi, &sa_ptr); if(status != CRYPTO_LIB_SUCCESS){ return status; } // Parse IV - memcpy(&(tc_sdls_processed_frame->tc_sec_header.iv)+(IV_SIZE-sa_ptr->shivf_len), ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN], sa_ptr->shivf_len); + memcpy((tc_sdls_processed_frame->tc_sec_header.iv)+(IV_SIZE-sa_ptr->shivf_len), &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN]), sa_ptr->shivf_len); // Parse Sequence Number - memcpy(&(tc_sdls_processed_frame->tc_sec_header.sn)+(TC_SN_SIZE-sa_ptr->shsnf_len), ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len], sa_ptr->shsnf_len); + memcpy((tc_sdls_processed_frame->tc_sec_header.sn)+(TC_SN_SIZE-sa_ptr->shsnf_len), &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len]), sa_ptr->shsnf_len); // Parse pad length - memcpy(&(tc_sdls_processed_frame->tc_sec_header.pad)+(TC_PAD_SIZE-sa_ptr->shplf_len), ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len] , sa_ptr->shplf_len); + memcpy((tc_sdls_processed_frame->tc_sec_header.pad)+(TC_PAD_SIZE-sa_ptr->shplf_len), &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len]) , sa_ptr->shplf_len); // Check MAC, if applicable if((sa_service_type == SA_AUTHENTICATION) || (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) { + uint16 tc_mac_start_index = tc_sdls_processed_frame->tc_header.fl + 1 - fecf_len - sa_ptr->stmacf_len; + // Parse the received MAC + memcpy((tc_sdls_processed_frame->tc_sec_trailer.mac)+(MAC_SIZE-sa_ptr->stmacf_len), &(ingest[tc_mac_start_index]) , sa_ptr->stmacf_len); if (crypto_config->ignore_anti_replay==TC_IGNORE_ANTI_REPLAY_FALSE ) { // If sequence number field is greater than zero, use as arsn @@ -2985,314 +2989,26 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro else { // Check IV is in ARCW - status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, + status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv+(IV_SIZE-sa_ptr->shivf_len), sa_ptr->iv, sa_ptr->shivf_len, sa_ptr->arcw[sa_ptr->arcw_len-1]); - if (status != CRYPTO_LIB_SUCCESS) { return status; } - // TODO: Update SA IV through SADB_Routine function call - } - - } - } - - // Decrypt, if applicable - - // Extended PDU processing, if applicable - - if((sa_ptr->est == 1) && (sa_ptr->ast == 0)) - { - // Encryption Only - #ifdef DEBUG - OS_printf(KBLU "ENCRYPTED TC Received!\n" RESET); - #endif - - #ifdef TC_DEBUG - OS_printf("IV: \n"); - #endif - for (x = byte_idx; x < (byte_idx + sa_ptr->shivf_len); x++) - { - tc_sdls_processed_frame->tc_sec_header.iv[x-byte_idx] = (uint8)ingest[x]; - #ifdef TC_DEBUG - OS_printf("\t iv[%d] = 0x%02x\n", x-byte_idx, tc_sdls_processed_frame->tc_sec_header.iv[x-byte_idx]); - #endif - } - byte_idx += sa_ptr->shivf_len; - report.snval = tc_sdls_processed_frame->tc_sec_header.iv[sa_ptr->shivf_len-1]; - - #ifdef DEBUG - OS_printf("\t tc_sec_header.iv[%d] = 0x%02x \n", sa_ptr->shivf_len-1, tc_sdls_processed_frame->tc_sec_header.iv[sa_ptr->shivf_len-1]); - OS_printf("\t sa[%d].iv[%d] = 0x%02x \n", tc_sdls_processed_frame->tc_sec_header.spi, sa_ptr->shivf_len-1, sa_ptr->iv[sa_ptr->shivf_len-1]); - #endif - - // Check IV is in ARCW - if ( crypto_config->ignore_anti_replay==TC_IGNORE_ANTI_REPLAY_FALSE ) - { - - if ( Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, - sa_ptr->arcw[sa_ptr->arcw_len-1]) != CRYPTO_LIB_SUCCESS ) - { - report.af = 1; - report.bsnf = 1; - if (log_summary.rs > 0) + printf("Received IV is\n\t"); + for(int i=IV_SIZE-sa_ptr->shivf_len; ishivf_len+(IV_SIZE-sa_ptr->shivf_len); i++) + // for(int i=0; itc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len) == CRYPTO_LIB_SUCCESS ) - { // Replay - IV value lower than expected - report.af = 1; - report.bsnf = 1; - if (log_summary.rs > 0) - { - Crypto_increment((uint8*)&log_summary.num_se, 4); - log_summary.rs--; - log.blk[log_count].emt = IV_REPLAY_ERR_EID; - log.blk[log_count].emv[0] = 0x4E; - log.blk[log_count].emv[1] = 0x41; - log.blk[log_count].emv[2] = 0x53; - log.blk[log_count].emv[3] = 0x41; - log.blk[log_count++].em_len = 4; - } - OS_printf(KRED "Error: IV replay! Value lower than expected! \n" RESET); - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - status = OS_ERROR; + printf("%02x", tc_sdls_processed_frame->tc_sec_header.iv[i]); } - else - { // Adjust expected IV to acceptable received value // TODO - separate ground processing from fsw processing - for (int i = 0; i < (sa_ptr->shivf_len); i++) - { - sa_ptr->iv[i] = tc_sdls_processed_frame->tc_sec_header.iv[i]; - } - } - } - - - } - - if ( status != CRYPTO_LIB_SUCCESS ) - { // Exit - *len_ingest = 0; - return status; - } - - tc_sdls_processed_frame->tc_pdu_len = Crypto_Get_tcPayloadLength(tc_sdls_processed_frame, sa_ptr); - - x = x + tc_sdls_processed_frame->tc_pdu_len; - - #ifdef TC_DEBUG - OS_printf("TC: \n"); - for (int temp = 0; temp < tc_sdls_processed_frame->tc_pdu_len; temp++) - { - OS_printf("\t ingest[%d] = 0x%02x \n", temp, (uint8)ingest[temp+20]); - } - #endif - - // FECF - tc_sdls_processed_frame->tc_sec_trailer.fecf = ((uint8)ingest[x] << 8) | ((uint8)ingest[x+1]); - Crypto_FECF(tc_sdls_processed_frame->tc_sec_trailer.fecf, ingest, (tc_sdls_processed_frame->tc_header.fl - 1),tc_sdls_processed_frame); - - // Initialize the key - //itc_gcm128_init(&sa[tc_sdls_processed_frame->tc_sec_header.spi].gcm_ctx, (const unsigned char*) &ek_ring[sa[sa_ptr->ekid]); - - gcry_error = gcry_cipher_open( - &(tmp_hd), - GCRY_CIPHER_AES256, - GCRY_CIPHER_MODE_GCM, - GCRY_CIPHER_CBC_MAC - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } - #ifdef DEBUG - OS_printf("Key ID = %d, 0x", sa_ptr->ekid); - for(int y = 0; y < KEY_SIZE; y++) - { - OS_printf("%02x", ek_ring[sa_ptr->ekid].value[y]); - } - OS_printf("\n"); - #endif - gcry_error = gcry_cipher_setkey( - tmp_hd, - &(ek_ring[sa_ptr->ekid].value[0]), - KEY_SIZE - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } - gcry_error = gcry_cipher_setiv( - tmp_hd, - &(sa_ptr->iv[0]), - sa_ptr->iv_len - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } - - gcry_error = gcry_cipher_decrypt( - tmp_hd, - &(tc_sdls_processed_frame->tc_pdu[0]), // plaintext output - tc_sdls_processed_frame->tc_pdu_len, // length of data - &(ingest[20]), // ciphertext input - tc_sdls_processed_frame->tc_pdu_len // in data length - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_decrypt error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } - gcry_cipher_close(tmp_hd); - - // Increment the IV for next time - #ifdef INCREMENT - Crypto_increment(sa_ptr->iv, sa_ptr->shivf_len); - #endif - } - // Determine mode via SPI - else if ((sa_ptr->est == 1) && - (sa_ptr->ast == 1)) - { // Authenticated/Encrypted - #ifdef DEBUG - OS_printf(KBLU "Authenticated/Encrypted TC Received!\n" RESET); - #endif - #ifdef TC_DEBUG - OS_printf("IV: \n"); - #endif - for (x = byte_idx; x < (byte_idx + sa_ptr->shivf_len); x++) - { - tc_sdls_processed_frame->tc_sec_header.iv[x-byte_idx] = (uint8)ingest[x]; - #ifdef TC_DEBUG - OS_printf("\t iv[%d] = 0x%02x\n", x-byte_idx, tc_sdls_processed_frame->tc_sec_header.iv[x-byte_idx]); - #endif - } - byte_idx += sa_ptr->shivf_len; - report.snval = tc_sdls_processed_frame->tc_sec_header.iv[sa_ptr->shivf_len-1]; - - #ifdef DEBUG - OS_printf("\t tc_sec_header.iv[%d] = 0x%02x \n", sa_ptr->shivf_len-1, tc_sdls_processed_frame->tc_sec_header.iv[sa_ptr->shivf_len-1]); - OS_printf("\t sa[%d].iv[%d] = 0x%02x \n", tc_sdls_processed_frame->tc_sec_header.spi, sa_ptr->shivf_len-1, sa_ptr->iv[sa_ptr->shivf_len-1]); - #endif - - if ( crypto_config->ignore_anti_replay==TC_IGNORE_ANTI_REPLAY_FALSE ) - { - // Check IV is in ARCW - if ( Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, - sa_ptr->arcw[sa_ptr->arcw_len-1]) != OS_SUCCESS ) - { - report.af = 1; - report.bsnf = 1; - if (log_summary.rs > 0) + printf("\nSA IV is\n\t"); + for(int i=0; ishivf_len; i++) { - Crypto_increment((uint8*)&log_summary.num_se, 4); - log_summary.rs--; - log.blk[log_count].emt = IV_WINDOW_ERR_EID; - log.blk[log_count].emv[0] = 0x4E; - log.blk[log_count].emv[1] = 0x41; - log.blk[log_count].emv[2] = 0x53; - log.blk[log_count].emv[3] = 0x41; - log.blk[log_count++].em_len = 4; - } - OS_printf(KRED "Error: IV not in window! \n" RESET); - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - status = OS_ERROR; - } - else - { - if ( Crypto_compare_less_equal(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len) == OS_SUCCESS ) - { // Replay - IV value lower than expected - report.af = 1; - report.bsnf = 1; - if (log_summary.rs > 0) - { - Crypto_increment((uint8*)&log_summary.num_se, 4); - log_summary.rs--; - log.blk[log_count].emt = IV_REPLAY_ERR_EID; - log.blk[log_count].emv[0] = 0x4E; - log.blk[log_count].emv[1] = 0x41; - log.blk[log_count].emv[2] = 0x53; - log.blk[log_count].emv[3] = 0x41; - log.blk[log_count++].em_len = 4; - } - OS_printf(KRED "Error: IV replay! Value lower than expected! \n" RESET); - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - status = OS_ERROR; - } - else - { // Adjust expected IV to acceptable received value - for (int i = 0; i < (sa_ptr->shivf_len); i++) - { - sa_ptr->iv[i] = tc_sdls_processed_frame->tc_sec_header.iv[i]; - } + printf("%02x", sa_ptr->iv[i]); } + printf("\nARCW is: %02x\n", sa_ptr->arcw[0]); + if (status != CRYPTO_LIB_SUCCESS) { return status; } + // TODO: Update SA IV through SADB_Routine function call } + } - if ( status == OS_ERROR ) - { // Exit - *len_ingest = 0; - return status; - } - - tc_sdls_processed_frame->tc_pdu_len = Crypto_Get_tcPayloadLength(tc_sdls_processed_frame, sa_ptr); - - x = x + tc_sdls_processed_frame->tc_pdu_len; - - #ifdef TC_DEBUG - OS_printf("TC: \n"); - for (int temp = 0; temp < tc_sdls_processed_frame->tc_pdu_len; temp++) - { - OS_printf("\t ingest[%d] = 0x%02x \n", temp, (uint8)ingest[temp+20]); - } - #endif - - // Security Trailer - #ifdef TC_DEBUG - OS_printf("MAC: \n"); - #endif - for (y = x; y < (x + MAC_SIZE); y++) - { - tc_sdls_processed_frame->tc_sec_trailer.mac[y-x] = (uint8)ingest[y]; - #ifdef TC_DEBUG - OS_printf("\t mac[%d] = 0x%02x\n", y-x, tc_sdls_processed_frame->tc_sec_trailer.mac[y-x]); - #endif - } - x = x + sa_ptr->stmacf_len; - - // FECF - tc_sdls_processed_frame->tc_sec_trailer.fecf = ((uint8)ingest[x] << 8) | ((uint8)ingest[x+1]); - Crypto_FECF(tc_sdls_processed_frame->tc_sec_trailer.fecf, ingest, (tc_sdls_processed_frame->tc_header.fl - 1),tc_sdls_processed_frame); - - // Initialize the key - //itc_gcm128_init(&sa[tc_sdls_processed_frame->tc_sec_header.spi].gcm_ctx, (const unsigned char*) &ek_ring[sa[sa_ptr->ekid]); - gcry_error = gcry_cipher_open( &(tmp_hd), GCRY_CIPHER_AES256, @@ -3301,18 +3017,10 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { - OS_printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; + OS_printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; return status; } - #ifdef DEBUG - OS_printf("Key ID = %d, 0x", sa_ptr->ekid); - for(int y = 0; y < KEY_SIZE; y++) - { - OS_printf("%02x", ek_ring[sa_ptr->ekid].value[y]); - } - OS_printf("\n"); - #endif gcry_error = gcry_cipher_setkey( tmp_hd, ek_ring[sa_ptr->ekid].value, @@ -3321,547 +3029,92 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { OS_printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; return status; } gcry_error = gcry_cipher_setiv( tmp_hd, - sa_ptr->iv, - sa_ptr->iv_len + tc_sdls_processed_frame->tc_sec_header.iv, + sa_ptr->shivf_len ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { OS_printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; return status; } - #ifdef MAC_DEBUG - OS_printf("AAD = 0x"); - #endif - // Prepare additional authenticated data (AAD) - uint8 aad[sa_ptr->abm_len]; - for (y = 0; y < sa_ptr->abm_len; y++) + aad = (uint8*)malloc(tc_mac_start_index * sizeof(uint8)); + // Prepare additional authenticated data (AAD) + for (y = 0; y < tc_mac_start_index; y++) { aad[y] = (uint8) ((uint8)ingest[y] & (uint8)sa_ptr->abm[y]); - #ifdef MAC_DEBUG - OS_printf("%02x", (uint8) ingest[y]); - #endif } - #ifdef MAC_DEBUG - OS_printf("\n"); - #endif gcry_error = gcry_cipher_authenticate( tmp_hd, aad, // additional authenticated data - sa_ptr->abm_len // length of AAD + tc_mac_start_index // length of AAD ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { OS_printf(KRED "ERROR: gcry_cipher_authenticate error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); OS_printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error),gcry_strerror (gcry_error)); - status = OS_ERROR; - return status; - } - - gcry_error = gcry_cipher_decrypt( - tmp_hd, - tc_sdls_processed_frame->tc_pdu, // plaintext output - tc_sdls_processed_frame->tc_pdu_len, // length of data - &(ingest[*len_ingest - tc_sdls_processed_frame->tc_pdu_len - sa_ptr->stmacf_len - 2]), // ciphertext input - tc_sdls_processed_frame->tc_pdu_len // in data length - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_decrypt error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } - gcry_error = gcry_cipher_checktag( - tmp_hd, - tc_sdls_processed_frame->tc_sec_trailer.mac, // tag input - sa_ptr->stmacf_len // tag size - ); - - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_checktag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - - OS_printf("Calculated MAC = 0x"); - for (int z = 0; z < MAC_SIZE; z++) - { - OS_printf("%02x",tc_sdls_processed_frame->tc_sec_trailer.mac[z]); - } - OS_printf("\n"); - - gcry_error = gcry_cipher_gettag( - tmp_hd, - &(tc_sdls_processed_frame->tc_sec_trailer.mac[0]), // tag output - MAC_SIZE // tag size - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_checktag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - } - - OS_printf("Expected MAC = 0x"); - for (int z = 0; z < MAC_SIZE; z++) - { - OS_printf("%02x",tc_sdls_processed_frame->tc_sec_trailer.mac[z]); - } - OS_printf("\n"); - status = OS_ERROR; - report.bmacf = 1; - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - return status; - } - gcry_cipher_close(tmp_hd); - - // Increment the IV for next time - #ifdef INCREMENT - Crypto_increment(sa_ptr->iv, sa_ptr->shivf_len); - #endif - } - else if((sa_ptr->ast == 1) && (sa_ptr->est == 0)) - { - // Authentication only - #ifdef DEBUG - OS_printf(KBLU "Authenticated TC Received!\n" RESET); - #endif - #ifdef TC_DEBUG - OS_printf(KYEL "IV: \n\t"); - #endif - - for (x = byte_idx; x < (byte_idx + sa_ptr->shivf_len); x++) - { - tc_sdls_processed_frame->tc_sec_header.iv[x-byte_idx] = (uint8)ingest[x]; - #ifdef TC_DEBUG - OS_printf("%02x", tc_sdls_processed_frame->tc_sec_header.iv[x-byte_idx]); - #endif - } - - #ifdef TC_DEBUG - OS_printf("\n"RESET); - #endif - byte_idx += sa_ptr->shivf_len; - report.snval = tc_sdls_processed_frame->tc_sec_header.iv[sa_ptr->shivf_len-1]; - - #ifdef DEBUG - OS_printf("\ttc_sec_header.iv[%d] = 0x%02x \n", sa_ptr->shivf_len-1, tc_sdls_processed_frame->tc_sec_header.iv[sa_ptr->shivf_len-1]); - OS_printf("\tsa[%d].iv[%d] = 0x%02x \n", tc_sdls_processed_frame->tc_sec_header.spi, sa_ptr->shivf_len-1, sa_ptr->iv[sa_ptr->shivf_len-1]); - #endif - - if ( crypto_config->ignore_anti_replay==TC_IGNORE_ANTI_REPLAY_FALSE ) - { - // Check IV is in ARCW - if (Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, - sa_ptr->arcw[sa_ptr->arcw_len-1]) != OS_SUCCESS ) - { - report.af = 1; - report.bsnf = 1; - if (log_summary.rs > 0) - { - Crypto_increment((uint8*)&log_summary.num_se, 4); - log_summary.rs--; - log.blk[log_count].emt = IV_WINDOW_ERR_EID; - log.blk[log_count].emv[0] = 0x4E; - log.blk[log_count].emv[1] = 0x41; - log.blk[log_count].emv[2] = 0x53; - log.blk[log_count].emv[3] = 0x41; - log.blk[log_count++].em_len = 4; - } - OS_printf(KRED "Error: IV not in window! \n" RESET); - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - status = OS_ERROR; - } - else - { - if (Crypto_compare_less_equal(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len) == OS_SUCCESS ) - { // Replay - IV value lower than expected - report.af = 1; - report.bsnf = 1; - if (log_summary.rs > 0) - { - Crypto_increment((uint8*)&log_summary.num_se, 4); - log_summary.rs--; - log.blk[log_count].emt = IV_REPLAY_ERR_EID; - log.blk[log_count].emv[0] = 0x4E; - log.blk[log_count].emv[1] = 0x41; - log.blk[log_count].emv[2] = 0x53; - log.blk[log_count].emv[3] = 0x41; - log.blk[log_count++].em_len = 4; - } - OS_printf(KRED "Error: IV replay! Value lower than expected! \n" RESET); - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - status = OS_ERROR; - } - else - { // Adjust expected IV to acceptable received value - for (int i = 0; i < (sa_ptr->shivf_len); i++) - { - sa_ptr->iv[i] = tc_sdls_processed_frame->tc_sec_header.iv[i]; - } - } - } - } - if ( status == OS_ERROR ) - { // Exit - *len_ingest = 0; - return status; - } - tc_sdls_processed_frame->tc_pdu_len = Crypto_Get_tcPayloadLength(tc_sdls_processed_frame, sa_ptr); - - // Copy pdu data from ingest into memory - for(int i=0; itc_pdu_len; i++) - { - tc_sdls_processed_frame->tc_pdu[i] = ingest[x]; - x++; - } - // x = x + tc_sdls_processed_frame->tc_pdu_len; - - #ifdef TC_DEBUG - OS_printf("tc_pdu_len is: %d\n", tc_sdls_processed_frame->tc_pdu_len); - OS_printf("TC PDU ingest Payload: \n\t"); - for (int temp = 0; temp < tc_sdls_processed_frame->tc_pdu_len; temp++) - { - OS_printf("%02x", (uint8)ingest[temp+20]); - } - OS_printf("\n"); - #endif - - // Security Trailer - #ifdef TC_DEBUG - OS_printf("MAC: \n\t"); - #endif - for (y = x; y < (x + sa_ptr->stmacf_len); y++) - { - tc_sdls_processed_frame->tc_sec_trailer.mac[y-x] = (uint8)ingest[y]; - #ifdef TC_DEBUG - OS_printf("%02x", tc_sdls_processed_frame->tc_sec_trailer.mac[y-x]); - #endif - } - #ifdef TC_DEBUG - OS_printf("\n"); - #endif - x = x + sa_ptr->stmacf_len; - - // FECF - tc_sdls_processed_frame->tc_sec_trailer.fecf = ((uint8)ingest[x] << 8) | ((uint8)ingest[x+1]); - Crypto_FECF(tc_sdls_processed_frame->tc_sec_trailer.fecf, ingest, (tc_sdls_processed_frame->tc_header.fl-1),tc_sdls_processed_frame); - - gcry_error = gcry_cipher_open( - &(tmp_hd), - GCRY_CIPHER_AES256, - GCRY_CIPHER_MODE_GCM, - GCRY_CIPHER_CBC_MAC - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } - #ifdef DEBUG - OS_printf("Key ID = %d, 0x", sa_ptr->ekid); - for(int y = 0; y < KEY_SIZE; y++) - { - OS_printf("%02x", ek_ring[sa_ptr->ekid].value[y]); - } - OS_printf("\n"); - #endif - gcry_error = gcry_cipher_setkey( - tmp_hd, - ek_ring[sa_ptr->ekid].value, - KEY_SIZE - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } - gcry_error = gcry_cipher_setiv( - tmp_hd, - sa_ptr->iv, - sa_ptr->iv_len - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; + status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; return status; } - #ifdef MAC_DEBUG - OS_printf("AAD = 0x"); - #endif - // Prepare additional authenticated data (AAD) - for (y = 0; y < sa_ptr->abm_len; y++) - { - ingest[y] = (uint8) ((uint8)ingest[y] & (uint8)sa_ptr->abm[y]); - #ifdef DEBUG - OS_printf("%02x", (uint8) ingest[y]); - #endif - } - #ifdef DEBUG - OS_printf("\n"); - #endif - - gcry_error = gcry_cipher_authenticate( - tmp_hd, - ingest, // additional authenticated data - sa_ptr->abm_len // length of AAD - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_authenticate error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - OS_printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error),gcry_strerror (gcry_error)); - status = OS_ERROR; - return status; - } - // printf("PDU LEN: %d\n", tc_sdls_processed_frame->tc_pdu_len); - // gcry_error = gcry_cipher_decrypt( - // tmp_hd, - // tc_sdls_processed_frame->tc_pdu, // plaintext output - // tc_sdls_processed_frame->tc_pdu_len, // length of data - // &(ingest[*len_ingest - tc_sdls_processed_frame->tc_pdu_len - sa_ptr->stmacf_len - 2]), // ciphertext input - // tc_sdls_processed_frame->tc_pdu_len // in data length + // TODO Better without copies / CMAC updates + // char *garbage_buff = malloc(tc_mac_start_index * sizeof(uint8)); + // gcry_error = gcry_cipher_encrypt( + // tmp_hd, // plaintext output + // garbage_buff, // plaintext garbage out + // tc_mac_start_index, // length of data + // aad, // ciphertext input + // tc_mac_start_index // in data length // ); // if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) // { - // OS_printf(KRED "ERROR: gcry_cipher_decrypt error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - // status = OS_ERROR; + // OS_printf(KRED "ERROR: gcry_cipher_authentication error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + // OS_printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error),gcry_strerror (gcry_error)); + // status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; // return status; // } - char *garbage_buff = malloc(tc_sdls_processed_frame->tc_pdu_len * sizeof(unsigned char)); - - gcry_error = gcry_cipher_encrypt( - tmp_hd, - // tc_sdls_processed_frame->tc_pdu, // plaintext output - garbage_buff, - tc_sdls_processed_frame->tc_pdu_len, // length of data - &(ingest[*len_ingest - tc_sdls_processed_frame->tc_pdu_len - sa_ptr->stmacf_len - 2]), // ciphertext input - tc_sdls_processed_frame->tc_pdu_len // in data length - ); - free(garbage_buff); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_encrypt error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; - return status; - } + // free(garbage_buff); + + // + // gcry_error = gcry_cipher_checktag( + // tmp_hd, + // tc_sdls_processed_frame->tc_sec_trailer.mac, // tag input + // sa_ptr->stmacf_len // tag size + // ); + uint8* calculated_mac = malloc(sa_ptr->stmacf_len * sizeof(uint8)); gcry_error = gcry_cipher_gettag( tmp_hd, - &(tc_sdls_processed_frame->tc_sec_trailer.mac[0]), // tag output - sa_ptr->stmacf_len // tag size - ); + calculated_mac, // tag output + sa_ptr->stmacf_len // tag size + ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { - OS_printf(KRED "ERROR: gcry_cipher_gettag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + OS_printf(KRED "ERROR: gcry_cipher_checktag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; + return status; } - #ifdef MAC_DEBUG - OS_printf("TC_Process Expected MAC = 0x"); - for (int z = 0; z < sa_ptr->stmacf_len ; z++) - { - OS_printf("%02x", tc_sdls_processed_frame->tc_sec_trailer.mac[z]); - } - OS_printf("\n"); - OS_printf("TC_Process Actual MAC = 0x"); - for (int z = 0; z < sa_ptr->stmacf_len ; z++) + for(int i=0; istmacf_len; i++) { - OS_printf("%02x",tc_sdls_processed_frame->tc_sec_trailer.mac[z]); - } - OS_printf("\n"); - #endif - - #ifdef DEBUG - OS_printf("Using PDU length of: %d\n", tc_sdls_processed_frame->tc_pdu_len); - OS_printf("Printing entire frame sans header in memory:\n\t0x"); - // for(int i=0; itc_header); i++) - // { - // OS_printf("%02x", tc_sdls_processed_frame->tc_header[i]); - // } - OS_printf("%02x", tc_sdls_processed_frame->tc_sec_header.sh); - OS_printf("%04x", tc_sdls_processed_frame->tc_sec_header.spi); - for(int i=0; iiv_len; i++) + if (calculated_mac[i] != tc_sdls_processed_frame->tc_sec_trailer.mac[i]) { - OS_printf("%02x", tc_sdls_processed_frame->tc_sec_header.iv[i]); - } - for(int i=0; itc_pdu_len; i++) - { - OS_printf("%02x", tc_sdls_processed_frame->tc_pdu[i]); - } - for(int i=0; istmacf_len; i++) - { - OS_printf("%02x", tc_sdls_processed_frame->tc_sec_trailer.mac[i]); - } - OS_printf("%04x\n", tc_sdls_processed_frame->tc_sec_trailer.fecf); - #endif - - status = OS_ERROR; - report.bmacf = 1; - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - return status; - gcry_cipher_close(tmp_hd); - - // Increment the IV for next time - #ifdef INCREMENT - Crypto_increment(sa_ptr->iv, sa_ptr->shivf_len); - #endif - - } - else - { // Clear - #ifdef DEBUG - OS_printf(KBLU "CLEAR TC Received!\n" RESET); - #endif - - for (y = 10; y <= (tc_sdls_processed_frame->tc_header.fl -1); y++) //tfhdr+seghdr+sechdr=5+1+6=12 - { - tc_sdls_processed_frame->tc_pdu[y - 10] = (uint8)ingest[y]; - } - // FECF - tc_sdls_processed_frame->tc_sec_trailer.fecf = ((uint8)ingest[y] << 8) | ((uint8)ingest[y+1]); - Crypto_FECF((int) tc_sdls_processed_frame->tc_sec_trailer.fecf, ingest, (tc_sdls_processed_frame->tc_header.fl),tc_sdls_processed_frame); - } - - #ifdef TC_DEBUG - Crypto_tcPrint(tc_sdls_processed_frame); - #endif - - // // Zero ingest - // for (x = 0; x < *len_ingest; x++) - // { - // ingest[x] = 0; - // } - - - if(crypto_config->process_sdls_pdus==TC_PROCESS_SDLS_PDUS_FALSE) //If we don't want to process frame data for SDLS PDUs, only reverse security & return content. - { - // CCSDS Pass-through - #ifdef DEBUG - OS_printf(KGRN "CCSDS Pass-through (No Extended Procedure PDU Processing) \n" RESET); - #endif - if (crypto_config->has_pus_hdr==TC_HAS_PUS_HDR) { - for (x = 0; x < (tc_sdls_processed_frame->tc_header.fl - 11); x++) { //TODO - Need to account for security header! - ingest[x] = tc_sdls_processed_frame->tc_pdu[x]; - #ifdef CCSDS_DEBUG - OS_printf("tc_sdls_processed_frame->tc_pdu[%d] = 0x%02x\n", x, tc_sdls_processed_frame->tc_pdu[x]); - #endif - *len_ingest = x; - } - } else { - for (x = 0; x < (tc_sdls_processed_frame->tc_header.fl); x++) { //with no PUS header, entire PDU is data - ingest[x] = tc_sdls_processed_frame->tc_pdu[x]; - #ifdef CCSDS_DEBUG - OS_printf("tc_sdls_processed_frame->tc_pdu[%d] = 0x%02x\n", x, tc_sdls_processed_frame->tc_pdu[x]); - #endif - *len_ingest = x; + status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; + OS_printf(KRED "ERROR: MAC Validation Error.\n" RESET); + return status; } } } - else //Process SDLS PDU - { - if (crypto_config->has_pus_hdr==TC_HAS_PUS_HDR) - { - if ((tc_sdls_processed_frame->tc_pdu[0] == 0x18) && (tc_sdls_processed_frame->tc_pdu[1] == 0x80)) - // Crypto Lib Application ID - { - #ifdef DEBUG - OS_printf(KGRN "Received SDLS command: " RESET); - #endif - // CCSDS Header - sdls_frame.hdr.pvn = (tc_sdls_processed_frame->tc_pdu[0] & 0xE0) >> 5; - sdls_frame.hdr.type = (tc_sdls_processed_frame->tc_pdu[0] & 0x10) >> 4; - sdls_frame.hdr.shdr = (tc_sdls_processed_frame->tc_pdu[0] & 0x08) >> 3; - sdls_frame.hdr.appID = - ((tc_sdls_processed_frame->tc_pdu[0] & 0x07) << 8) | tc_sdls_processed_frame->tc_pdu[1]; - sdls_frame.hdr.seq = (tc_sdls_processed_frame->tc_pdu[2] & 0xC0) >> 6; - sdls_frame.hdr.pktid = - ((tc_sdls_processed_frame->tc_pdu[2] & 0x3F) << 8) | tc_sdls_processed_frame->tc_pdu[3]; - sdls_frame.hdr.pkt_length = (tc_sdls_processed_frame->tc_pdu[4] << 8) | tc_sdls_processed_frame->tc_pdu[5]; - - // CCSDS PUS - sdls_frame.pus.shf = (tc_sdls_processed_frame->tc_pdu[6] & 0x80) >> 7; - sdls_frame.pus.pusv = (tc_sdls_processed_frame->tc_pdu[6] & 0x70) >> 4; - sdls_frame.pus.ack = (tc_sdls_processed_frame->tc_pdu[6] & 0x0F); - sdls_frame.pus.st = tc_sdls_processed_frame->tc_pdu[7]; - sdls_frame.pus.sst = tc_sdls_processed_frame->tc_pdu[8]; - sdls_frame.pus.sid = (tc_sdls_processed_frame->tc_pdu[9] & 0xF0) >> 4; - sdls_frame.pus.spare = (tc_sdls_processed_frame->tc_pdu[9] & 0x0F); - - // SDLS TLV PDU - sdls_frame.pdu.type = (tc_sdls_processed_frame->tc_pdu[10] & 0x80) >> 7; - sdls_frame.pdu.uf = (tc_sdls_processed_frame->tc_pdu[10] & 0x40) >> 6; - sdls_frame.pdu.sg = (tc_sdls_processed_frame->tc_pdu[10] & 0x30) >> 4; - sdls_frame.pdu.pid = (tc_sdls_processed_frame->tc_pdu[10] & 0x0F); - sdls_frame.pdu.pdu_len = (tc_sdls_processed_frame->tc_pdu[11] << 8) | tc_sdls_processed_frame->tc_pdu[12]; - for (x = 13; x < (13 + sdls_frame.hdr.pkt_length); x++) { - sdls_frame.pdu.data[x - 13] = tc_sdls_processed_frame->tc_pdu[x]; - } - - #ifdef CCSDS_DEBUG - Crypto_ccsdsPrint(&sdls_frame); - #endif - - // Determine type of PDU - *len_ingest = Crypto_PDU(ingest, tc_sdls_processed_frame); - } - } - else if (tc_sdls_processed_frame->tc_header.vcid == TC_SDLS_EP_VCID) //TC SDLS PDU with no packet layer - { - #ifdef DEBUG - OS_printf(KGRN "Received SDLS command: " RESET); - #endif - // No Packet HDR or PUS in these frames - // SDLS TLV PDU - sdls_frame.pdu.type = (tc_sdls_processed_frame->tc_pdu[0] & 0x80) >> 7; - sdls_frame.pdu.uf = (tc_sdls_processed_frame->tc_pdu[0] & 0x40) >> 6; - sdls_frame.pdu.sg = (tc_sdls_processed_frame->tc_pdu[0] & 0x30) >> 4; - sdls_frame.pdu.pid = (tc_sdls_processed_frame->tc_pdu[0] & 0x0F); - sdls_frame.pdu.pdu_len = (tc_sdls_processed_frame->tc_pdu[1] << 8) | tc_sdls_processed_frame->tc_pdu[2]; - for (x = 3; x < (3 + tc_sdls_processed_frame->tc_header.fl); x++) { - //Todo - Consider how this behaves with large OTAR PDUs that are larger than 1 TC in size. Most likely fails. Must consider Uplink Sessions (sequence numbers). - sdls_frame.pdu.data[x - 3] = tc_sdls_processed_frame->tc_pdu[x]; - } - - #ifdef CCSDS_DEBUG - Crypto_ccsdsPrint(&sdls_frame); - #endif - - // Determine type of PDU - *len_ingest = Crypto_PDU(ingest, tc_sdls_processed_frame); - } - else { - //TODO - Process SDLS PDU with Packet Layer without PUS_HDR - } - }//End Process SDLS PDU - // Zero ingest - memset(ingest, 0, *len_ingest); - // for (x = 0; x < *len_ingest; x++) - // { - // ingest[x] = 0; - // } + // Decrypt, if applicable - #ifdef OCF_DEBUG - Crypto_fsrPrint(&report); - #endif - - #ifdef DEBUG - OS_printf(KYEL "----- Crypto_TC_ProcessSecurity END -----\n" RESET); - #endif + // Extended PDU processing, if applicable return status; } From d34c5a919508ea802e28d1910dcf2ae78c653428 Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Tue, 14 Dec 2021 15:34:19 -0800 Subject: [PATCH 03/18] WIP - IV refactor to pointer instead of hardcoded IV_SIZE memory blocks --- fsw/public_inc/crypto_error.h | 1 + fsw/public_inc/crypto_structs.h | 3 +- fsw/src/crypto.c | 55 +++++++++--------- fsw/src/crypto_print.c | 21 +++---- fsw/src/sadb_routine_inmemory.template.c | 56 ++++++++++--------- fsw/src_mysql/sadb_routine_mariadb.template.c | 2 +- 6 files changed, 70 insertions(+), 68 deletions(-) diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index 3abd60a6..4bc7b0b1 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -42,5 +42,6 @@ #define CRYPTO_LIB_ERR_BAD_ANTIREPLAY_WINDOW (-13) #define CRYPTO_LIB_ERR_LIBGCRYPT_ERROR (-14) #define CRYPTO_LIB_ERR_AUTHENTICATION_ERROR (-15) +#define CRYPTO_LIB_ERR_NULL_IV (-16) #endif //_crypto_error_h_ diff --git a/fsw/public_inc/crypto_structs.h b/fsw/public_inc/crypto_structs.h index 0a554349..1a1ef2e6 100644 --- a/fsw/public_inc/crypto_structs.h +++ b/fsw/public_inc/crypto_structs.h @@ -68,8 +68,7 @@ typedef struct uint8 stmacf_len:8; // Sec. Trailer MAC Field Length uint8 ecs_len :8; // Encryption Cipher Suite Length uint8 ecs[ECS_SIZE]; // Encryption Cipher Suite (algorithm / mode ID) - uint8 iv_len :8; // Initialization Vector Length - uint8 iv[IV_SIZE]; // Initialization Vector + uint8* iv; // Initialization Vector uint8 acs_len :8; // Authentication Cipher Suite Length uint8 acs :8; // Authentication Cipher Suite (algorithm / mode ID) uint16 abm_len :16; // Authentication Bit Mask Length diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index 0e8884a2..89e2295f 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -754,7 +754,7 @@ static void Crypto_Local_Init(void) tm_frame.tm_sec_header.spi = 0x0000; for ( int x = 0; x < IV_SIZE; x++) { // Initialization Vector - tm_frame.tm_sec_header.iv[x] = 0x00; + *(tm_frame.tm_sec_header.iv + x) = 0x00; } // TM Payload Data Unit for ( int x = 0; x < TM_FRAME_DATA_SIZE; x++) @@ -1590,7 +1590,7 @@ static int32 Crypto_Key_verify(char* ingest,TC_t* tc_frame) iv_loc = count; for (int y = 0; y < IV_SIZE; y++) { - ingest[count++] = tc_frame->tc_sec_header.iv[y]; + ingest[count++] = *(tc_frame->tc_sec_header.iv)+y; } ingest[count-1] = ingest[count-1] + x + 1; @@ -1810,21 +1810,21 @@ static int32 Crypto_SA_readARSN(char* ingest) } - if (sa_ptr->iv_len > 0) + if (sa_ptr->shivf_len > 0) { // Set IV - authenticated encryption - for (int x = 0; x < sa_ptr->iv_len - 1; x++) + for (int x = 0; x < sa_ptr->shivf_len - 1; x++) { - ingest[count++] = sa_ptr->iv[x]; + ingest[count++] = *(sa_ptr->iv + x); } // TODO: Do we need this? - if (sa_ptr->iv[IV_SIZE - 1] > 0) + if (*(sa_ptr->iv + sa_ptr->shivf_len - 1) > 0) { // Adjust to report last received, not expected - ingest[count++] = sa_ptr->iv[IV_SIZE - 1] - 1; + ingest[count++] = *(sa_ptr->iv +sa_ptr->shivf_len - 1) - 1; } else { - ingest[count++] = sa_ptr->iv[IV_SIZE - 1]; + ingest[count++] = *(sa_ptr->iv + sa_ptr->shivf_len - 1); } } else @@ -1834,12 +1834,12 @@ static int32 Crypto_SA_readARSN(char* ingest) #ifdef PDU_DEBUG OS_printf("spi = %d \n", spi); - if (sa_ptr->iv_len > 0) + if (sa_ptr->shivf_len > 0) { OS_printf("ARSN = 0x"); - for (int x = 0; x < sa_ptr->iv_len; x++) + for (int x = 0; x < sa_ptr->shivf_len; x++) { - OS_printf("%02x", sa_ptr->iv[x]); + OS_printf("%02x", *(sa_ptr->iv + x)); } OS_printf("\n"); } @@ -2528,7 +2528,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len { #ifdef SA_DEBUG OS_printf(KYEL "Using IV value:\n\t"); - for(int i=0; ishivf_len; i++) {OS_printf("%02x", sa_ptr->iv[i]);} + for(int i=0; ishivf_len; i++) {OS_printf("%02x", *(sa_ptr->iv + i));} OS_printf("\n" RESET); #endif @@ -2536,7 +2536,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len { // TODO: Likely API call // Copy in IV from SA - *(p_new_enc_frame + index) = sa_ptr->iv[i]; + *(p_new_enc_frame + index) = *(sa_ptr->iv + i); index++; } } @@ -2652,8 +2652,8 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len } gcry_error = gcry_cipher_setiv( tmp_hd, - &(sa_ptr->iv[0]), - sa_ptr->iv_len + sa_ptr->iv, + sa_ptr->shivf_len ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { @@ -2767,10 +2767,11 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len } #ifdef INCREMENT + if(sa_ptr->iv == NULL) { printf("\n\nNULL\n\n");} Crypto_increment(sa_ptr->iv, sa_ptr->shivf_len); #ifdef SA_DEBUG OS_printf(KYEL "Next IV value is:\n\t"); - for(int i=0; ishivf_len; i++) {OS_printf("%02x", sa_ptr->iv[i]);} + for(int i=0; ishivf_len; i++) {OS_printf("%02x", *(sa_ptr->iv + i));} OS_printf("\n" RESET); #endif #endif @@ -2962,7 +2963,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro status = sadb_routine->sadb_get_sa_from_spi(tc_sdls_processed_frame->tc_sec_header.spi, &sa_ptr); if(status != CRYPTO_LIB_SUCCESS){ return status; } // Parse IV - memcpy((tc_sdls_processed_frame->tc_sec_header.iv)+(IV_SIZE-sa_ptr->shivf_len), &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN]), sa_ptr->shivf_len); + memcpy((tc_sdls_processed_frame->tc_sec_header.iv), &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN]), sa_ptr->shivf_len); // Parse Sequence Number memcpy((tc_sdls_processed_frame->tc_sec_header.sn)+(TC_SN_SIZE-sa_ptr->shsnf_len), &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len]), sa_ptr->shsnf_len); // Parse pad length @@ -2989,18 +2990,18 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro else { // Check IV is in ARCW - status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv+(IV_SIZE-sa_ptr->shivf_len), sa_ptr->iv, sa_ptr->shivf_len, + status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, sa_ptr->arcw[sa_ptr->arcw_len-1]); printf("Received IV is\n\t"); - for(int i=IV_SIZE-sa_ptr->shivf_len; ishivf_len+(IV_SIZE-sa_ptr->shivf_len); i++) + for(int i=0; ishivf_len; i++) // for(int i=0; itc_sec_header.iv[i]); + printf("%02x", *(tc_sdls_processed_frame->tc_sec_header.iv + i)); } printf("\nSA IV is\n\t"); for(int i=0; ishivf_len; i++) { - printf("%02x", sa_ptr->iv[i]); + printf("%02x", *(sa_ptr->iv + i)); } printf("\nARCW is: %02x\n", sa_ptr->arcw[0]); if (status != CRYPTO_LIB_SUCCESS) { return status; } @@ -3199,7 +3200,7 @@ int32 Crypto_TM_ApplySecurity( char* ingest, int* len_ingest) } if (badIV == 1) { - sa_ptr->iv[IV_SIZE-1]++; + *(sa_ptr->iv + sa_ptr->shivf_len -1) = *(sa_ptr->iv + sa_ptr->shivf_len -1) + 1; } if (badMAC == 1) { @@ -3218,7 +3219,7 @@ int32 Crypto_TM_ApplySecurity( char* ingest, int* len_ingest) // Security Header tempTM[count++] = (uint8) ((spi & 0xFF00) >> 8); tempTM[count++] = (uint8) ((spi & 0x00FF)); - CFE_PSP_MemCpy(tm_frame.tm_sec_header.iv, sa_ptr->iv, IV_SIZE); + CFE_PSP_MemCpy(tm_frame.tm_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len); // Padding Length pad_len = Crypto_Get_tmLength(*len_ingest) - TM_MIN_SIZE + IV_SIZE + TM_PAD_SIZE - *len_ingest; @@ -3228,12 +3229,12 @@ int32 Crypto_TM_ApplySecurity( char* ingest, int* len_ingest) (sa_ptr->ast == 1)) { // Initialization Vector #ifdef INCREMENT - Crypto_increment(sa_ptr->iv, IV_SIZE); + Crypto_increment(sa_ptr->iv, sa_ptr->shivf_len); #endif if ((sa_ptr->est == 1) || (sa_ptr->ast == 1)) { for (x = 0; x < IV_SIZE; x++) { - tempTM[count++] = sa_ptr->iv[x]; + tempTM[count++] = *(sa_ptr->iv + x); } } pdu_loc = count; @@ -3332,8 +3333,8 @@ int32 Crypto_TM_ApplySecurity( char* ingest, int* len_ingest) } gcry_error = gcry_cipher_setiv( tmp_hd, - &(sa_ptr->iv[0]), - sa_ptr->iv_len + sa_ptr->iv, + sa_ptr->shivf_len ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { diff --git a/fsw/src/crypto_print.c b/fsw/src/crypto_print.c index 0bf6f0af..43fe97c0 100644 --- a/fsw/src/crypto_print.c +++ b/fsw/src/crypto_print.c @@ -179,19 +179,14 @@ void Crypto_saPrint(SecurityAssociation_t* sa) OS_printf("\t ecs[%d] = 0x%02x \n", ECS_SIZE-3, sa->ecs[ECS_SIZE - 3]); OS_printf("\t ecs[%d] = 0x%02x \n", ECS_SIZE-2, sa->ecs[ECS_SIZE - 2]); OS_printf("\t ecs[%d] = 0x%02x \n", ECS_SIZE-1, sa->ecs[ECS_SIZE - 1]); - OS_printf("\t iv_len = 0x%02x \n", sa->iv_len); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-12, sa->iv[IV_SIZE - 12]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-11, sa->iv[IV_SIZE - 11]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-10, sa->iv[IV_SIZE - 10]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-9, sa->iv[IV_SIZE - 9]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-8, sa->iv[IV_SIZE - 8]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-7, sa->iv[IV_SIZE - 7]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-6, sa->iv[IV_SIZE - 6]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-5, sa->iv[IV_SIZE - 5]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-4, sa->iv[IV_SIZE - 4]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-3, sa->iv[IV_SIZE - 3]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-2, sa->iv[IV_SIZE - 2]); - OS_printf("\t iv[%d] = 0x%02x \n", IV_SIZE-1, sa->iv[IV_SIZE - 1]); + OS_printf("\t iv_len = 0x%02x \n", sa->shivf_len); + if(sa->iv != NULL) + { + for(int i = 0; i < sa->shivf_len; i++) + { + OS_printf("\t iv[%d] = 0x%02x \n", i, *(sa->iv + i)); + } + } OS_printf("\t acs_len = 0x%02x \n", sa->acs_len); OS_printf("\t acs = 0x%02x \n", sa->acs); OS_printf("\t abm_len = 0x%04x \n", sa->abm_len); diff --git a/fsw/src/sadb_routine_inmemory.template.c b/fsw/src/sadb_routine_inmemory.template.c index bea0fc91..b9235bf5 100644 --- a/fsw/src/sadb_routine_inmemory.template.c +++ b/fsw/src/sadb_routine_inmemory.template.c @@ -74,6 +74,8 @@ static int32 sadb_config(void) sa[1].sa_state = SA_OPERATIONAL; sa[1].est = 0; sa[1].ast = 0; + sa[1].shivf_len = 12; + sa[1].iv = (uint8*) calloc(1, sa[1].shivf_len * sizeof(uint8)); sa[1].arc_len = 1; sa[1].arcw_len = 1; sa[1].arcw[0] = 5; @@ -88,8 +90,8 @@ static int32 sadb_config(void) sa[2].est = 1; sa[2].ast = 1; sa[2].shivf_len = 12; - sa[2].iv_len = IV_SIZE; - sa[2].iv[IV_SIZE-1] = 0; + sa[2].iv = (uint8*) calloc(1, sa[2].shivf_len * sizeof(uint8)); + *(sa[2].iv + sa[2].shivf_len - 1) = 0; sa[2].abm_len = 0x14; // 20 for (int i = 0; i < sa[2].abm_len; i++) { // Zero AAD bit mask @@ -105,8 +107,8 @@ static int32 sadb_config(void) sa[3].est = 1; sa[3].ast = 1; sa[3].shivf_len = 12; - sa[3].iv_len = IV_SIZE; - sa[3].iv[IV_SIZE-1] = 0; + sa[3].iv = (uint8*) calloc(1, sa[3].shivf_len * sizeof(uint8)); + *(sa[3].iv + sa[3].shivf_len - 1) = 0; sa[3].abm_len = 0x14; // 20 for (int i = 0; i < sa[3].abm_len; i++) { // Zero AAD bit mask @@ -124,8 +126,8 @@ static int32 sadb_config(void) sa[4].ast = 1; sa[4].shivf_len = 12; sa[4].stmacf_len = 16; - sa[4].iv_len = 12; - sa[4].iv[11] = 0; + sa[4].iv = (uint8*) calloc(1, sa[4].shivf_len * sizeof(uint8)); + *(sa[4].iv + 11) = 0; sa[4].abm_len = 0x14; // 20 for (int i = 0; i < sa[4].abm_len; i++) { // Zero AAD bit mask @@ -146,8 +148,8 @@ static int32 sadb_config(void) sa[5].est = 1; sa[5].ast = 1; sa[5].shivf_len = 12; - sa[5].iv_len = IV_SIZE; - sa[5].iv[IV_SIZE-1] = 0; + sa[5].iv = (uint8*) calloc(1, sa[5].shivf_len * sizeof(uint8)); + *(sa[5].iv + sa[5].shivf_len - 1) = 0; sa[5].abm_len = 0x14; // 20 for (int i = 0; i < sa[5].abm_len; i++) { // Zero AAD bit mask @@ -162,8 +164,8 @@ static int32 sadb_config(void) sa[6].est = 1; sa[6].ast = 1; sa[6].shivf_len = 12; - sa[6].iv_len = IV_SIZE; - sa[6].iv[IV_SIZE-1] = 0; + sa[6].iv = (uint8*) calloc(1, sa[6].shivf_len * sizeof(uint8)); + *(sa[6].iv + sa[6].shivf_len - 1) = 0; sa[6].abm_len = 0x14; // 20 for (int i = 0; i < sa[6].abm_len; i++) { // Zero AAD bit mask @@ -181,8 +183,8 @@ static int32 sadb_config(void) sa[7].est = 1; sa[7].ast = 1; sa[7].shivf_len = 12; - sa[7].iv_len = IV_SIZE; - sa[7].iv[IV_SIZE-1] = 0; + sa[7].iv = (uint8*) calloc(1, sa[7].shivf_len * sizeof(uint8)); + *(sa[7].iv + sa[7].shivf_len - 1) = 0; sa[7].abm_len = 0x14; // 20 for (int i = 0; i < sa[7].abm_len; i++) { // Zero AAD bit mask @@ -216,8 +218,8 @@ static int32 sadb_config(void) sa[9].est = 1; sa[9].ast = 0; sa[9].shivf_len = 12; - sa[9].iv_len = 12; - sa[9].iv[12] = 0; + sa[9].iv = (uint8*) calloc(1, sa[9].shivf_len * sizeof(uint8)); + *(sa[9].iv + 11) = 0; sa[9].abm_len = 0x14; // 20 for (int i = 0; i < sa[9].abm_len; i++) { // Zero AAD bit mask @@ -248,7 +250,8 @@ static int32 sadb_init(void) sa[x].ecs[1] = 0; sa[x].ecs[2] = 0; sa[x].ecs[3] = 0; - sa[x].iv_len = IV_SIZE; + sa[x].shivf_len = IV_SIZE; + sa[x].iv = NULL; sa[x].acs_len = 0; sa[x].acs = 0; sa[x].arc_len = 0; @@ -276,6 +279,7 @@ int32 expose_sadb_get_sa_from_spi(uint16 spi, SecurityAssociation_t** security_a static int32 sadb_get_sa_from_spi(uint16 spi,SecurityAssociation_t** security_association) { int32 status = OS_SUCCESS; + if(sa == NULL) { return CRYPTO_LIB_ERR_NO_INIT; } *security_association = &sa[spi]; #ifdef SA_DEBUG OS_printf(KYEL "DEBUG - Printing local copy of SA Entry for current SPI.\n" RESET); @@ -287,6 +291,7 @@ static int32 sadb_get_sa_from_spi(uint16 spi,SecurityAssociation_t** security_as static int32 sadb_get_operational_sa_from_gvcid(uint8 tfvn,uint16 scid,uint16 vcid,uint8 mapid,SecurityAssociation_t** security_association) { int32 status = CRYPTO_LIB_ERROR; + if(sa == NULL) { return CRYPTO_LIB_ERR_NO_INIT; } for (int i=0; i<10; i++) { @@ -294,6 +299,7 @@ static int32 sadb_get_operational_sa_from_gvcid(uint8 tfvn,uint16 scid,uint16 vc (crypto_config->unique_sa_per_mapid==TC_UNIQUE_SA_PER_MAP_ID_FALSE || sa[i].gvcid_tc_blk.mapid == mapid)) //only require MapID match is unique SA per MapID set (only relevant when using segmentation hdrs) { *security_association = &sa[i]; + if(sa[i].iv == NULL) { return CRYPTO_LIB_ERR_NULL_IV; } #ifdef SA_DEBUG OS_printf("Valid operational SA found at index %d.\n", i); @@ -577,13 +583,13 @@ static int32 sadb_sa_rekey(void) #ifdef PDU_DEBUG OS_printf("SPI %d IV updated to: 0x", spi); #endif - if (sa[spi].iv_len > 0) + if (sa[spi].shivf_len > 0) { // Set IV - authenticated encryption - for (x = count; x < (sa[spi].iv_len + count); x++) + for (x = count; x < (sa[spi].shivf_len + count); x++) { // TODO: Uncomment once fixed in ESA implementation // TODO: Assuming this was fixed... - sa[spi].iv[x - count] = (uint8) sdls_frame.pdu.data[x]; + *(sa[spi].iv + x - count) = (uint8) sdls_frame.pdu.data[x]; #ifdef PDU_DEBUG OS_printf("%02x", sdls_frame.pdu.data[x]); #endif @@ -682,10 +688,10 @@ static int32 sadb_sa_create(void) { sa[spi].ecs[x] = ((uint8)sdls_frame.pdu.data[count++]); } - sa[spi].iv_len = ((uint8)sdls_frame.pdu.data[count++]); - for (int x = 0; x < sa[spi].iv_len; x++) + sa[spi].shivf_len = ((uint8)sdls_frame.pdu.data[count++]); + for (int x = 0; x < sa[spi].shivf_len; x++) { - sa[spi].iv[x] = ((uint8)sdls_frame.pdu.data[count++]); + *(sa[spi].iv + x) = ((uint8)sdls_frame.pdu.data[count++]); } sa[spi].acs_len = ((uint8)sdls_frame.pdu.data[count++]); for (int x = 0; x < sa[spi].acs_len; x++) @@ -776,16 +782,16 @@ static int32 sadb_sa_setARSN(void) #ifdef PDU_DEBUG OS_printf("SPI %d IV updated to: 0x", spi); #endif - if (sa[spi].iv_len > 0) + if (sa[spi].shivf_len > 0) { // Set IV - authenticated encryption for (int x = 0; x < IV_SIZE; x++) { - sa[spi].iv[x] = (uint8) sdls_frame.pdu.data[x + 2]; + *(sa[spi].iv + x) = (uint8) sdls_frame.pdu.data[x + 2]; #ifdef PDU_DEBUG - OS_printf("%02x", sa[spi].iv[x]); + OS_printf("%02x", *(sa[spi].iv + x)); #endif } - Crypto_increment((uint8*)sa[spi].iv, IV_SIZE); + Crypto_increment(sa[spi].iv, sa[spi].shivf_len); } else { // Set SN diff --git a/fsw/src_mysql/sadb_routine_mariadb.template.c b/fsw/src_mysql/sadb_routine_mariadb.template.c index cd75e25d..4b4cf60c 100644 --- a/fsw/src_mysql/sadb_routine_mariadb.template.c +++ b/fsw/src_mysql/sadb_routine_mariadb.template.c @@ -192,6 +192,7 @@ static int32 parse_sa_from_mysql_query(char* query, SecurityAssociation_t** secu char *field_names[num_fields]; //[64]; 64 == max length of column name in MySQL + //TODO -- Need to store mysql query hex string and then malloc sa->iv according to size. while((row = mysql_fetch_row(result))){ for(int i=0; i < num_fields; i++) { @@ -224,7 +225,6 @@ static int32 parse_sa_from_mysql_query(char* query, SecurityAssociation_t** secu if(strcmp(field_names[i],"stmacf_len")==0){sa->stmacf_len=atoi(row[i]);continue;} if(strcmp(field_names[i],"ecs_len")==0){sa->ecs_len=atoi(row[i]);continue;} if(strcmp(field_names[i],"HEX(ecs)")==0){convert_hexstring_to_byte_array(row[i],sa->ecs);continue;} - if(strcmp(field_names[i],"iv_len")==0){sa->iv_len=atoi(row[i]);continue;} //if(strcmp(field_names[i],"HEX(iv)")==0){memcpy(&(sa->iv),&row[i],IV_SIZE);continue;} if(strcmp(field_names[i],"HEX(iv)")==0){convert_hexstring_to_byte_array(row[i],sa->iv);continue;} if(strcmp(field_names[i],"acs_len")==0){sa->acs_len=atoi(row[i]);continue;} From 8dbd9655e188e562c3c4a5cf230a940c9367623f Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Tue, 14 Dec 2021 16:44:20 -0800 Subject: [PATCH 04/18] Replace hardcoded SA byte-blocks with uint8 pointers --- fsw/crypto_util/app/ut_tc_apply.c | 34 +++++- fsw/public_inc/crypto_error.h | 1 + fsw/public_inc/crypto_structs.h | 6 +- fsw/src/crypto.c | 33 +++--- fsw/src/crypto_print.c | 25 ++++- fsw/src/sadb_routine_inmemory.template.c | 102 ++++++++---------- fsw/src_mysql/sadb_routine_mariadb.template.c | 3 +- 7 files changed, 121 insertions(+), 83 deletions(-) diff --git a/fsw/crypto_util/app/ut_tc_apply.c b/fsw/crypto_util/app/ut_tc_apply.c index 34beec5a..f73324dc 100644 --- a/fsw/crypto_util/app/ut_tc_apply.c +++ b/fsw/crypto_util/app/ut_tc_apply.c @@ -22,6 +22,8 @@ #include "utest.h" #include "crypto.h" #include "crypto_error.h" +#include "sadb_routine.h" + // TODO: Should this be set up with a set of tests, or continue to Crypto_Init() each time. For now I think the current setup is the best path. @@ -53,7 +55,29 @@ UTEST(TC_APPLY_SECURITY, NO_CRYPTO_INIT) } // Nominal Test. This should read a raw_tc_sdls_ping.dat file, continue down the "happy path", and return CRYPTO_LIB_SUCCESS -UTEST(TC_APPLY_SECURITY, HAPPY_PATH) +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_CLEAR) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + + hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + + int32 return_val = CRYPTO_LIB_ERROR; + + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) { //Setup & Initialize CryptoLib Crypto_Init_Unit_Test(); @@ -68,6 +92,13 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH) int32 return_val = CRYPTO_LIB_ERROR; + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); + expose_sadb_get_sa_from_spi(1,&test_association); + test_association->sa_state = SA_NONE; + expose_sadb_get_sa_from_spi(4,&test_association); + test_association->sa_state = SA_OPERATIONAL; + //test_association->ast=0; + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); Crypto_Shutdown(); free(raw_tc_sdls_ping_b); @@ -75,6 +106,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH) ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); } + // Bad Space Craft ID. This should pass the flawed .dat file, and return MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND UTEST(TC_APPLY_SECURITY, BAD_SPACE_CRAFT_ID) { diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index 4bc7b0b1..8c1ef16e 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -43,5 +43,6 @@ #define CRYPTO_LIB_ERR_LIBGCRYPT_ERROR (-14) #define CRYPTO_LIB_ERR_AUTHENTICATION_ERROR (-15) #define CRYPTO_LIB_ERR_NULL_IV (-16) +#define CRYPTO_LIB_ERR_NULL_ABM (-17) #endif //_crypto_error_h_ diff --git a/fsw/public_inc/crypto_structs.h b/fsw/public_inc/crypto_structs.h index 1a1ef2e6..d820ca8d 100644 --- a/fsw/public_inc/crypto_structs.h +++ b/fsw/public_inc/crypto_structs.h @@ -72,11 +72,11 @@ typedef struct uint8 acs_len :8; // Authentication Cipher Suite Length uint8 acs :8; // Authentication Cipher Suite (algorithm / mode ID) uint16 abm_len :16; // Authentication Bit Mask Length - uint8 abm[ABM_SIZE]; // Authentication Bit Mask (Primary Hdr. through Security Hdr.) + uint8* abm; // Authentication Bit Mask (Primary Hdr. through Security Hdr.) uint8 arc_len :8; // Anti-Replay Counter Length - uint8 arc[ARC_SIZE]; // Anti-Replay Counter + uint8* arc; // Anti-Replay Counter uint8 arcw_len:8; // Anti-Replay Counter Window Length - uint8 arcw[ARCW_SIZE]; // Anti-Replay Counter Window + uint16 arcw; // Anti-Replay Counter Window } SecurityAssociation_t; #define SA_SIZE (sizeof(SecurityAssociation_t)) diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index 89e2295f..37faca7d 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -2558,7 +2558,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len Crypto_increment(sa_ptr->arc, sa_ptr->shsnf_len); for (int i=0; i < sa_ptr->shsnf_len; i++) { - *(p_new_enc_frame + index) = sa_ptr->arc[i]; + *(p_new_enc_frame + index) = *(sa_ptr->arc + i); index++; } } @@ -2669,7 +2669,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len uint8 bit_masked_data[sa_ptr->abm_len]; for (int y = 0; y < sa_ptr->abm_len; y++) { - bit_masked_data[y] = p_new_enc_frame[y] & sa_ptr->abm[y]; + bit_masked_data[y] = p_new_enc_frame[y] & *(sa_ptr->abm + y); } #ifdef MAC_DEBUG OS_printf(KYEL "Preparing AAD:\n"); @@ -2766,15 +2766,18 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len } } - #ifdef INCREMENT - if(sa_ptr->iv == NULL) { printf("\n\nNULL\n\n");} - Crypto_increment(sa_ptr->iv, sa_ptr->shivf_len); - #ifdef SA_DEBUG - OS_printf(KYEL "Next IV value is:\n\t"); - for(int i=0; ishivf_len; i++) {OS_printf("%02x", *(sa_ptr->iv + i));} - OS_printf("\n" RESET); + if (sa_service_type != SA_PLAINTEXT) + { + #ifdef INCREMENT + if(sa_ptr->iv == NULL) { printf("\n\nNULL\n\n");} + Crypto_increment(sa_ptr->iv, sa_ptr->shivf_len); + #ifdef SA_DEBUG + OS_printf(KYEL "Next IV value is:\n\t"); + for(int i=0; ishivf_len; i++) {OS_printf("%02x", *(sa_ptr->iv + i));} + OS_printf("\n" RESET); + #endif #endif - #endif + } /* ** End Authentication / Encryption */ @@ -2983,7 +2986,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro { // Check Sequence Number is in ARCW status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.sn, sa_ptr->arc, sa_ptr->shsnf_len, - sa_ptr->arcw[sa_ptr->arcw_len-1]); + sa_ptr->arcw); if (status != CRYPTO_LIB_SUCCESS) { return status; } // TODO: Update SA ARC through SADB_Routine function call } @@ -2991,7 +2994,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro { // Check IV is in ARCW status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, - sa_ptr->arcw[sa_ptr->arcw_len-1]); + sa_ptr->arcw); printf("Received IV is\n\t"); for(int i=0; ishivf_len; i++) // for(int i=0; iiv + i)); } - printf("\nARCW is: %02x\n", sa_ptr->arcw[0]); + printf("\nARCW is: %d\n", sa_ptr->arcw); if (status != CRYPTO_LIB_SUCCESS) { return status; } // TODO: Update SA IV through SADB_Routine function call } @@ -3049,7 +3052,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro // Prepare additional authenticated data (AAD) for (y = 0; y < tc_mac_start_index; y++) { - aad[y] = (uint8) ((uint8)ingest[y] & (uint8)sa_ptr->abm[y]); + aad[y] = (uint8) ((uint8)ingest[y] & (uint8) *(sa_ptr->abm + y)); } gcry_error = gcry_cipher_authenticate( @@ -3299,7 +3302,7 @@ int32 Crypto_TM_ApplySecurity( char* ingest, int* len_ingest) // Prepare additional authenticated data for (y = 0; y < sa_ptr->abm_len; y++) { - aad[y] = ingest[y] & sa_ptr->abm[y]; + aad[y] = ingest[y] & *(sa_ptr->abm + y); #ifdef MAC_DEBUG OS_printf("%02x", aad[y]); #endif diff --git a/fsw/src/crypto_print.c b/fsw/src/crypto_print.c index 43fe97c0..81f9e243 100644 --- a/fsw/src/crypto_print.c +++ b/fsw/src/crypto_print.c @@ -190,13 +190,28 @@ void Crypto_saPrint(SecurityAssociation_t* sa) OS_printf("\t acs_len = 0x%02x \n", sa->acs_len); OS_printf("\t acs = 0x%02x \n", sa->acs); OS_printf("\t abm_len = 0x%04x \n", sa->abm_len); - OS_printf("\t abm[0] = 0x%02x \n", sa->abm[0]); - OS_printf("\t abm[1] = 0x%02x \n", sa->abm[1]); + if(sa->abm != NULL) + { + OS_printf("\t abm: \n"); + for(int i = 0; i < sa->abm_len; i++) + { + OS_printf("%02x", *(sa->abm + i)); + } + OS_printf("\n"); + } OS_printf("\t arc_len = 0x%02x \n", sa->arc_len); - OS_printf("\t arc[0] = 0x%02x \n", sa->arc[0]); - OS_printf("\t arc[1] = 0x%02x \n", sa->arc[1]); + if(sa->arc != NULL) + { + OS_printf("\t arc: \n"); + for(int i = 0; i < sa->arc_len; i++) + { + OS_printf("%02x", *(sa->arc + i)); + } + OS_printf("\n"); + } + OS_printf("\t arcw_len = 0x%02x \n", sa->arcw_len); - OS_printf("\t arcw[0] = 0x%02x \n", sa->arcw[0]); + OS_printf("\t arcw = 0x%d \n", sa->arcw); } //Hex Print: diff --git a/fsw/src/sadb_routine_inmemory.template.c b/fsw/src/sadb_routine_inmemory.template.c index b9235bf5..e9721562 100644 --- a/fsw/src/sadb_routine_inmemory.template.c +++ b/fsw/src/sadb_routine_inmemory.template.c @@ -74,11 +74,11 @@ static int32 sadb_config(void) sa[1].sa_state = SA_OPERATIONAL; sa[1].est = 0; sa[1].ast = 0; - sa[1].shivf_len = 12; - sa[1].iv = (uint8*) calloc(1, sa[1].shivf_len * sizeof(uint8)); + //sa[1].shivf_len = 12; + //sa[1].iv = (uint8*) calloc(1, sa[1].shivf_len * sizeof(uint8)); sa[1].arc_len = 1; sa[1].arcw_len = 1; - sa[1].arcw[0] = 5; + sa[1].arcw = 5; sa[1].gvcid_tc_blk.tfvn = 0; sa[1].gvcid_tc_blk.scid = SCID & 0x3FF; sa[1].gvcid_tc_blk.vcid = 0; @@ -92,14 +92,11 @@ static int32 sadb_config(void) sa[2].shivf_len = 12; sa[2].iv = (uint8*) calloc(1, sa[2].shivf_len * sizeof(uint8)); *(sa[2].iv + sa[2].shivf_len - 1) = 0; - sa[2].abm_len = 0x14; // 20 - for (int i = 0; i < sa[2].abm_len; i++) - { // Zero AAD bit mask - sa[2].abm[i] = 0x00; - } + sa[2].abm_len = ABM_SIZE; // 20 + sa[2].abm = (uint8*) calloc(1, sa[2].abm_len * sizeof(uint8)); sa[2].arcw_len = 1; - sa[2].arcw[0] = 5; - sa[2].arc_len = (sa[2].arcw[0] * 2) + 1; + sa[2].arcw = 5; + sa[2].arc_len = (sa[2].arcw * 2) + 1; // SA 3 - KEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 129 sa[3].spi = 3; sa[3].ekid = 129; @@ -109,14 +106,11 @@ static int32 sadb_config(void) sa[3].shivf_len = 12; sa[3].iv = (uint8*) calloc(1, sa[3].shivf_len * sizeof(uint8)); *(sa[3].iv + sa[3].shivf_len - 1) = 0; - sa[3].abm_len = 0x14; // 20 - for (int i = 0; i < sa[3].abm_len; i++) - { // Zero AAD bit mask - sa[3].abm[i] = 0x00; - } + sa[3].abm_len = ABM_SIZE; // 20 + sa[3].abm = (uint8*) calloc(1, sa[3].abm_len * sizeof(uint8)); sa[3].arcw_len = 1; - sa[3].arcw[0] = 5; - sa[3].arc_len = (sa[3].arcw[0] * 2) + 1; + sa[3].arcw = 5; + sa[3].arc_len = (sa[3].arcw * 2) + 1; // SA 4 - KEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 130 // SA 4 VC0/1 is now 4-VC0, 7-VC1 sa[4].spi = 4; @@ -128,14 +122,11 @@ static int32 sadb_config(void) sa[4].stmacf_len = 16; sa[4].iv = (uint8*) calloc(1, sa[4].shivf_len * sizeof(uint8)); *(sa[4].iv + 11) = 0; - sa[4].abm_len = 0x14; // 20 - for (int i = 0; i < sa[4].abm_len; i++) - { // Zero AAD bit mask - sa[4].abm[i] = 0x00; - } + sa[4].abm_len = ABM_SIZE; // 20 + sa[4].abm = (uint8*) calloc(1, sa[4].abm_len * sizeof(uint8)); sa[4].arcw_len = 1; - sa[4].arcw[0] = 5; - sa[4].arc_len = (sa[4].arcw[0] * 2) + 1; + sa[4].arcw = 5; + sa[4].arc_len = (sa[4].arcw * 2) + 1; sa[4].gvcid_tc_blk.tfvn = 0; sa[4].gvcid_tc_blk.scid = SCID & 0x3FF; sa[4].gvcid_tc_blk.vcid = 0; @@ -150,14 +141,11 @@ static int32 sadb_config(void) sa[5].shivf_len = 12; sa[5].iv = (uint8*) calloc(1, sa[5].shivf_len * sizeof(uint8)); *(sa[5].iv + sa[5].shivf_len - 1) = 0; - sa[5].abm_len = 0x14; // 20 - for (int i = 0; i < sa[5].abm_len; i++) - { // Zero AAD bit mask - sa[5].abm[i] = 0x00; - } + sa[5].abm_len = ABM_SIZE; // 20 + sa[5].abm = (uint8*) calloc(1, sa[5].abm_len * sizeof(uint8)); sa[5].arcw_len = 1; - sa[5].arcw[0] = 5; - sa[5].arc_len = (sa[5].arcw[0] * 2) + 1; + sa[5].arcw = 5; + sa[5].arc_len = (sa[5].arcw * 2) + 1; // SA 6 - UNKEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: - sa[6].spi = 6; sa[6].sa_state = SA_UNKEYED; @@ -166,14 +154,11 @@ static int32 sadb_config(void) sa[6].shivf_len = 12; sa[6].iv = (uint8*) calloc(1, sa[6].shivf_len * sizeof(uint8)); *(sa[6].iv + sa[6].shivf_len - 1) = 0; - sa[6].abm_len = 0x14; // 20 - for (int i = 0; i < sa[6].abm_len; i++) - { // Zero AAD bit mask - sa[6].abm[i] = 0x00; - } + sa[6].abm_len = ABM_SIZE; // 20 + sa[6].abm = (uint8*) calloc(1, sa[6].abm_len * sizeof(uint8)); sa[6].arcw_len = 1; - sa[6].arcw[0] = 5; - sa[6].arc_len = (sa[6].arcw[0] * 2) + 1; + sa[6].arcw = 5; + sa[6].arc_len = (sa[6].arcw * 2) + 1; //itc_gcm128_init(&(sa[6].gcm_ctx), (unsigned char *)&(ek_ring[sa[6].ekid])); // SA 7 - KEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 130 @@ -185,14 +170,11 @@ static int32 sadb_config(void) sa[7].shivf_len = 12; sa[7].iv = (uint8*) calloc(1, sa[7].shivf_len * sizeof(uint8)); *(sa[7].iv + sa[7].shivf_len - 1) = 0; - sa[7].abm_len = 0x14; // 20 - for (int i = 0; i < sa[7].abm_len; i++) - { // Zero AAD bit mask - sa[7].abm[i] = 0x00; - } + sa[7].abm_len = ABM_SIZE; // 20 + sa[7].abm = (uint8*) calloc(1, sa[7].abm_len * sizeof(uint8)); sa[7].arcw_len = 1; - sa[7].arcw[0] = 5; - sa[7].arc_len = (sa[7].arcw[0] * 2) + 1; + sa[7].arcw = 5; + sa[7].arc_len = (sa[7].arcw * 2) + 1; sa[7].gvcid_tc_blk.tfvn = 0; sa[7].gvcid_tc_blk.scid = SCID & 0x3FF; sa[7].gvcid_tc_blk.vcid = 1; @@ -205,7 +187,7 @@ static int32 sadb_config(void) sa[8].ast = 0; sa[8].arc_len = 1; sa[8].arcw_len = 1; - sa[8].arcw[0] = 5; + sa[8].arcw = 5; sa[8].gvcid_tc_blk.tfvn = 0; sa[8].gvcid_tc_blk.scid = SCID & 0x3FF; sa[8].gvcid_tc_blk.vcid = 1; @@ -220,14 +202,11 @@ static int32 sadb_config(void) sa[9].shivf_len = 12; sa[9].iv = (uint8*) calloc(1, sa[9].shivf_len * sizeof(uint8)); *(sa[9].iv + 11) = 0; - sa[9].abm_len = 0x14; // 20 - for (int i = 0; i < sa[9].abm_len; i++) - { // Zero AAD bit mask - sa[9].abm[i] = 0x00; - } + sa[9].abm_len = ABM_SIZE; // 20 + sa[9].abm = (uint8*) calloc(1, sa[9].abm_len * sizeof(uint8)); sa[9].arcw_len = 1; - sa[9].arcw[0] = 5; - sa[9].arc_len = (sa[9].arcw[0] * 2) + 1; + sa[9].arcw = 5; + sa[9].arc_len = (sa[9].arcw * 2) + 1; sa[9].gvcid_tc_blk.tfvn = 0; sa[9].gvcid_tc_blk.scid = SCID & 0x3FF; sa[9].gvcid_tc_blk.vcid = 0; @@ -255,7 +234,7 @@ static int32 sadb_init(void) sa[x].acs_len = 0; sa[x].acs = 0; sa[x].arc_len = 0; - sa[x].arc[0] = 5; + sa[x].arc = NULL; // calloc and set to 5? } return status; } @@ -281,6 +260,8 @@ static int32 sadb_get_sa_from_spi(uint16 spi,SecurityAssociation_t** security_as int32 status = OS_SUCCESS; if(sa == NULL) { return CRYPTO_LIB_ERR_NO_INIT; } *security_association = &sa[spi]; + if(sa[spi].iv == NULL && ( sa[spi].ast == 1 || sa[spi].est == 1 )) { return CRYPTO_LIB_ERR_NULL_IV; } //Must have IV if doing encryption or authentication + if(sa[spi].abm == NULL && sa[spi].ast) { return CRYPTO_LIB_ERR_NULL_ABM; } //Must have IV if doing encryption or authentication #ifdef SA_DEBUG OS_printf(KYEL "DEBUG - Printing local copy of SA Entry for current SPI.\n" RESET); Crypto_saPrint(*security_association); @@ -299,7 +280,8 @@ static int32 sadb_get_operational_sa_from_gvcid(uint8 tfvn,uint16 scid,uint16 vc (crypto_config->unique_sa_per_mapid==TC_UNIQUE_SA_PER_MAP_ID_FALSE || sa[i].gvcid_tc_blk.mapid == mapid)) //only require MapID match is unique SA per MapID set (only relevant when using segmentation hdrs) { *security_association = &sa[i]; - if(sa[i].iv == NULL) { return CRYPTO_LIB_ERR_NULL_IV; } + if(sa[i].iv == NULL && ( sa[i].ast == 1 || sa[i].est == 1 )) { return CRYPTO_LIB_ERR_NULL_IV; } + if(sa[i].abm == NULL && sa[i].ast) { return CRYPTO_LIB_ERR_NULL_ABM; } //Must have IV if doing encryption or authentication #ifdef SA_DEBUG OS_printf("Valid operational SA found at index %d.\n", i); @@ -680,6 +662,8 @@ static int32 sadb_sa_create(void) sa[spi].est = ((uint8)sdls_frame.pdu.data[2] & 0x80) >> 7; sa[spi].ast = ((uint8)sdls_frame.pdu.data[2] & 0x40) >> 6; sa[spi].shivf_len = ((uint8)sdls_frame.pdu.data[2] & 0x3F); + if(sa[spi].iv != NULL) { free(sa[spi].iv); } + sa[spi].iv = (uint8*) calloc(1, sa[spi].shivf_len * sizeof(uint8)); sa[spi].shsnf_len = ((uint8)sdls_frame.pdu.data[3] & 0xFC) >> 2; sa[spi].shplf_len = ((uint8)sdls_frame.pdu.data[3] & 0x03); sa[spi].stmacf_len = ((uint8)sdls_frame.pdu.data[4]); @@ -705,14 +689,16 @@ static int32 sadb_sa_create(void) sa[spi].abm[x] = ((uint8)sdls_frame.pdu.data[count++]); } sa[spi].arc_len = ((uint8)sdls_frame.pdu.data[count++]); + if(sa[spi].arc != NULL) { free(sa[spi].arc); } + sa[spi].arc = (uint8*) calloc(1, sa[spi].arc_len * sizeof(uint8)); for (int x = 0; x < sa[spi].arc_len; x++) { - sa[spi].arc[x] = ((uint8)sdls_frame.pdu.data[count++]); + *(sa[spi].arc + x) = ((uint8)sdls_frame.pdu.data[count++]); } sa[spi].arcw_len = ((uint8)sdls_frame.pdu.data[count++]); for (int x = 0; x < sa[spi].arcw_len; x++) { - sa[spi].arcw[x] = ((uint8)sdls_frame.pdu.data[count++]); + sa[spi].arcw = sa[spi].arcw | ( ((uint8)sdls_frame.pdu.data[count++]) << (sa[spi].arcw_len - x) ); } // TODO: Checks for valid data @@ -831,7 +817,7 @@ static int32 sadb_sa_setARSNW(void) for(int x = 0; x < sa[spi].arcw_len; x++) { - sa[spi].arcw[x] = (uint8) sdls_frame.pdu.data[x+3]; + sa[spi].arcw = (((uint8) sdls_frame.pdu.data[x+3]) << (sa[spi].arcw_len - x) ); } } else diff --git a/fsw/src_mysql/sadb_routine_mariadb.template.c b/fsw/src_mysql/sadb_routine_mariadb.template.c index 4b4cf60c..389fc6cb 100644 --- a/fsw/src_mysql/sadb_routine_mariadb.template.c +++ b/fsw/src_mysql/sadb_routine_mariadb.template.c @@ -193,6 +193,7 @@ static int32 parse_sa_from_mysql_query(char* query, SecurityAssociation_t** secu //TODO -- Need to store mysql query hex string and then malloc sa->iv according to size. + //TODO -- IV && arc && abm as uint8* instead of uint8[]!!! while((row = mysql_fetch_row(result))){ for(int i=0; i < num_fields; i++) { @@ -234,7 +235,7 @@ static int32 parse_sa_from_mysql_query(char* query, SecurityAssociation_t** secu if(strcmp(field_names[i],"arc_len")==0){sa->arc_len=atoi(row[i]);continue;} if(strcmp(field_names[i],"HEX(arc)")==0){convert_hexstring_to_byte_array(row[i],sa->arc);continue;} if(strcmp(field_names[i],"arcw_len")==0){sa->arcw_len=atoi(row[i]);continue;} - if(strcmp(field_names[i],"HEX(arcw)")==0){convert_hexstring_to_byte_array(row[i],sa->arcw);continue;} + if(strcmp(field_names[i],"arcw")==0){sa->arcw=atoi(row[i]);continue;} //printf("%s:%s ",field_names[i], row[i] ? row[i] : "NULL"); } //printf("\n"); From dfee1cb584526bad78c8f53082749c9bd0b59303 Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Tue, 14 Dec 2021 20:30:53 -0500 Subject: [PATCH 05/18] Fixed Process Security MAC Code --- fsw/crypto_util/CMakeLists.txt | 2 +- fsw/crypto_util/app/et_dt_validation.c | 213 +++++++++++++++++++++-- fsw/crypto_util/app/ut_tc_apply.c | 36 +++- fsw/src/crypto.c | 11 ++ fsw/src/sadb_routine_inmemory.template.c | 4 + 5 files changed, 248 insertions(+), 18 deletions(-) diff --git a/fsw/crypto_util/CMakeLists.txt b/fsw/crypto_util/CMakeLists.txt index 4aed428e..c9b7a420 100644 --- a/fsw/crypto_util/CMakeLists.txt +++ b/fsw/crypto_util/CMakeLists.txt @@ -21,7 +21,7 @@ include_directories(../crypto/public_inc) if(${ENCTEST}) find_package (Python3 REQUIRED COMPONENTS Interpreter Development) - execute_process(COMMAND pip show pycryptodome RESULT_VARIABLE EXIT_CODE OUTPUT_QUIET) + execute_process(COMMAND pip3 show pycryptodome RESULT_VARIABLE EXIT_CODE OUTPUT_QUIET) if(NOT ${EXIT_CODE} EQUAL 0) message(FATAL_ERROR "The \"pycryptodome\" Python3 package is not installed, and is required for ENCTEST.") endif() diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index 979b32d8..40758276 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -967,13 +967,14 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) // NIST supplied vectors char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; char *buffer_nist_iv_h = "d79cf22d504cc793c3fb6c8a"; - char *buffer_cyber_chef_mac_h = "99eff39be8327e6950f03a329209d577"; + //char *buffer_cyber_chef_mac_h = "99eff39be8327e6950f03a329209d577"; + char *buffer_cyber_chef_mac_h = "34d0e323f5e4b80426401d4aa37930da"; char *buffer_nist_pt_h = "722ee47da4b77424733546c2d400c4e5"; // Create a MAC'd frame by adding our headers and a fecf // | Header | SPI | iv | plaintext | mac |fecf| - char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c40099eff39be8327e6950f03a329209d5776cb8"; // modified - // char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c4e599eff39be8327e6950f03a329209d5776cb8"; // original + char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c4e534d0e323f5e4b80426401d4aa37930daf55f"; + uint8 *buffer_nist_iv_b, *buffer_nist_pt_b, *buffer_nist_key_b, *buffer_cyber_chef_mac_b , *buffer_nist_mac_frame_b, *buffer_nist_cp_b = NULL; int buffer_nist_iv_len, buffer_nist_pt_len, buffer_nist_key_len, buffer_cyber_chef_mac_len , buffer_nist_mac_frame_len, buffer_nist_cp_len = 0; @@ -1019,19 +1020,19 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) // Note: For comparison, interested in the TF payload (exclude headers and FECF if present) // Calc payload index: total length - pt length - #ifdef DEBUG - printf("Expected MAC: "); - for (int i=0; istmacf_len; i++) - { - printf("%02x ", tc_nist_processed_frame->tc_sec_trailer.mac[i]); - } - printf("\n"); - #endif + // #ifdef DEBUG + // printf("Expected MAC: "); + // for (int i=0; istmacf_len; i++) + // { + // printf("%02x ", tc_nist_processed_frame->tc_sec_trailer.mac[i]); + // } + // printf("\n"); + // #endif Crypto_Shutdown(); // Verify the MAC @@ -1051,5 +1052,185 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) free(buffer_nist_mac_frame_b); free(buffer_nist_cp_b); } + +/** + * @brief Unit Test: Bad Data, Fail MAC validation + **/ +UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_DATA) +{ + // Setup & Initialize CryptoLib + uint16 enc_frame_len = 0; + int32 status; + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); + Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); + Crypto_Init(); + // NIST supplied vectors + char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; + char *buffer_nist_iv_h = "d79cf22d504cc793c3fb6c8a"; + //char *buffer_cyber_chef_mac_h = "99eff39be8327e6950f03a329209d577"; + char *buffer_cyber_chef_mac_h = "34d0e323f5e4b80426401d4aa37930da"; + char *buffer_nist_pt_h = "722ee47da4b77424733546c2d400c4e5"; + + // Create a MAC'd frame by adding our headers and a fecf + // | Header | SPI | iv | plaintext | mac |fecf| + char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c40034d0e323f5e4b80426401d4aa37930da123b"; + + uint8 *buffer_nist_iv_b, *buffer_nist_pt_b, *buffer_nist_key_b, *buffer_cyber_chef_mac_b , *buffer_nist_mac_frame_b, *buffer_nist_cp_b = NULL; + int buffer_nist_iv_len, buffer_nist_pt_len, buffer_nist_key_len, buffer_cyber_chef_mac_len , buffer_nist_mac_frame_len, buffer_nist_cp_len = 0; + + // Setup Processed Frame For Decryption + TC_t *tc_nist_processed_frame; + tc_nist_processed_frame = malloc(sizeof(uint8) * TC_SIZE); + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); + // Deactivate SA 1 + expose_sadb_get_sa_from_spi(1,&test_association); + test_association->sa_state = SA_NONE; + // Activate SA 9 + expose_sadb_get_sa_from_spi(9, &test_association); + test_association->ast = 1; + test_association->est = 0; + test_association->arc_len = 0; + test_association->abm_len = 1024; + memset(test_association->abm, 0xFF, (test_association->abm_len*sizeof(unsigned char))); + test_association->shivf_len = 12; + test_association->stmacf_len = 16; + test_association->sa_state = SA_OPERATIONAL; + + // Insert key into keyring of SA 9 + hex_conversion(buffer_nist_key_h, &buffer_nist_key_b, &buffer_nist_key_len); + memcpy(ek_ring[test_association->ekid].value, buffer_nist_key_b, buffer_nist_key_len); + + // Convert input plaintext + // TODO: Account for length of header and FECF (5+2) + hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); + // Convert/Set input IV + hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); + // Convert input mac + hex_conversion(buffer_cyber_chef_mac_h, &buffer_cyber_chef_mac_b, &buffer_cyber_chef_mac_len); + // Convert mac frame + hex_conversion(buffer_nist_mac_frame_h, &buffer_nist_mac_frame_b, &buffer_nist_mac_frame_len); + + status = Crypto_TC_ProcessSecurity(buffer_nist_mac_frame_b, &buffer_nist_mac_frame_len, tc_nist_processed_frame); + printf("TC_Process returned status %d\n", status); + + // Note: For comparison, interested in the TF payload (exclude headers and FECF if present) + // Calc payload index: total length - pt length + // #ifdef DEBUG + // printf("Expected MAC: "); + // for (int i=0; istmacf_len; i++) + // { + // printf("%02x ", tc_nist_processed_frame->tc_sec_trailer.mac[i]); + // } + // printf("\n"); + // #endif + + Crypto_Shutdown(); + ASSERT_EQ(CRYPTO_LIB_ERR_AUTHENTICATION_ERROR, status); + free(buffer_nist_iv_b); + free(buffer_nist_key_b); + free(buffer_cyber_chef_mac_b); + free(buffer_nist_mac_frame_b); + free(buffer_nist_cp_b); +} + +/** + * @brief Unit Test: Bad MAC, Fail MAC validation + **/ +UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_MAC) +{ + // Setup & Initialize CryptoLib + uint16 enc_frame_len = 0; + int32 status; + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); + Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); + Crypto_Init(); + // NIST supplied vectors + char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; + char *buffer_nist_iv_h = "d79cf22d504cc793c3fb6c8a"; + //char *buffer_cyber_chef_mac_h = "99eff39be8327e6950f03a329209d577"; + char *buffer_cyber_chef_mac_h = "34d0e323f5e4b80426401d4aa37930da"; + char *buffer_nist_pt_h = "722ee47da4b77424733546c2d400c4e5"; + + // Create a MAC'd frame by adding our headers and a fecf + // | Header | SPI | iv | plaintext | mac |fecf| + char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c4e534d0e323f5e4b80426401d4aa37930009f68"; + + uint8 *buffer_nist_iv_b, *buffer_nist_pt_b, *buffer_nist_key_b, *buffer_cyber_chef_mac_b , *buffer_nist_mac_frame_b, *buffer_nist_cp_b = NULL; + int buffer_nist_iv_len, buffer_nist_pt_len, buffer_nist_key_len, buffer_cyber_chef_mac_len , buffer_nist_mac_frame_len, buffer_nist_cp_len = 0; + + // Setup Processed Frame For Decryption + TC_t *tc_nist_processed_frame; + tc_nist_processed_frame = malloc(sizeof(uint8) * TC_SIZE); + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); + // Deactivate SA 1 + expose_sadb_get_sa_from_spi(1,&test_association); + test_association->sa_state = SA_NONE; + // Activate SA 9 + expose_sadb_get_sa_from_spi(9, &test_association); + test_association->ast = 1; + test_association->est = 0; + test_association->arc_len = 0; + test_association->abm_len = 1024; + memset(test_association->abm, 0xFF, (test_association->abm_len*sizeof(unsigned char))); + test_association->shivf_len = 12; + test_association->stmacf_len = 16; + test_association->sa_state = SA_OPERATIONAL; + + // Insert key into keyring of SA 9 + hex_conversion(buffer_nist_key_h, &buffer_nist_key_b, &buffer_nist_key_len); + memcpy(ek_ring[test_association->ekid].value, buffer_nist_key_b, buffer_nist_key_len); + + // Convert input plaintext + // TODO: Account for length of header and FECF (5+2) + hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); + // Convert/Set input IV + hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); + // Convert input mac + hex_conversion(buffer_cyber_chef_mac_h, &buffer_cyber_chef_mac_b, &buffer_cyber_chef_mac_len); + // Convert mac frame + hex_conversion(buffer_nist_mac_frame_h, &buffer_nist_mac_frame_b, &buffer_nist_mac_frame_len); + + status = Crypto_TC_ProcessSecurity(buffer_nist_mac_frame_b, &buffer_nist_mac_frame_len, tc_nist_processed_frame); + printf("TC_Process returned status %d\n", status); + + // Note: For comparison, interested in the TF payload (exclude headers and FECF if present) + // Calc payload index: total length - pt length + // #ifdef DEBUG + // printf("Expected MAC: "); + // for (int i=0; istmacf_len; i++) + // { + // printf("%02x ", tc_nist_processed_frame->tc_sec_trailer.mac[i]); + // } + // printf("\n"); + // #endif + + Crypto_Shutdown(); + ASSERT_EQ(CRYPTO_LIB_ERR_AUTHENTICATION_ERROR, status); + free(buffer_nist_iv_b); + free(buffer_nist_key_b); + free(buffer_cyber_chef_mac_b); + free(buffer_nist_mac_frame_b); + free(buffer_nist_cp_b); +} UTEST_MAIN(); diff --git a/fsw/crypto_util/app/ut_tc_apply.c b/fsw/crypto_util/app/ut_tc_apply.c index f73324dc..ea20f9b6 100644 --- a/fsw/crypto_util/app/ut_tc_apply.c +++ b/fsw/crypto_util/app/ut_tc_apply.c @@ -77,6 +77,9 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_CLEAR) ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); } +/** + * @brief Unit Test: Nominal Encryption + **/ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) { //Setup & Initialize CryptoLib @@ -97,7 +100,38 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) test_association->sa_state = SA_NONE; expose_sadb_get_sa_from_spi(4,&test_association); test_association->sa_state = SA_OPERATIONAL; - //test_association->ast=0; + test_association->ast=0; + + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Nominal Authorized Encryption + **/ +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_AUTH_ENC) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + + hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + + int32 return_val = CRYPTO_LIB_ERROR; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); + expose_sadb_get_sa_from_spi(1,&test_association); + test_association->sa_state = SA_NONE; + expose_sadb_get_sa_from_spi(4,&test_association); + test_association->sa_state = SA_OPERATIONAL; return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); Crypto_Shutdown(); diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index 37faca7d..aad1df44 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -3050,10 +3050,19 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro aad = (uint8*)malloc(tc_mac_start_index * sizeof(uint8)); // Prepare additional authenticated data (AAD) + #ifdef DEBUG + OS_printf(KYEL "AAD: \n\t" RESET); + #endif for (y = 0; y < tc_mac_start_index; y++) { aad[y] = (uint8) ((uint8)ingest[y] & (uint8) *(sa_ptr->abm + y)); + #ifdef DEBUG + OS_printf("%02x", aad[y]); + #endif } + #ifdef DEBUG + OS_printf("\n"); + #endif gcry_error = gcry_cipher_authenticate( tmp_hd, @@ -3107,6 +3116,8 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro for(int i=0; istmacf_len; i++) { + // printf("Expected: %02x\n", calculated_mac[i]); + // printf("Actual: %02x\n", tc_sdls_processed_frame->tc_sec_trailer.mac[i]); if (calculated_mac[i] != tc_sdls_processed_frame->tc_sec_trailer.mac[i]) { status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; diff --git a/fsw/src/sadb_routine_inmemory.template.c b/fsw/src/sadb_routine_inmemory.template.c index e9721562..c9806afc 100644 --- a/fsw/src/sadb_routine_inmemory.template.c +++ b/fsw/src/sadb_routine_inmemory.template.c @@ -83,6 +83,7 @@ static int32 sadb_config(void) sa[1].gvcid_tc_blk.scid = SCID & 0x3FF; sa[1].gvcid_tc_blk.vcid = 0; sa[1].gvcid_tc_blk.mapid = TYPE_TC; + // SA 2 - KEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 128 sa[2].spi = 2; sa[2].ekid = 128; @@ -97,6 +98,7 @@ static int32 sadb_config(void) sa[2].arcw_len = 1; sa[2].arcw = 5; sa[2].arc_len = (sa[2].arcw * 2) + 1; + // SA 3 - KEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 129 sa[3].spi = 3; sa[3].ekid = 129; @@ -111,6 +113,7 @@ static int32 sadb_config(void) sa[3].arcw_len = 1; sa[3].arcw = 5; sa[3].arc_len = (sa[3].arcw * 2) + 1; + // SA 4 - KEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 130 // SA 4 VC0/1 is now 4-VC0, 7-VC1 sa[4].spi = 4; @@ -146,6 +149,7 @@ static int32 sadb_config(void) sa[5].arcw_len = 1; sa[5].arcw = 5; sa[5].arc_len = (sa[5].arcw * 2) + 1; + // SA 6 - UNKEYED; ARCW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: - sa[6].spi = 6; sa[6].sa_state = SA_UNKEYED; From cdc1ea94262dcac83b5fd6163375b95c82c450bb Mon Sep 17 00:00:00 2001 From: "D. Cody Cutright" Date: Tue, 14 Dec 2021 21:27:29 -0500 Subject: [PATCH 06/18] Add decryption to Process Security function --- fsw/public_inc/crypto_error.h | 3 +- fsw/src/crypto.c | 178 ++++++++++++++++++++++++++-------- 2 files changed, 142 insertions(+), 39 deletions(-) diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index 8c1ef16e..f8961664 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -43,6 +43,7 @@ #define CRYPTO_LIB_ERR_LIBGCRYPT_ERROR (-14) #define CRYPTO_LIB_ERR_AUTHENTICATION_ERROR (-15) #define CRYPTO_LIB_ERR_NULL_IV (-16) -#define CRYPTO_LIB_ERR_NULL_ABM (-17) +#define CRYPTO_LIB_ERR_NULL_ABM (-17) +#define CRYPTO_LIB_ERR_DECRYPT_ERROR (-18) #endif //_crypto_error_h_ diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index 83a2e3af..8b4acf34 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -87,6 +87,7 @@ static int32 Crypto_User_ModifyKey(void); static int32 Crypto_User_ModifyActiveTM(void); static int32 Crypto_User_ModifyVCID(void); // Determine Payload Data Unit +static int32 Crypto_Process_Extended_Procedure_Pdu(TC_t* tc_sdls_processed_frame, char* ingest); static int32 Crypto_PDU(char* ingest, TC_t* tc_frame); // Managed Parameter Functions static int32 Crypto_Get_Managed_Parameters_For_Gvcid(uint8 tfvn,uint16 scid,uint8 vcid,GvcidManagedParameters_t* managed_parameters_in, @@ -3262,18 +3263,20 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro // Check IV is in ARCW status = Crypto_window(tc_sdls_processed_frame->tc_sec_header.iv, sa_ptr->iv, sa_ptr->shivf_len, sa_ptr->arcw); - printf("Received IV is\n\t"); - for(int i=0; ishivf_len; i++) - // for(int i=0; itc_sec_header.iv + i)); - } - printf("\nSA IV is\n\t"); - for(int i=0; ishivf_len; i++) - { - printf("%02x", *(sa_ptr->iv + i)); - } - printf("\nARCW is: %d\n", sa_ptr->arcw); + #ifdef DEBUG + printf("Received IV is\n\t"); + for(int i=0; ishivf_len; i++) + // for(int i=0; itc_sec_header.iv + i)); + } + printf("\nSA IV is\n\t"); + for(int i=0; ishivf_len; i++) + { + printf("%02x", *(sa_ptr->iv + i)); + } + printf("\nARCW is: %d\n", sa_ptr->arcw); + #endif if (status != CRYPTO_LIB_SUCCESS) { return status; } // TODO: Update SA IV through SADB_Routine function call } @@ -3344,30 +3347,6 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro return status; } - // TODO Better without copies / CMAC updates - // char *garbage_buff = malloc(tc_mac_start_index * sizeof(uint8)); - // gcry_error = gcry_cipher_encrypt( - // tmp_hd, // plaintext output - // garbage_buff, // plaintext garbage out - // tc_mac_start_index, // length of data - // aad, // ciphertext input - // tc_mac_start_index // in data length - // ); - // if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - // { - // OS_printf(KRED "ERROR: gcry_cipher_authentication error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - // OS_printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error),gcry_strerror (gcry_error)); - // status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; - // return status; - // } - // free(garbage_buff); - - // - // gcry_error = gcry_cipher_checktag( - // tmp_hd, - // tc_sdls_processed_frame->tc_sec_trailer.mac, // tag input - // sa_ptr->stmacf_len // tag size - // ); uint8* calculated_mac = malloc(sa_ptr->stmacf_len * sizeof(uint8)); gcry_error = gcry_cipher_gettag( tmp_hd, @@ -3383,8 +3362,6 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro for(int i=0; istmacf_len; i++) { - // printf("Expected: %02x\n", calculated_mac[i]); - // printf("Actual: %02x\n", tc_sdls_processed_frame->tc_sec_trailer.mac[i]); if (calculated_mac[i] != tc_sdls_processed_frame->tc_sec_trailer.mac[i]) { status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; @@ -3395,12 +3372,137 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro } // Decrypt, if applicable + if((sa_service_type == SA_ENCRYPTION) || + (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) + { + uint16 tc_enc_payload_start_index = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len; + tc_sdls_processed_frame->tc_pdu_len = tc_sdls_processed_frame->tc_header.fl + 1 - tc_enc_payload_start_index - sa_ptr->stmacf_len - fecf_len; + + gcry_error = gcry_cipher_decrypt( + tmp_hd, + &(tc_sdls_processed_frame->tc_pdu[0]), // plaintext output + tc_sdls_processed_frame->tc_pdu_len, // length of data + &(ingest[tc_enc_payload_start_index]), // ciphertext input + tc_sdls_processed_frame->tc_pdu_len // in data length + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_decrypt error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = CRYPTO_LIB_ERR_DECRYPT_ERROR; + return status; + } + } + + if(sa_service_type != SA_PLAINTEXT) + { + gcry_cipher_close(tmp_hd); + } + + if(sa_service_type == SA_PLAINTEXT) + { + // TODO: Plaintext ARSN + + uint16 tc_enc_payload_start_index = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len; + tc_sdls_processed_frame->tc_pdu_len = tc_sdls_processed_frame->tc_header.fl + 1 - tc_enc_payload_start_index - sa_ptr->stmacf_len - fecf_len; + memcpy(tc_sdls_processed_frame->tc_pdu, &(ingest[tc_enc_payload_start_index]), tc_sdls_processed_frame->tc_pdu_len); + } // Extended PDU processing, if applicable + if(crypto_config->process_sdls_pdus == TC_PROCESS_SDLS_PDUS_TRUE) + { + status = Crypto_Process_Extended_Procedure_Pdu(tc_sdls_processed_frame, ingest); + } return status; } +/** + * @brief Function: Crypto_Process_Extended_Procedure_Pdu + * @param tc_sdls_processed_frame: TC_t* + * @param ingest: char* + * @note TODO - Actually update based on variable config + * */ +static int32 Crypto_Process_Extended_Procedure_Pdu(TC_t* tc_sdls_processed_frame, char* ingest) +{ + int32 status = CRYPTO_LIB_SUCCESS; + if (crypto_config->has_pus_hdr==TC_HAS_PUS_HDR) + { + if ((tc_sdls_processed_frame->tc_pdu[0] == 0x18) && (tc_sdls_processed_frame->tc_pdu[1] == 0x80)) + // Crypto Lib Application ID + { + #ifdef DEBUG + OS_printf(KGRN "Received SDLS command: " RESET); + #endif + // CCSDS Header + sdls_frame.hdr.pvn = (tc_sdls_processed_frame->tc_pdu[0] & 0xE0) >> 5; + sdls_frame.hdr.type = (tc_sdls_processed_frame->tc_pdu[0] & 0x10) >> 4; + sdls_frame.hdr.shdr = (tc_sdls_processed_frame->tc_pdu[0] & 0x08) >> 3; + sdls_frame.hdr.appID = + ((tc_sdls_processed_frame->tc_pdu[0] & 0x07) << 8) | tc_sdls_processed_frame->tc_pdu[1]; + sdls_frame.hdr.seq = (tc_sdls_processed_frame->tc_pdu[2] & 0xC0) >> 6; + sdls_frame.hdr.pktid = + ((tc_sdls_processed_frame->tc_pdu[2] & 0x3F) << 8) | tc_sdls_processed_frame->tc_pdu[3]; + sdls_frame.hdr.pkt_length = (tc_sdls_processed_frame->tc_pdu[4] << 8) | tc_sdls_processed_frame->tc_pdu[5]; + + // CCSDS PUS + sdls_frame.pus.shf = (tc_sdls_processed_frame->tc_pdu[6] & 0x80) >> 7; + sdls_frame.pus.pusv = (tc_sdls_processed_frame->tc_pdu[6] & 0x70) >> 4; + sdls_frame.pus.ack = (tc_sdls_processed_frame->tc_pdu[6] & 0x0F); + sdls_frame.pus.st = tc_sdls_processed_frame->tc_pdu[7]; + sdls_frame.pus.sst = tc_sdls_processed_frame->tc_pdu[8]; + sdls_frame.pus.sid = (tc_sdls_processed_frame->tc_pdu[9] & 0xF0) >> 4; + sdls_frame.pus.spare = (tc_sdls_processed_frame->tc_pdu[9] & 0x0F); + + // SDLS TLV PDU + sdls_frame.pdu.type = (tc_sdls_processed_frame->tc_pdu[10] & 0x80) >> 7; + sdls_frame.pdu.uf = (tc_sdls_processed_frame->tc_pdu[10] & 0x40) >> 6; + sdls_frame.pdu.sg = (tc_sdls_processed_frame->tc_pdu[10] & 0x30) >> 4; + sdls_frame.pdu.pid = (tc_sdls_processed_frame->tc_pdu[10] & 0x0F); + sdls_frame.pdu.pdu_len = (tc_sdls_processed_frame->tc_pdu[11] << 8) | tc_sdls_processed_frame->tc_pdu[12]; + for (int x = 13; x < (13 + sdls_frame.hdr.pkt_length); x++) { + sdls_frame.pdu.data[x - 13] = tc_sdls_processed_frame->tc_pdu[x]; + } + + #ifdef CCSDS_DEBUG + Crypto_ccsdsPrint(&sdls_frame); + #endif + + // Determine type of PDU + status = Crypto_PDU(ingest, tc_sdls_processed_frame); + } + } + else if (tc_sdls_processed_frame->tc_header.vcid == TC_SDLS_EP_VCID) //TC SDLS PDU with no packet layer + { + #ifdef DEBUG + OS_printf(KGRN "Received SDLS command: " RESET); + #endif + // No Packet HDR or PUS in these frames + // SDLS TLV PDU + sdls_frame.pdu.type = (tc_sdls_processed_frame->tc_pdu[0] & 0x80) >> 7; + sdls_frame.pdu.uf = (tc_sdls_processed_frame->tc_pdu[0] & 0x40) >> 6; + sdls_frame.pdu.sg = (tc_sdls_processed_frame->tc_pdu[0] & 0x30) >> 4; + sdls_frame.pdu.pid = (tc_sdls_processed_frame->tc_pdu[0] & 0x0F); + sdls_frame.pdu.pdu_len = (tc_sdls_processed_frame->tc_pdu[1] << 8) | tc_sdls_processed_frame->tc_pdu[2]; + for (int x = 3; x < (3 + tc_sdls_processed_frame->tc_header.fl); x++) { + //Todo - Consider how this behaves with large OTAR PDUs that are larger than 1 TC in size. Most likely fails. Must consider Uplink Sessions (sequence numbers). + sdls_frame.pdu.data[x - 3] = tc_sdls_processed_frame->tc_pdu[x]; + } + + #ifdef CCSDS_DEBUG + Crypto_ccsdsPrint(&sdls_frame); + #endif + + // Determine type of PDU + status = Crypto_PDU(ingest, tc_sdls_processed_frame); + } + else { + //TODO - Process SDLS PDU with Packet Layer without PUS_HDR + } + + return status; +}//End Process SDLS PDU + + /** * @brief Function: Crypto_TM_ApplySecurity * @param ingest: char* From 22c591cbafc0ba7fef28b053ad6867b4ff19ef7d Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Tue, 14 Dec 2021 22:09:40 -0500 Subject: [PATCH 07/18] Added Assert for Return Status --- fsw/crypto_util/app/et_dt_validation.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index 04f98abd..d165c055 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -130,7 +130,8 @@ UTEST(ET_VALIDATION, AUTH_ENCRYPTION_TEST) tc_sdls_processed_frame = malloc(sizeof(uint8) * TC_SIZE); memset(tc_sdls_processed_frame, 0, (sizeof(uint8) * TC_SIZE)); // Ensure that Process Security can activate SA 4 - Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); + return_val = Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); // Expose SA 1 for testing expose_sadb_get_sa_from_spi(1,&test_association); // Deactive SA 1 From 3296a70f77d2fd9d84b61eb8f89537f2ada8c013 Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Tue, 14 Dec 2021 23:27:00 -0500 Subject: [PATCH 08/18] Continuing TCApply/Process fixes --- fsw/crypto_util/app/et_dt_validation.c | 45 ++++++--- fsw/src/crypto.c | 118 +++++++++++++---------- fsw/src/sadb_routine_inmemory.template.c | 2 +- 3 files changed, 99 insertions(+), 66 deletions(-) diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index d165c055..b1ae3229 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -143,6 +143,7 @@ UTEST(ET_VALIDATION, AUTH_ENCRYPTION_TEST) test_association->iv[11] = 1; test_association->ast = 1; test_association->est = 1; + test_association->sa_state = SA_OPERATIONAL; int32 ret_status = Crypto_TC_ApplySecurity(enc_test_ping_b, enc_test_ping_len, &ptr_enc_frame, &enc_frame_len); // Get Truth Baseline @@ -192,8 +193,8 @@ UTEST(DT_VALIDATION, AUTH_DECRYPTION_TEST) memset(tc_sdls_processed_frame, 0, (sizeof(uint8) * TC_SIZE)); // Ensure that Process Security can activate SA 4 - Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); - + return_val = Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); + ASSERT_EQ(return_val, CRYPTO_LIB_SUCCESS); // Expose SA 1 for testing expose_sadb_get_sa_from_spi(1,&test_association); @@ -207,10 +208,23 @@ UTEST(DT_VALIDATION, AUTH_DECRYPTION_TEST) test_association->iv[11] = 1; test_association->ast = 1; test_association->est = 1; + test_association->sa_state = SA_OPERATIONAL; Crypto_TC_ProcessSecurity(dec_test_ping_b, &dec_test_ping_len, tc_sdls_processed_frame); Crypto_Shutdown(); + + printf("PDU:\n\t"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + printf("%02x", enc_test_ping_b[i]); + } + printf("\nPF PDU:\n\t"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + printf("%02x", tc_sdls_processed_frame->tc_pdu[i]); + } + printf("\n"); for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) { ASSERT_EQ(enc_test_ping_b[i], tc_sdls_processed_frame->tc_pdu[i]); @@ -265,7 +279,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_ct_h, &buffer_nist_ct_b, &buffer_nist_ct_len); @@ -323,7 +337,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) expose_sadb_get_sa_from_spi(9, &test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; - + expose_sadb_get_sa_from_spi(9, &test_association); // Insert key into keyring of SA 9 hex_conversion(buffer_nist_key_h, &buffer_nist_key_b, &buffer_nist_key_len); memcpy(ek_ring[test_association->ekid].value, buffer_nist_key_b, buffer_nist_key_len); @@ -333,7 +347,8 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); + printf("NIST IV LEN: %d\n", buffer_nist_iv_len); // Convert input encryptedtext hex_conversion(buffer_nist_et_h, &buffer_nist_et_b, &buffer_nist_et_len); @@ -396,7 +411,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_ct_h, &buffer_nist_ct_b, &buffer_nist_ct_len); @@ -463,7 +478,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_et_h, &buffer_nist_et_b, &buffer_nist_et_len); @@ -524,7 +539,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_ct_h, &buffer_nist_ct_b, &buffer_nist_ct_len); @@ -591,7 +606,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_et_h, &buffer_nist_et_b, &buffer_nist_et_len); @@ -652,7 +667,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_ct_h, &buffer_nist_ct_b, &buffer_nist_ct_len); @@ -719,7 +734,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_et_h, &buffer_nist_et_b, &buffer_nist_et_len); @@ -780,7 +795,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_ct_h, &buffer_nist_ct_b, &buffer_nist_ct_len); @@ -847,7 +862,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input ciphertext hex_conversion(buffer_nist_et_h, &buffer_nist_et_b, &buffer_nist_et_len); @@ -924,7 +939,7 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input aad hex_conversion(buffer_nist_aad_h, &buffer_nist_aad_b, &buffer_nist_aad_len); // Convert input mac @@ -1000,7 +1015,7 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) hex_conversion(buffer_nist_pt_h, &buffer_nist_pt_b, &buffer_nist_pt_len); // Convert/Set input IV hex_conversion(buffer_nist_iv_h, &buffer_nist_iv_b, &buffer_nist_iv_len); - memcpy(&test_association->iv[0], buffer_nist_iv_b, buffer_nist_iv_len); + memcpy(test_association->iv, buffer_nist_iv_b, buffer_nist_iv_len); // Convert input mac hex_conversion(buffer_cyber_chef_mac_h, &buffer_cyber_chef_mac_b, &buffer_cyber_chef_mac_len); diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index 8b4acf34..94ca4c44 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -2602,7 +2602,6 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len status = Crypto_Get_Managed_Parameters_For_Gvcid(temp_tc_header.tfvn,temp_tc_header.scid,temp_tc_header.vcid,gvcid_managed_parameters,¤t_managed_parameters); if(status != OS_SUCCESS) {return status;} //Unable to get necessary Managed Parameters for TC TF -- return with error. - uint8 segmentation_hdr = 0x00; uint8 map_id = 0; if(current_managed_parameters->has_segmentation_hdr==TC_HAS_SEGMENT_HDRS){ @@ -2625,7 +2624,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len status = CRYPTO_LIB_ERR_INVALID_CC_FLAG; } - if (status == OS_SUCCESS) + if (status == CRYPTO_LIB_SUCCESS) { // Query SA DB for active SA / SDLS parameters if(sadb_routine == NULL) //This should not happen, but tested here for safety @@ -2637,10 +2636,9 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len { status = sadb_routine->sadb_get_operational_sa_from_gvcid(temp_tc_header.tfvn, temp_tc_header.scid, temp_tc_header.vcid, map_id,&sa_ptr); } - // If unable to get operational SA, can return - if (status != OS_SUCCESS) + if (status != CRYPTO_LIB_SUCCESS) { return status; } @@ -2926,15 +2924,16 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len if ((sa_service_type == SA_AUTHENTICATION) || \ (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) { - uint8 bit_masked_data[sa_ptr->abm_len]; - for (int y = 0; y < sa_ptr->abm_len; y++) + uint16 bit_masked_data_len = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len; + uint8 bit_masked_data[bit_masked_data_len]; + for (int y = 0; y < bit_masked_data_len; y++) { bit_masked_data[y] = p_new_enc_frame[y] & *(sa_ptr->abm + y); } #ifdef MAC_DEBUG - OS_printf(KYEL "Preparing AAD:\n"); - OS_printf("\tUsing ABM Length of %d\n\t", sa_ptr->abm_len); - for (int y = 0; y < sa_ptr->abm_len; y++) + OS_printf(KYEL "Preparing Header AAD:\n"); + OS_printf("\tUsing Header AAD Length of %d\n\t", bit_masked_data_len); + for (int y = 0; y < bit_masked_data_len; y++) { OS_printf("%02x", bit_masked_data[y]); } @@ -2944,7 +2943,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len gcry_error = gcry_cipher_authenticate( tmp_hd, bit_masked_data, // additional authenticated data - sa_ptr->abm_len // length of AAD + bit_masked_data_len // length of AAD ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { @@ -2973,10 +2972,10 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len gcry_error = gcry_cipher_encrypt( tmp_hd, - &p_new_enc_frame[index], // ciphertext output - tf_payload_len, // length of data - (p_in_frame + TC_FRAME_HEADER_SIZE + segment_hdr_len), // plaintext input - tf_payload_len // in data length + &p_new_enc_frame[index], // ciphertext output + tf_payload_len, // length of data + (p_in_frame + TC_FRAME_HEADER_SIZE + segment_hdr_len), // plaintext input + tf_payload_len // in data length ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) @@ -3240,6 +3239,46 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro // Parse pad length memcpy((tc_sdls_processed_frame->tc_sec_header.pad)+(TC_PAD_SIZE-sa_ptr->shplf_len), &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len]) , sa_ptr->shplf_len); + if((sa_service_type == SA_AUTHENTICATION) || + (sa_service_type == SA_AUTHENTICATED_ENCRYPTION) || + (sa_service_type == SA_ENCRYPTION)) + { + gcry_error = gcry_cipher_open( + &(tmp_hd), + GCRY_CIPHER_AES256, + GCRY_CIPHER_MODE_GCM, + GCRY_CIPHER_CBC_MAC + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + return status; + } + gcry_error = gcry_cipher_setkey( + tmp_hd, + ek_ring[sa_ptr->ekid].value, + KEY_SIZE + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + return status; + } + gcry_error = gcry_cipher_setiv( + tmp_hd, + tc_sdls_processed_frame->tc_sec_header.iv, + sa_ptr->shivf_len + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + return status; + } + } + // Check MAC, if applicable if((sa_service_type == SA_AUTHENTICATION) || (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) @@ -3283,41 +3322,6 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro } - gcry_error = gcry_cipher_open( - &(tmp_hd), - GCRY_CIPHER_AES256, - GCRY_CIPHER_MODE_GCM, - GCRY_CIPHER_CBC_MAC - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); - status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; - return status; - } - gcry_error = gcry_cipher_setkey( - tmp_hd, - ek_ring[sa_ptr->ekid].value, - KEY_SIZE - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; - return status; - } - gcry_error = gcry_cipher_setiv( - tmp_hd, - tc_sdls_processed_frame->tc_sec_header.iv, - sa_ptr->shivf_len - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; - return status; - } - aad = (uint8*)malloc(tc_mac_start_index * sizeof(uint8)); // Prepare additional authenticated data (AAD) #ifdef DEBUG @@ -3360,6 +3364,20 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro return status; } + #ifdef DEBUG + OS_printf(KRED "Expected: \n\t" RESET); + for(int i=0; istmacf_len; i++) + { + printf("%02x", calculated_mac[i]); + } + OS_printf(KRED "\nActual: \n\t" RESET); + for(int i=0; istmacf_len; i++) + { + printf("%02x", tc_sdls_processed_frame->tc_sec_trailer.mac[i]); + } + printf("\n"); + #endif + for(int i=0; istmacf_len; i++) { if (calculated_mac[i] != tc_sdls_processed_frame->tc_sec_trailer.mac[i]) @@ -3380,7 +3398,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro gcry_error = gcry_cipher_decrypt( tmp_hd, - &(tc_sdls_processed_frame->tc_pdu[0]), // plaintext output + tc_sdls_processed_frame->tc_pdu, // plaintext output tc_sdls_processed_frame->tc_pdu_len, // length of data &(ingest[tc_enc_payload_start_index]), // ciphertext input tc_sdls_processed_frame->tc_pdu_len // in data length diff --git a/fsw/src/sadb_routine_inmemory.template.c b/fsw/src/sadb_routine_inmemory.template.c index c2e11192..a1727992 100644 --- a/fsw/src/sadb_routine_inmemory.template.c +++ b/fsw/src/sadb_routine_inmemory.template.c @@ -306,7 +306,7 @@ static int32 sadb_get_sa_from_spi(uint16 spi,SecurityAssociation_t** security_as **/ static int32 sadb_get_operational_sa_from_gvcid(uint8 tfvn,uint16 scid,uint16 vcid,uint8 mapid,SecurityAssociation_t** security_association) { - int32 status = CRYPTO_LIB_ERROR; + int32 status = CRYPTO_LIB_ERR_NO_OPERATIONAL_SA; if(sa == NULL) { return CRYPTO_LIB_ERR_NO_INIT; } for (int i=0; i<10; i++) From 7c732ca624a3cec29f8950fb543c2c7877e8811c Mon Sep 17 00:00:00 2001 From: "D. Cody Cutright" Date: Tue, 14 Dec 2021 23:40:38 -0500 Subject: [PATCH 09/18] Pass ball to Robert --- fsw/src/crypto.c | 111 ++++++++++++++++++++++++++++++----------------- 1 file changed, 71 insertions(+), 40 deletions(-) diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index 94ca4c44..d5a5ebba 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -2609,7 +2609,6 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len map_id = segmentation_hdr & 0x3F; } - // Check if command frame flag set if ((temp_tc_header.cc == 1) && (status == OS_SUCCESS)) { @@ -2920,40 +2919,6 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len return status; } - // Prepare additional authenticated data, if needed - if ((sa_service_type == SA_AUTHENTICATION) || \ - (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) - { - uint16 bit_masked_data_len = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len; - uint8 bit_masked_data[bit_masked_data_len]; - for (int y = 0; y < bit_masked_data_len; y++) - { - bit_masked_data[y] = p_new_enc_frame[y] & *(sa_ptr->abm + y); - } - #ifdef MAC_DEBUG - OS_printf(KYEL "Preparing Header AAD:\n"); - OS_printf("\tUsing Header AAD Length of %d\n\t", bit_masked_data_len); - for (int y = 0; y < bit_masked_data_len; y++) - { - OS_printf("%02x", bit_masked_data[y]); - } - OS_printf("\n" RESET); - #endif - - gcry_error = gcry_cipher_authenticate( - tmp_hd, - bit_masked_data, // additional authenticated data - bit_masked_data_len // length of AAD - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_authenticate error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - OS_printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error),gcry_strerror (gcry_error)); - status = OS_ERROR; - return status; - } - } - if ((sa_service_type == SA_ENCRYPTION) || \ (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) { @@ -2995,11 +2960,80 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len } OS_printf("\n"); #endif + + // Close cipher, so we can authenticate encrypted data + gcry_cipher_close(tmp_hd); } + // Prepare additional authenticated data, if needed if ((sa_service_type == SA_AUTHENTICATION) || \ (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) { + gcry_error = gcry_cipher_open( + &(tmp_hd), + GCRY_CIPHER_AES256, + GCRY_CIPHER_MODE_GCM, + GCRY_CIPHER_CBC_MAC + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = OS_ERROR; + return status; + } + gcry_error = gcry_cipher_setkey( + tmp_hd, + &(ek_ring[sa_ptr->ekid].value[0]), + KEY_SIZE //TODO: look into this + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = OS_ERROR; + return status; + } + gcry_error = gcry_cipher_setiv( + tmp_hd, + sa_ptr->iv, + sa_ptr->shivf_len + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = OS_ERROR; + return status; + } + + uint16 bit_masked_data_len = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + tf_payload_len; + uint8 bit_masked_data[bit_masked_data_len]; + + for (int y = 0; y < bit_masked_data_len; y++) + { + bit_masked_data[y] = p_new_enc_frame[y] & *(sa_ptr->abm + y); + } + #ifdef MAC_DEBUG + OS_printf(KYEL "Preparing Header AAD:\n"); + OS_printf("\tUsing Header AAD Length of %d\n\t", bit_masked_data_len); + for (int y = 0; y < bit_masked_data_len; y++) + { + OS_printf("%02x", bit_masked_data[y]); + } + OS_printf("\n" RESET); + #endif + + gcry_error = gcry_cipher_authenticate( + tmp_hd, + bit_masked_data, // additional authenticated data + bit_masked_data_len // length of AAD + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_authenticate error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + OS_printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error),gcry_strerror (gcry_error)); + status = OS_ERROR; + return status; + } + // TODO - Know if FECF exists mac_loc = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + tf_payload_len; #ifdef MAC_DEBUG @@ -3008,7 +3042,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len #endif gcry_error = gcry_cipher_gettag( tmp_hd, - &p_new_enc_frame[mac_loc], // tag output + &p_new_enc_frame[mac_loc], // tag output MAC_SIZE // tag size // TODO - use sa_ptr->abm_len instead of hardcoded mac size? ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) @@ -3017,10 +3051,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len status = OS_ERROR; return status; } - } - // Zeroise any sensitive information - if (sa_service_type != SA_PLAINTEXT) - { + // Zeroise any sensitive information gcry_cipher_close(tmp_hd); } } From cc6f9223f12c8d61327d99b741ddcd27eb8d6fb1 Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Wed, 15 Dec 2021 00:00:56 -0500 Subject: [PATCH 10/18] Modified Python --- python/encryption_test.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/encryption_test.py b/python/encryption_test.py index cfa038d4..bcf75dab 100644 --- a/python/encryption_test.py +++ b/python/encryption_test.py @@ -34,7 +34,7 @@ def __init__(self): # Function: Encrypt # Encrypts data - given a key, iv, header, and bitmask def encrypt(self, data, key, iv, header, bitmask): - hex_header = header + iv # Combines Header and IV (AAD) + hex_header = header + iv + data # Combines Header and IV (AAD) bitmask_b = bytes.fromhex(bitmask) header_b = bytes.fromhex(hex_header) key_b = bytes.fromhex(key) @@ -55,6 +55,8 @@ def encrypt(self, data, key, iv, header, bitmask): value_i = int.from_bytes(pieces, byteorder="big") & int.from_bytes(bitmask_b, byteorder="big") value_b = value_i.to_bytes(max(len(pieces), len(bitmask_b)), byteorder="big") zeroed_header_b += value_b + print("ZEROED AAD:", zeroed_header_b.hex()) + print("DATA:", data_b.hex()) cipher.update(zeroed_header_b) #cipher.update(header_b) # Get Cipher and tag From 506fe0831f4be76cbd2acd9f707781a2551e8d22 Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Wed, 15 Dec 2021 12:44:03 -0800 Subject: [PATCH 11/18] Fix unit test reliance on expose_sa function to use sadb routine calls --- .../create_sadb.sql | 10 +- ...db_jpl_unit_test_security_associations.sql | 12 +- fsw/crypto_util/app/et_dt_validation.c | 102 ++-- fsw/crypto_util/app/ut_tc_apply.c | 448 +++++++++--------- fsw/public_inc/sadb_routine.h | 4 - fsw/src/sadb_routine_inmemory.template.c | 7 - fsw/src_mysql/sadb_routine_mariadb.template.c | 3 +- 7 files changed, 306 insertions(+), 280 deletions(-) diff --git a/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb.sql b/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb.sql index f3c62743..f9eec3fb 100644 --- a/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb.sql +++ b/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb.sql @@ -22,15 +22,15 @@ CREATE TABLE security_associations ,shplf_len SMALLINT NOT NULL DEFAULT 0 ,stmacf_len SMALLINT NOT NULL DEFAULT 0 ,ecs_len SMALLINT - ,ecs BINARY(4) NOT NULL DEFAULT X'00000000' -- ECS_SIZE=4 + ,ecs VARBINARY(4) NOT NULL DEFAULT X'00000000' -- ECS_SIZE=4 ,iv_len SMALLINT NOT NULL DEFAULT 12 - ,iv BINARY(12) NOT NULL DEFAULT X'000000000000000000000000' -- IV_SIZE=12 + ,iv VARBINARY(20) NOT NULL DEFAULT X'000000000000000000000000' -- IV_SIZE=12 ,acs_len SMALLINT NOT NULL DEFAULT 0 ,acs SMALLINT NOT NULL DEFAULT 0 ,abm_len MEDIUMINT - ,abm BINARY(20) NOT NULL DEFAULT X'1111111111111111111111111111111111111111' -- ABM_SIZE=20 + ,abm VARBINARY(1024) NOT NULL DEFAULT X'1111111111111111111111111111111111111111' -- ABM_SIZE=1024 ,arc_len SMALLINT NOT NULL DEFAULT 0 - ,arc BINARY(20) NOT NULL DEFAULT X'0000000000000000000000000000000000000000' -- ARC_SIZE=20 , TBD why so large... + ,arc VARBINARY(20) NOT NULL DEFAULT X'0000000000000000000000000000000000000000' -- ARC_SIZE=20 , TBD why so large... ,arcw_len SMALLINT - ,arcw BINARY(1) NOT NULL DEFAULT X'00' -- ARCW_SIZE=1 + ,arcw SMALLINT NOT NULL DEFAULT 0 -- ARCW_SIZE=1 ); \ No newline at end of file diff --git a/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb_jpl_unit_test_security_associations.sql b/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb_jpl_unit_test_security_associations.sql index 045e5310..ad554915 100644 --- a/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb_jpl_unit_test_security_associations.sql +++ b/fsw/crypto_sadb/sadb_mariadb_admin_scripts/create_sadb_jpl_unit_test_security_associations.sql @@ -2,21 +2,21 @@ USE sadb; -- SA 1 - CLEAR MODE INSERT INTO security_associations (spi,sa_state,est,ast,arc_len,arc,arcw_len,arcw,tfvn,scid,vcid,mapid) -VALUES (1,0,0,0,1,X'0000000000000000000000000000000000000000',1,X'05',0,3,0,0); +VALUES (1,0,0,0,1,X'0000000000000000000000000000000000000000',1,5,0,3,0,0); -- SA 2 - OPERATIONAL; ARCW:5; AES-GCM; IV:00...01; IV-len:12; MAC-len:16; Key-ID: 130, SCID 44, VC-0 INSERT INTO security_associations (spi,ekid,sa_state,est,ast,shivf_len,iv_len,iv,abm_len,abm,arcw_len,arcw,arc_len,tfvn,scid,vcid,mapid) --- VALUES (2,130,3,1,1,12,12,X'000000000000000000000001',20,X'0000000000000000000000000000000000000000',1,X'05',11,0,44,0,0); -VALUES (2,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,X'05',0,0,44,0,0); +-- VALUES (2,130,3,1,1,12,12,X'000000000000000000000001',20,X'0000000000000000000000000000000000000000',1,5,11,0,44,0,0); +VALUES (2,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,5,0,0,44,0,0); -- SA 3 - OPERATIONAL; ARCW:5; AES-GCM; IV:00...01; IV-len:12; MAC-len:16; Key-ID: 130, SCID 44, VC-1 INSERT INTO security_associations (spi,ekid,sa_state,est,ast,shivf_len,iv_len,iv,abm_len,abm,arcw_len,arcw,arc_len,tfvn,scid,vcid,mapid) -VALUES (3,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,X'05',0,0,44,1,0); +VALUES (3,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,5,0,0,44,1,0); -- SA 4 - OPERATIONAL; ARCW:5; AES-GCM; IV:00...01; IV-len:12; MAC-len:16; Key-ID: 130, SCID 44, VC-2 INSERT INTO security_associations (spi,ekid,sa_state,est,ast,shivf_len,iv_len,iv,abm_len,abm,arcw_len,arcw,arc_len,tfvn,scid,vcid,mapid) -VALUES (4,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,X'05',0,0,44,2,0); +VALUES (4,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,5,0,0,44,2,0); -- SA 5 - OPERATIONAL; ARCW:5; AES-GCM; IV:00...01; IV-len:12; MAC-len:16; Key-ID: 130, SCID 44, VC-3 INSERT INTO security_associations (spi,ekid,sa_state,est,ast,shivf_len,iv_len,iv,abm_len,abm,arcw_len,arcw,arc_len,tfvn,scid,vcid,mapid) -VALUES (4,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,X'05',0,0,44,3,0); \ No newline at end of file +VALUES (4,130,3,1,0,12,12,X'000000000000000000000001',19,X'00000000000000000000000000000000000000',1,5,0,0,44,3,0); \ No newline at end of file diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index b1ae3229..d0a3fb19 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -107,6 +107,7 @@ UTEST(ET_VALIDATION, AUTH_ENCRYPTION_TEST) { //Setup & Initialize CryptoLib Crypto_Init_Unit_Test(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); uint8* expected = NULL; long expected_length = 0; @@ -133,11 +134,11 @@ UTEST(ET_VALIDATION, AUTH_ENCRYPTION_TEST) return_val = Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); // Expose SA 1 for testing - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); // Deactive SA 1 test_association->sa_state = SA_NONE; // Expose SA 4 for testing - expose_sadb_get_sa_from_spi(4, &test_association); + sadb_routine->sadb_get_sa_from_spi(4,&test_association); test_association->arc_len = 0; test_association->gvcid_tc_blk.vcid=1; test_association->iv[11] = 1; @@ -171,6 +172,7 @@ UTEST(DT_VALIDATION, AUTH_DECRYPTION_TEST) { //Setup & Initialize CryptoLib Crypto_Init_Unit_Test(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); char *activate_sa4_h = "2003002000ff000100001880d2c9000e197f0b001b0004000400003040d95ea61a"; char *dec_test_ping_h = "2003043400FF00040000000000000000000000017E1D8EEA8D45CEBA17888E0CDCD747DC78E5F372F997F2A63AA5DFC168395DC987"; @@ -196,13 +198,13 @@ UTEST(DT_VALIDATION, AUTH_DECRYPTION_TEST) return_val = Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); ASSERT_EQ(return_val, CRYPTO_LIB_SUCCESS); // Expose SA 1 for testing - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); // Deactive SA 1 test_association->sa_state = SA_NONE; // Expose SA 4 for testing - expose_sadb_get_sa_from_spi(4, &test_association); + sadb_routine->sadb_get_sa_from_spi(4,&test_association); test_association->arc_len = 0; test_association->gvcid_tc_blk.vcid=1; test_association->iv[11] = 1; @@ -250,6 +252,8 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "ef9f9284cf599eac3b119905a7d18851e7e374cf63aea04358586b0f757670f8"; @@ -263,10 +267,10 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -314,6 +318,8 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "ef9f9284cf599eac3b119905a7d18851e7e374cf63aea04358586b0f757670f8"; @@ -331,13 +337,13 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); // Insert key into keyring of SA 9 hex_conversion(buffer_nist_key_h, &buffer_nist_key_b, &buffer_nist_key_len); memcpy(ek_ring[test_association->ekid].value, buffer_nist_key_b, buffer_nist_key_len); @@ -382,6 +388,8 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "e9ccd6eef27f740d1d5c70b187734e11e76a8ac0ad1702ff02180c5c1c9e5399"; @@ -395,10 +403,10 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -445,6 +453,8 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "e9ccd6eef27f740d1d5c70b187734e11e76a8ac0ad1702ff02180c5c1c9e5399"; @@ -462,10 +472,10 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -510,6 +520,8 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "7ecc9dcb3d5b413cadc3af7b7812758bd869295f8aaf611ba9935de76bd87013"; @@ -523,10 +535,10 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -573,6 +585,8 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "7ecc9dcb3d5b413cadc3af7b7812758bd869295f8aaf611ba9935de76bd87013"; @@ -590,10 +604,10 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -638,6 +652,8 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "a881373e248615e3d6576f5a5fb68883515ae72d6a2938e3a6f0b8dcb639c9c0"; @@ -651,10 +667,10 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -701,6 +717,8 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "a881373e248615e3d6576f5a5fb68883515ae72d6a2938e3a6f0b8dcb639c9c0"; @@ -718,10 +736,10 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -766,6 +784,8 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "84c90349539c2a7989cb24dfae5e4182382ae94ba717d385977017f74f0d87d6"; @@ -779,10 +799,10 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -829,6 +849,8 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "84c90349539c2a7989cb24dfae5e4182382ae94ba717d385977017f74f0d87d6"; @@ -846,10 +868,10 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->arc_len = 0; test_association->sa_state = SA_OPERATIONAL; @@ -904,6 +926,8 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; @@ -918,10 +942,10 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->ast = 1; test_association->est = 0; test_association->arc_len = 0; @@ -980,6 +1004,8 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors // NOTE: Added Transfer Frame header to the plaintext char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; @@ -993,10 +1019,10 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->ast = 1; test_association->est = 0; test_association->arc_len = 0; @@ -1053,6 +1079,8 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; char *buffer_nist_iv_h = "d79cf22d504cc793c3fb6c8a"; @@ -1076,10 +1104,10 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->ast = 1; test_association->est = 0; test_association->arc_len = 0; @@ -1154,6 +1182,8 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_DATA) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; char *buffer_nist_iv_h = "d79cf22d504cc793c3fb6c8a"; @@ -1176,10 +1206,10 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_DATA) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->ast = 1; test_association->est = 0; test_association->arc_len = 0; @@ -1244,6 +1274,8 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_MAC) Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + // NIST supplied vectors char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; char *buffer_nist_iv_h = "d79cf22d504cc793c3fb6c8a"; @@ -1266,10 +1298,10 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_MAC) SecurityAssociation_t* test_association = NULL; test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); // Deactivate SA 1 - expose_sadb_get_sa_from_spi(1,&test_association); + sadb_routine->sadb_get_sa_from_spi(1,&test_association); test_association->sa_state = SA_NONE; // Activate SA 9 - expose_sadb_get_sa_from_spi(9, &test_association); + sadb_routine->sadb_get_sa_from_spi(9,&test_association); test_association->ast = 1; test_association->est = 0; test_association->arc_len = 0; diff --git a/fsw/crypto_util/app/ut_tc_apply.c b/fsw/crypto_util/app/ut_tc_apply.c index 6082d889..133d62b9 100644 --- a/fsw/crypto_util/app/ut_tc_apply.c +++ b/fsw/crypto_util/app/ut_tc_apply.c @@ -1,222 +1,226 @@ -/* Copyright (C) 2009 - 2022 National Aeronautics and Space Administration. All Foreign Rights are Reserved to the U.S. Government. - - This software is provided "as is" without any warranty of any kind, either expressed, implied, or statutory, including, but not - limited to, any warranty that the software will conform to specifications, any implied warranties of merchantability, fitness - for a particular purpose, and freedom from infringement, and any warranty that the documentation will conform to the program, or - any warranty that the software will be error free. - - In no event shall NASA be liable for any damages, including, but not limited to direct, indirect, special or consequential damages, - arising out of, resulting from, or in any way connected with the software or its documentation, whether or not based upon warranty, - contract, tort or otherwise, and whether or not loss was sustained from, or arose out of the results of, or use of, the software, - documentation or services provided hereunder. - - ITC Team - NASA IV&V - jstar-development-team@mail.nasa.gov -*/ - -/** - * Unit Tests that macke use of TC_ApplySecurity function on the data. - **/ -#include "ut_tc_apply.h" -#include "utest.h" -#include "crypto.h" -#include "crypto_error.h" -#include "sadb_routine.h" - - -/** - * @brief Unit Test: No Crypto_Init() - * - * TC_ApplySecurity should reject functionality if the Crypto_Init() function has not been called. - **/ -UTEST(TC_APPLY_SECURITY, NO_CRYPTO_INIT) -{ - // No Crypto_Init(), but we still Configure It; - long buffer_size = 0; - char *raw_tc_sdls_ping_h = "20030015001880d2c70008197f0b00310000b1fe3128"; - uint8 *raw_tc_sdls_ping_b = NULL; - int raw_tc_sdls_ping_len = 0; - - hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_TRUE, 0x3F); - Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); - - uint8 *ptr_enc_frame = NULL; - uint16 enc_frame_len = 0; - int32 return_val = CRYPTO_LIB_ERROR; - - return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); - ASSERT_EQ(CRYPTO_LIB_ERR_NO_INIT, return_val); - free(raw_tc_sdls_ping_b); - Crypto_Shutdown(); -} - -/** - * @brief Unit Test: Nominal Case - * This should read a raw_tc_sdls_ping and continue down the "happy Path", finally returning CRYPTO_LIB_SUCCESS - **/ -UTEST(TC_APPLY_SECURITY, HAPPY_PATH_CLEAR) -{ - //Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); - char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; - uint8 *raw_tc_sdls_ping_b = NULL; - int raw_tc_sdls_ping_len = 0; - - hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); - - uint8 *ptr_enc_frame = NULL; - uint16 enc_frame_len = 0; - - int32 return_val = CRYPTO_LIB_ERROR; - - return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); - Crypto_Shutdown(); - free(raw_tc_sdls_ping_b); - free(ptr_enc_frame); - ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); -} - -/** - * @brief Unit Test: Nominal Encryption - **/ -UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) -{ - //Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); - char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; - uint8 *raw_tc_sdls_ping_b = NULL; - int raw_tc_sdls_ping_len = 0; - - hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); - - uint8 *ptr_enc_frame = NULL; - uint16 enc_frame_len = 0; - - int32 return_val = CRYPTO_LIB_ERROR; - - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); - expose_sadb_get_sa_from_spi(1,&test_association); - test_association->sa_state = SA_NONE; - expose_sadb_get_sa_from_spi(4,&test_association); - test_association->sa_state = SA_OPERATIONAL; - test_association->ast=0; - - return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); - Crypto_Shutdown(); - free(raw_tc_sdls_ping_b); - free(ptr_enc_frame); - ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); -} - -/** - * @brief Unit Test: Nominal Authorized Encryption - **/ -UTEST(TC_APPLY_SECURITY, HAPPY_PATH_AUTH_ENC) -{ - //Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); - char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; - uint8 *raw_tc_sdls_ping_b = NULL; - int raw_tc_sdls_ping_len = 0; - - hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); - - uint8 *ptr_enc_frame = NULL; - uint16 enc_frame_len = 0; - - int32 return_val = CRYPTO_LIB_ERROR; - - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); - expose_sadb_get_sa_from_spi(1,&test_association); - test_association->sa_state = SA_NONE; - expose_sadb_get_sa_from_spi(4,&test_association); - test_association->sa_state = SA_OPERATIONAL; - - return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); - Crypto_Shutdown(); - free(raw_tc_sdls_ping_b); - free(ptr_enc_frame); - ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); -} - -/** - * @brief Unit Test: Bad Spacecraft ID - * This should pass the flawed hex string, and return CRYPTO_LIB_ERR_INVALID_SCID - * Bad Space Craft ID. This should pass the flawed .dat file, and return MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND -**/ -UTEST(TC_APPLY_SECURITY, BAD_SPACE_CRAFT_ID) -{ - //Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); - char *raw_tc_sdls_ping_bad_scid_h = "20010015000080d2c70008197f0b00310000b1fe3128"; - uint8 *raw_tc_sdls_ping_bad_scid_b = NULL; - int raw_tc_sdls_ping_bad_scid_len = 0; - - hex_conversion(raw_tc_sdls_ping_bad_scid_h, &raw_tc_sdls_ping_bad_scid_b, &raw_tc_sdls_ping_bad_scid_len); - - uint8 *ptr_enc_frame = NULL; - uint16 enc_frame_len = 0; - - uint32 return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_bad_scid_b, raw_tc_sdls_ping_bad_scid_len, &ptr_enc_frame, &enc_frame_len); - free(raw_tc_sdls_ping_bad_scid_b); - free(ptr_enc_frame); - Crypto_Shutdown(); - ASSERT_EQ(MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND, return_val); -} - -/** - * @brief Unit Test: Bad Virtual Channel ID - * This will be passed a flawed hex string with an invalid virtual channel ID. CRYPTO_LIB_ERR_INVALID_VCID should be returned. - **/ -UTEST(TC_APPLY_SECURITY, BAD_VIRTUAL_CHANNEL_ID) -{ - //Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); - char *raw_tc_sdls_ping_bad_vcid_h = "20032015000080d2c70008197f0b00310000b1fe3128"; - uint8 *raw_tc_sdls_ping_bad_vcid_b = NULL; - int raw_tc_sdls_ping_bad_vcid_len = 0; - - hex_conversion(raw_tc_sdls_ping_bad_vcid_h, &raw_tc_sdls_ping_bad_vcid_b, &raw_tc_sdls_ping_bad_vcid_len); - - uint8 *ptr_enc_frame = NULL; - uint16 enc_frame_len = 0; - int32 return_val = CRYPTO_LIB_ERROR; - - return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_bad_vcid_b, raw_tc_sdls_ping_bad_vcid_len, &ptr_enc_frame, &enc_frame_len); - free(raw_tc_sdls_ping_bad_vcid_b); - free(ptr_enc_frame); - Crypto_Shutdown(); - ASSERT_EQ(MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND, return_val); -} - -/** - * @brief Unit Test: Null Buffer -> TC_ApplySecurity - * Tests how ApplySecurity function handles a null buffer. Should reject functionality, and return CRYPTO_LIB_ERR_NULL_BUFFER - **/ -UTEST(TC_APPLY_SECURITY, NULL_BUFFER) -{ - //Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); - long buffer_size = 0; - char *buffer = NULL; - uint16 buffer_size_i = (uint16) buffer_size; - - uint8 *ptr_enc_frame = NULL; - uint16 enc_frame_len = 0; - int32 return_val = -1; - - return_val = Crypto_TC_ApplySecurity(buffer, buffer_size_i, &ptr_enc_frame, &enc_frame_len); - - Crypto_Shutdown(); - ASSERT_EQ(CRYPTO_LIB_ERR_NULL_BUFFER, return_val); -} - -//TODO: -/* What should be returned if something goes wrong with Control Command Flag? - Should a NULL pointer be returned....The original pointer? - We need to decide on this functionality and write a test for this -*/ - -UTEST_MAIN(); +/* Copyright (C) 2009 - 2022 National Aeronautics and Space Administration. All Foreign Rights are Reserved to the U.S. Government. + + This software is provided "as is" without any warranty of any kind, either expressed, implied, or statutory, including, but not + limited to, any warranty that the software will conform to specifications, any implied warranties of merchantability, fitness + for a particular purpose, and freedom from infringement, and any warranty that the documentation will conform to the program, or + any warranty that the software will be error free. + + In no event shall NASA be liable for any damages, including, but not limited to direct, indirect, special or consequential damages, + arising out of, resulting from, or in any way connected with the software or its documentation, whether or not based upon warranty, + contract, tort or otherwise, and whether or not loss was sustained from, or arose out of the results of, or use of, the software, + documentation or services provided hereunder. + + ITC Team + NASA IV&V + jstar-development-team@mail.nasa.gov +*/ + +/** + * Unit Tests that macke use of TC_ApplySecurity function on the data. + **/ +#include "ut_tc_apply.h" +#include "utest.h" +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" + + +/** + * @brief Unit Test: No Crypto_Init() + * + * TC_ApplySecurity should reject functionality if the Crypto_Init() function has not been called. + **/ +UTEST(TC_APPLY_SECURITY, NO_CRYPTO_INIT) +{ + // No Crypto_Init(), but we still Configure It; + long buffer_size = 0; + char *raw_tc_sdls_ping_h = "20030015001880d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + + hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_TRUE, 0x3F); + Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + int32 return_val = CRYPTO_LIB_ERROR; + + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + ASSERT_EQ(CRYPTO_LIB_ERR_NO_INIT, return_val); + free(raw_tc_sdls_ping_b); + Crypto_Shutdown(); +} + +/** + * @brief Unit Test: Nominal Case + * This should read a raw_tc_sdls_ping and continue down the "happy Path", finally returning CRYPTO_LIB_SUCCESS + **/ +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_CLEAR) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + + hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + + int32 return_val = CRYPTO_LIB_ERROR; + + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Nominal Encryption + **/ +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + + int32 return_val = CRYPTO_LIB_ERROR; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); + //Expose the SADB Security Association for test edits. + sadb_routine->sadb_get_sa_from_spi(1,&test_association); + test_association->sa_state = SA_NONE; + sadb_routine->sadb_get_sa_from_spi(4,&test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast=0; + + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Nominal Authorized Encryption + **/ +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_AUTH_ENC) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char *raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + + int32 return_val = CRYPTO_LIB_ERROR; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(unsigned char)); + //Expose the SADB Security Association for test edits. + sadb_routine->sadb_get_sa_from_spi(1,&test_association); + test_association->sa_state = SA_NONE; + sadb_routine->sadb_get_sa_from_spi(4,&test_association); + test_association->sa_state = SA_OPERATIONAL; + + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Bad Spacecraft ID + * This should pass the flawed hex string, and return CRYPTO_LIB_ERR_INVALID_SCID + * Bad Space Craft ID. This should pass the flawed .dat file, and return MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND +**/ +UTEST(TC_APPLY_SECURITY, BAD_SPACE_CRAFT_ID) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char *raw_tc_sdls_ping_bad_scid_h = "20010015000080d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_bad_scid_b = NULL; + int raw_tc_sdls_ping_bad_scid_len = 0; + + hex_conversion(raw_tc_sdls_ping_bad_scid_h, &raw_tc_sdls_ping_bad_scid_b, &raw_tc_sdls_ping_bad_scid_len); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + + uint32 return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_bad_scid_b, raw_tc_sdls_ping_bad_scid_len, &ptr_enc_frame, &enc_frame_len); + free(raw_tc_sdls_ping_bad_scid_b); + free(ptr_enc_frame); + Crypto_Shutdown(); + ASSERT_EQ(MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND, return_val); +} + +/** + * @brief Unit Test: Bad Virtual Channel ID + * This will be passed a flawed hex string with an invalid virtual channel ID. CRYPTO_LIB_ERR_INVALID_VCID should be returned. + **/ +UTEST(TC_APPLY_SECURITY, BAD_VIRTUAL_CHANNEL_ID) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char *raw_tc_sdls_ping_bad_vcid_h = "20032015000080d2c70008197f0b00310000b1fe3128"; + uint8 *raw_tc_sdls_ping_bad_vcid_b = NULL; + int raw_tc_sdls_ping_bad_vcid_len = 0; + + hex_conversion(raw_tc_sdls_ping_bad_vcid_h, &raw_tc_sdls_ping_bad_vcid_b, &raw_tc_sdls_ping_bad_vcid_len); + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + int32 return_val = CRYPTO_LIB_ERROR; + + return_val = Crypto_TC_ApplySecurity(raw_tc_sdls_ping_bad_vcid_b, raw_tc_sdls_ping_bad_vcid_len, &ptr_enc_frame, &enc_frame_len); + free(raw_tc_sdls_ping_bad_vcid_b); + free(ptr_enc_frame); + Crypto_Shutdown(); + ASSERT_EQ(MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND, return_val); +} + +/** + * @brief Unit Test: Null Buffer -> TC_ApplySecurity + * Tests how ApplySecurity function handles a null buffer. Should reject functionality, and return CRYPTO_LIB_ERR_NULL_BUFFER + **/ +UTEST(TC_APPLY_SECURITY, NULL_BUFFER) +{ + //Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + long buffer_size = 0; + char *buffer = NULL; + uint16 buffer_size_i = (uint16) buffer_size; + + uint8 *ptr_enc_frame = NULL; + uint16 enc_frame_len = 0; + int32 return_val = -1; + + return_val = Crypto_TC_ApplySecurity(buffer, buffer_size_i, &ptr_enc_frame, &enc_frame_len); + + Crypto_Shutdown(); + ASSERT_EQ(CRYPTO_LIB_ERR_NULL_BUFFER, return_val); +} + +//TODO: +/* What should be returned if something goes wrong with Control Command Flag? + Should a NULL pointer be returned....The original pointer? + We need to decide on this functionality and write a test for this +*/ + +UTEST_MAIN(); diff --git a/fsw/public_inc/sadb_routine.h b/fsw/public_inc/sadb_routine.h index 528f5b99..838ecb04 100644 --- a/fsw/public_inc/sadb_routine.h +++ b/fsw/public_inc/sadb_routine.h @@ -47,10 +47,6 @@ typedef struct { } SadbRoutineStruct, *SadbRoutine; -#ifdef ENCTEST -int32 expose_sadb_get_sa_from_spi(uint16, SecurityAssociation_t**); -#endif - SadbRoutine get_sadb_routine_mariadb(void); SadbRoutine get_sadb_routine_inmemory(void); SadbRoutine init_parse_sadb_routine(char *); diff --git a/fsw/src/sadb_routine_inmemory.template.c b/fsw/src/sadb_routine_inmemory.template.c index a1727992..94b1d0c7 100644 --- a/fsw/src/sadb_routine_inmemory.template.c +++ b/fsw/src/sadb_routine_inmemory.template.c @@ -265,13 +265,6 @@ static int32 sadb_close(void) return OS_SUCCESS; } -#ifdef ENCTEST -int32 expose_sadb_get_sa_from_spi(uint16 spi, SecurityAssociation_t** security_association) -{ - sadb_get_sa_from_spi(spi, security_association); -} -#endif - /* ** Security Association Interaction Functions */ diff --git a/fsw/src_mysql/sadb_routine_mariadb.template.c b/fsw/src_mysql/sadb_routine_mariadb.template.c index 389fc6cb..592e354d 100644 --- a/fsw/src_mysql/sadb_routine_mariadb.template.c +++ b/fsw/src_mysql/sadb_routine_mariadb.template.c @@ -49,7 +49,7 @@ const static char* SQL_SADB_GET_SA_BY_GVCID = "SELECT spi,ekid,akid,sa_state,tfv " FROM security_associations WHERE tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d' AND sa_state='%d'"; const static char* SQL_SADB_UPDATE_IV_ARC_BY_SPI = "UPDATE security_associations" " SET iv=X'%s', arc=X'%s'" - " WHERE spi='%d'AND tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d'"; + " WHERE spi='%d' AND tfvn='%d' AND scid='%d' AND vcid='%d' AND mapid='%d'"; // sadb_routine mariaDB private helper functions static int32 parse_sa_from_mysql_query(char* query, SecurityAssociation_t** security_association); @@ -278,6 +278,7 @@ static char* convert_byte_array_to_hexstring(void* src_buffer, size_t buffer_len hexstr[i*2+0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; hexstr[i*2+1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA; } + hexstr[buffer_length*2] = '\0'; return hexstr; } From 43cea33c5d489de6dcc2c15a4be6ca3b0c2e4aee Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Wed, 15 Dec 2021 17:18:49 -0800 Subject: [PATCH 12/18] Add support for AEAD vs non-AEAD algorithms in Crypto_TC_ApplySecurity function --- fsw/public_inc/crypto_config.h | 3 + fsw/public_inc/crypto_error.h | 40 ++++++----- fsw/public_inc/crypto_structs.h | 4 +- fsw/src/crypto.c | 122 ++++++++++++++++++++++++++------ python/encryption_test.py | 2 +- 5 files changed, 127 insertions(+), 44 deletions(-) diff --git a/fsw/public_inc/crypto_config.h b/fsw/public_inc/crypto_config.h index 677a0da0..5bbcb017 100644 --- a/fsw/public_inc/crypto_config.h +++ b/fsw/public_inc/crypto_config.h @@ -175,4 +175,7 @@ // TC Behavior Defines #define TC_SDLS_EP_VCID 4 //VCID which has SDLS PDUs (JPL uses VCIDs to determine TC type, there is no space packet layer with APIDs). Set to -1 if uses SP APIDs. +// Logic Behavior Defines + #define CRYPTO_FALSE 0 + #define CRYPTO_TRUE 1 #endif \ No newline at end of file diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index f8961664..ee650b6a 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -26,24 +26,26 @@ #define MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND 104 -#define CRYPTO_LIB_SUCCESS (0) -#define CRYPTO_LIB_ERROR (-1) -#define CRYPTO_LIB_ERR_NO_INIT (-2) -#define CRYPTO_LIB_ERR_INVALID_TFVN (-3) -#define CRYPTO_LIB_ERR_INVALID_SCID (-4) -#define CRYPTO_LIB_ERR_INVALID_VCID (-5) -#define CRYPTO_LIB_ERR_INVALID_MAPID (-6) -#define CRYPTO_LIB_ERR_INVALID_CC_FLAG (-7) -#define CRYPTO_LIB_ERR_NO_OPERATIONAL_SA (-8) -#define CRYPTO_LIB_ERR_NULL_BUFFER (-9) -#define CRYPTO_LIB_ERR_UT_BYTE_MISMATCH (-10) -#define CRYPTO_LIB_ERR_NO_CONFIG (-11) -#define CRYPTO_LIB_ERR_INVALID_FECF (-12) -#define CRYPTO_LIB_ERR_BAD_ANTIREPLAY_WINDOW (-13) -#define CRYPTO_LIB_ERR_LIBGCRYPT_ERROR (-14) -#define CRYPTO_LIB_ERR_AUTHENTICATION_ERROR (-15) -#define CRYPTO_LIB_ERR_NULL_IV (-16) -#define CRYPTO_LIB_ERR_NULL_ABM (-17) -#define CRYPTO_LIB_ERR_DECRYPT_ERROR (-18) +#define CRYPTO_LIB_SUCCESS (0) +#define CRYPTO_LIB_ERROR (-1) +#define CRYPTO_LIB_ERR_NO_INIT (-2) +#define CRYPTO_LIB_ERR_INVALID_TFVN (-3) +#define CRYPTO_LIB_ERR_INVALID_SCID (-4) +#define CRYPTO_LIB_ERR_INVALID_VCID (-5) +#define CRYPTO_LIB_ERR_INVALID_MAPID (-6) +#define CRYPTO_LIB_ERR_INVALID_CC_FLAG (-7) +#define CRYPTO_LIB_ERR_NO_OPERATIONAL_SA (-8) +#define CRYPTO_LIB_ERR_NULL_BUFFER (-9) +#define CRYPTO_LIB_ERR_UT_BYTE_MISMATCH (-10) +#define CRYPTO_LIB_ERR_NO_CONFIG (-11) +#define CRYPTO_LIB_ERR_INVALID_FECF (-12) +#define CRYPTO_LIB_ERR_BAD_ANTIREPLAY_WINDOW (-13) +#define CRYPTO_LIB_ERR_LIBGCRYPT_ERROR (-14) +#define CRYPTO_LIB_ERR_AUTHENTICATION_ERROR (-15) +#define CRYPTO_LIB_ERR_NULL_IV (-16) +#define CRYPTO_LIB_ERR_NULL_ABM (-17) +#define CRYPTO_LIB_ERR_DECRYPT_ERROR (-18) +#define CRYPTO_LIB_ERR_ABM_TOO_SHORT_FOR_AAD (-19) +#define CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR (-20) #endif //_crypto_error_h_ diff --git a/fsw/public_inc/crypto_structs.h b/fsw/public_inc/crypto_structs.h index d820ca8d..eeaacd81 100644 --- a/fsw/public_inc/crypto_structs.h +++ b/fsw/public_inc/crypto_structs.h @@ -68,7 +68,7 @@ typedef struct uint8 stmacf_len:8; // Sec. Trailer MAC Field Length uint8 ecs_len :8; // Encryption Cipher Suite Length uint8 ecs[ECS_SIZE]; // Encryption Cipher Suite (algorithm / mode ID) - uint8* iv; // Initialization Vector + uint8* iv; // Initialization Vector uint8 acs_len :8; // Authentication Cipher Suite Length uint8 acs :8; // Authentication Cipher Suite (algorithm / mode ID) uint16 abm_len :16; // Authentication Bit Mask Length @@ -76,7 +76,7 @@ typedef struct uint8 arc_len :8; // Anti-Replay Counter Length uint8* arc; // Anti-Replay Counter uint8 arcw_len:8; // Anti-Replay Counter Window Length - uint16 arcw; // Anti-Replay Counter Window + uint16 arcw; // Anti-Replay Counter Window } SecurityAssociation_t; #define SA_SIZE (sizeof(SecurityAssociation_t)) diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index d5a5ebba..c47837d6 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -53,6 +53,8 @@ static SadbRoutine sadb_routine = NULL; // Assisting Functions static int32 Crypto_Get_tcPayloadLength(TC_t* tc_frame, SecurityAssociation_t *sa_ptr); static int32 Crypto_Get_tmLength(int len); +static uint8 Crypto_Is_AEAD_Algorithm(uint32 cipher_suite_id); +static uint8* Crypto_Prepare_TC_AAD(uint8* buffer, uint16 len_aad, uint8* abm_buffer); static void Crypto_TM_updatePDU(char* ingest, int len_ingest); static void Crypto_TM_updateOCF(void); static void Crypto_Local_Config(void); @@ -961,6 +963,48 @@ static int32 Crypto_Get_tmLength(int len) return len; } +/** + * @brief Function: Crypto_Is_AEAD_Algorithm + * Looks up cipher suite ID and determines if it's an AEAD algorithm. Returns 1 if true, 0 if false; + * @param cipher_suite_id: uint32 + **/ +static uint8 Crypto_Is_AEAD_Algorithm(uint32 cipher_suite_id) +{ + //CryptoLib only supports AES-GCM, which is an AEAD (Authenticated Encryption with Associated Data) algorithm, so return true/1. + //TODO - Add cipher suite mapping to which algorithms are AEAD and which are not. + return CRYPTO_TRUE; +} + +/** + * @brief Function: Crypto_Prepare_TC_AAD + * Callocs and returns pointer to buffer where AAD is created & bitwise-anded with bitmask! + * Note: Function caller is responsible for freeing the returned buffer! + * @param buffer: uint8* + * @param len_aad: uint16 + * @param abm_buffer: uint8* + **/ +static uint8* Crypto_Prepare_TC_AAD(uint8* buffer, uint16 len_aad, uint8* abm_buffer) +{ + uint8* aad = (uint8*) calloc(1,len_aad*sizeof(uint8)); + + for (int i = 0; i < len_aad; i++) + { + aad[i] = buffer[i] & abm_buffer[i]; + } + + #ifdef MAC_DEBUG + OS_printf(KYEL "Preparing AAD:\n"); + OS_printf("\tUsing AAD Length of %d\n\t", len_aad); + for (int i = 0; i < len_aad; i++) + { + OS_printf("%02x", aad[i]); + } + OS_printf("\n" RESET); + #endif + + return aad; +} + /** * @brief Function: Crypto_TM_updatePDU * Update the Telemetry Payload Data Unit @@ -2554,7 +2598,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len uint16 mac_loc = 0; uint16 tf_payload_len = 0x0000; uint16 new_fecf = 0x0000; - uint8 aad[20]; + uint8* aad; gcry_cipher_hd_t tmp_hd; gcry_error_t gcry_error = GPG_ERR_NO_ERROR; uint16 new_enc_frame_header_field_length = 0; @@ -2636,6 +2680,9 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len status = sadb_routine->sadb_get_operational_sa_from_gvcid(temp_tc_header.tfvn, temp_tc_header.scid, temp_tc_header.vcid, map_id,&sa_ptr); } + uint32 encryption_cipher = (sa_ptr->ecs[0] << 3) | (sa_ptr->ecs[1] << 2) | (sa_ptr->ecs[2] << 1) | sa_ptr->ecs[3]; + uint8 ecs_is_aead_algorithm = Crypto_Is_AEAD_Algorithm(encryption_cipher); + // If unable to get operational SA, can return if (status != CRYPTO_LIB_SUCCESS) { @@ -2935,6 +2982,30 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len OS_printf("\n"); #endif + if(sa_service_type == SA_AUTHENTICATED_ENCRYPTION && ecs_is_aead_algorithm==CRYPTO_TRUE) // Algorithm is AEAD algorithm, Add AAD before encrypt! + { + //Prepare the Header AAD (CCSDS 335.0-B-1 4.2.3.2.2.3) + uint16 aad_len = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len; + if(sa_ptr->abm_len < aad_len) { return CRYPTO_LIB_ERR_ABM_TOO_SHORT_FOR_AAD; } + aad = Crypto_Prepare_TC_AAD(p_new_enc_frame, aad_len, sa_ptr->abm); + + //Add the AAD to the libgcrypt cipher handle + gcry_error = gcry_cipher_authenticate( + tmp_hd, + aad, // additional authenticated data + aad_len // length of AAD + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_authenticate error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + OS_printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error),gcry_strerror (gcry_error)); + status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; + return status; + } + + free(aad); + } + gcry_error = gcry_cipher_encrypt( tmp_hd, &p_new_enc_frame[index], // ciphertext output @@ -2961,13 +3032,34 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len OS_printf("\n"); #endif + //Get MAC & insert into p_new_enc_frame + if(sa_service_type == SA_AUTHENTICATED_ENCRYPTION && ecs_is_aead_algorithm==CRYPTO_TRUE) + { + mac_loc = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + tf_payload_len; + #ifdef MAC_DEBUG + OS_printf(KYEL "MAC location is: %d\n" RESET, mac_loc); + OS_printf(KYEL "MAC size is: %d\n" RESET, MAC_SIZE); + #endif + gcry_error = gcry_cipher_gettag( + tmp_hd, + &p_new_enc_frame[mac_loc], // tag output + MAC_SIZE // tag size // TODO - use sa_ptr->abm_len instead of hardcoded mac size? + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_checktag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + status = CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR; + return status; + } + } + // Close cipher, so we can authenticate encrypted data gcry_cipher_close(tmp_hd); } // Prepare additional authenticated data, if needed if ((sa_service_type == SA_AUTHENTICATION) || \ - (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) + ( (sa_service_type == SA_AUTHENTICATED_ENCRYPTION) && ecs_is_aead_algorithm==CRYPTO_FALSE ) ) //Authenticated Encryption without AEAD algorithm, AEAD algorithms handled in encryption block! { gcry_error = gcry_cipher_open( &(tmp_hd), @@ -3004,27 +3096,14 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len return status; } - uint16 bit_masked_data_len = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + tf_payload_len; - uint8 bit_masked_data[bit_masked_data_len]; - - for (int y = 0; y < bit_masked_data_len; y++) - { - bit_masked_data[y] = p_new_enc_frame[y] & *(sa_ptr->abm + y); - } - #ifdef MAC_DEBUG - OS_printf(KYEL "Preparing Header AAD:\n"); - OS_printf("\tUsing Header AAD Length of %d\n\t", bit_masked_data_len); - for (int y = 0; y < bit_masked_data_len; y++) - { - OS_printf("%02x", bit_masked_data[y]); - } - OS_printf("\n" RESET); - #endif + uint16 aad_len = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + tf_payload_len; + if(sa_ptr->abm_len < aad_len) { return CRYPTO_LIB_ERR_ABM_TOO_SHORT_FOR_AAD; } + aad = Crypto_Prepare_TC_AAD(p_new_enc_frame, aad_len, sa_ptr->abm); gcry_error = gcry_cipher_authenticate( tmp_hd, - bit_masked_data, // additional authenticated data - bit_masked_data_len // length of AAD + aad, // additional authenticated data + aad_len // length of AAD ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { @@ -3034,7 +3113,6 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len return status; } - // TODO - Know if FECF exists mac_loc = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + tf_payload_len; #ifdef MAC_DEBUG OS_printf(KYEL "MAC location is: %d\n" RESET, mac_loc); @@ -3048,7 +3126,7 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { OS_printf(KRED "ERROR: gcry_cipher_checktag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = OS_ERROR; + status = CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR; return status; } // Zeroise any sensitive information diff --git a/python/encryption_test.py b/python/encryption_test.py index bcf75dab..f430331e 100644 --- a/python/encryption_test.py +++ b/python/encryption_test.py @@ -34,7 +34,7 @@ def __init__(self): # Function: Encrypt # Encrypts data - given a key, iv, header, and bitmask def encrypt(self, data, key, iv, header, bitmask): - hex_header = header + iv + data # Combines Header and IV (AAD) + hex_header = header + iv # Combines Header and IV (AAD) bitmask_b = bytes.fromhex(bitmask) header_b = bytes.fromhex(hex_header) key_b = bytes.fromhex(key) From 64c836f2764d51c8f6a1e504601b64a4a2f4405a Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Wed, 15 Dec 2021 19:42:16 -0800 Subject: [PATCH 13/18] Update ProcessSecurity to support AEAD and authentication only auth modes --- fsw/crypto_util/app/et_dt_validation.c | 16 +-- fsw/public_inc/crypto_error.h | 2 + fsw/src/crypto.c | 135 +++++++++++++------------ 3 files changed, 79 insertions(+), 74 deletions(-) diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index d0a3fb19..1d4aa209 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -196,7 +196,7 @@ UTEST(DT_VALIDATION, AUTH_DECRYPTION_TEST) // Ensure that Process Security can activate SA 4 return_val = Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); - ASSERT_EQ(return_val, CRYPTO_LIB_SUCCESS); + ASSERT_EQ(CRYPTO_LIB_SUCCESS,return_val); // Expose SA 1 for testing sadb_routine->sadb_get_sa_from_spi(1,&test_association); @@ -212,7 +212,8 @@ UTEST(DT_VALIDATION, AUTH_DECRYPTION_TEST) test_association->est = 1; test_association->sa_state = SA_OPERATIONAL; - Crypto_TC_ProcessSecurity(dec_test_ping_b, &dec_test_ping_len, tc_sdls_processed_frame); + return_val = Crypto_TC_ProcessSecurity(dec_test_ping_b, &dec_test_ping_len, tc_sdls_processed_frame); + ASSERT_EQ(9,return_val); // 9 is the number of pings in that EP PDU. Crypto_Shutdown(); @@ -950,7 +951,7 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) test_association->est = 0; test_association->arc_len = 0; test_association->shivf_len = 12; - test_association->abm_len = 19; + test_association->abm_len = 1024; test_association->stmacf_len = 16; test_association->sa_state = SA_OPERATIONAL; @@ -1027,7 +1028,7 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) test_association->est = 0; test_association->arc_len = 0; test_association->shivf_len = 12; - test_association->abm_len = 19; + test_association->abm_len = 1024; memset(test_association->abm, 0xFF, (test_association->abm_len*sizeof(unsigned char))); // Bitmask test_association->stmacf_len = 16; test_association->sa_state = SA_OPERATIONAL; @@ -1084,14 +1085,13 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) // NIST supplied vectors char *buffer_nist_key_h = "78dc4e0aaf52d935c3c01eea57428f00ca1fd475f5da86a49c8dd73d68c8e223"; char *buffer_nist_iv_h = "d79cf22d504cc793c3fb6c8a"; - //char *buffer_cyber_chef_mac_h = "99eff39be8327e6950f03a329209d577"; char *buffer_cyber_chef_mac_h = "34d0e323f5e4b80426401d4aa37930da"; char *buffer_nist_pt_h = "722ee47da4b77424733546c2d400c4e5"; // Create a MAC'd frame by adding our headers and a fecf // | Header | SPI | iv | plaintext | mac |fecf| char *buffer_nist_mac_frame_h = "2003003500FF0009D79CF22D504CC793C3FB6C8A722ee47da4b77424733546c2d400c4e534d0e323f5e4b80426401d4aa37930daf55f"; - + uint8 *buffer_nist_iv_b, *buffer_nist_pt_b, *buffer_nist_key_b, *buffer_cyber_chef_mac_b , *buffer_nist_mac_frame_b, *buffer_nist_cp_b = NULL; int buffer_nist_iv_len, buffer_nist_pt_len, buffer_nist_key_len, buffer_cyber_chef_mac_len , buffer_nist_mac_frame_len, buffer_nist_cp_len = 0; @@ -1254,7 +1254,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_DATA) // #endif Crypto_Shutdown(); - ASSERT_EQ(CRYPTO_LIB_ERR_AUTHENTICATION_ERROR, status); + ASSERT_EQ(CRYPTO_LIB_ERR_MAC_VALIDATION_ERROR, status); free(buffer_nist_iv_b); free(buffer_nist_key_b); free(buffer_cyber_chef_mac_b); @@ -1346,7 +1346,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_MAC) // #endif Crypto_Shutdown(); - ASSERT_EQ(CRYPTO_LIB_ERR_AUTHENTICATION_ERROR, status); + ASSERT_EQ(CRYPTO_LIB_ERR_MAC_VALIDATION_ERROR, status); free(buffer_nist_iv_b); free(buffer_nist_key_b); free(buffer_cyber_chef_mac_b); diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index ee650b6a..cc9ad946 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -47,5 +47,7 @@ #define CRYPTO_LIB_ERR_DECRYPT_ERROR (-18) #define CRYPTO_LIB_ERR_ABM_TOO_SHORT_FOR_AAD (-19) #define CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR (-20) +#define CRYPTO_LIB_ERR_MAC_VALIDATION_ERROR (-21) + #endif //_crypto_error_h_ diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index c47837d6..a8014cc4 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -2275,7 +2275,7 @@ static int32 Crypto_User_ModifyVCID(void) **/ static int32 Crypto_PDU(char* ingest,TC_t* tc_frame) { - int32 status = OS_SUCCESS; + int32 status = CRYPTO_LIB_SUCCESS; switch (sdls_frame.pdu.type) { @@ -2602,6 +2602,8 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len gcry_cipher_hd_t tmp_hd; gcry_error_t gcry_error = GPG_ERR_NO_ERROR; uint16 new_enc_frame_header_field_length = 0; + uint32 encryption_cipher; + uint8 ecs_is_aead_algorithm; #ifdef DEBUG OS_printf(KYEL "\n----- Crypto_TC_ApplySecurity START -----\n" RESET); @@ -2680,9 +2682,6 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len status = sadb_routine->sadb_get_operational_sa_from_gvcid(temp_tc_header.tfvn, temp_tc_header.scid, temp_tc_header.vcid, map_id,&sa_ptr); } - uint32 encryption_cipher = (sa_ptr->ecs[0] << 3) | (sa_ptr->ecs[1] << 2) | (sa_ptr->ecs[2] << 1) | sa_ptr->ecs[3]; - uint8 ecs_is_aead_algorithm = Crypto_Is_AEAD_Algorithm(encryption_cipher); - // If unable to get operational SA, can return if (status != CRYPTO_LIB_SUCCESS) { @@ -2720,6 +2719,13 @@ int32 Crypto_TC_ApplySecurity(const uint8* p_in_frame, const uint16 in_frame_len return status; } + // Determine Algorithm cipher & mode. // TODO - Parse authentication_cipher, and handle AEAD cases properly + if(sa_service_type != SA_PLAINTEXT) + { + encryption_cipher = (sa_ptr->ecs[0] << 24) | (sa_ptr->ecs[1] << 16) | (sa_ptr->ecs[2] << 8) | sa_ptr->ecs[3]; + ecs_is_aead_algorithm = Crypto_Is_AEAD_Algorithm(encryption_cipher); + } + #ifdef TC_DEBUG switch(sa_service_type) { @@ -3213,6 +3219,8 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro SecurityAssociation_t* sa_ptr = NULL; uint8 sa_service_type = -1; uint8* aad; + uint32 encryption_cipher; + uint8 ecs_is_aead_algorithm; if(crypto_config == NULL) { @@ -3268,6 +3276,9 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro return status; } + encryption_cipher = (sa_ptr->ecs[0] << 24) | (sa_ptr->ecs[1] << 16) | (sa_ptr->ecs[2] << 8) | sa_ptr->ecs[3]; + ecs_is_aead_algorithm = Crypto_Is_AEAD_Algorithm(encryption_cipher); + // Determine SA Service Type if ((sa_ptr->est == 0) && (sa_ptr->ast == 0)) { @@ -3294,6 +3305,13 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro return status; } + // Determine Algorithm cipher & mode. // TODO - Parse authentication_cipher, and handle AEAD cases properly + if(sa_service_type != SA_PLAINTEXT) + { + encryption_cipher = (sa_ptr->ecs[0] << 24) | (sa_ptr->ecs[1] << 16) | (sa_ptr->ecs[2] << 8) | sa_ptr->ecs[3]; + ecs_is_aead_algorithm = Crypto_Is_AEAD_Algorithm(encryption_cipher); + } + #ifdef TC_DEBUG switch(sa_service_type) { @@ -3431,26 +3449,14 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro } - aad = (uint8*)malloc(tc_mac_start_index * sizeof(uint8)); - // Prepare additional authenticated data (AAD) - #ifdef DEBUG - OS_printf(KYEL "AAD: \n\t" RESET); - #endif - for (y = 0; y < tc_mac_start_index; y++) - { - aad[y] = (uint8) ((uint8)ingest[y] & (uint8) *(sa_ptr->abm + y)); - #ifdef DEBUG - OS_printf("%02x", aad[y]); - #endif - } - #ifdef DEBUG - OS_printf("\n"); - #endif + uint16 aad_len = tc_mac_start_index; + if((sa_service_type == SA_AUTHENTICATED_ENCRYPTION) && (ecs_is_aead_algorithm == CRYPTO_TRUE)) { aad_len = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len; } + aad = Crypto_Prepare_TC_AAD(ingest, aad_len, sa_ptr->abm); gcry_error = gcry_cipher_authenticate( tmp_hd, aad, // additional authenticated data - tc_mac_start_index // length of AAD + aad_len // length of AAD ); if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { @@ -3459,65 +3465,62 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; return status; } - - uint8* calculated_mac = malloc(sa_ptr->stmacf_len * sizeof(uint8)); - gcry_error = gcry_cipher_gettag( - tmp_hd, - calculated_mac, // tag output - sa_ptr->stmacf_len // tag size - ); - if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) - { - OS_printf(KRED "ERROR: gcry_cipher_checktag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); - status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; - return status; - } - - #ifdef DEBUG - OS_printf(KRED "Expected: \n\t" RESET); - for(int i=0; istmacf_len; i++) - { - printf("%02x", calculated_mac[i]); - } - OS_printf(KRED "\nActual: \n\t" RESET); - for(int i=0; istmacf_len; i++) - { - printf("%02x", tc_sdls_processed_frame->tc_sec_trailer.mac[i]); - } - printf("\n"); - #endif - - for(int i=0; istmacf_len; i++) - { - if (calculated_mac[i] != tc_sdls_processed_frame->tc_sec_trailer.mac[i]) - { - status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; - OS_printf(KRED "ERROR: MAC Validation Error.\n" RESET); - return status; - } - } } // Decrypt, if applicable if((sa_service_type == SA_ENCRYPTION) || - (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) + (sa_service_type == SA_AUTHENTICATED_ENCRYPTION) || + (sa_service_type == SA_AUTHENTICATION)) { uint16 tc_enc_payload_start_index = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len; tc_sdls_processed_frame->tc_pdu_len = tc_sdls_processed_frame->tc_header.fl + 1 - tc_enc_payload_start_index - sa_ptr->stmacf_len - fecf_len; - - gcry_error = gcry_cipher_decrypt( - tmp_hd, - tc_sdls_processed_frame->tc_pdu, // plaintext output - tc_sdls_processed_frame->tc_pdu_len, // length of data - &(ingest[tc_enc_payload_start_index]), // ciphertext input - tc_sdls_processed_frame->tc_pdu_len // in data length - ); + + if(sa_service_type == SA_AUTHENTICATION) + {//Authenticate only! No input data passed into decryption function, only AAD. + gcry_error = gcry_cipher_decrypt( + tmp_hd, + NULL, // plaintext output + 0, // length of data + NULL, // ciphertext input + 0 // in data length + ); + //If authentication only, don't decrypt the data. Just pass the data PDU through. + memcpy(tc_sdls_processed_frame->tc_pdu,&(ingest[tc_enc_payload_start_index]),tc_sdls_processed_frame->tc_pdu_len); + } else + { // Decrypt + gcry_error = gcry_cipher_decrypt( + tmp_hd, + tc_sdls_processed_frame->tc_pdu, // plaintext output + tc_sdls_processed_frame->tc_pdu_len, // length of data + &(ingest[tc_enc_payload_start_index]), // ciphertext input + tc_sdls_processed_frame->tc_pdu_len // in data length + ); + } if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { OS_printf(KRED "ERROR: gcry_cipher_decrypt error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); status = CRYPTO_LIB_ERR_DECRYPT_ERROR; return status; } + + if ((sa_service_type == SA_AUTHENTICATED_ENCRYPTION) || + (sa_service_type == SA_AUTHENTICATION)) + { + + gcry_error = gcry_cipher_checktag( + tmp_hd, + tc_sdls_processed_frame->tc_sec_trailer.mac, // Frame Expected Tag + sa_ptr->stmacf_len // tag size + ); + if((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + OS_printf(KRED "ERROR: gcry_cipher_checktag error code %d\n" RESET,gcry_error & GPG_ERR_CODE_MASK); + fprintf(stderr,"gcry_cipher_decrypt failed: %s\n", gpg_strerror (gcry_error)); + status = CRYPTO_LIB_ERR_MAC_VALIDATION_ERROR; + return status; + } + } + } if(sa_service_type != SA_PLAINTEXT) From a5a4af7ddd7f8feee06bcfd59b2916a9060e54b1 Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Wed, 15 Dec 2021 19:50:16 -0800 Subject: [PATCH 14/18] Commit new check fecf configuration parameter throughout code & tests --- fsw/crypto_util/app/et_dt_validation.c | 30 +++++++++++++------------- fsw/public_inc/crypto.h | 2 +- fsw/src/crypto.c | 6 ++++-- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index 1d4aa209..b0396c98 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -249,7 +249,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib //Crypto_Init_Unit_Test(); - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); @@ -315,7 +315,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); @@ -385,7 +385,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); @@ -450,7 +450,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); @@ -517,7 +517,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); @@ -582,7 +582,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_2) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); @@ -649,7 +649,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); @@ -714,7 +714,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_3) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); @@ -781,7 +781,7 @@ UTEST(NIST_ENC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); @@ -846,7 +846,7 @@ UTEST(NIST_DEC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_4) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); @@ -923,7 +923,7 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); @@ -1001,7 +1001,7 @@ UTEST(NIST_ENC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_1) uint8 *ptr_enc_frame = NULL; uint16 enc_frame_len = 0; // Setup & Initialize CryptoLib - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_NO_SEGMENT_HDRS); Crypto_Init(); @@ -1076,7 +1076,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0) // Setup & Initialize CryptoLib uint16 enc_frame_len = 0; int32 status; - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); @@ -1178,7 +1178,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_DATA) // Setup & Initialize CryptoLib uint16 enc_frame_len = 0; int32 status; - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); @@ -1270,7 +1270,7 @@ UTEST(NIST_DEC_MAC_VALIDATION, AES_GCM_256_IV_96_PT_128_TEST_0_BAD_MAC) // Setup & Initialize CryptoLib uint16 enc_frame_len = 0; int32 status; - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Init(); diff --git a/fsw/public_inc/crypto.h b/fsw/public_inc/crypto.h index 65113223..bf749055 100644 --- a/fsw/public_inc/crypto.h +++ b/fsw/public_inc/crypto.h @@ -41,7 +41,7 @@ */ // Crypto Library Configuration functions -extern int32 Crypto_Config_CryptoLib(uint8 sadb_type, uint8 crypto_create_fecf, uint8 process_sdls_pdus, uint8 has_pus_hdr, uint8 ignore_sa_state, uint8 ignore_anti_replay, uint8 unique_sa_per_mapid, uint8 vcid_bitmask); +extern int32 Crypto_Config_CryptoLib(uint8 sadb_type, uint8 crypto_create_fecf, uint8 process_sdls_pdus, uint8 has_pus_hdr, uint8 ignore_sa_state, uint8 ignore_anti_replay, uint8 unique_sa_per_mapid, uint8 crypto_check_fecf, uint8 vcid_bitmask); extern int32 Crypto_Config_MariaDB(char* mysql_username, char* mysql_password, char* mysql_hostname, char* mysql_database, uint16 mysql_port); extern int32 Crypto_Config_Add_Gvcid_Managed_Parameter(uint8 tfvn, uint16 scid, uint8 vcid, uint8 has_fecf, uint8 has_segmentation_hdr); diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index a8014cc4..fe957a8d 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -137,7 +137,7 @@ static uint16 crc16Table[256]; int32 Crypto_Init_Unit_Test(void) { int32 status = OS_SUCCESS; - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,1,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); status = Crypto_Init(); @@ -266,10 +266,11 @@ int32 Crypto_Shutdown(void) * @param ignore_sa_state: uint8 * @param ignore_anti_replay: uint8 * @param unique_sa_per_mapid: uint8 +* @param crypto_check_fecf: uint8 * @param vcid_bitmask: uint8 * @return int32: Success/Failure **/ -int32 Crypto_Config_CryptoLib(uint8 sadb_type, uint8 crypto_create_fecf, uint8 process_sdls_pdus, uint8 has_pus_hdr, uint8 ignore_sa_state, uint8 ignore_anti_replay, uint8 unique_sa_per_mapid, uint8 vcid_bitmask) +int32 Crypto_Config_CryptoLib(uint8 sadb_type, uint8 crypto_create_fecf, uint8 process_sdls_pdus, uint8 has_pus_hdr, uint8 ignore_sa_state, uint8 ignore_anti_replay, uint8 unique_sa_per_mapid,uint8 crypto_check_fecf, uint8 vcid_bitmask) { int32 status = OS_SUCCESS; crypto_config = (CryptoConfig_t*) calloc(1, CRYPTO_CONFIG_SIZE); @@ -280,6 +281,7 @@ int32 Crypto_Config_CryptoLib(uint8 sadb_type, uint8 crypto_create_fecf, uint8 p crypto_config->ignore_sa_state=ignore_sa_state; crypto_config->ignore_anti_replay=ignore_anti_replay; crypto_config->unique_sa_per_mapid = unique_sa_per_mapid; + crypto_config->crypto_check_fecf = crypto_check_fecf; crypto_config->vcid_bitmask=vcid_bitmask; return status; } From db441f205d4773324a86170d7c30039fabb23c0f Mon Sep 17 00:00:00 2001 From: Ibraheem Saleh Date: Wed, 15 Dec 2021 20:42:00 -0800 Subject: [PATCH 15/18] Update SADB MariaDB implementation to support variable IV, ABM, and ARC fields --- fsw/crypto_util/app/ut_tc_apply.c | 2 +- fsw/public_inc/crypto_error.h | 1 + fsw/src_mysql/sadb_routine_mariadb.template.c | 23 +++++++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/fsw/crypto_util/app/ut_tc_apply.c b/fsw/crypto_util/app/ut_tc_apply.c index 133d62b9..47f4329d 100644 --- a/fsw/crypto_util/app/ut_tc_apply.c +++ b/fsw/crypto_util/app/ut_tc_apply.c @@ -39,7 +39,7 @@ UTEST(TC_APPLY_SECURITY, NO_CRYPTO_INIT) int raw_tc_sdls_ping_len = 0; hex_conversion(raw_tc_sdls_ping_h, &raw_tc_sdls_ping_b, &raw_tc_sdls_ping_len); - Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_TRUE, 0x3F); + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY,CRYPTO_TC_CREATE_FECF_TRUE,TC_PROCESS_SDLS_PDUS_TRUE,TC_HAS_PUS_HDR,TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_FALSE, TC_UNIQUE_SA_PER_MAP_ID_TRUE, TC_CHECK_FECF_TRUE, 0x3F); Crypto_Config_Add_Gvcid_Managed_Parameter(0,0x0003,0,TC_HAS_FECF,TC_HAS_SEGMENT_HDRS); uint8 *ptr_enc_frame = NULL; diff --git a/fsw/public_inc/crypto_error.h b/fsw/public_inc/crypto_error.h index cc9ad946..04510b0a 100644 --- a/fsw/public_inc/crypto_error.h +++ b/fsw/public_inc/crypto_error.h @@ -20,6 +20,7 @@ #include "sadb_mariadb_error.h" #define SADB_INVALID_SADB_TYPE 201 +#define SADB_NULL_SA_USED 202 #define CRYPTO_CONFIGURATION_NOT_COMPLETE 101 #define CRYPTO_MANAGED_PARAM_CONFIGURATION_NOT_COMPLETE 102 #define CRYPTO_MARIADB_CONFIGURATION_NOT_COMPLETE 103 diff --git a/fsw/src_mysql/sadb_routine_mariadb.template.c b/fsw/src_mysql/sadb_routine_mariadb.template.c index 592e354d..13acbec5 100644 --- a/fsw/src_mysql/sadb_routine_mariadb.template.c +++ b/fsw/src_mysql/sadb_routine_mariadb.template.c @@ -136,6 +136,7 @@ static int32 sadb_get_operational_sa_from_gvcid(uint8 tfvn,uint16 scid,uint16 vc static int32 sadb_save_sa(SecurityAssociation_t* sa) { int32 status = OS_SUCCESS; + if(sa==NULL) {return SADB_NULL_SA_USED;} char update_sa_query[2048]; snprintf(update_sa_query, sizeof(update_sa_query),SQL_SADB_UPDATE_IV_ARC_BY_SPI,convert_byte_array_to_hexstring(sa->iv,sa->shivf_len),convert_byte_array_to_hexstring(sa->arc,sa->shsnf_len),sa->spi,sa->gvcid_tc_blk.tfvn,sa->gvcid_tc_blk.scid,sa->gvcid_tc_blk.vcid,sa->gvcid_tc_blk.mapid); @@ -151,6 +152,9 @@ static int32 sadb_save_sa(SecurityAssociation_t* sa) // todo - if query fails, need to push failure message to error stack instead of just return code. //We free the allocated SA memory in the save function. + if(sa->iv != NULL) free(sa->iv); + if(sa->abm != NULL) free(sa->abm); + if(sa->arc != NULL) free(sa->arc); free(sa); return status; } @@ -194,6 +198,9 @@ static int32 parse_sa_from_mysql_query(char* query, SecurityAssociation_t** secu //TODO -- Need to store mysql query hex string and then malloc sa->iv according to size. //TODO -- IV && arc && abm as uint8* instead of uint8[]!!! + char* iv_byte_str; + char* arc_byte_str; + char* abm_byte_str; while((row = mysql_fetch_row(result))){ for(int i=0; i < num_fields; i++) { @@ -226,20 +233,28 @@ static int32 parse_sa_from_mysql_query(char* query, SecurityAssociation_t** secu if(strcmp(field_names[i],"stmacf_len")==0){sa->stmacf_len=atoi(row[i]);continue;} if(strcmp(field_names[i],"ecs_len")==0){sa->ecs_len=atoi(row[i]);continue;} if(strcmp(field_names[i],"HEX(ecs)")==0){convert_hexstring_to_byte_array(row[i],sa->ecs);continue;} - //if(strcmp(field_names[i],"HEX(iv)")==0){memcpy(&(sa->iv),&row[i],IV_SIZE);continue;} - if(strcmp(field_names[i],"HEX(iv)")==0){convert_hexstring_to_byte_array(row[i],sa->iv);continue;} + // if(strcmp(field_names[i],"HEX(iv)")==0){memcpy(&(sa->iv),&row[i],IV_SIZE);continue;} + if(strcmp(field_names[i],"HEX(iv)")==0){iv_byte_str = row[i];continue;} if(strcmp(field_names[i],"acs_len")==0){sa->acs_len=atoi(row[i]);continue;} if(strcmp(field_names[i],"acs")==0){sa->acs=atoi(row[i]);continue;} if(strcmp(field_names[i],"abm_len")==0){sa->abm_len=atoi(row[i]);continue;} - if(strcmp(field_names[i],"HEX(abm)")==0){convert_hexstring_to_byte_array(row[i],sa->abm);continue;} + if(strcmp(field_names[i],"HEX(abm)")==0){abm_byte_str = row[i];continue;} + // if(strcmp(field_names[i],"HEX(abm)")==0){convert_hexstring_to_byte_array(row[i],sa->abm);continue;} if(strcmp(field_names[i],"arc_len")==0){sa->arc_len=atoi(row[i]);continue;} - if(strcmp(field_names[i],"HEX(arc)")==0){convert_hexstring_to_byte_array(row[i],sa->arc);continue;} + if(strcmp(field_names[i],"HEX(arc)")==0){arc_byte_str = row[i];continue;} + // if(strcmp(field_names[i],"HEX(arc)")==0){convert_hexstring_to_byte_array(row[i],sa->arc);continue;} if(strcmp(field_names[i],"arcw_len")==0){sa->arcw_len=atoi(row[i]);continue;} if(strcmp(field_names[i],"arcw")==0){sa->arcw=atoi(row[i]);continue;} //printf("%s:%s ",field_names[i], row[i] ? row[i] : "NULL"); } //printf("\n"); } + sa->iv = (uint8*) calloc(1, sa->shivf_len * sizeof(uint8)); + sa->arc = (uint8*) calloc(1, sa->arc_len * sizeof(uint8)); + sa->abm = (uint8*) calloc(1, sa->abm_len * sizeof(uint8)); + convert_hexstring_to_byte_array(iv_byte_str,sa->iv); + convert_hexstring_to_byte_array(arc_byte_str,sa->arc); + convert_hexstring_to_byte_array(abm_byte_str,sa->abm); *security_association = sa; mysql_free_result(result); From aaf2422ff22c6669213208b923944b61752979da Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Thu, 16 Dec 2021 16:20:53 -0500 Subject: [PATCH 16/18] Minor Bug fixes for PR, added UT runs for PRs as well to .git/workflow folder --- .github/workflows/build.yml | 2 +- .github/workflows/utest.yml | 2 +- .github/workflows/validation.yml | 2 +- fsw/crypto_util/app/et_dt_validation.c | 3 ++- fsw/src/crypto.c | 4 ++-- 5 files changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5a53488f..f613a234 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,7 @@ name: Build on: - push: + [push, pull_request]: branches: [ collab_main ] env: diff --git a/.github/workflows/utest.yml b/.github/workflows/utest.yml index 51677f00..e1c0af43 100644 --- a/.github/workflows/utest.yml +++ b/.github/workflows/utest.yml @@ -1,7 +1,7 @@ name: Unit Tests on: - push: + [push, pull_request]: branches: [ collab_main ] env: diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index 58178d2b..7e62b7a3 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -1,7 +1,7 @@ name: Validation Tests on: - push: + [push, pull_request]: branches: [ collab_main ] env: diff --git a/fsw/crypto_util/app/et_dt_validation.c b/fsw/crypto_util/app/et_dt_validation.c index b0396c98..0757f46d 100644 --- a/fsw/crypto_util/app/et_dt_validation.c +++ b/fsw/crypto_util/app/et_dt_validation.c @@ -132,6 +132,7 @@ UTEST(ET_VALIDATION, AUTH_ENCRYPTION_TEST) memset(tc_sdls_processed_frame, 0, (sizeof(uint8) * TC_SIZE)); // Ensure that Process Security can activate SA 4 return_val = Crypto_TC_ProcessSecurity(activate_sa4_b, &activate_sa4_len, tc_sdls_processed_frame); + printf("Verifying TC_Process Return Value\n"); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); // Expose SA 1 for testing sadb_routine->sadb_get_sa_from_spi(1,&test_association); @@ -149,7 +150,7 @@ UTEST(ET_VALIDATION, AUTH_ENCRYPTION_TEST) int32 ret_status = Crypto_TC_ApplySecurity(enc_test_ping_b, enc_test_ping_len, &ptr_enc_frame, &enc_frame_len); // Get Truth Baseline python_auth_encryption("1880d2ca0008197f0b0031000039c5", "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210", "000000000000000000000001", "2003043400FF0004", "00", &expected, &expected_length); - + for(int i = 0; i < expected_length; i++) { printf("[%d]: %02x -> %02x \n", i, expected[i], ptr_enc_frame[i]); diff --git a/fsw/src/crypto.c b/fsw/src/crypto.c index fe957a8d..f037a0e8 100644 --- a/fsw/src/crypto.c +++ b/fsw/src/crypto.c @@ -1800,7 +1800,7 @@ static int32 Crypto_Key_verify(char* ingest,TC_t* tc_frame) iv_loc = count; for (int y = 0; y < IV_SIZE; y++) { - ingest[count++] = *(tc_frame->tc_sec_header.iv)+y; + ingest[count++] = *(tc_frame->tc_sec_header.iv+y); } ingest[count-1] = ingest[count-1] + x + 1; @@ -3344,7 +3344,7 @@ int32 Crypto_TC_ProcessSecurity( char* ingest, int* len_ingest,TC_t* tc_sdls_pro { if(crypto_config->crypto_check_fecf == TC_CHECK_FECF_TRUE) { - uint16 received_fecf = (tc_sdls_processed_frame->tc_header.fl-1 & 0xFF00) | (tc_sdls_processed_frame->tc_header.fl & 0x00FF); + uint16 received_fecf = (((ingest[tc_sdls_processed_frame->tc_header.fl - 1] << 8) & 0xFF00) | (ingest[tc_sdls_processed_frame->tc_header.fl] & 0x00FF)); // Calculate our own uint16 calculated_fecf = Crypto_Calc_FECF(ingest, *len_ingest-2); // Compare From f0abea212cdfc915b53b118611920a1d7bf16b57 Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Thu, 16 Dec 2021 16:23:23 -0500 Subject: [PATCH 17/18] Fix for workflow -- hopefully --- .github/workflows/build.yml | 3 +-- .github/workflows/utest.yml | 3 +-- .github/workflows/validation.yml | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f613a234..723c67c6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,7 +1,6 @@ name: Build -on: - [push, pull_request]: +on: [push, pull_request] branches: [ collab_main ] env: diff --git a/.github/workflows/utest.yml b/.github/workflows/utest.yml index e1c0af43..a826c6af 100644 --- a/.github/workflows/utest.yml +++ b/.github/workflows/utest.yml @@ -1,7 +1,6 @@ name: Unit Tests -on: - [push, pull_request]: +on: [push, pull_request] branches: [ collab_main ] env: diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index 7e62b7a3..007b0173 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -1,7 +1,6 @@ name: Validation Tests -on: - [push, pull_request]: +on: [push, pull_request] branches: [ collab_main ] env: From 510b80978e75ddb46b679828deecea741aadd7c3 Mon Sep 17 00:00:00 2001 From: Robert Brown Date: Thu, 16 Dec 2021 16:25:36 -0500 Subject: [PATCH 18/18] Fix for workflow -- hopefully --- .github/workflows/build.yml | 5 ++++- .github/workflows/utest.yml | 5 ++++- .github/workflows/validation.yml | 6 ++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 723c67c6..e0664768 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,6 +1,9 @@ name: Build -on: [push, pull_request] +on: + push: + branches: [ collab_main ] + pull_request: branches: [ collab_main ] env: diff --git a/.github/workflows/utest.yml b/.github/workflows/utest.yml index a826c6af..32dd1234 100644 --- a/.github/workflows/utest.yml +++ b/.github/workflows/utest.yml @@ -1,6 +1,9 @@ name: Unit Tests -on: [push, pull_request] +on: + push: + branches: [ collab_main ] + pull_request: branches: [ collab_main ] env: diff --git a/.github/workflows/validation.yml b/.github/workflows/validation.yml index 007b0173..c2018286 100644 --- a/.github/workflows/validation.yml +++ b/.github/workflows/validation.yml @@ -1,8 +1,10 @@ name: Validation Tests -on: [push, pull_request] +on: + push: + branches: [ collab_main ] + pull_request: branches: [ collab_main ] - env: # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) BUILD_TYPE: DEBUG