diff --git a/.github/workflows/utest.yml b/.github/workflows/utest.yml index 9a70ef05..c9325fbd 100644 --- a/.github/workflows/utest.yml +++ b/.github/workflows/utest.yml @@ -31,9 +31,17 @@ jobs: # Build your program with the given configuration run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} - - name: Test + - name: Test-TC_APPLY working-directory: ${{github.workspace}}/build # Execute tests defined by the CMake configuration. # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail run: ${{github.workspace}}/build/bin/ut_tc_apply + + - name: Test-CRYPTO_CONFIG + working-directory: ${{github.workspace}}/build + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ${{github.workspace}}/build/bin/ut_crypto_config + + diff --git a/src/src_main/crypto.c b/src/src_main/crypto.c index e125a08d..dd5bc8b8 100644 --- a/src/src_main/crypto.c +++ b/src/src_main/crypto.c @@ -181,7 +181,8 @@ int32_t Crypto_compare_less_equal(uint8_t *actual, uint8_t *expected, int length uint8_t Crypto_Prep_Reply(uint8_t *ingest, uint8_t appID) { uint8_t count = 0; - + if(ingest == NULL) return count; + // Prepare CCSDS for reply sdls_frame.hdr.pvn = 0; sdls_frame.hdr.type = 0; @@ -581,7 +582,7 @@ int32_t Crypto_PDU(uint8_t *ingest, TC_t *tc_frame) } #ifdef CCSDS_DEBUG - if (status > 0) + if ((status > 0) && (ingest != NULL)) { printf(KMAG "CCSDS message put on software bus: 0x" RESET); for (int x = 0; x < status; x++) diff --git a/src/src_main/crypto_key_mgmt.c b/src/src_main/crypto_key_mgmt.c index 3054c6ed..ef260156 100644 --- a/src/src_main/crypto_key_mgmt.c +++ b/src/src_main/crypto_key_mgmt.c @@ -283,7 +283,7 @@ int32_t Crypto_Key_inventory(uint8_t *ingest) uint16_t range = 0; crypto_key_t* ek_ring = cryptography_if->get_ek_ring(); - if ( ek_ring == NULL ) + if ( ek_ring == NULL || ingest == NULL) { return CRYPTOGRAPHY_UNSUPPORTED_OPERATION_FOR_KEY_RING; } @@ -330,7 +330,7 @@ int32_t Crypto_Key_verify(uint8_t *ingest, TC_t *tc_frame) #ifdef PDU_DEBUG printf("Crypto_Key_verify: Requested %d key(s) to verify \n", pdu_keys); #endif - + // Read in PDU for (int x = 0; x < pdu_keys; x++) { @@ -349,13 +349,12 @@ int32_t Crypto_Key_verify(uint8_t *ingest, TC_t *tc_frame) printf("\n"); #endif } - + // Prepare for Reply sdls_frame.pdu.pdu_len = pdu_keys * (2 + IV_SIZE + CHALLENGE_SIZE + CHALLENGE_MAC_SIZE); sdls_frame.hdr.pkt_length = sdls_frame.pdu.pdu_len + 9; count = Crypto_Prep_Reply(ingest, 128); crypto_key_t* ek_ring = cryptography_if->get_ek_ring(); - if ( ek_ring == NULL ) // Can't verify key without a key ring, action supported for this cryptography interface! { return CRYPTOGRAPHY_UNSUPPORTED_OPERATION_FOR_KEY_RING; diff --git a/src/src_main/crypto_mc.c b/src/src_main/crypto_mc.c index faf8d08b..90fa0a43 100644 --- a/src/src_main/crypto_mc.c +++ b/src/src_main/crypto_mc.c @@ -48,6 +48,7 @@ int32_t Crypto_MC_ping(uint8_t *ingest) **/ int32_t Crypto_MC_status(uint8_t *ingest) { + if(ingest == NULL) return CRYPTO_LIB_ERROR; int count = 0; // TODO: Update log_summary.rs; @@ -78,6 +79,7 @@ int32_t Crypto_MC_status(uint8_t *ingest) **/ int32_t Crypto_MC_dump(uint8_t *ingest) { + if(ingest == NULL) return CRYPTO_LIB_ERROR; int count = 0; // Prepare for Reply @@ -113,6 +115,7 @@ int32_t Crypto_MC_dump(uint8_t *ingest) **/ int32_t Crypto_MC_erase(uint8_t *ingest) { + if(ingest == NULL) return CRYPTO_LIB_ERROR; int count = 0; // Zero Logs @@ -152,6 +155,7 @@ int32_t Crypto_MC_erase(uint8_t *ingest) **/ int32_t Crypto_MC_selftest(uint8_t *ingest) { + if(ingest == NULL) return CRYPTO_LIB_ERROR; uint8_t count = 0; uint8_t result = ST_OK; @@ -174,6 +178,7 @@ int32_t Crypto_MC_selftest(uint8_t *ingest) **/ int32_t Crypto_SA_readARSN(uint8_t *ingest) { + if(ingest == NULL) return CRYPTO_LIB_ERROR; uint8_t count = 0; uint16_t spi = 0x0000; SecurityAssociation_t *sa_ptr; diff --git a/src/src_main/crypto_tc.c b/src/src_main/crypto_tc.c index 67e64649..16164c4f 100644 --- a/src/src_main/crypto_tc.c +++ b/src/src_main/crypto_tc.c @@ -121,6 +121,7 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t *p_in_frame, const uint16_t in_fra printf(KYEL "DEBUG - Received Control/Command frame - nothing to do.\n" RESET); #endif status = CRYPTO_LIB_ERR_INVALID_CC_FLAG; + return status; } if (status == CRYPTO_LIB_SUCCESS) diff --git a/src/src_main/sadb_routine_inmemory.template.c b/src/src_main/sadb_routine_inmemory.template.c index 4b61dfd5..eb83f946 100644 --- a/src/src_main/sadb_routine_inmemory.template.c +++ b/src/src_main/sadb_routine_inmemory.template.c @@ -950,6 +950,8 @@ static int32_t sadb_sa_setARSNW(void) **/ static int32_t sadb_sa_status(uint8_t *ingest) { + if(ingest == NULL) return CRYPTO_LIB_ERROR; + // Local variables int count = 0; uint16_t spi = 0x0000; diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 66aaaf3b..4c71adcd 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,6 +25,14 @@ add_test(NAME UT_TC_APPLY COMMAND ${PROJECT_BINARY_DIR}/bin/ut_tc_apply WORKING_DIRECTORY ${PROJECT_TEST_DIR}) +add_test(NAME UT_CRYPTO_CONFIG + COMMAND ${PROJECT_BINARY_DIR}/bin/ut_crypto_config + WORKING_DIRECTORY ${PROJECT_TEST_DIR}) + +add_test(NAME UT_CRYPTO + COMMAND ${PROJECT_BINARY_DIR}/bin/ut_crypto + WORKING_DIRECTORY ${PROJECT_TEST_DIR}) + if(${ENCTEST}) add_test(NAME ET_DT_VALIDATION COMMAND ${PROJECT_BINARY_DIR}/bin/et_dt_validation diff --git a/util/include/ut_crypto.h b/util/include/ut_crypto.h new file mode 100644 index 00000000..c7e278c4 --- /dev/null +++ b/util/include/ut_crypto.h @@ -0,0 +1,35 @@ +/* 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 +*/ + +#ifndef CRYPTOLIB_UT_CRYPTO_H +#define CRYPTOLIB_UT_CRYPTO_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "crypto.h" +#include "shared_util.h" +#include + +#ifdef __cplusplus +} /* Close scope of 'extern "C"' declaration which encloses file. */ +#endif + +#endif // CRYPTOLIB_UT_CRYPTO_H \ No newline at end of file diff --git a/util/include/ut_crypto_config.h b/util/include/ut_crypto_config.h new file mode 100644 index 00000000..a94e6106 --- /dev/null +++ b/util/include/ut_crypto_config.h @@ -0,0 +1,35 @@ +/* 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 +*/ + +#ifndef CRYPTOLIB_UT_CRYPTO_CONFIG_H +#define CRYPTOLIB_UT_CRYPTO_CONFIG_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +#include "crypto.h" +#include "shared_util.h" +#include + +#ifdef __cplusplus +} /* Close scope of 'extern "C"' declaration which encloses file. */ +#endif + +#endif // CRYPTOLIB_UT_CRYPTO_CONFIG_H \ No newline at end of file diff --git a/util/src_util/ut_crypto.c b/util/src_util/ut_crypto.c new file mode 100644 index 00000000..52caa077 --- /dev/null +++ b/util/src_util/ut_crypto.c @@ -0,0 +1,345 @@ +/* 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 CRYPTO_C functionality on the data. + **/ +#include "ut_crypto.h" +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" +#include "utest.h" + +UTEST(CRYPTO_C, CALC_CRC16) +{ + char *data_h = "2003002000ff000100001880d2c9000e197f0b001b0004000400003040d95e"; + uint8_t *data_b = NULL; + int data_b_len = 0; + Crypto_Init_Unit_Test(); + + hex_conversion(data_h, (char **)&data_b, &data_b_len); + + int size = 31; + uint16_t crc = 0x00; + uint16_t validated_crc = 0xA61A; + crc = Crypto_Calc_CRC16(data_b, size); + + //printf("CRC = 0x%04x\n", crc); + ASSERT_EQ(crc, validated_crc); +} + +UTEST(CRYPTO_C, HAPPY_BAD_CC) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, 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_NO_FECF, TC_NO_SEGMENT_HDRS); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_NO_FECF, TC_NO_SEGMENT_HDRS); + Crypto_Init(); + char *raw_tc_sdls_ping_h = "3003002000ff000100001880d2c9000e197f0b001b0004000400003040d95ea61a"; + char *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_t *ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + int32_t return_val = CRYPTO_LIB_ERROR; + + return_val = + Crypto_TC_ApplySecurity((uint8_t *)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_ERR_INVALID_CC_FLAG, return_val); +} + +//TODO: This needs to be reworked to actually better test. +UTEST(CRYPTO_C, PDU_SWITCH) +{ + int32_t status = CRYPTO_LIB_ERROR; + + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, 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(); + + sdls_frame.pdu.type = 0; + sdls_frame.pdu.uf = 0; + sdls_frame.pdu.sg = SG_KEY_MGMT; + sdls_frame.pdu.pid = PID_OTAR; + uint8_t *ingest = NULL; + + TC_t *tc_frame; + tc_frame = malloc(sizeof(uint8_t) * TC_SIZE); + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_KEY_ACTIVATION; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_KEY_DEACTIVATION; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pdu_len = 0; + sdls_frame.pdu.pid = PID_KEY_VERIFICATION; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_KEY_DESTRUCTION; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_KEY_INVENTORY; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTOGRAPHY_UNSUPPORTED_OPERATION_FOR_KEY_RING); + + sdls_frame.pdu.pid = SG_KEY_MGMT; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.sg = SG_SA_MGMT; + sdls_frame.pdu.pid = PID_CREATE_SA; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_DELETE_SA; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_SET_ARSNW; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_REKEY_SA; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_EXPIRE_SA; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_SET_ARSN; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_START_SA; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_STOP_SA; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = PID_READ_ARSN; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_ERROR); + + sdls_frame.pdu.pid = PID_SA_STATUS; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_ERROR); + + sdls_frame.pdu.pid = 0b111; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.sg = SG_SEC_MON_CTRL; + sdls_frame.pdu.pid = PID_LOG_STATUS; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_ERROR); + + sdls_frame.pdu.pid = PID_DUMP_LOG; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_ERROR); + + sdls_frame.pdu.pid = PID_ERASE_LOG; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_ERROR); + + sdls_frame.pdu.pid = PID_SELF_TEST; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_ERROR); + + sdls_frame.pdu.pid = PID_ALARM_FLAG; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 0b1111; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.sg = PID_LOG_STATUS; + sdls_frame.pdu.pid = PID_LOG_STATUS; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.uf = 1; + sdls_frame.pdu.pid = 0; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 1; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 2; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 3; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 4; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 5; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 6; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 7; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.pid = 8; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); + + sdls_frame.pdu.type = 1; + sdls_frame.pdu.pid = 8; + status = Crypto_PDU(ingest, tc_frame); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); +} + +UTEST(CRYPTO_C, EXT_PROC_PDU) +{ + uint8_t *ingest = NULL; + TC_t *tc_frame = NULL; + tc_frame = malloc(sizeof(uint8_t) * TC_SIZE); + int32_t status = CRYPTO_LIB_ERROR; + crypto_config->has_pus_hdr = TC_NO_PUS_HDR; + tc_frame->tc_header.vcid = TC_SDLS_EP_VCID; + tc_frame->tc_header.fl = 1; + + status = Crypto_Process_Extended_Procedure_Pdu(tc_frame, ingest); + ASSERT_EQ(status, CRYPTO_LIB_SUCCESS); +} + + +//TODO: Move these to their own unit test files. + +//TODO: This test will need to be reworked when this functionality exists. +UTEST(CRYPTO_AOS, APPLY_SECURITY) +{ + int32_t status = CRYPTO_LIB_ERROR; + uint8_t ingest[1024] = {0}; + int len_ingest = 0; + + status = Crypto_AOS_ApplySecurity(&ingest[0], &len_ingest); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(CRYPTO_AOS, PROCESS_SECURITY) +{ + int32_t status = CRYPTO_LIB_ERROR; + uint8_t ingest[1024] = {0}; + int len_ingest = 0; + + status = Crypto_AOS_ProcessSecurity(&ingest[0], &len_ingest); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(CRYPTO_MC, STATUS) +{ + int count = 0; + uint8_t ingest[1024] = {0}; + + count = Crypto_MC_status(ingest); + ASSERT_EQ(11, count); +} + +UTEST(CRYPTO_MC, DUMP) +{ + int count = 0; + uint8_t ingest[1024] = {0}; + + count = Crypto_MC_dump(ingest); + ASSERT_EQ(((log_count * 4) + (log_count * 2) + 9), count); +} + +UTEST(CRYPTO_MC, ERASE) +{ + int count = 0; + uint8_t ingest[1024] = {0}; + + count = Crypto_MC_erase(ingest); + ASSERT_EQ(11, count); +} + +UTEST(CRYPTO_MC, SELFTEST) +{ + int count = 0; + uint8_t ingest[1024] = {0}; + + count = Crypto_MC_selftest(ingest); + ASSERT_EQ(10, count); +} + +UTEST(CRYPTO_MC, READARSN) +{ + int count = 0; + uint8_t ingest[1024] = {0}; + + count = Crypto_SA_readARSN(ingest); + ASSERT_EQ(11, count); +} + +UTEST(CRYPTO_MC, PROCESS) +{ + uint8_t ingest[1024] = {0}; + int len_ingest = 0; + int32_t status = CRYPTO_LIB_ERROR; + + status = Crypto_TM_ProcessSecurity(ingest, &len_ingest); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(CRYPTO_MC, TMLENGTH) +{ + int length = 0; + + length = Crypto_Get_tmLength(length); + ASSERT_EQ(1145, length); +} + + + +UTEST_MAIN(); \ No newline at end of file diff --git a/util/src_util/ut_crypto_config.c b/util/src_util/ut_crypto_config.c new file mode 100644 index 00000000..7ab4e07f --- /dev/null +++ b/util/src_util/ut_crypto_config.c @@ -0,0 +1,180 @@ +/* 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 CRYPTO_CONFIG functionality on the data. + **/ +#include "ut_crypto_config.h" +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" +#include "utest.h" + + +UTEST(CRYPTO_CONFIG, CRYPTO_INIT_WITH_INCOMPLETE_CONFIG) +{ + // Make use of Crypto_Init_With_Configs + int32_t status = CRYPTO_LIB_ERROR; + CryptoConfig_t *crypto_config_p = NULL; + GvcidManagedParameters_t *gvcid_managed_paramenters_p = NULL; + SadbMariaDBConfig_t *sadb_mariadb_config_p = NULL; + CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config_p = NULL; + + status = Crypto_Init_With_Configs(crypto_config_p, gvcid_managed_paramenters_p, sadb_mariadb_config_p, cryptography_kmc_crypto_config_p); + ASSERT_EQ(CRYPTO_CONFIGURATION_NOT_COMPLETE, status); +} + +UTEST(CRYPTO_CONFIG, CRYPTO_INIT_NO_MANAGED_PARAM_CONFIG) +{ + int32_t status = CRYPTO_LIB_ERROR; + CryptoConfig_t *crypto_config_p = malloc(sizeof(CryptoConfig_t) * sizeof(uint8_t)); + GvcidManagedParameters_t *gvcid_managed_paramenters_p = NULL; + SadbMariaDBConfig_t *sadb_mariadb_config_p = NULL; + CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config_p = NULL; + status = Crypto_Init_With_Configs(crypto_config_p, gvcid_managed_paramenters_p, sadb_mariadb_config_p, cryptography_kmc_crypto_config_p); + free(crypto_config_p); + ASSERT_EQ(CRYPTO_MANAGED_PARAM_CONFIGURATION_NOT_COMPLETE, status); +} + +UTEST(CRYPTO_CONFIG, CRYPTO_INIT_MARIADB_NULL) +{ + int32_t status = CRYPTO_LIB_ERROR; + CryptoConfig_t *crypto_config_p = malloc(sizeof(CryptoConfig_t) * sizeof(uint8_t)); + GvcidManagedParameters_t *gvcid_managed_paramenters_p = malloc(sizeof(GvcidManagedParameters_t)); + gvcid_managed_paramenters_p->next = NULL; + SadbMariaDBConfig_t *sadb_mariadb_config_p = NULL; + CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config_p = NULL; + + crypto_config->sadb_type = SADB_TYPE_MARIADB; + status = Crypto_Init_With_Configs(crypto_config_p, gvcid_managed_paramenters_p, sadb_mariadb_config_p, cryptography_kmc_crypto_config_p); + + free(crypto_config_p); + free(gvcid_managed_paramenters_p); + ASSERT_EQ(CRYPTO_MARIADB_CONFIGURATION_NOT_COMPLETE, status); +} + +UTEST(CRYPTO_CONFIG, CRYPTO_INIT_KMCCRYPTO_NULL) +{ + int32_t status = CRYPTO_LIB_ERROR; + CryptoConfig_t *crypto_config_p = malloc(sizeof(CryptoConfig_t) * sizeof(uint8_t)); + GvcidManagedParameters_t *gvcid_managed_paramenters_p = malloc(sizeof(GvcidManagedParameters_t)); + gvcid_managed_paramenters_p->next = NULL; + SadbMariaDBConfig_t *sadb_mariadb_config_p = malloc(sizeof(SadbMariaDBConfig_t) * sizeof(uint8_t)); + CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config_p = NULL; + + crypto_config->sadb_type = SADB_TYPE_MARIADB; + crypto_config->cryptography_type = CRYPTOGRAPHY_TYPE_KMCCRYPTO; + + status = Crypto_Init_With_Configs(crypto_config_p, gvcid_managed_paramenters_p, sadb_mariadb_config_p, cryptography_kmc_crypto_config_p); + free(crypto_config_p); + free(gvcid_managed_paramenters_p); + free(sadb_mariadb_config_p); + ASSERT_EQ(CRYPTOGRAPHY_KMC_CRYPTO_SERVICE_CONFIGURATION_NOT_COMPLETE, status); +} + +UTEST(CRYPTO_CONFIG, CRYPTO_INIT_INVALID_INTERFACE) +{ + int32_t status = CRYPTO_LIB_ERROR; + CryptoConfig_t *crypto_config_p = malloc(sizeof(CryptoConfig_t) * sizeof(uint8_t)); + GvcidManagedParameters_t *gvcid_managed_paramenters_p = malloc(sizeof(GvcidManagedParameters_t)); + gvcid_managed_paramenters_p->next = NULL; + SadbMariaDBConfig_t *sadb_mariadb_config_p = malloc(sizeof(SadbMariaDBConfig_t) * sizeof(uint8_t)); + CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config_p = NULL; + + crypto_config->sadb_type = SADB_TYPE_MARIADB; + crypto_config->cryptography_type = 2; // Currently an invalid ENUM + + status = Crypto_Init_With_Configs(crypto_config_p, gvcid_managed_paramenters_p, sadb_mariadb_config_p, cryptography_kmc_crypto_config_p); + free(crypto_config_p); + free(gvcid_managed_paramenters_p); + free(sadb_mariadb_config_p); + ASSERT_EQ(CRYPTOGRAPHY_INVALID_CRYPTO_INTERFACE_TYPE, status); +} + +UTEST(CRYPTO_CONFIG, CRYPTO_INIT_INVALID_SADB) +{ + int32_t status = CRYPTO_LIB_ERROR; + CryptoConfig_t *crypto_config_p = malloc(sizeof(CryptoConfig_t) * sizeof(uint8_t)); + GvcidManagedParameters_t *gvcid_managed_paramenters_p = malloc(sizeof(GvcidManagedParameters_t) * sizeof(uint8_t)); + gvcid_managed_paramenters_p->next = NULL; + SadbMariaDBConfig_t *sadb_mariadb_config_p = malloc(sizeof(SadbMariaDBConfig_t) * sizeof(uint8_t)); + CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config_p = NULL; + + crypto_config->sadb_type = 2; // Currently an invalid ENUM + crypto_config->cryptography_type = 2; // Currently an invalid ENUM + + status = Crypto_Init_With_Configs(crypto_config_p, gvcid_managed_paramenters_p, sadb_mariadb_config_p, cryptography_kmc_crypto_config_p); + free(crypto_config_p); + free(gvcid_managed_paramenters_p); + free(sadb_mariadb_config_p); + ASSERT_EQ(SADB_INVALID_SADB_TYPE, status); +} + +// TODO: Not able to force the Crypto_Lib_Error ATM +UTEST(CRYPTO_CONFIG, CRYPTO_CONFIG_MDB) +{ + int32_t status = CRYPTO_LIB_ERROR; + char* mysql_username = "ITC_JPL"; + char* mysql_password = "ITC_JPL"; + char* mysql_hostname = "ITC_JPL"; + char* mysql_database = "ITC_JPL"; + uint16_t mysql_port = 9999; + uint8_t enc_conn = 123; + char* ssl_cert = "NONE"; + char* ssl_key = "NONE"; + char* ssl_ca = "NONE"; + char* ssl_capath = "NONE"; + status = Crypto_Config_MariaDB(mysql_username, mysql_password, mysql_hostname, mysql_database, mysql_port, enc_conn, ssl_cert, ssl_key, ssl_ca, ssl_capath); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(CRYPTO_CONFIG, CRYPTO_CONFIG_KMC) +{ + int32_t status = CRYPTO_LIB_ERROR; + char* hostname = "ITC_JPL"; + int16_t port = 9999; + char* cert_path = "NONE"; + char* key_path = "NONE"; + uint8_t ssl_host_val = 123; + + status = Crypto_Config_Kmc_Crypto_Service(hostname,port,cert_path,key_path,ssl_host_val); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +#ifdef TODO_NEEDSWORK +UTEST(CRYPTO_CONFIG, CRYPTO_INIT_KMC_OK) +{ + int32_t status = CRYPTO_LIB_ERROR; + CryptoConfig_t *crypto_config_p = malloc(sizeof(CryptoConfig_t) * sizeof(uint8_t)); + GvcidManagedParameters_t *gvcid_managed_paramenters_p = malloc(sizeof(GvcidManagedParameters_t) * sizeof(uint8_t)); + SadbMariaDBConfig_t *sadb_mariadb_config_p = malloc(sizeof(SadbMariaDBConfig_t) * sizeof(uint8_t)); + CryptographyKmcCryptoServiceConfig_t *cryptography_kmc_crypto_config_p = malloc(sizeof(CryptographyKmcCryptoServiceConfig_t) * sizeof(uint8_t)); + + crypto_config->sadb_type = SADB_TYPE_MARIADB; + crypto_config->cryptography_type = CRYPTOGRAPHY_TYPE_KMCCRYPTO; + + status = Crypto_Init_With_Configs(crypto_config_p, gvcid_managed_paramenters_p, sadb_mariadb_config_p, cryptography_kmc_crypto_config_p); + free(crypto_config_p); + free(gvcid_managed_paramenters_p); + free(sadb_mariadb_config_p); + free(cryptography_kmc_crypto_config_p); + ASSERT_EQ(CRYPTOGRAPHY_KMC_CRYPTO_SERVICE_CONFIGURATION_NOT_COMPLETE, status); +} +#endif + +UTEST_MAIN(); \ No newline at end of file diff --git a/util/src_util/ut_tc_apply.c b/util/src_util/ut_tc_apply.c index 09bb038e..d9d73b03 100644 --- a/util/src_util/ut_tc_apply.c +++ b/util/src_util/ut_tc_apply.c @@ -47,13 +47,31 @@ UTEST(TC_APPLY_SECURITY, NO_CRYPTO_INIT) uint16_t enc_frame_len = 0; int32_t return_val = CRYPTO_LIB_ERROR; - return_val = - Crypto_TC_ApplySecurity((uint8_t *)raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + return_val = Crypto_TC_ApplySecurity((uint8_t *)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(); } +UTEST(TC_APPLY_SECURITY, NO_CONFIG) +{ + // No Crypto_Init(), but we still Configure It; + char *raw_tc_sdls_ping_h = "20030015001880d2c70008197f0b00310000b1fe3128"; + char *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_t *ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + int32_t return_val = CRYPTO_LIB_ERROR; + + return_val = Crypto_TC_ApplySecurity((uint8_t *)raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + ASSERT_EQ(CRYPTO_LIB_ERR_NO_CONFIG, 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