diff --git a/include/crypto.h b/include/crypto.h index 0a6175ab..d9a8fd7c 100644 --- a/include/crypto.h +++ b/include/crypto.h @@ -47,6 +47,11 @@ #define CRYPTO_LIB_REVISION 3 #define CRYPTO_LIB_MISSION_REV 0 +/* +** TC_BLOCK_SIZE +*/ +#define TC_BLOCK_SIZE 16 + /* ** User Prototypes */ @@ -65,6 +70,7 @@ extern int32_t Crypto_Config_Kmc_Crypto_Service(char* protocol, char* kmc_crypto uint8_t kmc_ignore_ssl_hostname_validation, char* mtls_client_cert_path, char* mtls_client_cert_type, char* mtls_client_key_path, char* mtls_client_key_pass, char* mtls_issuer_cert); +extern int32_t Crypto_Config_Cam(uint8_t cam_enabled, char* cookie_file_path, char* keytab_file_path, uint8_t login_method, char* access_manager_uri, char* username, char* cam_home); extern int32_t Crypto_Config_Add_Gvcid_Managed_Parameter(uint8_t tfvn, uint16_t scid, uint8_t vcid, uint8_t has_fecf, uint8_t has_segmentation_hdr, uint16_t max_tc_frame_size); @@ -83,6 +89,9 @@ extern int32_t Crypto_Shutdown(void); // Free all allocated memory extern int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_frame_length, uint8_t** pp_enc_frame, uint16_t* p_enc_frame_len); extern int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdls_processed_frame); +extern int32_t Crypto_TC_ApplySecurity_Cam(const uint8_t* p_in_frame, const uint16_t in_frame_length, + uint8_t** pp_enc_frame, uint16_t* p_enc_frame_len, char* cam_cookies); +extern int32_t Crypto_TC_ProcessSecurity_Cam(uint8_t* ingest, int *len_ingest, TC_t* tc_sdls_processed_frame, char* cam_cookies); // Telemetry (TM) extern int32_t Crypto_TM_ApplySecurity(uint8_t* ingest, int *len_ingest); extern int32_t Crypto_TM_ProcessSecurity(uint8_t* ingest, int *len_ingest); @@ -90,6 +99,9 @@ extern int32_t Crypto_TM_ProcessSecurity(uint8_t* ingest, int *len_ingest); extern int32_t Crypto_AOS_ApplySecurity(uint8_t* ingest, int *len_ingest); extern int32_t Crypto_AOS_ProcessSecurity(uint8_t* ingest, int *len_ingest); +// Crypo Error Support Functions +extern char* Crypto_Get_Error_Code_Enum_String(int32_t crypto_error_code); + /* ** Internal Prototypes */ @@ -153,7 +165,8 @@ int32_t crypto_config_add_gvcid_managed_parameter_recursion(uint8_t tfvn, uint16 GvcidManagedParameters_t* managed_parameter); void Crypto_Free_Managed_Parameters(GvcidManagedParameters_t* managed_parameters); - +// Project-wide support functions +extern char* crypto_deep_copy_string(char* src_string); /* ** Extern Global Variables @@ -166,6 +179,7 @@ extern TM_t tm_frame; extern CryptoConfig_t* crypto_config; extern SadbMariaDBConfig_t* sadb_mariadb_config; extern CryptographyKmcCryptoServiceConfig_t* cryptography_kmc_crypto_config; +extern CamConfig_t* cam_config; extern GvcidManagedParameters_t* gvcid_managed_parameters; extern GvcidManagedParameters_t* current_managed_parameters; extern SadbRoutine sadb_routine; diff --git a/include/crypto_config.h b/include/crypto_config.h index 07dcfa02..2eb46c4f 100644 --- a/include/crypto_config.h +++ b/include/crypto_config.h @@ -54,6 +54,10 @@ #define RESET #endif + +// Max Frame Size +#define TC_MAX_FRAME_SIZE 1024 + // Spacecraft Defines #define SCID 0x0003 // 0xC3D2 @@ -162,7 +166,6 @@ #define TC_SH_SIZE 8 /* bits */ #define TC_SN_SIZE 2 #define TC_SN_WINDOW 10 /* +/- value */ -#define TC_PAD_SIZE 0 #define TC_FRAME_DATA_SIZE 1740 /* bytes */ // CCSDS PUS Defines diff --git a/include/crypto_config_structs.h b/include/crypto_config_structs.h index 4ee47783..c2e4381d 100644 --- a/include/crypto_config_structs.h +++ b/include/crypto_config_structs.h @@ -89,6 +89,18 @@ typedef enum SA_INCREMENT_NONTRANSMITTED_IV_TRUE } SaIncrementNonTransmittedIvPortion; +typedef enum +{ + CAM_ENABLED_FALSE, + CAM_ENABLED_TRUE +} CamEnabledBool; + +typedef enum +{ + CAM_LOGIN_NONE, // Using already populated cam_cookie_file + CAM_LOGIN_KERBEROS, // Using already logged-in Kerberos to generate CAM cookies + CAM_LOGIN_KEYTAB_FILE // using keytab file to login and generate CAM cookies +} CamLoginMethod; /* ** Used for selecting supported algorithms */ @@ -102,7 +114,8 @@ typedef enum typedef enum { CRYPTO_CIPHER_NONE, - CRYPTO_CIPHER_AES256_GCM + CRYPTO_CIPHER_AES256_GCM, + CRYPTO_CIPHER_AES256_CBC } EncCipherSuite; /* @@ -180,5 +193,21 @@ typedef struct } CryptographyKmcCryptoServiceConfig_t; #define CRYPTOGRAPHY_KMC_CRYPTO_SERVICE_CONFIG_SIZE (sizeof(CryptographyKmcCryptoServiceConfig_t)) +/* +** Common Access Manager (CAM) Configuration Block +*/ +typedef struct +{ + uint8_t cam_enabled; + char* cookie_file_path; + char* keytab_file_path; + char* access_manager_uri; + char* username; + char* cam_home; + uint8_t login_method; + +} CamConfig_t; +#define CAM_CONFIG_SIZE (sizeof(CamConfig_t)) + #endif \ No newline at end of file diff --git a/include/crypto_error.h b/include/crypto_error.h index 5f9b582a..a244cc4b 100644 --- a/include/crypto_error.h +++ b/include/crypto_error.h @@ -18,12 +18,18 @@ #ifndef _crypto_error_h_ #define _crypto_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 -#define MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND 104 +/* + * If error codes are added to this header file, their enum string must be added to the error lists (in crypto_error.c) + * AND logic for returning the enum string must be added to crypto_error.c -- Crypto_Get_Error_Code_Enum_String(int32_t) + */ + +#define CRYPTO_CONFIGURATION_NOT_COMPLETE 100 +#define CRYPTO_MANAGED_PARAM_CONFIGURATION_NOT_COMPLETE 101 +#define CRYPTO_MARIADB_CONFIGURATION_NOT_COMPLETE 102 +#define MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND 103 + +#define SADB_INVALID_SADB_TYPE 200 +#define SADB_NULL_SA_USED 201 #define SADB_MARIADB_CONNECTION_FAILED 300 #define SADB_QUERY_FAILED 301 @@ -34,21 +40,34 @@ #define CRYPTOGRAPHY_UNSUPPORTED_OPERATION_FOR_KEY_RING 401 #define CRYPTOGRAPHY_LIBRARY_INITIALIZIATION_ERROR 402 -#define CRYPTOGRAPHY_KMC_CRYPTO_SERVICE_CONFIGURATION_NOT_COMPLETE 501 -#define CRYPTOGRAPHY_KMC_CURL_INITIALIZATION_FAILURE 502 -#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_CONNECTION_ERROR 503 -#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_ENCRYPT_ERROR 504 -#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_DECRYPT_ERROR 505 -#define CRYPTOGRAHPY_KMC_CRYPTO_JSON_PARSE_ERROR 506 -#define CRYPTOGRAHPY_KMC_CIPHER_TEXT_NOT_FOUND_IN_JSON_RESPONSE 507 -#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE 508 -#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AUTHENTICATION_ERROR 509 -#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_MAC_VALIDATION_ERROR 510 -#define CRYPTOGRAHPY_KMC_ICV_NOT_FOUND_IN_JSON_RESPONSE 511 -#define CRYPTOGRAHPY_KMC_NULL_ENCRYPTION_KEY_REFERENCE_IN_SA 512 -#define CRYPTOGRAHPY_KMC_NULL_AUTHENTICATION_KEY_REFERENCE_IN_SA 513 -#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE 514 +#define CRYPTOGRAPHY_KMC_CRYPTO_SERVICE_CONFIGURATION_NOT_COMPLETE 500 +#define CRYPTOGRAPHY_KMC_CURL_INITIALIZATION_FAILURE 501 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_CONNECTION_ERROR 502 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_ENCRYPT_ERROR 503 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_DECRYPT_ERROR 504 +#define CRYPTOGRAHPY_KMC_CRYPTO_JSON_PARSE_ERROR 505 +#define CRYPTOGRAHPY_KMC_CIPHER_TEXT_NOT_FOUND_IN_JSON_RESPONSE 506 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE 507 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AUTHENTICATION_ERROR 508 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_MAC_VALIDATION_ERROR 509 +#define CRYPTOGRAHPY_KMC_ICV_NOT_FOUND_IN_JSON_RESPONSE 510 +#define CRYPTOGRAHPY_KMC_NULL_ENCRYPTION_KEY_REFERENCE_IN_SA 511 +#define CRYPTOGRAHPY_KMC_NULL_AUTHENTICATION_KEY_REFERENCE_IN_SA 512 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE 513 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_DECRYPT_ERROR 514 +#define CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_ENCRYPT_ERROR 515 +#define CAM_CONFIG_NOT_SUPPORTED_ERROR 600 +#define CAM_INVALID_COOKIE_FILE_CONFIGURATION_NULL 601 +#define CAM_AUTHENTICATION_FAILURE_REDIRECT 602 +#define CAM_AUTHENTICATION_REQUIRED 603 +#define CAM_GET_SSO_TOKEN_FAILURE 604 +#define CAM_INVALID_CONFIGURATION_ACCESS_MANAGER_URI_NULL 605 +#define CAM_INVALID_CONFIGURATION_KEYTAB_FILE_PATH_NULL 606 +#define CAM_INVALID_CONFIGURATION_KEYTAB_FILE_USERNAME_NULL 607 +#define CAM_KEYTAB_FILE_KINIT_FAILURE 608 +#define CAM_KERBEROS_REQUEST_TIME_OUT 609 +#define CAM_MAX_AUTH_RETRIES_REACHED 610 #define CRYPTO_LIB_SUCCESS (0) #define CRYPTO_LIB_ERROR (-1) @@ -92,5 +111,19 @@ #define CRYPTO_LIB_ERR_FRAME_COUNTER_DOESNT_MATCH_SA (-39) #define CRYPTO_LIB_ERR_INPUT_FRAME_TOO_SHORT_FOR_TC_STANDARD (-40) #define CRYPTO_LIB_ERR_INPUT_FRAME_LENGTH_SHORTER_THAN_FRAME_HEADERS_LENGTH (-41) +#define CRYPTO_LIB_ERR_UNSUPPORTED_ECS_MODE (-42) +#define CRYPTO_LIB_ERR_NULL_MODE_PTR (-43) +#define CRYPTO_LIB_ERR_UNSUPPORTED_MODE (-44) + +extern char *crypto_enum_errlist_core[]; +extern char *crypto_enum_errlist_config[]; +extern char *crypto_enum_errlist_sadb_if[]; +extern char *crypto_enum_errlist_sadb_mariadb[]; +extern char *crypto_enum_errlist_crypto_if[]; +extern char *crypto_enum_errlist_crypto_kmc[]; +extern char *crypto_enum_errlist_crypto_cam[]; + +#define CRYPTO_UNDEFINED_ERROR "CRYPTO_UNDEFINED_ERROR_CODE" + -#endif //_crypto_error_h_ +#endif //_crypto_error_h_ \ No newline at end of file diff --git a/include/cryptography_interface.h b/include/cryptography_interface.h index 104d9851..dc63ff85 100644 --- a/include/cryptography_interface.h +++ b/include/cryptography_interface.h @@ -34,8 +34,17 @@ typedef struct crypto_key_t* (*get_ek_ring)(void); int32_t (*cryptography_shutdown)(void); // Cryptography Interface Functions - int32_t (*cryptography_encrypt)(void); - int32_t (*cryptography_decrypt)(void); + int32_t (*cryptography_encrypt)(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len,uint8_t* ecs, uint8_t padding, char* cam_cookies); + int32_t (*cryptography_decrypt)(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len, + uint8_t* ecs, uint8_t* acs, char* cam_cookies); int32_t (*cryptography_authenticate)(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -43,7 +52,7 @@ typedef struct uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs); + uint8_t ecs, uint8_t acs, char* cam_cookies); int32_t (*cryptography_validate_authentication)(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -51,7 +60,7 @@ typedef struct uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs); + uint8_t ecs, uint8_t acs, char* cam_cookies); int32_t (*cryptography_aead_encrypt)(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -60,7 +69,7 @@ typedef struct uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t encrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs); + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies); int32_t (*cryptography_aead_decrypt)(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -69,9 +78,10 @@ typedef struct uint8_t* aad, uint32_t aad_len, uint8_t* mac, uint32_t mac_size, uint8_t decrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs); + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies); int32_t (*cryptography_get_acs_algo)(int8_t algo_enum); int32_t (*cryptography_get_ecs_algo)(int8_t algo_enum); + } CryptographyInterfaceStruct, *CryptographyInterface; diff --git a/src/crypto_sadb/sadb_mariadb_sql/create_sadb.sql b/src/crypto_sadb/sadb_mariadb_sql/create_sadb.sql index 246facd4..eec885e1 100644 --- a/src/crypto_sadb/sadb_mariadb_sql/create_sadb.sql +++ b/src/crypto_sadb/sadb_mariadb_sql/create_sadb.sql @@ -34,4 +34,4 @@ CREATE TABLE IF NOT EXISTS security_associations ,arsnw SMALLINT NOT NULL DEFAULT 0 -- ARSNW_SIZE=1 ); -create unique index if not exists main_spi on security_associations (spi,scid,vcid,tfvn,mapid); \ No newline at end of file +create unique index if not exists main_spi on security_associations (spi,scid); \ No newline at end of file diff --git a/src/crypto_sadb/test_sadb_mariadb_sql/create_sadb_ivv_unit_tests.sql b/src/crypto_sadb/test_sadb_mariadb_sql/create_sadb_ivv_unit_tests.sql index e9292a6d..65b921a7 100644 --- a/src/crypto_sadb/test_sadb_mariadb_sql/create_sadb_ivv_unit_tests.sql +++ b/src/crypto_sadb/test_sadb_mariadb_sql/create_sadb_ivv_unit_tests.sql @@ -19,4 +19,4 @@ VALUES (4,'itc/test/key4',3,X'01',1,1,6,12,16,X'000000000000FFFFFFFFFFFC',20,X'0 -- SA 5 - OPERATIONAL; ENC; ARSNW: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,ecs,est,ast,shivf_len,iv_len,stmacf_len,iv,abm_len,abm,arsnw,arsn_len,arsn,tfvn,scid,vcid,mapid,ecs_len,acs_len,acs,shsnf_len) -VALUES (5,'itc/test/key5',3,X'01',0,1,12,12,16,X'000000000000000000000001',36,X'000000000000000000000000000000000000000000000000000000000000000000000000',5,3,X'05FFFC',0,3,3,0,1,1,X'01',2); +VALUES (5,'itc/test/key5',3,X'01',0,1,12,12,16,X'000000000000000000000001',36,X'000000000000000000000000000000000000000000000000000000000000000000000000',5,3,X'05FFFC',0,3,3,0,1,1,X'00',2); diff --git a/src/src_cryptography/src_kmc_crypto_service/cryptography_interface_kmc_crypto_service.template.c b/src/src_cryptography/src_kmc_crypto_service/cryptography_interface_kmc_crypto_service.template.c index f44c8c81..0b9c5f2b 100644 --- a/src/src_cryptography/src_kmc_crypto_service/cryptography_interface_kmc_crypto_service.template.c +++ b/src/src_cryptography/src_kmc_crypto_service/cryptography_interface_kmc_crypto_service.template.c @@ -27,14 +27,37 @@ // JSON marshalling libraries #include "jsmn.h" +#define CAM_MAX_AUTH_RETRIES 4 + +// libcurl call-back response handling Structures +typedef struct { + char* response; + size_t size; +} memory_write; +#define MEMORY_WRITE_SIZE (sizeof(memory_write)) +typedef struct { + char* response; + size_t size; +} memory_read; +#define MEMORY_READ_SIZE (sizeof(memory_read)) + // Cryptography Interface Initialization & Management Functions static int32_t cryptography_config(void); static int32_t cryptography_init(void); static crypto_key_t* get_ek_ring(void); static int32_t cryptography_shutdown(void); // Cryptography Interface Functions -static int32_t cryptography_encrypt(void); -static int32_t cryptography_decrypt(void); +static int32_t cryptography_encrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len,uint8_t* ecs, uint8_t padding, char* cam_cookies); +static int32_t cryptography_decrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len, + uint8_t* ecs, uint8_t* acs, char* cam_cookies); static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -42,7 +65,7 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs); + uint8_t ecs, uint8_t acs, char* cam_cookies); static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -50,7 +73,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs); + uint8_t ecs, uint8_t acs, char* cam_cookies); static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -59,7 +82,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t encrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs); + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies); static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -68,15 +91,20 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t decrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs); + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies); static int32_t cryptography_get_acs_algo(int8_t algo_enum); static int32_t cryptography_get_ecs_algo(int8_t algo_enum); //Local support functions static int32_t get_auth_algorithm_from_acs(uint8_t acs_enum, const char** algo_ptr); +static int32_t get_cam_sso_token(void); +static int32_t initialize_kerberos_keytab_file_login(void); +static int32_t curl_perform_with_cam_retries(CURL* curl_handle,memory_write* chunk_write, memory_read* chunk_read); // libcurl call back and support function declarations -static void configure_curl_connect_opts(CURL* curl); +static int32_t configure_curl_connect_opts(CURL* curl, char* cam_cookies); +static int32_t handle_cam_cookies(CURL* curl,char* cam_cookies); +static int32_t curl_response_error_check(CURL* curl, char* response); static size_t write_callback(void* data, size_t size, size_t nmemb, void* userp); static size_t read_callback(char* dest, size_t size, size_t nmemb, void* userp); static char* int_to_str(uint32_t int_src, uint32_t* converted_str_length); @@ -92,7 +120,7 @@ static CURL* curl; struct curl_slist *http_headers_list; // KMC Crypto Service Endpoints static char* kmc_root_uri; -static const char* status_endpoint = "key-info?keyRef=kmc/test/KEY0"; +//static const char* status_endpoint = "/status"; static const char* encrypt_endpoint = "encrypt?keyRef=%s&transformation=%s&iv=%s"; static const char* encrypt_offset_endpoint = "encrypt?keyRef=%s&transformation=%s&iv=%s&encryptOffset=%s&macLength=%s"; static const char* decrypt_endpoint = "decrypt?metadata=keyLength:%s,keyRef:%s,cipherTransformation:%s,initialVector:%s,cryptoAlgorithm:%s,metadataType:EncryptionMetadata"; @@ -100,8 +128,12 @@ static const char* decrypt_offset_endpoint = "decrypt?metadata=keyLength:%s,keyR static const char* icv_create_endpoint = "icv-create?keyRef=%s"; static const char* icv_verify_endpoint = "icv-verify?metadata=integrityCheckValue:%s,keyRef:%s,cryptoAlgorithm:%s,macLength:%s,metadataType:IntegrityCheckMetadata"; +// CAM Security Endpoints +static const char* cam_kerberos_uri = "%s/cam-api/ssoToken?loginMethod=kerberos"; + // Supported KMC Cipher Transformation Strings static const char* AES_GCM_TRANSFORMATION="AES/GCM/NoPadding"; +static const char* AES_CBC_TRANSFORMATION="AES/CBC/PKCS5Padding"; static const char* AES_CRYPTO_ALGORITHM="AES"; //static const char* AES_CBC_TRANSFORMATION="AES/CBC/PKCS5Padding"; static const char* AES_CMAC_TRANSFORMATION="AESCMAC"; @@ -110,19 +142,6 @@ static const char* HMAC_SHA512="HmacSHA512"; //static const char* AES_DES_CMAC_TRANSFORMATION="DESedeCMAC"; -// libcurl call-back response handling Structures -typedef struct { - char* response; - size_t size; -} memory_write; -#define MEMORY_WRITE_SIZE (sizeof(memory_write)) -typedef struct { - char* response; - size_t size; -} memory_read; -#define MEMORY_READ_SIZE (sizeof(memory_read)) - - CryptographyInterface get_cryptography_interface_kmc_crypto_service(void) { cryptography_if_struct.cryptography_config = cryptography_config; @@ -170,51 +189,61 @@ static int32_t cryptography_config(void) cryptography_kmc_crypto_config->kmc_crypto_hostname, port_str, cryptography_kmc_crypto_config->kmc_crypto_app_uri); - - char* status_uri = (char*) malloc(strlen(kmc_root_uri)+strlen(status_endpoint)); - status_uri[0] = '\0'; - strcat(status_uri, kmc_root_uri); - strcat(status_uri, status_endpoint); + free(port_str); + //KMC Crypto Service status check is impossible in certain CAM configs, commenting it out. + // Also, when this library is started up (EG by SDLS service), there's no guarantee the Crypto Service is available at config time. + //char* status_uri = (char*) malloc(strlen(kmc_root_uri)+strlen(status_endpoint) + 1); + //status_uri[0] = '\0'; + //strcat(status_uri, kmc_root_uri); + //strcat(status_uri, status_endpoint); #ifdef DEBUG printf("Setting up cURL connection to KMC Crypto Service with Params:\n"); printf("\tKMC Root URI: %s\n",kmc_root_uri); - printf("\tKMC Status URL: %s\n",status_uri); + //printf("\tKMC Status URL: %s\n",status_uri); //printf("\tPort: %d\n",cryptography_kmc_crypto_config->kmc_crypto_port); printf("\tSSL Client Cert: %s\n",cryptography_kmc_crypto_config->mtls_client_cert_path); printf("\tSSL Client Key: %s\n",cryptography_kmc_crypto_config->mtls_client_key_path); printf("\tSSL CA Bundle: %s\n",cryptography_kmc_crypto_config->mtls_ca_bundle); #endif - configure_curl_connect_opts(curl); - curl_easy_setopt(curl, CURLOPT_URL, status_uri); - - memory_write* chunk = calloc(1,MEMORY_WRITE_SIZE); - /* send all data to this function */ - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); - /* we pass our 'chunk' struct to the callback function */ - curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void* )chunk); - - CURLcode res; - res = curl_easy_perform(curl); - - if(res != CURLE_OK) // This is not return code, this is successful response! - { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_CONNECTION_ERROR; - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - return status; - } - - if(chunk->response == NULL) // No response, possibly because service is CAM secured. - { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; - fprintf(stderr, "curl_easy_perform() unexpected empty response: \n%s\n", - "Empty Crypto Service response can be caused by CAM security, CryptoLib doesn't support a CAM secured KMC Crypto Service."); - return status; - } - -#ifdef DEBUG - printf("cURL response:\n\t %s\n",chunk->response); -#endif + //status = configure_curl_connect_opts(curl, NULL); + //if(status != CRYPTO_LIB_SUCCESS) + //{ + // return status; + //} + //curl_easy_setopt(curl, CURLOPT_URL, status_uri); + + //memory_write* chunk = calloc(1,MEMORY_WRITE_SIZE); + ///* send all data to this function */ + //curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + ///* we pass our 'chunk' struct to the callback function */ + //curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void* )chunk); + + //CURLcode res; + //res = curl_easy_perform(curl); + + //if(res != CURLE_OK) // This is not return code, this is successful response! + //{ + // status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_CONNECTION_ERROR; + // fprintf(stderr, "curl_easy_perform() failed: %s\n", + // curl_easy_strerror(res)); + // free(status_uri); + // free(kmc_root_uri); + // return status; + //} + + //if(chunk->response == NULL) // No response, possibly because service is CAM secured. + //{ + // status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; + // fprintf(stderr, "curl_easy_perform() unexpected empty response: \n%s\n", + // "Empty Crypto Service response can be caused by CAM security, is CAM configured?"); + // free(status_uri); + // return status; + //} + +//#ifdef DEBUG +// printf("cURL response:\n\t %s\n",chunk->response); +//#endif + //free(status_uri); } return status; } @@ -243,20 +272,388 @@ static crypto_key_t* get_ek_ring(void) } static int32_t cryptography_shutdown(void) { -// if(curl){ -// curl_easy_cleanup(curl); -// curl_global_cleanup(); -// } -// if(http_headers_list != NULL){ -// curl_slist_free_all(http_headers_list); -// } + if(curl){ + curl_easy_cleanup(curl); + curl_global_cleanup(); + } + if(http_headers_list != NULL){ + curl_slist_free_all(http_headers_list); + } if(kmc_root_uri != NULL){ free(kmc_root_uri); } return CRYPTO_LIB_SUCCESS; } -static int32_t cryptography_encrypt(void){ return CRYPTO_LIB_SUCCESS; } -static int32_t cryptography_decrypt(void){ return CRYPTO_LIB_SUCCESS; } + +static int32_t cryptography_encrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len,uint8_t* ecs, uint8_t padding, char* cam_cookies) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + key = key; // Direct key input is not supported in KMC interface + len_key = len_key; // Direct key input is not supported in KMC interface + + // Remove pre-padding to block (KMC does not want it) + if(*ecs == CRYPTO_CIPHER_AES256_CBC && padding > 0) + { + len_data_in = len_data_in - padding; + } + + curl_easy_reset(curl); + status = configure_curl_connect_opts(curl, cam_cookies); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } + // Base64 URL encode IV for KMC REST Encrypt + char* iv_base64 = (char*)calloc(1,B64ENCODE_OUT_SAFESIZE(iv_len)+1); + base64urlEncode(iv,iv_len,iv_base64,NULL); + + uint8_t* encrypt_payload = data_in; + size_t encrypt_payload_len = len_data_in; + +#ifdef DEBUG + printf("IV Base64 URL Encoded: %s\n",iv_base64); +#endif + + if(sa_ptr->ek_ref == NULL) + { + status = CRYPTOGRAHPY_KMC_NULL_ENCRYPTION_KEY_REFERENCE_IN_SA; + return status; + } + + char* encrypt_uri; + + int len_encrypt_endpoint = strlen(encrypt_endpoint)+strlen(sa_ptr->ek_ref)+strlen(iv_base64)+strlen(AES_CBC_TRANSFORMATION); + char* encrypt_endpoint_final = (char*) malloc(len_encrypt_endpoint); + + snprintf(encrypt_endpoint_final,len_encrypt_endpoint,encrypt_endpoint,sa_ptr->ek_ref,AES_CBC_TRANSFORMATION, iv_base64); + + encrypt_uri = (char*) malloc(strlen(kmc_root_uri)+len_encrypt_endpoint); + encrypt_uri[0] = '\0'; + strcat(encrypt_uri, kmc_root_uri); + strcat(encrypt_uri, encrypt_endpoint_final); + + +#ifdef DEBUG + printf("Encrypt URI: %s\n",encrypt_uri); +#endif + curl_easy_setopt(curl, CURLOPT_URL, encrypt_uri); + + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_headers_list); + + memory_write* chunk_write = (memory_write*) calloc(1,MEMORY_WRITE_SIZE); + memory_read* chunk_read = (memory_read*) calloc(1,MEMORY_READ_SIZE);; + /* Configure CURL for POST */ + curl_easy_setopt(curl, CURLOPT_POST, 1L); + /* send all data to this function */ + curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); + /* we pass our 'chunk' struct to the callback function */ + curl_easy_setopt(curl, CURLOPT_READDATA, chunk_read); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + /* we pass our 'chunk' struct to the callback function */ + curl_easy_setopt(curl, CURLOPT_WRITEDATA, chunk_write); + + /* size of the POST data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) encrypt_payload_len); + /* binary data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, encrypt_payload); + +#ifdef DEBUG + printf("Data to Encrypt: \n"); + for (uint32_t i=0; i < encrypt_payload_len; i++) + { + printf("%02x ", encrypt_payload[i]); + } + printf("\n"); +#endif + + status = curl_perform_with_cam_retries(curl, chunk_write, chunk_read); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } + + /* JSON Response Handling */ + + // Parse the JSON string response + jsmn_parser p; + jsmntok_t t[64]; /* We expect no more than 64 JSON tokens */ + jsmn_init(&p); + int parse_result = jsmn_parse(&p, chunk_write->response, strlen(chunk_write->response), t, 64); // "chunk->response" is the char array holding the json content + + // Find the 'base64ciphertext' token + if (parse_result < 0) { + status = CRYPTOGRAHPY_KMC_CRYPTO_JSON_PARSE_ERROR; + printf("Failed to parse JSON: %d\n", parse_result); + return status; + } + + int json_idx = 0; + uint8_t ciphertext_found = CRYPTO_FALSE; + char* ciphertext_base64 = NULL; + for (json_idx = 1; json_idx < parse_result; json_idx++) + { + if (jsoneq(chunk_write->response, &t[json_idx], "base64ciphertext") == 0) + { + /* We may use strndup() to fetch string value */ +#ifdef DEBUG + printf("Json base64ciphertext: %.*s\n", t[json_idx + 1].end - t[json_idx + 1].start, + chunk_write->response + t[json_idx + 1].start); +#endif + uint32_t len_ciphertext = t[json_idx + 1].end - t[json_idx + 1].start; + ciphertext_base64 = malloc(len_ciphertext+1); + memcpy(ciphertext_base64,chunk_write->response + t[json_idx + 1].start, len_ciphertext); + ciphertext_base64[len_ciphertext] = '\0'; +#ifdef DEBUG + printf("Parsed base64ciphertext: %s\n",ciphertext_base64); +#endif + json_idx++; + ciphertext_found = CRYPTO_TRUE; + continue; + } + + if (jsoneq(chunk_write->response, &t[json_idx], "httpCode") == 0) + { + /* We may use strndup() to fetch string value */ +#ifdef DEBUG + printf("httpCode: %.*s\n", t[json_idx + 1].end - t[json_idx + 1].start, + chunk_write->response + t[json_idx + 1].start); +#endif + uint32_t len_httpcode = t[json_idx + 1].end - t[json_idx + 1].start; + char* http_code_str = malloc(len_httpcode+1); + memcpy(http_code_str,chunk_write->response + t[json_idx + 1].start, len_httpcode); + http_code_str[len_httpcode] = '\0'; + int http_code = atoi(http_code_str); +#ifdef DEBUG + printf("Parsed http code: %d\n",http_code); +#endif + if(http_code != 200) + { + status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE; + fprintf(stderr,"KMC Crypto Failure Response:\n%s\n",chunk_write->response); + return status; + } + free(http_code_str); + json_idx++; + continue; + } + + } + if(ciphertext_found == CRYPTO_FALSE){ + status = CRYPTOGRAHPY_KMC_CIPHER_TEXT_NOT_FOUND_IN_JSON_RESPONSE; + return status; + } + + /* JSON Response Handling End */ + + + uint8_t* ciphertext_decoded = malloc((len_data_out)*2 + 1); + size_t ciphertext_decoded_len = 0; + base64Decode(ciphertext_base64,strlen(ciphertext_base64),ciphertext_decoded, &ciphertext_decoded_len); +#ifdef DEBUG + printf("Decoded Cipher Text Length: %ld\n",ciphertext_decoded_len); + printf("Decoded Cipher Text: \n"); + for (uint32_t i=0; i < ciphertext_decoded_len; i++) + { + printf("%02x ", ciphertext_decoded[i]); + } + printf("\n"); +#endif + + // Crypto Service returns aad - cipher_text - tag + memcpy(data_out,ciphertext_decoded,len_data_out); + return status; +} + +static int32_t cryptography_decrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len, + uint8_t* ecs, uint8_t* acs, char* cam_cookies) +{int32_t status = CRYPTO_LIB_SUCCESS; + key = key; // Direct key input is not supported in KMC interface + ecs = ecs; + acs = acs; + + // Get the key length in bits, in string format. + // TODO -- Parse the key length from the keyInfo endpoint of the Crypto Service! + uint32_t key_len_in_bits = len_key * 8; // 8 bits per byte. + uint32_t key_len_in_bits_str_len = 0; + char* key_len_in_bits_str = int_to_str(key_len_in_bits, &key_len_in_bits); + + curl_easy_reset(curl); + status = configure_curl_connect_opts(curl, cam_cookies); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } + // Base64 URL encode IV for KMC REST Encrypt + char* iv_base64 = (char*)calloc(1,B64ENCODE_OUT_SAFESIZE(iv_len)+1); + base64urlEncode(iv,iv_len,iv_base64,NULL); + + uint8_t* decrypt_payload = data_in; + size_t decrypt_payload_len = len_data_in; + +#ifdef DEBUG + printf("IV Base64 URL Encoded: %s\n",iv_base64); +#endif + + + if(sa_ptr->ek_ref == NULL) + { + status = CRYPTOGRAHPY_KMC_NULL_ENCRYPTION_KEY_REFERENCE_IN_SA; + return status; + } + + char* decrypt_uri; + + int len_decrypt_endpoint = strlen(decrypt_endpoint)+ key_len_in_bits_str_len + strlen(sa_ptr->ek_ref)+strlen(iv_base64)+strlen(AES_CBC_TRANSFORMATION) + strlen(AES_CRYPTO_ALGORITHM); + char* decrypt_endpoint_final = (char*) malloc(len_decrypt_endpoint); + + snprintf(decrypt_endpoint_final,len_decrypt_endpoint,decrypt_endpoint,key_len_in_bits_str,sa_ptr->ek_ref,AES_CBC_TRANSFORMATION, iv_base64, AES_CRYPTO_ALGORITHM); + free(key_len_in_bits_str); + decrypt_uri = (char*) malloc(strlen(kmc_root_uri)+len_decrypt_endpoint); + decrypt_uri[0] = '\0'; + strcat(decrypt_uri, kmc_root_uri); + strcat(decrypt_uri, decrypt_endpoint_final); + +#ifdef DEBUG + printf("Decrypt URI: %s\n",decrypt_uri); +#endif + curl_easy_setopt(curl, CURLOPT_URL, decrypt_uri); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, http_headers_list); + + memory_write* chunk_write = (memory_write*) calloc(1,MEMORY_WRITE_SIZE); + memory_read* chunk_read = (memory_read*) calloc(1,MEMORY_READ_SIZE);; + + /* Configure CURL for POST */ + curl_easy_setopt(curl, CURLOPT_POST, 1L); + /* send all data to this function */ + curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback); + /* we pass our 'chunk' struct to the callback function */ + curl_easy_setopt(curl, CURLOPT_READDATA, chunk_read); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); + /* we pass our 'chunk' struct to the callback function */ + curl_easy_setopt(curl, CURLOPT_WRITEDATA, chunk_write); + + /* size of the POST data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) decrypt_payload_len); + /* binary data */ + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, decrypt_payload); + +#ifdef DEBUG + printf("Len of decrypt payload: %ld\n",decrypt_payload_len); + printf("Data to Decrypt: \n"); + for (uint32_t i=0; i < decrypt_payload_len; i++) + { + printf("%02x ", decrypt_payload[i]); + } + printf("\n"); +#endif + + status = curl_perform_with_cam_retries(curl, chunk_write, chunk_read); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } + + /* JSON Response Handling */ + + // Parse the JSON string response + jsmn_parser p; + jsmntok_t t[64]; /* We expect no more than 64 JSON tokens */ + jsmn_init(&p); + int parse_result = jsmn_parse(&p, chunk_write->response, strlen(chunk_write->response), t, 64); // "chunk->response" is the char array holding the json content + + // Find the 'base64ciphertext' token + if (parse_result < 0) { + status = CRYPTOGRAHPY_KMC_CRYPTO_JSON_PARSE_ERROR; + printf("Failed to parse JSON: %d\n", parse_result); + return status; + } + + int json_idx = 0; + uint8_t ciphertext_found = CRYPTO_FALSE; + char* cleartext_base64 = NULL; + for (json_idx = 1; json_idx < parse_result; json_idx++) + { + // check "httpCode" field for non-200 status codes!!! + if (jsoneq(chunk_write->response, &t[json_idx], "base64cleartext") == 0) + { + /* We may use strndup() to fetch string value */ +#ifdef DEBUG + printf("Json base64cleartext: %.*s\n", t[json_idx + 1].end - t[json_idx + 1].start, + chunk_write->response + t[json_idx + 1].start); +#endif + uint32_t len_cleartext = t[json_idx + 1].end - t[json_idx + 1].start; + cleartext_base64 = malloc(len_cleartext+1); + memcpy(cleartext_base64,chunk_write->response + t[json_idx + 1].start, len_cleartext); + cleartext_base64[len_cleartext] = '\0'; +#ifdef DEBUG + printf("Parsed base64cleartext: %s\n",cleartext_base64); +#endif + json_idx++; + ciphertext_found = CRYPTO_TRUE; + continue; + } + if (jsoneq(chunk_write->response, &t[json_idx], "httpCode") == 0) + { + /* We may use strndup() to fetch string value */ +#ifdef DEBUG + printf("httpCode: %.*s\n", t[json_idx + 1].end - t[json_idx + 1].start, + chunk_write->response + t[json_idx + 1].start); +#endif + uint32_t len_httpcode = t[json_idx + 1].end - t[json_idx + 1].start; + char* http_code_str = malloc(len_httpcode+1); + memcpy(http_code_str,chunk_write->response + t[json_idx + 1].start, len_httpcode); + http_code_str[len_httpcode] = '\0'; + int http_code = atoi(http_code_str); +#ifdef DEBUG + printf("Parsed http code: %d\n",http_code); +#endif + if(http_code != 200) + { + status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE; + fprintf(stderr,"KMC Crypto Failure Response:\n%s\n",chunk_write->response); + return status; + } + free(http_code_str); + json_idx++; + continue; + } + } + if(ciphertext_found == CRYPTO_FALSE){ + status = CRYPTOGRAHPY_KMC_CIPHER_TEXT_NOT_FOUND_IN_JSON_RESPONSE; + return status; + } + + /* JSON Response Handling End */ + + uint8_t* cleartext_decoded = malloc((len_data_out)*2 + 1); + size_t cleartext_decoded_len = 0; + base64Decode(cleartext_base64,strlen(cleartext_base64),cleartext_decoded, &cleartext_decoded_len); +#ifdef DEBUG + printf("Decoded Cipher Text Length: %ld\n",cleartext_decoded_len); + printf("Decoded Cipher Text: \n"); + for (uint32_t i=0; i < cleartext_decoded_len; i++) + { + printf("%02x ", cleartext_decoded[i]); + } + printf("\n"); +#endif + + // Copy the decrypted data to the output stream + // Crypto Service returns aad - clear_text + memcpy(data_out,cleartext_decoded, len_data_out); + + return status; +} + + static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -264,7 +661,7 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs) + uint8_t ecs, uint8_t acs, char* cam_cookies) { int32_t status = CRYPTO_LIB_SUCCESS; @@ -275,10 +672,13 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, iv = iv; iv_len = iv_len; ecs = ecs; - + curl_easy_reset(curl); - configure_curl_connect_opts(curl); - + status = configure_curl_connect_opts(curl, cam_cookies); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } // Base64 URL encode IV for KMC REST Encrypt // Not needed for CMAC/HMAC (only supported auth ciphers now) // char* iv_base64 = (char*)calloc(1,B64ENCODE_OUT_SAFESIZE(iv_len)+1); @@ -352,30 +752,12 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, printf("\n"); #endif - CURLcode res; - res = curl_easy_perform(curl); - - if(res != CURLE_OK) // This is not return code, this is successful response! - { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AUTHENTICATION_ERROR; - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - return status; - } - -#ifdef DEBUG - printf("\ncURL Authenticate Response:\n\t %s\n",chunk_write->response); -#endif - - if(chunk_write->response == NULL) // No response, possibly because service is CAM secured. + status = curl_perform_with_cam_retries(curl, chunk_write, chunk_read); + if(status != CRYPTO_LIB_SUCCESS) { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; - fprintf(stderr, "curl_easy_perform() unexpected empty response: \n%s\n", - "Empty Crypto Service response can be caused by CAM security, CryptoLib doesn't support a CAM secured KMC Crypto Service."); return status; } - /* JSON Response Handling */ // Parse the JSON string response @@ -471,6 +853,7 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, return status; } json_idx++; + free(http_code_str); continue; } @@ -507,7 +890,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs) + uint8_t ecs, uint8_t acs, char* cam_cookies) { int32_t status = CRYPTO_LIB_SUCCESS; @@ -534,8 +917,11 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le } curl_easy_reset(curl); - configure_curl_connect_opts(curl); - + status = configure_curl_connect_opts(curl, cam_cookies); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } uint8_t* auth_payload = aad; size_t auth_payload_len = aad_len; @@ -564,7 +950,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le int len_auth_endpoint = strlen(icv_verify_endpoint)+strlen(mac_base64)+strlen(sa_ptr->ak_ref)+strlen(auth_algorithm)+mac_size_str_len; char* auth_endpoint_final = (char*) malloc(len_auth_endpoint); snprintf(auth_endpoint_final,len_auth_endpoint,icv_verify_endpoint,mac_base64,sa_ptr->ak_ref,auth_algorithm,mac_size_str); - + free(mac_size_str); char* auth_uri = (char*) malloc(strlen(kmc_root_uri)+len_auth_endpoint); auth_uri[0] = '\0'; strcat(auth_uri, kmc_root_uri); @@ -605,26 +991,9 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le printf("\n"); #endif - CURLcode res; - res = curl_easy_perform(curl); - - if(res != CURLE_OK) // This is not return code, this is successful response! - { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AUTHENTICATION_ERROR; - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - return status; - } - -#ifdef DEBUG - printf("\ncURL Authenticate Response:\n\t %s\n",chunk_write->response); -#endif - - if(chunk_write->response == NULL) // No response, possibly because service is CAM secured. + status = curl_perform_with_cam_retries(curl, chunk_write, chunk_read); + if(status != CRYPTO_LIB_SUCCESS) { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; - fprintf(stderr, "curl_easy_perform() unexpected empty response: \n%s\n", - "Empty Crypto Service response can be caused by CAM security, CryptoLib doesn't support a CAM secured KMC Crypto Service."); return status; } @@ -669,6 +1038,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le return status; } json_idx++; + free(http_code_str); continue; } @@ -713,7 +1083,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t encrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs) + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies) { int32_t status = CRYPTO_LIB_SUCCESS; key = key; // Direct key input is not supported in KMC interface @@ -722,8 +1092,11 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, acs = acs; curl_easy_reset(curl); - configure_curl_connect_opts(curl); - + status = configure_curl_connect_opts(curl, cam_cookies); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } // Base64 URL encode IV for KMC REST Encrypt char* iv_base64 = (char*)calloc(1,B64ENCODE_OUT_SAFESIZE(iv_len)+1); base64urlEncode(iv,iv_len,iv_base64,NULL); @@ -738,6 +1111,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, if(sa_ptr->ek_ref == NULL) { status = CRYPTOGRAHPY_KMC_NULL_ENCRYPTION_KEY_REFERENCE_IN_SA; + free(iv_base64); return status; } @@ -759,6 +1133,11 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, snprintf(encrypt_endpoint_final,len_encrypt_endpoint,encrypt_offset_endpoint,sa_ptr->ek_ref,AES_GCM_TRANSFORMATION, iv_base64,aad_offset_str,mac_size_str); + free(aad_offset_str); + free(mac_size_str); +#ifdef DEBUG + printf("KMC ROOT URI: %s\n",kmc_root_uri); +#endif encrypt_uri = (char*) malloc(strlen(kmc_root_uri)+len_encrypt_endpoint); encrypt_uri[0] = '\0'; strcat(encrypt_uri, kmc_root_uri); @@ -783,6 +1162,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, { memcpy(&encrypt_payload[aad_len],data_in,len_data_in); } + free(encrypt_endpoint_final); } else //No AAD -- just prepare the endpoint URI { @@ -795,6 +1175,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, encrypt_uri[0] = '\0'; strcat(encrypt_uri, kmc_root_uri); strcat(encrypt_uri, encrypt_endpoint_final); + free(encrypt_endpoint_final); } #ifdef DEBUG @@ -830,26 +1211,22 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, printf("\n"); #endif - CURLcode res; - res = curl_easy_perform(curl); - - if(res != CURLE_OK) // This is not return code, this is successful response! + status = curl_perform_with_cam_retries(curl,chunk_write,chunk_read); +#ifdef DEBUG + printf("Curl Perform Final Status Code: %d\n",status); + if(chunk_write->response != NULL) { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_ENCRYPT_ERROR; - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - return status; + printf("Chunk Write Response Length: %ld\n", strlen(chunk_write->response)); + printf("Chunk Write Response: %s\n", chunk_write->response); } - -#ifdef DEBUG - printf("\ncURL Encrypt Response:\n\t %s\n",chunk_write->response); #endif - - if(chunk_write->response == NULL) // No response, possibly because service is CAM secured. + if(status != CRYPTO_LIB_SUCCESS) { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; - fprintf(stderr, "curl_easy_perform() unexpected empty response: \n%s\n", - "Empty Crypto Service response can be caused by CAM security, CryptoLib doesn't support a CAM secured KMC Crypto Service."); + free(iv_base64); + free(encrypt_uri); + free(chunk_write); + free(chunk_read); + free(encrypt_payload); return status; } @@ -865,6 +1242,11 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, if (parse_result < 0) { status = CRYPTOGRAHPY_KMC_CRYPTO_JSON_PARSE_ERROR; printf("Failed to parse JSON: %d\n", parse_result); + free(iv_base64); + free(encrypt_uri); + free(chunk_write); + free(chunk_read); + free(encrypt_payload); return status; } @@ -911,15 +1293,29 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, { status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE; fprintf(stderr,"KMC Crypto Failure Response:\n%s\n",chunk_write->response); + free(iv_base64); + free(http_code_str); + free(encrypt_uri); + free(ciphertext_base64); + free(chunk_write); + free(chunk_read); + free(encrypt_payload); return status; } json_idx++; + free(http_code_str); continue; } } if(ciphertext_found == CRYPTO_FALSE){ status = CRYPTOGRAHPY_KMC_CIPHER_TEXT_NOT_FOUND_IN_JSON_RESPONSE; + free(encrypt_uri); + free(iv_base64); + free(ciphertext_base64); + free(chunk_write); + free(chunk_read); + free(encrypt_payload); return status; } @@ -954,6 +1350,14 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, if(encrypt_bool == CRYPTO_FALSE) { data_offset = 0; } memcpy(mac,ciphertext_decoded + aad_len + data_offset, mac_size); } + free(ciphertext_base64); + free(ciphertext_decoded); + free(iv_base64); + free(encrypt_uri); + free(encrypt_payload); + free(chunk_write->response); + free(chunk_write); + free(chunk_read); return status; } static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, @@ -964,7 +1368,7 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t decrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs) + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies) { int32_t status = CRYPTO_LIB_SUCCESS; key = key; // Direct key input is not supported in KMC interface @@ -980,7 +1384,11 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, curl_easy_reset(curl); - configure_curl_connect_opts(curl); + status = configure_curl_connect_opts(curl, cam_cookies); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } // Base64 URL encode IV for KMC REST Encrypt char* iv_base64 = (char*)calloc(1,B64ENCODE_OUT_SAFESIZE(iv_len)+1); @@ -1018,6 +1426,10 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, snprintf(decrypt_endpoint_final,len_decrypt_endpoint,decrypt_offset_endpoint,key_len_in_bits_str,sa_ptr->ek_ref,AES_GCM_TRANSFORMATION, iv_base64, AES_CRYPTO_ALGORITHM, mac_size_str, aad_offset_str); + free(key_len_in_bits_str); + free(aad_offset_str); + free(mac_size_str); + decrypt_uri = (char*) malloc(strlen(kmc_root_uri)+len_decrypt_endpoint); decrypt_uri[0] = '\0'; strcat(decrypt_uri, kmc_root_uri); @@ -1047,6 +1459,8 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, if(decrypt_bool == CRYPTO_FALSE) { data_offset = 0; } memcpy(&decrypt_payload[aad_len + data_offset],mac,mac_size); } + + free(decrypt_endpoint_final); } else //No AAD - just prepare the endpoint URI string { @@ -1059,6 +1473,7 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, decrypt_uri[0] = '\0'; strcat(decrypt_uri, kmc_root_uri); strcat(decrypt_uri, decrypt_endpoint_final); + free(decrypt_endpoint_final); } #ifdef DEBUG printf("Decrypt URI: %s\n",decrypt_uri); @@ -1094,26 +1509,12 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, printf("\n"); #endif - CURLcode res; - res = curl_easy_perform(curl); - - if(res != CURLE_OK) // This is not return code, this is successful response! - { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_DECRYPT_ERROR; - fprintf(stderr, "curl_easy_perform() failed: %s\n", - curl_easy_strerror(res)); - return status; - } - -#ifdef DEBUG - printf("\ncURL Decrypt Response:\n\t %s\n",chunk_write->response); -#endif - - if(chunk_write->response == NULL) // No response, possibly because service is CAM secured. + status = curl_perform_with_cam_retries(curl, chunk_write, chunk_read); + if(status != CRYPTO_LIB_SUCCESS) { - status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; - fprintf(stderr, "curl_easy_perform() unexpected empty response: \n%s\n", - "Empty Crypto Service response can be caused by CAM security, CryptoLib doesn't support a CAM secured KMC Crypto Service."); + free(decrypt_payload); + free(decrypt_uri); + free(iv_base64); return status; } @@ -1129,6 +1530,9 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, if (parse_result < 0) { status = CRYPTOGRAHPY_KMC_CRYPTO_JSON_PARSE_ERROR; printf("Failed to parse JSON: %d\n", parse_result); + free(decrypt_payload); + free(decrypt_uri); + free(iv_base64); return status; } @@ -1175,14 +1579,27 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, { status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE; fprintf(stderr,"KMC Crypto Failure Response:\n%s\n",chunk_write->response); + free(chunk_read); + free(chunk_write); + free(http_code_str); + free(cleartext_base64); + free(decrypt_uri); + free(iv_base64); return status; } + free(http_code_str); json_idx++; continue; } } if(ciphertext_found == CRYPTO_FALSE){ status = CRYPTOGRAHPY_KMC_CIPHER_TEXT_NOT_FOUND_IN_JSON_RESPONSE; + free(chunk_read); + free(chunk_write); + free(cleartext_base64); + free(decrypt_payload); + free(decrypt_uri); + free(iv_base64); return status; } @@ -1207,6 +1624,14 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, { memcpy(data_out,cleartext_decoded + aad_len, len_data_out); } + free(cleartext_decoded); + free(chunk_read); + free(chunk_write->response); + free(chunk_write); + free(cleartext_base64); + free(decrypt_payload); + free(decrypt_uri); + free(iv_base64); return status; } @@ -1285,8 +1710,16 @@ static size_t read_callback(char* dest, size_t size, size_t nmemb, void* userp) return 0; /* no more data left to deliver */ } -static void configure_curl_connect_opts(CURL* curl_handle) +static int32_t configure_curl_connect_opts(CURL* curl_handle, char* cam_cookies) { + int32_t status = CRYPTO_LIB_SUCCESS; + + status = handle_cam_cookies(curl_handle, cam_cookies); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } + //curl_easy_setopt(curl_handle, CURLOPT_PROTOCOLS,CURLPROTO_HTTPS); // use default CURLPROTO_ALL #ifdef DEBUG printf("KMC Crypto Port: %d\n",cryptography_kmc_crypto_config->kmc_crypto_port); @@ -1328,7 +1761,157 @@ static void configure_curl_connect_opts(CURL* curl_handle) curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYHOST, 0L); curl_easy_setopt(curl_handle, CURLOPT_SSL_VERIFYPEER, 0L); } + + return status; +} + +static int32_t handle_cam_cookies(CURL* curl_handle, char* cam_cookies) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + + if(cam_config != NULL) + { + if(cam_config->cam_enabled) + { + if(cam_cookies != NULL) // cam_cookies passed in should ALWAYS take precedence in what's used + { + curl_easy_setopt(curl_handle, CURLOPT_COOKIE, cam_cookies); + return status; + } + + if(cam_config->cookie_file_path == NULL) // all auth methods rely on cookie file sets/gets, error if null + { + status = CAM_INVALID_COOKIE_FILE_CONFIGURATION_NULL; + return status; + } + else + { +#ifdef DEBUG + printf("Setting CURLOPT_COOKIEFILE: %s\n",cam_config->cookie_file_path); +#endif + curl_easy_setopt(curl_handle, CURLOPT_COOKIEFILE, cam_config->cookie_file_path); + } + } + } + return status; +} + +static int32_t get_cam_sso_token() +{ + int32_t status = CRYPTO_LIB_SUCCESS; + CURL* curl_cam; + CURLcode res; + + if(cam_config->login_method == CAM_LOGIN_KEYTAB_FILE) + { + status = initialize_kerberos_keytab_file_login(); + if(status != CRYPTO_LIB_SUCCESS) + { + return status; + } + } + + curl_cam = curl_easy_init(); + + if(curl_cam == NULL) + { + status = CRYPTOGRAPHY_KMC_CURL_INITIALIZATION_FAILURE; + return status; + } + + // Set CA verification options for curl_cam handle from KMC Crypto Configs... + if(cryptography_kmc_crypto_config->mtls_ca_bundle != NULL){ + curl_easy_setopt(curl_cam, CURLOPT_CAINFO, cryptography_kmc_crypto_config->mtls_ca_bundle); + } + if(cryptography_kmc_crypto_config->mtls_ca_path != NULL){ + curl_easy_setopt(curl_cam, CURLOPT_CAPATH, cryptography_kmc_crypto_config->mtls_ca_path); + } + if(cryptography_kmc_crypto_config->mtls_issuer_cert != NULL){ + curl_easy_setopt(curl_cam, CURLOPT_ISSUERCERT, cryptography_kmc_crypto_config->mtls_issuer_cert); + } + if(cryptography_kmc_crypto_config->ignore_ssl_hostname_validation == CRYPTO_TRUE){ + curl_easy_setopt(curl_cam, CURLOPT_SSL_VERIFYHOST, 0L); + curl_easy_setopt(curl_cam, CURLOPT_SSL_VERIFYPEER, 0L); + } + + if(cam_config->access_manager_uri == NULL) + { + status = CAM_INVALID_CONFIGURATION_ACCESS_MANAGER_URI_NULL; + return status; + } + + if(cam_config->cookie_file_path == NULL) + { + status = CAM_INVALID_COOKIE_FILE_CONFIGURATION_NULL; + return status; + } + + curl_easy_setopt(curl_cam,CURLOPT_HTTPAUTH,CURLAUTH_NEGOTIATE); + + if(cam_config->username != NULL) + { + curl_easy_setopt(curl_cam, CURLOPT_USERNAME, cam_config->username); + } else + { + curl_easy_setopt(curl_cam, CURLOPT_USERNAME, ":"); + } + + // Build the CAM getSsoToken URI + int len_kerberos_endpoint = strlen(cam_kerberos_uri) + strlen(cam_config->access_manager_uri); + char* kerberos_endpoint_final = (char*) malloc(len_kerberos_endpoint + 1); + snprintf(kerberos_endpoint_final,len_kerberos_endpoint,cam_kerberos_uri,cam_config->access_manager_uri); + +#ifdef DEBUG + printf("CAM Kerberos Endpoint: %s\n",kerberos_endpoint_final); +#endif + curl_easy_setopt(curl_cam,CURLOPT_URL,kerberos_endpoint_final); + + memory_read* chunk_read = (memory_read*) calloc(1,MEMORY_READ_SIZE); + memory_write* chunk_write = (memory_write*) calloc(1,MEMORY_WRITE_SIZE); + + /* Configure CURL for POST */ + curl_easy_setopt(curl_cam, CURLOPT_POST, 1L); + /* send all data to this function */ + curl_easy_setopt(curl_cam, CURLOPT_READFUNCTION, read_callback); + curl_easy_setopt(curl_cam,CURLOPT_WRITEFUNCTION, write_callback); + /* we pass our 'chunk' struct to the callback function */ + curl_easy_setopt(curl_cam, CURLOPT_READDATA, chunk_read); + curl_easy_setopt(curl_cam,CURLOPT_WRITEDATA,chunk_write); + curl_easy_setopt(curl_cam, CURLOPT_COOKIEJAR, cam_config->cookie_file_path); + + res = curl_easy_perform(curl_cam); + + if(res != CURLE_OK) // This is not return code, this is successful response that's unusable! + { + status = CAM_GET_SSO_TOKEN_FAILURE; + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + + //return status; + } + + long response_code; + curl_easy_getinfo(curl_cam, CURLINFO_RESPONSE_CODE, &response_code); +#ifdef DEBUG + printf("Response Code From getSsoToken: %ld\n",response_code); +#endif + + if(response_code == 408) // CAM getSsoToken timeout error + { + status = CAM_KERBEROS_REQUEST_TIME_OUT; + } + else if(response_code != 200) //getSsoToken failed! + { + status = CAM_GET_SSO_TOKEN_FAILURE; + } + // Cookies don't write to COOKIEJAR until cleanup. + curl_easy_cleanup(curl_cam); + free(kerberos_endpoint_final); + free(chunk_read); + free(chunk_write); + return status; } + static char* int_to_str(uint32_t int_src, uint32_t* converted_str_length) { int int_str_len = snprintf( NULL, 0, "%d", int_src); @@ -1349,6 +1932,173 @@ static int jsoneq(const char* json, jsmntok_t* tok, const char* s) return -1; } +int32_t curl_response_error_check(CURL* curl_handle, char* response) +{ + int32_t response_status = CRYPTO_LIB_SUCCESS; + + long response_code; + curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &response_code); + +#ifdef DEBUG + printf("cURL response code: %ld\n",response_code); +#endif + + if(response_code == 302) // redirected + { + if(cam_config != NULL) + { + if(cam_config->cam_enabled && cam_config->login_method == CAM_LOGIN_NONE) // redirect with cam enabled and no auth method means auth failure + { + response_status = CAM_AUTHENTICATION_FAILURE_REDIRECT; + return response_status; + } else if (cam_config->cam_enabled && (cam_config->login_method == CAM_LOGIN_KERBEROS || cam_config->login_method == CAM_LOGIN_KEYTAB_FILE)) + { + response_status = CAM_AUTHENTICATION_REQUIRED; + return response_status; + } + else // CAM not enabled, but cam is configured and 302 received -- likely misconfiguration! + { + response_status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE; + return response_status; + } + } + } + + if(response_code != 200) // unhandled error case + { + response_status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE; + return response_status; + } + +#ifdef DEBUG + printf("\ncURL Response Body:\n\t %s\n", response); +#endif + + if(response == NULL) // No response, possibly because service is CAM secured. + { + response_status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; + fprintf(stderr, "curl_easy_perform() unexpected empty response: \n%s\n", + "Empty Crypto Service response can be caused by CAM security, is CAM configured?"); + return response_status; + } + + return response_status; + +} + +int32_t curl_perform_with_cam_retries(CURL* curl_handle,memory_write* chunk_write, memory_read* chunk_read) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + uint8_t cam_retry = 0; + while(cam_retry < CAM_MAX_AUTH_RETRIES) + { +#ifdef DEBUG + printf("Entering CAM Authentication Retry Loop, Loop #: %d\n",cam_retry); +#endif + CURLcode res; + res = curl_easy_perform(curl_handle); + + if(res != CURLE_OK) // This is not a response w/return code, this is something breaking! + { + status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE; + fprintf(stderr, "curl_easy_perform() failed: %s\n", + curl_easy_strerror(res)); + break; // Go to Post retry loop cleanup and return status. + } + + status = curl_response_error_check(curl_handle, chunk_write->response); + + if(status == CRYPTO_LIB_SUCCESS) // Crypto Service REST call worked! Break out of retry loop. + { + break; + } + else + { + // Zero out chunk_write/chunk_read for next cURL perform call + memset(chunk_write,0,MEMORY_WRITE_SIZE); + memset(chunk_read,0,MEMORY_READ_SIZE); + } + + if(status == CAM_AUTHENTICATION_REQUIRED) // CAM_AUTHENTICATION_REQUIRED code indicates CAM config setup for authentication + { +#ifdef DEBUG + printf("Attempting to authenticate and retrieve CAM SSO Token.\n"); +#endif + status = get_cam_sso_token(); + if(status == CAM_KERBEROS_REQUEST_TIME_OUT) + { + //Non-fatal getSsoToken failure... Attempt CAM retry... + } + else if(status != CRYPTO_LIB_SUCCESS) + { + return status; // Fatal getSsoToken error, break + } + else if(status == CRYPTO_LIB_SUCCESS) + { + //Re-handle CAM cookie file, when cookie file is regenerated above, the existing curl_handle doesn't recognize it + status = handle_cam_cookies(curl_handle,NULL); + } + } else // Conditions not met for CAM retry! Break out of retry loop. + { + break; + } + cam_retry++; + if(cam_retry == CAM_MAX_AUTH_RETRIES) + { + status = CAM_MAX_AUTH_RETRIES_REACHED; + } + } + if(status == CRYPTO_LIB_SUCCESS && chunk_write->response==NULL) // no error case detected, but invalid NULL response! + { + status = CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE; + } + return status; +} + +/** + * @brief Function: initialize_kerberos_keytab_file_login + * + * This function launches the kinit kerberos utility to initialize credentials for the current user. + * It may be possible to do this with the KRB5 API, but for now all we are doing is launching the kinit utility. + * Consider using the KRB5 APIs in a future release: https://web.mit.edu/kerberos/krb5-devel/doc/appdev/refs/api/index.html + * + * +**/ +int32_t initialize_kerberos_keytab_file_login(void) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + + if(cam_config->keytab_file_path == NULL) + { + status = CAM_INVALID_CONFIGURATION_KEYTAB_FILE_PATH_NULL; + return status; + } + + if(cam_config->username == NULL) + { + status = CAM_INVALID_CONFIGURATION_KEYTAB_FILE_USERNAME_NULL; + return status; + } + + // Build the kinit shell command with keytab file path + username + char* kinit_shell_command_base = "kinit -kt %s %s"; + uint32_t len_kinit_shell_command = strlen(kinit_shell_command_base) + strlen(cam_config->keytab_file_path) + strlen(cam_config->username); + char* kinit_shell_command = malloc(len_kinit_shell_command + 1); + snprintf(kinit_shell_command,len_kinit_shell_command,kinit_shell_command_base,cam_config->keytab_file_path,cam_config->username); + + int32_t kinit_status = system(kinit_shell_command); +#ifdef DEBUG + printf("Kinit Status: %d\n", kinit_status); +#endif + if(kinit_status != CRYPTO_LIB_SUCCESS) + { + status = CAM_KEYTAB_FILE_KINIT_FAILURE; + } + + free(kinit_shell_command); + return status; +} + /** * @brief Function: cryptography_get_acs_algo. Maps Cryptolib ACS enums to KMC enums * It is possible for supported algos to vary between crypto libraries diff --git a/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c b/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c index 3f8908a1..c5205b96 100644 --- a/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c +++ b/src/src_cryptography/src_libgcrypt/cryptography_interface_libgcrypt.template.c @@ -26,8 +26,17 @@ static int32_t cryptography_init(void); static crypto_key_t* get_ek_ring(void); static int32_t cryptography_shutdown(void); // Cryptography Interface Functions -static int32_t cryptography_encrypt(void); -static int32_t cryptography_decrypt(void); +static int32_t cryptography_encrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len,uint8_t* ecs, uint8_t padding, char* cam_cookies); +static int32_t cryptography_decrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len, + uint8_t* ecs, uint8_t* acs, char* cam_cookies); static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -35,7 +44,7 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs); + uint8_t ecs, uint8_t acs, char* cam_cookies); static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -43,7 +52,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs); + uint8_t ecs, uint8_t acs, char* cam_cookies); static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -52,7 +61,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t encrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs); + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies); static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -61,9 +70,11 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t decrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs); + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies); static int32_t cryptography_get_acs_algo(int8_t algo_enum); static int32_t cryptography_get_ecs_algo(int8_t algo_enum); +static int32_t cryptography_get_ecs_mode(int8_t algo_enum); + /* ** Module Variables */ @@ -548,8 +559,7 @@ static int32_t cryptography_init(void) return status; } static int32_t cryptography_shutdown(void){ return CRYPTO_LIB_SUCCESS; } -static int32_t cryptography_encrypt(void){ return CRYPTO_LIB_SUCCESS; } -static int32_t cryptography_decrypt(void){ return CRYPTO_LIB_SUCCESS; } + static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -557,7 +567,7 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs) + uint8_t ecs, uint8_t acs, char* cam_cookies) { gcry_error_t gcry_error = GPG_ERR_NO_ERROR; gcry_mac_hd_t tmp_mac_hd; @@ -580,6 +590,7 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out, // Using to fix warning len_data_out = len_data_out; ecs = ecs; + cam_cookies = cam_cookies; // Select correct libgcrypt acs enum int32_t algo = cryptography_get_acs_algo(acs); @@ -675,7 +686,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le uint8_t* iv, uint32_t iv_len, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, - uint8_t ecs, uint8_t acs) + uint8_t ecs, uint8_t acs, char* cam_cookies) { gcry_error_t gcry_error = GPG_ERR_NO_ERROR; gcry_mac_hd_t tmp_mac_hd; @@ -698,6 +709,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le // Using to fix warning len_data_out = len_data_out; ecs = ecs; + cam_cookies = cam_cookies; // Select correct libgcrypt acs enum int32_t algo = cryptography_get_acs_algo(acs); @@ -823,6 +835,138 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le gcry_mac_close(tmp_mac_hd); return status; } + +static int32_t cryptography_encrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len,uint8_t* ecs, uint8_t padding, char* cam_cookies) +{ + gcry_error_t gcry_error = GPG_ERR_NO_ERROR; + gcry_cipher_hd_t tmp_hd; + int32_t status = CRYPTO_LIB_SUCCESS; + uint8_t* key_ptr = key; + + data_out = data_out; // TODO: Look into tailoring these out, as they're not used or needed. + len_data_out = len_data_out; + padding = padding; + cam_cookies = cam_cookies; + + if(sa_ptr != NULL) //Using SA key pointer + { + key_ptr = &(ek_ring[sa_ptr->ekid].value[0]); + } + + // Select correct libgcrypt algorith enum + int32_t algo = -1; + if (ecs != NULL) + { + algo = cryptography_get_ecs_algo(*ecs); + if (algo == CRYPTO_LIB_ERR_UNSUPPORTED_MODE) + { + return CRYPTO_LIB_ERR_UNSUPPORTED_MODE; + } + } + else + { + return CRYPTO_LIB_ERR_NULL_MODE_PTR; + } + + // Verify the mode to accompany the algorithm enum + int32_t mode = -1; + mode = cryptography_get_ecs_mode(*ecs); + if (mode == CRYPTO_LIB_ERR_UNSUPPORTED_MODE) return CRYPTO_LIB_ERR_UNSUPPORTED_MODE; + + // Check that key length to be used is atleast as long as the algo requirement + if (sa_ptr != NULL && len_key > ek_ring[sa_ptr->ekid].key_len) + { + return CRYPTO_LIB_ERR_KEY_LENGTH_ERROR; + } + + gcry_error = gcry_cipher_open(&(tmp_hd), algo, mode, GCRY_CIPHER_NONE); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + return status; + } + gcry_error = gcry_cipher_setkey(tmp_hd, key_ptr, len_key); + +#ifdef SA_DEBUG + uint32_t i; + printf(KYEL "Printing Key:\n\t"); + for (i = 0; i < len_key; i++) + { + printf("%02X", *(key_ptr + i)); + } + printf("\n"); +#endif + + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + gcry_cipher_close(tmp_hd); + return status; + } + gcry_error = gcry_cipher_setiv(tmp_hd, iv, iv_len); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + gcry_cipher_close(tmp_hd); + return status; + } + +#ifdef TC_DEBUG + size_t j; + printf("Input payload length is %ld\n", len_data_in); + printf(KYEL "Printing Frame Data prior to encryption:\n\t"); + for (j = 0; j < len_data_in; j++) + { + printf("%02X", *(data_in + j)); + } + printf("\n"); +#endif + + + gcry_error = gcry_cipher_encrypt(tmp_hd, data_in, len_data_in, NULL, 0); + // TODO: Add PKCS#7 padding to data_in, and increment len_data_in to match necessary block size + // TODO: Remember to remove the padding. + // TODO: Does this interfere with max frame size? Does that need to be taken into account? + // gcry_error = gcry_cipher_encrypt(tmp_hd, + // data_out, // ciphertext output + // len_data_out, // length of data + // data_in, // plaintext input + // len_data_in // in data length + // ); + + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_encrypt error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_ENCRYPTION_ERROR; + gcry_cipher_close(tmp_hd); + return status; + } + +#ifdef TC_DEBUG + printf("Output payload length is %ld\n", len_data_out); + printf(KYEL "Printing TC Frame Data after encryption:\n\t"); + for (j = 0; j < len_data_out; j++) + { + printf("%02X", *(data_out + j)); + } + printf("\n"); +#endif + + gcry_cipher_close(tmp_hd); + return status; +} + static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -831,7 +975,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t encrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs) + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies) { gcry_error_t gcry_error = GPG_ERR_NO_ERROR; gcry_cipher_hd_t tmp_hd; @@ -840,6 +984,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, // Fix warning acs = acs; + cam_cookies = cam_cookies; if(sa_ptr != NULL) //Using SA key pointer { @@ -861,13 +1006,18 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, return CRYPTO_LIB_ERR_NULL_ECS_PTR; } + // Verify the mode to accompany the ecs enum + int32_t mode = -1; + mode = cryptography_get_ecs_mode(*ecs); + if (mode == CRYPTO_LIB_ERR_UNSUPPORTED_ECS_MODE) return CRYPTO_LIB_ERR_UNSUPPORTED_ECS_MODE; + // Check that key length to be used is atleast as long as the algo requirement if (sa_ptr != NULL && len_key > ek_ring[sa_ptr->ekid].key_len) { return CRYPTO_LIB_ERR_KEY_LENGTH_ERROR; } - gcry_error = gcry_cipher_open(&(tmp_hd), GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_GCM, GCRY_CIPHER_NONE); + gcry_error = gcry_cipher_open(&(tmp_hd), algo, mode, GCRY_CIPHER_NONE); if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) { printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); @@ -934,6 +1084,9 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, if(encrypt_bool == CRYPTO_TRUE) { + // TODO: Add PKCS#7 padding to data_in, and increment len_data_in to match necessary block size + // TODO: Remember to remove the padding. + // TODO: Does this interfere with max frame size? Does that need to be taken into account? gcry_error = gcry_cipher_encrypt(tmp_hd, data_out, // ciphertext output len_data_out, // length of data @@ -999,6 +1152,101 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out, return status; } +static int32_t cryptography_decrypt(uint8_t* data_out, size_t len_data_out, + uint8_t* data_in, size_t len_data_in, + uint8_t* key, uint32_t len_key, + SecurityAssociation_t* sa_ptr, + uint8_t* iv, uint32_t iv_len, + uint8_t* ecs, uint8_t* acs, char* cam_cookies) +{ + gcry_cipher_hd_t tmp_hd; + gcry_error_t gcry_error = GPG_ERR_NO_ERROR; + int32_t status = CRYPTO_LIB_SUCCESS; + uint8_t* key_ptr = key; + + // Fix warnings + acs = acs; + cam_cookies = cam_cookies; + + if(sa_ptr != NULL) //Using SA key pointer + { + key_ptr = &(ek_ring[sa_ptr->ekid].value[0]); + } + + // Select correct libgcrypt ecs enum + int32_t algo = -1; + if (ecs != NULL) + { + algo = cryptography_get_ecs_algo(*ecs); + if (algo == CRYPTO_LIB_ERR_UNSUPPORTED_ECS) + { + return CRYPTO_LIB_ERR_UNSUPPORTED_ECS; + } + } + else + { + return CRYPTO_LIB_ERR_NULL_ECS_PTR; + } + + // Verify the mode to accompany the algorithm enum + int32_t mode = -1; + mode = cryptography_get_ecs_mode(*ecs); + if (mode == CRYPTO_LIB_ERR_UNSUPPORTED_MODE) return CRYPTO_LIB_ERR_UNSUPPORTED_MODE; + + // Check that key length to be used is atleast as long as the algo requirement + if (sa_ptr != NULL && len_key > ek_ring[sa_ptr->ekid].key_len) + { + return CRYPTO_LIB_ERR_KEY_LENGTH_ERROR; + } + + gcry_error = gcry_cipher_open(&(tmp_hd), algo, mode, GCRY_CIPHER_NONE); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + return status; + } + gcry_error = gcry_cipher_setkey(tmp_hd, key_ptr, len_key); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + gcry_cipher_close(tmp_hd); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + return status; + } + + gcry_error = gcry_cipher_setiv(tmp_hd, iv, iv_len); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + gcry_cipher_close(tmp_hd); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + return status; + } + + gcry_error = gcry_cipher_decrypt(tmp_hd, + data_out, // plaintext output + len_data_out, // length of data + data_in, // in place decryption + len_data_in // in data length + ); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_decrypt error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + gcry_cipher_close(tmp_hd); + status = CRYPTO_LIB_ERR_DECRYPT_ERROR; + return status; + } + + + gcry_cipher_close(tmp_hd); + return status; + +} + static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, uint8_t* data_in, size_t len_data_in, uint8_t* key, uint32_t len_key, @@ -1007,7 +1255,7 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, uint8_t* mac, uint32_t mac_size, uint8_t* aad, uint32_t aad_len, uint8_t decrypt_bool, uint8_t authenticate_bool, - uint8_t aad_bool, uint8_t* ecs, uint8_t* acs) + uint8_t aad_bool, uint8_t* ecs, uint8_t* acs, char* cam_cookies) { gcry_cipher_hd_t tmp_hd; gcry_error_t gcry_error = GPG_ERR_NO_ERROR; @@ -1016,6 +1264,7 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, // Fix warnings acs = acs; + cam_cookies = cam_cookies; if(sa_ptr != NULL) //Using SA key pointer { @@ -1069,7 +1318,7 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out, status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; return status; } - + if (aad_bool == CRYPTO_TRUE) { gcry_error = gcry_cipher_authenticate(tmp_hd, @@ -1180,13 +1429,44 @@ int32_t cryptography_get_ecs_algo(int8_t algo_enum) case CRYPTO_CIPHER_AES256_GCM: algo = GCRY_CIPHER_AES256; break; + case CRYPTO_CIPHER_AES256_CBC: + algo = GCRY_CIPHER_AES256; + break; default: #ifdef DEBUG - printf("ECS Algo Enum not supported\n"); + printf("Algo Enum not supported\n"); #endif break; } return (int)algo; } + +/** + * @brief Function: cryptography_get_ecs_mode. Maps Cryptolib ECS enums to libgcrypt enums + * It is possible for supported algos to vary between crypto libraries + * @param algo_enum + **/ +int32_t cryptography_get_ecs_mode(int8_t algo_enum) +{ + int32_t mode = CRYPTO_LIB_ERR_UNSUPPORTED_ECS_MODE; // All valid algos will be positive + switch (algo_enum) + { + case CRYPTO_CIPHER_AES256_GCM: + mode = GCRY_CIPHER_MODE_GCM; + break; + case CRYPTO_CIPHER_AES256_CBC: + mode = GCRY_CIPHER_MODE_CBC; + break; + + default: +#ifdef DEBUG + printf("ECS Mode Enum not supported\n"); +#endif + break; + } + + return (int)mode; +} + diff --git a/src/src_main/crypto.c b/src/src_main/crypto.c index 47c70ac2..0aebb2e9 100644 --- a/src/src_main/crypto.c +++ b/src/src_main/crypto.c @@ -893,6 +893,8 @@ int32_t Crypto_Get_ECS_Algo_Keylen(uint8_t algo) case CRYPTO_CIPHER_AES256_GCM: retval = 32; break; + case CRYPTO_CIPHER_AES256_CBC: + retval = 32; default: break; } @@ -900,6 +902,7 @@ int32_t Crypto_Get_ECS_Algo_Keylen(uint8_t algo) return retval; } + /* ** @brief: For a given algorithm, return the associated key length in bytes ** @param: algo diff --git a/src/src_main/crypto_config.c b/src/src_main/crypto_config.c index 4dc3638b..0eea91a3 100644 --- a/src/src_main/crypto_config.c +++ b/src/src_main/crypto_config.c @@ -19,6 +19,7 @@ /* ** Includes */ +#include #include "crypto.h" /* @@ -29,9 +30,13 @@ CryptographyInterface cryptography_if = NULL; CryptoConfig_t* crypto_config = NULL; SadbMariaDBConfig_t* sadb_mariadb_config = NULL; CryptographyKmcCryptoServiceConfig_t* cryptography_kmc_crypto_config = NULL; +CamConfig_t* cam_config = NULL; GvcidManagedParameters_t* gvcid_managed_parameters = NULL; GvcidManagedParameters_t* current_managed_parameters = NULL; +// Free all configuration structs +int32_t crypto_free_config_structs(void); + /* ** Initialization Functions */ @@ -169,9 +174,11 @@ int32_t Crypto_Init(void) Crypto_Calc_CRC_Init_Table(); // cFS Standard Initialized Message +#ifdef DEBUG printf(KBLU "Crypto Lib Intialized. Version %d.%d.%d.%d\n" RESET, CRYPTO_LIB_MAJOR_VERSION, CRYPTO_LIB_MINOR_VERSION, CRYPTO_LIB_REVISION, CRYPTO_LIB_MISSION_REV); - } +#endif + } else { printf(KBLU "Error, Crypto Lib NOT Intialized, sadb_init() returned error:%d. Version .%d.%d.%d\n" RESET, CRYPTO_LIB_MAJOR_VERSION, @@ -190,18 +197,9 @@ int32_t Crypto_Shutdown(void) { int32_t status = CRYPTO_LIB_SUCCESS; - if (crypto_config != NULL) - { - free(crypto_config); - crypto_config = NULL; - } - if (sadb_mariadb_config != NULL) - { - free(sadb_mariadb_config); - sadb_mariadb_config = NULL; - } - current_managed_parameters = NULL; + crypto_free_config_structs(); + current_managed_parameters = NULL; if (gvcid_managed_parameters != NULL) { Crypto_Free_Managed_Parameters(gvcid_managed_parameters); @@ -275,18 +273,18 @@ int32_t Crypto_Config_MariaDB(char* mysql_hostname, char* mysql_database, uint16 sadb_mariadb_config = (SadbMariaDBConfig_t*)calloc(1, SADB_MARIADB_CONFIG_SIZE); if (sadb_mariadb_config != NULL) { - sadb_mariadb_config->mysql_username=mysql_username; - sadb_mariadb_config->mysql_password=mysql_password; - sadb_mariadb_config->mysql_hostname=mysql_hostname; - sadb_mariadb_config->mysql_database=mysql_database; + sadb_mariadb_config->mysql_username=crypto_deep_copy_string(mysql_username); + sadb_mariadb_config->mysql_password=crypto_deep_copy_string(mysql_password); + sadb_mariadb_config->mysql_hostname=crypto_deep_copy_string(mysql_hostname); + sadb_mariadb_config->mysql_database=crypto_deep_copy_string(mysql_database); sadb_mariadb_config->mysql_port=mysql_port; /*start - encrypted connection related parameters*/ - sadb_mariadb_config->mysql_mtls_cert = mysql_mtls_cert; - sadb_mariadb_config->mysql_mtls_key = mysql_mtls_key; - sadb_mariadb_config->mysql_mtls_ca = mysql_tls_ca; - sadb_mariadb_config->mysql_mtls_capath = mysql_tls_capath; + sadb_mariadb_config->mysql_mtls_cert = crypto_deep_copy_string(mysql_mtls_cert); + sadb_mariadb_config->mysql_mtls_key = crypto_deep_copy_string(mysql_mtls_key); + sadb_mariadb_config->mysql_mtls_ca = crypto_deep_copy_string(mysql_tls_ca); + sadb_mariadb_config->mysql_mtls_capath = crypto_deep_copy_string(mysql_tls_capath); sadb_mariadb_config->mysql_tls_verify_server = mysql_tls_verify_server; - sadb_mariadb_config->mysql_mtls_client_key_password = mysql_mtls_client_key_password; + sadb_mariadb_config->mysql_mtls_client_key_password = crypto_deep_copy_string(mysql_mtls_client_key_password); sadb_mariadb_config->mysql_require_secure_transport = mysql_require_secure_transport; /*end - encrypted connection related parameters*/ status = CRYPTO_LIB_SUCCESS; @@ -294,7 +292,7 @@ int32_t Crypto_Config_MariaDB(char* mysql_hostname, char* mysql_database, uint16 return status; } -extern int32_t Crypto_Config_Kmc_Crypto_Service(char* protocol, char* kmc_crypto_hostname, uint16_t kmc_crypto_port, +int32_t Crypto_Config_Kmc_Crypto_Service(char* protocol, char* kmc_crypto_hostname, uint16_t kmc_crypto_port, char* kmc_crypto_app, char* kmc_tls_ca_bundle, char* kmc_tls_ca_path, uint8_t kmc_ignore_ssl_hostname_validation, char* mtls_client_cert_path, char* mtls_client_cert_type, char* mtls_client_key_path, @@ -302,26 +300,48 @@ extern int32_t Crypto_Config_Kmc_Crypto_Service(char* protocol, char* kmc_crypto { int32_t status = CRYPTO_LIB_SUCCESS; cryptography_kmc_crypto_config = (CryptographyKmcCryptoServiceConfig_t* )calloc(1, CRYPTOGRAPHY_KMC_CRYPTO_SERVICE_CONFIG_SIZE); - cryptography_kmc_crypto_config->protocol = protocol; - cryptography_kmc_crypto_config->kmc_crypto_hostname = kmc_crypto_hostname; + cryptography_kmc_crypto_config->protocol = crypto_deep_copy_string(protocol); + cryptography_kmc_crypto_config->kmc_crypto_hostname = crypto_deep_copy_string(kmc_crypto_hostname); cryptography_kmc_crypto_config->kmc_crypto_port = kmc_crypto_port; if(kmc_crypto_app != NULL){ - cryptography_kmc_crypto_config->kmc_crypto_app_uri = kmc_crypto_app; + cryptography_kmc_crypto_config->kmc_crypto_app_uri = crypto_deep_copy_string(kmc_crypto_app); } else{ - cryptography_kmc_crypto_config->kmc_crypto_app_uri = "crypto-service"; + cryptography_kmc_crypto_config->kmc_crypto_app_uri = crypto_deep_copy_string("crypto-service"); } - cryptography_kmc_crypto_config->mtls_client_cert_path = mtls_client_cert_path; - cryptography_kmc_crypto_config->mtls_client_cert_type = mtls_client_cert_type; - cryptography_kmc_crypto_config->mtls_client_key_path = mtls_client_key_path; - cryptography_kmc_crypto_config->mtls_client_key_pass = mtls_client_key_pass; - cryptography_kmc_crypto_config->mtls_ca_bundle = kmc_tls_ca_bundle; - cryptography_kmc_crypto_config->mtls_ca_path = kmc_tls_ca_path; - cryptography_kmc_crypto_config->mtls_issuer_cert = mtls_issuer_cert; + cryptography_kmc_crypto_config->mtls_client_cert_path = crypto_deep_copy_string(mtls_client_cert_path); + cryptography_kmc_crypto_config->mtls_client_cert_type = crypto_deep_copy_string(mtls_client_cert_type); + cryptography_kmc_crypto_config->mtls_client_key_path = crypto_deep_copy_string(mtls_client_key_path); + cryptography_kmc_crypto_config->mtls_client_key_pass = crypto_deep_copy_string(mtls_client_key_pass); + cryptography_kmc_crypto_config->mtls_ca_bundle = crypto_deep_copy_string(kmc_tls_ca_bundle); + cryptography_kmc_crypto_config->mtls_ca_path = crypto_deep_copy_string(kmc_tls_ca_path); + cryptography_kmc_crypto_config->mtls_issuer_cert = crypto_deep_copy_string(mtls_issuer_cert); cryptography_kmc_crypto_config->ignore_ssl_hostname_validation = kmc_ignore_ssl_hostname_validation; return status; } +/** + * @brief Function: Crypto_Config_Cam + * @param cam_enabled: uint8_t + * @param cookie_file_path: char* + * @param keytab_file_path: char* + * @param login_method: uint8_t + * @return int32_t: Success/Failure +**/ +int32_t Crypto_Config_Cam(uint8_t cam_enabled, char* cookie_file_path, char* keytab_file_path, uint8_t login_method, char* access_manager_uri, char* username, char* cam_home) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + cam_config = (CamConfig_t*)calloc(1,CAM_CONFIG_SIZE); + cam_config->cam_enabled = cam_enabled; + cam_config->cookie_file_path = crypto_deep_copy_string(cookie_file_path); + cam_config->keytab_file_path = crypto_deep_copy_string(keytab_file_path); + cam_config->login_method = login_method; + cam_config->access_manager_uri = crypto_deep_copy_string(access_manager_uri); + cam_config->username = crypto_deep_copy_string(username); + cam_config->cam_home = crypto_deep_copy_string(cam_home); + + return status; +} /** * @brief Function: Crypto_Config_Add_Gvcid_Managed_Parameter @@ -366,6 +386,75 @@ int32_t Crypto_Config_Add_Gvcid_Managed_Parameter(uint8_t tfvn, uint16_t scid, u } } +int32_t crypto_free_config_structs(void) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + + free(crypto_config); //no strings in this struct, just free it. + crypto_config=NULL; + + // Config structs with char* types that are malloc'd and must be freed individually. + if(sadb_mariadb_config != NULL) + { + free(sadb_mariadb_config->mysql_username); + free(sadb_mariadb_config->mysql_password); + free(sadb_mariadb_config->mysql_hostname); + free(sadb_mariadb_config->mysql_database); + free(sadb_mariadb_config->mysql_mtls_cert); + free(sadb_mariadb_config->mysql_mtls_key); + free(sadb_mariadb_config->mysql_mtls_ca); + free(sadb_mariadb_config->mysql_mtls_capath); + free(sadb_mariadb_config->mysql_mtls_client_key_password); + free(sadb_mariadb_config); + sadb_mariadb_config=NULL; + } + if(cryptography_kmc_crypto_config != NULL) + { + free(cryptography_kmc_crypto_config->kmc_crypto_hostname); + free(cryptography_kmc_crypto_config->protocol); + free(cryptography_kmc_crypto_config->kmc_crypto_app_uri); + free(cryptography_kmc_crypto_config->mtls_client_cert_path); + free(cryptography_kmc_crypto_config->mtls_client_cert_type); + free(cryptography_kmc_crypto_config->mtls_client_key_path); + free(cryptography_kmc_crypto_config->mtls_client_key_pass); + free(cryptography_kmc_crypto_config->mtls_ca_bundle); + free(cryptography_kmc_crypto_config->mtls_ca_path); + free(cryptography_kmc_crypto_config->mtls_issuer_cert); + free(cryptography_kmc_crypto_config); + cryptography_kmc_crypto_config=NULL; + } + if(cam_config != NULL) + { + free(cam_config->cookie_file_path); + free(cam_config->keytab_file_path); + free(cam_config->access_manager_uri); + free(cam_config->username); + free(cam_config->cam_home); + free(cam_config); + cam_config=NULL; + } + return status; +} + +/** + * @brief Function: crypto_deep_copy_string + * Used to malloc a local copy of an externally referenced string. The string MUST BE null-terminated. + * @param src_string: Pointer to externally-memory-managed string. + * @return char*: Pointer to locally-memory-managed string copy. + **/ + +char* crypto_deep_copy_string(char* src_string) +{ + if(src_string == NULL) + { + return NULL; + } + // Note that the strlen() function doesn't count the null character \0 while calculating the length. + char* deep_copied_str = malloc((strlen(src_string) + 1) * sizeof(char)); + memcpy(deep_copied_str,src_string,strlen(src_string)+1); + return deep_copied_str; +} + /** * @brief Function: crypto_config_add_gvcid_managed_parameter_recursion * @param tfvn: uint8 diff --git a/src/src_main/crypto_error.c b/src/src_main/crypto_error.c new file mode 100644 index 00000000..4f4cd046 --- /dev/null +++ b/src/src_main/crypto_error.c @@ -0,0 +1,225 @@ +/* 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 +*/ + +#include "crypto_error.h" +#include "crypto.h" + +char *crypto_enum_errlist_core[] = +{ + "CRYPTO_LIB_SUCCESS" + ,"CRYPTO_LIB_ERROR" + ,"CRYPTO_LIB_ERR_NO_INIT" + ,"CRYPTO_LIB_ERR_INVALID_TFVN" + ,"CRYPTO_LIB_ERR_INVALID_SCID" + ,"CRYPTO_LIB_ERR_INVALID_VCID" + ,"CRYPTO_LIB_ERR_INVALID_MAPID" + ,"CRYPTO_LIB_ERR_INVALID_CC_FLAG" + ,"CRYPTO_LIB_ERR_NO_OPERATIONAL_SA" + ,"CRYPTO_LIB_ERR_NULL_BUFFER" + ,"CRYPTO_LIB_ERR_UT_BYTE_MISMATCH" + ,"CRYPTO_LIB_ERR_NO_CONFIG" + ,"CRYPTO_LIB_ERR_INVALID_FECF" + ,"CRYPTO_LIB_ERR_ARSN_OUTSIDE_WINDOW" + ,"CRYPTO_LIB_ERR_LIBGCRYPT_ERROR" + ,"CRYPTO_LIB_ERR_AUTHENTICATION_ERROR" + ,"CRYPTO_LIB_ERR_NULL_IV" + ,"CRYPTO_LIB_ERR_NULL_ABM" + ,"CRYPTO_LIB_ERR_DECRYPT_ERROR" + ,"CRYPTO_LIB_ERR_ABM_TOO_SHORT_FOR_AAD" + ,"CRYPTO_LIB_ERR_MAC_RETRIEVAL_ERROR" + ,"CRYPTO_LIB_ERR_MAC_VALIDATION_ERROR" + ,"CRYPTO_LIB_ERR_INVALID_HEADER" + ,"CRYPTO_LIB_ERR_IV_OUTSIDE_WINDOW" + ,"CRYPTO_LIB_ERR_NULL_ARSN" + ,"CRYPTO_LIB_ERR_NULL_SA" + ,"CRYPTO_LIB_ERR_UNSUPPORTED_ACS" + ,"CRYPTO_LIB_ERR_ENCRYPTION_ERROR" + ,"CRYPTO_LIB_ERR_INVALID_SA_CONFIGURATION" + ,"CRYPTO_LIB_ERR_TC_FRAME_SIZE_EXCEEDS_MANAGED_PARAM_MAX_LIMIT" + ,"CRYPTO_LIB_ERR_TC_FRAME_SIZE_EXCEEDS_SPEC_LIMIT" + ,"CRYPTO_LIB_ERR_UNSUPPORTED_ECS" + ,"CRYPTO_LIB_ERR_KEY_LENGTH_ERROR" + ,"CRYPTO_LIB_ERR_NULL_ECS_PTR" + ,"CRYPTO_LIB_ERR_IV_NOT_SUPPORTED_FOR_ACS_ALGO" + ,"CRYPTO_LIB_ERR_NULL_CIPHERS" + ,"CRYPTO_LIB_ERR_NO_ECS_SET_FOR_ENCRYPTION_MODE" + ,"CRYPTO_LIB_ERR_IV_LEN_SHORTER_THAN_SEC_HEADER_LENGTH" + ,"CRYPTO_LIB_ERR_ARSN_LEN_SHORTER_THAN_SEC_HEADER_LENGTH" + ,"CRYPTO_LIB_ERR_FRAME_COUNTER_DOESNT_MATCH_SA" + ,"CRYPTO_LIB_ERR_INPUT_FRAME_TOO_SHORT_FOR_TC_STANDARD" + ,"CRYPTO_LIB_ERR_INPUT_FRAME_LENGTH_SHORTER_THAN_FRAME_HEADERS_LENGTH" + ,"CRYPTO_LIB_ERR_UNSUPPORTED_ECS_MODE" + ,"CRYPTO_LIB_ERR_NULL_MODE_PTR" + ,"CRYPTO_LIB_ERR_UNSUPPORTED_MODE" +}; + +char *crypto_enum_errlist_config[] = +{ + "CRYPTO_CONFIGURATION_NOT_COMPLETE" + ,"CRYPTO_MANAGED_PARAM_CONFIGURATION_NOT_COMPLETE" + ,"CRYPTO_MARIADB_CONFIGURATION_NOT_COMPLETE" + ,"MANAGED_PARAMETERS_FOR_GVCID_NOT_FOUND" +}; + +char *crypto_enum_errlist_sadb_if[] = +{ + "SADB_INVALID_SADB_TYPE" + ,"SADB_NULL_SA_USED" +}; +char *crypto_enum_errlist_sadb_mariadb[] = +{ + "SADB_MARIADB_CONNECTION_FAILED" + ,"SADB_QUERY_FAILED" + ,"SADB_QUERY_EMPTY_RESULTS" + ,"SADB_INSERT_FAILED" +}; +char *crypto_enum_errlist_crypto_if[] = +{ + "CRYPTOGRAPHY_INVALID_CRYPTO_INTERFACE_TYPE" + ,"CRYPTOGRAPHY_UNSUPPORTED_OPERATION_FOR_KEY_RING" + ,"CRYPTOGRAPHY_LIBRARY_INITIALIZIATION_ERROR" +}; +char *crypto_enum_errlist_crypto_kmc[] = +{ + "CRYPTOGRAPHY_KMC_CRYPTO_SERVICE_CONFIGURATION_NOT_COMPLETE" + ,"CRYPTOGRAPHY_KMC_CURL_INITIALIZATION_FAILURE" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_CONNECTION_ERROR" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_ENCRYPT_ERROR" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AEAD_DECRYPT_ERROR" + ,"CRYPTOGRAHPY_KMC_CRYPTO_JSON_PARSE_ERROR" + ,"CRYPTOGRAHPY_KMC_CIPHER_TEXT_NOT_FOUND_IN_JSON_RESPONSE" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_GENERIC_FAILURE" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_AUTHENTICATION_ERROR" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_MAC_VALIDATION_ERROR" + ,"CRYPTOGRAHPY_KMC_ICV_NOT_FOUND_IN_JSON_RESPONSE" + ,"CRYPTOGRAHPY_KMC_NULL_ENCRYPTION_KEY_REFERENCE_IN_SA" + ,"CRYPTOGRAHPY_KMC_NULL_AUTHENTICATION_KEY_REFERENCE_IN_SA" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_EMPTY_RESPONSE" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_DECRYPT_ERROR" + ,"CRYPTOGRAHPY_KMC_CRYPTO_SERVICE_ENCRYPT_ERROR" +}; + +char *crypto_enum_errlist_crypto_cam[] = +{ + "CAM_CONFIG_NOT_SUPPORTED_ERROR" + ,"CAM_INVALID_COOKIE_FILE_CONFIGURATION_NULL" + ,"CAM_AUTHENTICATION_FAILURE_REDIRECT" + ,"CAM_AUTHENTICATION_REQUIRED" + ,"CAM_GET_SSO_TOKEN_FAILURE" + ,"CAM_INVALID_CONFIGURATION_ACCESS_MANAGER_URI_NULL" + ,"CAM_INVALID_CONFIGURATION_KEYTAB_FILE_PATH_NULL" + ,"CAM_INVALID_CONFIGURATION_KEYTAB_FILE_USERNAME_NULL" + ,"CAM_KEYTAB_FILE_KINIT_FAILURE" +}; + +/* +** @brief: For a given crypto error code, return the associated error code enum string +** @param: int32_t + * @return: char* +*/ +char* Crypto_Get_Error_Code_Enum_String(int32_t crypto_error_code) +{ + if(crypto_error_code >= 600) // CAM Error Codes + { + if(crypto_error_code > 610) + { + return CRYPTO_UNDEFINED_ERROR; + } + else + { + return crypto_enum_errlist_crypto_cam[crypto_error_code % 600]; + } + + } + else if(crypto_error_code >= 500) // KMC Error Codes + { + if(crypto_error_code > 515) + { + return CRYPTO_UNDEFINED_ERROR; + } + else + { + return crypto_enum_errlist_crypto_kmc[crypto_error_code % 500]; + } + } + else if(crypto_error_code >= 400) // Crypto Interface Error Codes + { + if(crypto_error_code > 402) + { + return CRYPTO_UNDEFINED_ERROR; + } + else + { + return crypto_enum_errlist_crypto_if[crypto_error_code % 400]; + } + + } + else if(crypto_error_code >= 300) // SADB MariadDB Error Codes + { + if(crypto_error_code > 303) + { + return CRYPTO_UNDEFINED_ERROR; + } + else + { + return crypto_enum_errlist_sadb_mariadb[crypto_error_code % 300]; + } + + } + else if(crypto_error_code >= 200) // SADB Interface Error Codes + { + if(crypto_error_code > 201) + { + return CRYPTO_UNDEFINED_ERROR; + } + else + { + return crypto_enum_errlist_sadb_if[crypto_error_code % 200]; + } + } + else if(crypto_error_code >= 100) // Configuration Error Codes + { + if(crypto_error_code > 103) + { + return CRYPTO_UNDEFINED_ERROR; + } + else + { + return crypto_enum_errlist_config[crypto_error_code % 100]; + } + } + else if(crypto_error_code > 0) // Unused Error Codes 1-100 + { + return CRYPTO_UNDEFINED_ERROR; + } + else if(crypto_error_code <= 0) // Cryptolib Core Error Codes + { + if(crypto_error_code < -44) + { + return CRYPTO_UNDEFINED_ERROR; + } + else + { + return crypto_enum_errlist_core[(crypto_error_code * (-1))]; + } + } + else + { + return CRYPTO_UNDEFINED_ERROR; + } +} \ No newline at end of file diff --git a/src/src_main/crypto_key_mgmt.c b/src/src_main/crypto_key_mgmt.c index 2bd3b19a..556fd308 100644 --- a/src/src_main/crypto_key_mgmt.c +++ b/src/src_main/crypto_key_mgmt.c @@ -107,7 +107,8 @@ int32_t Crypto_Key_OTAR(void) CRYPTO_TRUE, // authenticate CRYPTO_FALSE, // AAD Bool &ecs, // encryption cipher - NULL // authentication cipher + NULL, // authentication cipher + NULL // cam_cookies ); // Read in Decrypted Data @@ -400,7 +401,8 @@ int32_t Crypto_Key_verify(uint8_t* ingest, TC_t* tc_frame) CRYPTO_TRUE, // Authenticate CRYPTO_FALSE, // AAD &ecs, // encryption cipher - NULL // authentication cipher + NULL, // authentication cipher + NULL // cam_cookies ); count += CHALLENGE_SIZE + CHALLENGE_MAC_SIZE; // Don't forget to increment count! diff --git a/src/src_main/crypto_tc.c b/src/src_main/crypto_tc.c index 07349f04..600943d9 100644 --- a/src/src_main/crypto_tc.c +++ b/src/src_main/crypto_tc.c @@ -38,6 +38,22 @@ static int32_t crypto_handle_incrementing_nontransmitted_counter(uint8_t* dest, **/ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_frame_length, uint8_t** pp_in_frame, uint16_t* p_enc_frame_len) +{ + //Passthrough to maintain original function signature when CAM isn't used. + return Crypto_TC_ApplySecurity_Cam(p_in_frame, in_frame_length, pp_in_frame, p_enc_frame_len,NULL); +} +/** + * @brief Function: Crypto_TC_ApplySecurity_Cam + * Applies Security to incoming frame. Encryption, Authentication, and Authenticated Encryption + * @param p_in_frame: uint8* + * @param in_frame_length: uint16 + * @param pp_in_frame: uint8_t** + * @param p_enc_frame_len: uint16 + * @param cam_cookies: char* + * @return int32: Success/Failure + **/ +int32_t Crypto_TC_ApplySecurity_Cam(const uint8_t* p_in_frame, const uint16_t in_frame_length, uint8_t** pp_in_frame, + uint16_t* p_enc_frame_len, char* cam_cookies) { // Local Variables int32_t status = CRYPTO_LIB_SUCCESS; @@ -50,9 +66,10 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra uint16_t new_fecf = 0x0000; uint8_t* aad = NULL; uint16_t new_enc_frame_header_field_length = 0; - uint32_t encryption_cipher; + uint32_t encryption_cipher = 0; uint8_t ecs_is_aead_algorithm; int i; + uint32_t pkcs_padding = 0; #ifdef DEBUG printf(KYEL "\n----- Crypto_TC_ApplySecurity START -----\n" RESET); @@ -241,9 +258,23 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra } #endif - // Determine length of buffer to be malloced - // TODO: Determine TC_PAD_SIZE - // TODO: Note: Currently assumes ciphertext output length is same as ciphertext input length + // Determine if segment header exists + uint8_t segment_hdr_len = SEGMENT_HDR_SIZE; + if (current_managed_parameters->has_segmentation_hdr == TC_NO_SEGMENT_HDRS) + { + segment_hdr_len = 0; + } + + // Determine if FECF exists + uint8_t fecf_len = FECF_SIZE; + if (current_managed_parameters->has_fecf == TC_NO_FECF) + { + fecf_len = 0; + } + + // Calculate tf_payload length here to be used in other logic + tf_payload_len = temp_tc_header.fl - TC_FRAME_HEADER_SIZE - segment_hdr_len - fecf_len + 1; + switch (sa_service_type) { case SA_PLAINTEXT: @@ -255,22 +286,48 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra // Ingest length + spi_index (2) + shivf_len (varies) + shsnf_len (varies) // + shplf_len + arsn_len + pad_size + stmacf_len // TODO: If ARSN is transmitted in the SHSNF field (as in CMAC... don't double count those bytes) - *p_enc_frame_len = temp_tc_header.fl + 1 + 2 + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + - TC_PAD_SIZE + sa_ptr->stmacf_len; - new_enc_frame_header_field_length = (*p_enc_frame_len) - 1; + *p_enc_frame_len = temp_tc_header.fl + 1 + 2 + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + sa_ptr->stmacf_len; + new_enc_frame_header_field_length = (*p_enc_frame_len) - 1; break; case SA_ENCRYPTION: - // Ingest length + spi_index (2) + shivf_len (varies) + shsnf_len (varies) + // Ingest length + 1 (accounts for -1 to length) + spi_index (2) + shivf_len (varies) + shsnf_len (varies) // + shplf_len + arsn_len + pad_size *p_enc_frame_len = temp_tc_header.fl + 1 + 2 + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + - sa_ptr->arsn_len + TC_PAD_SIZE; + sa_ptr->arsn_len; //should point to shplf_len + new_enc_frame_header_field_length = (*p_enc_frame_len) - 1; + + // Handle Padding, if necessary + if(*(sa_ptr->ecs) == CRYPTO_CIPHER_AES256_CBC) + { + pkcs_padding = tf_payload_len % TC_BLOCK_SIZE; // Block Sizes of 16 + + pkcs_padding = TC_BLOCK_SIZE - pkcs_padding; //Could potentially need 16 bytes of padding. + + *p_enc_frame_len += pkcs_padding; // Add the necessary padding to the frame_len + new pad length field + + new_enc_frame_header_field_length = (*p_enc_frame_len) - 1; +#ifdef DEBUG + + printf("SHPLF_LEN: %d\n", sa_ptr->shplf_len); + printf("Padding Needed: %d\n", pkcs_padding); + printf("Previous data_len: %d\n", tf_payload_len); + printf("New data_len: %d\n", (tf_payload_len + pkcs_padding)); + printf("New enc_frame_len: %d\n", (*p_enc_frame_len)); +#endif + // Don't Exceed Max Frame Size! 1024 + if(*p_enc_frame_len > TC_MAX_FRAME_SIZE) + { + return CRYPTO_LIB_ERR_TC_FRAME_SIZE_EXCEEDS_SPEC_LIMIT; + } + + } break; case SA_AUTHENTICATED_ENCRYPTION: // Ingest length + spi_index (2) + shivf_len (varies) + shsnf_len (varies) // + shplf_len + arsn_len + pad_size + stmacf_len *p_enc_frame_len = temp_tc_header.fl + 1 + 2 + sa_ptr->shivf_len + sa_ptr->shsnf_len + sa_ptr->shplf_len + - sa_ptr->arsn_len + TC_PAD_SIZE + sa_ptr->stmacf_len; + sa_ptr->arsn_len + sa_ptr->stmacf_len; new_enc_frame_header_field_length = (*p_enc_frame_len) - 1; break; default: @@ -289,7 +346,6 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra status = CRYPTO_LIB_ERR_TC_FRAME_SIZE_EXCEEDS_MANAGED_PARAM_MAX_LIMIT; return status; } - // Ensure the frame to be created will not violate spec max length if (*p_enc_frame_len > 1024) { @@ -308,12 +364,7 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra } memset(p_new_enc_frame, 0, *p_enc_frame_len); - // Determine if segment header exists - uint8_t segment_hdr_len = SEGMENT_HDR_SIZE; - if (current_managed_parameters->has_segmentation_hdr == TC_NO_SEGMENT_HDRS) - { - segment_hdr_len = 0; - } + #ifdef TC_DEBUG printf(KYEL "DEBUG - Total TC Buffer to be malloced is: %d bytes\n" RESET, *p_enc_frame_len); @@ -325,7 +376,6 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra printf(KYEL "\tshsnf_len\t = %d\n" RESET, sa_ptr->shsnf_len); printf(KYEL "\tshplf len\t = %d\n" RESET, sa_ptr->shplf_len); printf(KYEL "\tarsn_len\t = %d\n" RESET, sa_ptr->arsn_len); - printf(KYEL "\tpad_size\t = %d\n" RESET, TC_PAD_SIZE); printf(KYEL "\tstmacf_len\t = %d\n" RESET, sa_ptr->stmacf_len); #endif @@ -391,12 +441,18 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra return CRYPTO_LIB_ERR_NULL_CIPHERS; } - if(sa_ptr->est == 0 && sa_ptr->ast == 1 && sa_ptr->acs !=NULL && *(sa_ptr->acs) == CRYPTO_MAC_CMAC_AES256 \ - && *(sa_ptr->acs) == CRYPTO_MAC_HMAC_SHA256 && *(sa_ptr->acs) == CRYPTO_MAC_HMAC_SHA512 && - sa_ptr->iv_len > 0 ) + if(sa_ptr->est == 0 && sa_ptr->ast == 1) { - return CRYPTO_LIB_ERR_IV_NOT_SUPPORTED_FOR_ACS_ALGO; - } + if(sa_ptr->acs !=NULL && sa_ptr->acs_len != 0) + { + if((*(sa_ptr->acs) == CRYPTO_MAC_CMAC_AES256 || *(sa_ptr->acs) == CRYPTO_MAC_HMAC_SHA256 || *(sa_ptr->acs) == CRYPTO_MAC_HMAC_SHA512) && + sa_ptr->iv_len > 0 ) + { + return CRYPTO_LIB_ERR_IV_NOT_SUPPORTED_FOR_ACS_ALGO; + } + } + } + // Start index from the transmitted portion for (i = sa_ptr->iv_len - sa_ptr->shivf_len; i < sa_ptr->iv_len; i++) @@ -428,31 +484,53 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra */ // TODO: Revisit this // TODO: Likely SA API Call - for (i = 0; i < sa_ptr->shplf_len; i++) + /* 4.1.1.5.2 The Pad Length field shall contain the count of fill bytes used in the + ** cryptographic process, consisting of an integral number of octets. - CCSDS 3550b1 + */ + // TODO: Set this depending on crypto cipher used + + if(pkcs_padding) { - /* 4.1.1.5.2 The Pad Length field shall contain the count of fill bytes used in the - ** cryptographic process, consisting of an integral number of octets. - CCSDS 3550b1 - */ - // TODO: Set this depending on crypto cipher used - *(p_new_enc_frame + index) = 0x00; - index++; + uint8_t hex_padding[3] = {0}; //TODO: Create #Define for the 3 + pkcs_padding = pkcs_padding & 0x00FFFFFF; // Truncate to be maxiumum of 3 bytes in size + + // Byte Magic + hex_padding[0] = (pkcs_padding >> 16) & 0xFF; + hex_padding[1] = (pkcs_padding >> 8) & 0xFF; + hex_padding[2] = (pkcs_padding) & 0xFF; + + uint8_t padding_start = 0; + padding_start = 3 - sa_ptr->shplf_len; + + for (i = 0; i < sa_ptr->shplf_len; i++) + { + *(p_new_enc_frame + index) = hex_padding[padding_start++]; + index++; + } } + /* ** End Security Header Fields */ - // Determine if FECF exists - uint8_t fecf_len = FECF_SIZE; - if (current_managed_parameters->has_fecf == TC_NO_FECF) - { - fecf_len = 0; - } - // Copy in original TF data - except FECF // Will be over-written if using encryption later - tf_payload_len = temp_tc_header.fl - TC_FRAME_HEADER_SIZE - segment_hdr_len - fecf_len + 1; + //tf_payload_len = temp_tc_header.fl - TC_FRAME_HEADER_SIZE - segment_hdr_len - fecf_len + 1; + memcpy((p_new_enc_frame + index), (p_in_frame + TC_FRAME_HEADER_SIZE + segment_hdr_len), tf_payload_len); + index += tf_payload_len; + for (uint32_t i = 0; i < pkcs_padding; i++) + { + /* 4.1.1.5.2 The Pad Length field shall contain the count of fill bytes used in the + ** cryptographic process, consisting of an integral number of octets. - CCSDS 3550b1 + */ + // TODO: Set this depending on crypto cipher used + *(p_new_enc_frame + index + i) = (uint8_t)pkcs_padding; // How much padding is needed? + // index++; + } + index -= tf_payload_len; + tf_payload_len += pkcs_padding; /* ** Begin Authentication / Encryption @@ -498,9 +576,9 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra if(ecs_is_aead_algorithm == CRYPTO_TRUE) { - status = cryptography_if->cryptography_aead_encrypt(&p_new_enc_frame[index], // ciphertext output - (size_t)tf_payload_len, // length of data + (size_t)tf_payload_len, + //&p_new_enc_frame[index], // length of data (uint8_t*)(p_in_frame + TC_FRAME_HEADER_SIZE + segment_hdr_len), // plaintext input (size_t)tf_payload_len, // in data length NULL, // Using SA key reference, key is null @@ -516,7 +594,8 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra (sa_ptr->ast==1), (sa_ptr->ast==1), sa_ptr->ecs, // encryption cipher - sa_ptr->acs // authentication cipher + sa_ptr->acs, // authentication cipher + cam_cookies ); } else // non aead algorithm @@ -524,7 +603,21 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra // TODO - implement non-AEAD algorithm logic if (sa_service_type == SA_ENCRYPTION) { - status = cryptography_if->cryptography_encrypt(); + status = cryptography_if->cryptography_encrypt(&p_new_enc_frame[index], // ciphertext output + (size_t)tf_payload_len, + &p_new_enc_frame[index], // length of data + //(uint8_t*)(p_in_frame + TC_FRAME_HEADER_SIZE + segment_hdr_len), // plaintext input + (size_t)tf_payload_len, // in data length + //new_frame_length, + NULL, // Using SA key reference, key is null + Crypto_Get_ECS_Algo_Keylen(*sa_ptr->ecs), // Length of key derived from sa_ptr key_ref + sa_ptr, // SA (for key reference) + sa_ptr->iv, // IV + sa_ptr->iv_len, // IV Length + sa_ptr->ecs, // encryption cipher + pkcs_padding, + cam_cookies + ); } if (sa_service_type == SA_AUTHENTICATION) @@ -543,12 +636,14 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra aad, // AAD Input aad_len, // Length of AAD *sa_ptr->ecs, // encryption cipher - *sa_ptr->acs // authentication cipher + *sa_ptr->acs, // authentication cipher + cam_cookies ); } } if (status != CRYPTO_LIB_SUCCESS) { + free(aad); return status; // Cryptography IF call failed, return. } } @@ -636,7 +731,7 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra #ifdef DEBUG printf(KYEL "----- Crypto_TC_ApplySecurity END -----\n" RESET); #endif - + free(aad); return status; } @@ -649,6 +744,20 @@ int32_t Crypto_TC_ApplySecurity(const uint8_t* p_in_frame, const uint16_t in_fra * @return int32: Success/Failure **/ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdls_processed_frame) +{ + // Pass-through to maintain original function signature when CAM isn't used. + return Crypto_TC_ProcessSecurity_Cam(ingest, len_ingest, tc_sdls_processed_frame, NULL); +} + +/** + * @brief Function: Crypto_TC_ProcessSecurity + * Performs Authenticated decryption, decryption, and authentication + * @param ingest: uint8_t* + * @param len_ingest: int* + * @param tc_sdls_processed_frame: TC_t* + * @return int32: Success/Failure + **/ +int32_t Crypto_TC_ProcessSecurity_Cam(uint8_t* ingest, int *len_ingest, TC_t* tc_sdls_processed_frame, char* cam_cookies) // Loads the ingest frame into the global tc_frame while performing decryption { // Local Variables @@ -745,6 +854,8 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl tc_sdls_processed_frame->tc_sec_header.iv_field_len = sa_ptr->iv_len; tc_sdls_processed_frame->tc_sec_header.sn_field_len = sa_ptr->arsn_len; tc_sdls_processed_frame->tc_sec_header.pad_field_len = sa_ptr->shplf_len; + //sprintf(tc_sdls_processed_frame->tc_sec_header.pad, "%x", pkcs_padding); + tc_sdls_processed_frame->tc_sec_trailer.mac_field_len = sa_ptr->stmacf_len; // Determine SA Service Type @@ -889,9 +1000,10 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl #endif // 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); + //tc_sdls_processed_frame->tc_sec_header.pad = malloc((sa_ptr->shplf_len * sizeof(uint8_t))); + memcpy((tc_sdls_processed_frame->tc_sec_header.pad), + &(ingest[TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + sa_ptr->shsnf_len]), + sa_ptr->shplf_len); // Parse MAC, prepare AAD if ((sa_service_type == SA_AUTHENTICATION) || (sa_service_type == SA_AUTHENTICATED_ENCRYPTION)) @@ -917,10 +1029,11 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl } aad = Crypto_Prepare_TC_AAD(ingest, aad_len, sa_ptr->abm); } - + uint16_t 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; + // Todo -- if encrypt only, ignore stmacf_len entirely to avoid erroring on SA misconfiguration... Or just throw a warning/error indicating SA misconfiguration? 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; @@ -953,7 +1066,8 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl (sa_ptr->ast), // Authentication Bool (sa_ptr->ast), // AAD Bool sa_ptr->ecs, // encryption cipher - sa_ptr->acs // authentication cipher + sa_ptr->acs, // authentication cipher + cam_cookies ); }else if (sa_service_type != SA_PLAINTEXT && ecs_is_aead_algorithm == CRYPTO_FALSE) // Non aead algorithm @@ -975,12 +1089,38 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl aad, // additional authenticated data aad_len, // length of AAD CRYPTO_CIPHER_NONE, //encryption cipher - *sa_ptr->acs //authentication cipher + *sa_ptr->acs, //authentication cipher + cam_cookies ); } if(sa_service_type == SA_ENCRYPTION || sa_service_type == SA_AUTHENTICATED_ENCRYPTION) { - status = cryptography_if->cryptography_decrypt(); + status = cryptography_if->cryptography_decrypt(tc_sdls_processed_frame->tc_pdu, // plaintext output + (size_t)(tc_sdls_processed_frame->tc_pdu_len), // length of data + &(ingest[tc_enc_payload_start_index]), // ciphertext input + (size_t)(tc_sdls_processed_frame->tc_pdu_len), // in data length + NULL, // Key + Crypto_Get_ECS_Algo_Keylen(*sa_ptr->ecs), + sa_ptr, // SA for key reference + tc_sdls_processed_frame->tc_sec_header.iv, // IV + sa_ptr->iv_len, // IV Length + sa_ptr->ecs, // encryption cipher + sa_ptr->acs, // authentication cipher + cam_cookies + + ); + + //Handle Padding Removal + if(sa_ptr->shplf_len != 0) + { + int padding_location = TC_FRAME_HEADER_SIZE + segment_hdr_len + SPI_LEN + sa_ptr->shivf_len + + sa_ptr->shsnf_len; + uint16_t padding_amount = 0; + // Get Padding Amount from ingest frame + padding_amount = (int)ingest[padding_location]; + // Remove Padding from final decrypted portion + tc_sdls_processed_frame->tc_pdu_len -= padding_amount; + } } } else if(sa_service_type == SA_PLAINTEXT) @@ -991,6 +1131,7 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl if (status != CRYPTO_LIB_SUCCESS) { + free(aad); return status; // Cryptography IF call failed, return. } @@ -1001,6 +1142,7 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl if(status != CRYPTO_LIB_SUCCESS) { + free(aad); return status; } @@ -1008,16 +1150,30 @@ int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t* tc_sdl status = sadb_routine->sadb_save_sa(sa_ptr); if(status != CRYPTO_LIB_SUCCESS) { + free(aad); return status; } } + else + { + if (crypto_config->sadb_type == SADB_TYPE_MARIADB) + { + if(sa_ptr->ecs != NULL) free(sa_ptr->ecs); + if(sa_ptr->ek_ref != NULL) free(sa_ptr->ek_ref); + if(sa_ptr->iv != NULL) free(sa_ptr->iv); + if(sa_ptr->abm != NULL) free(sa_ptr->abm); + if(sa_ptr->arsn != NULL) free(sa_ptr->arsn); + if(sa_ptr->acs != NULL) free(sa_ptr->acs); + free(sa_ptr); + } + } // Extended PDU processing, if applicable if (status == CRYPTO_LIB_SUCCESS && crypto_config->process_sdls_pdus == TC_PROCESS_SDLS_PDUS_TRUE) { status = Crypto_Process_Extended_Procedure_Pdu(tc_sdls_processed_frame, ingest); } - + free(aad); return status; } @@ -1177,5 +1333,6 @@ static int32_t crypto_handle_incrementing_nontransmitted_counter(uint8_t* dest, { status = CRYPTO_LIB_ERR_FRAME_COUNTER_DOESNT_MATCH_SA; } + free(temp_counter); return status; } \ No newline at end of file diff --git a/src/src_main/crypto_tm.c b/src/src_main/crypto_tm.c index 8c8f3088..b0d01f43 100644 --- a/src/src_main/crypto_tm.c +++ b/src/src_main/crypto_tm.c @@ -233,7 +233,8 @@ int32_t Crypto_TM_ApplySecurity(uint8_t* ingest, int *len_ingest) CRYPTO_FALSE, // Authenticate // TODO -- Set to SA value, manually setting to false here so existing tests pass. Existing data was generated with authenticate then encrypt, when it should have been encrypt then authenticate. CRYPTO_TRUE, // Use AAD sa_ptr->ecs, // encryption cipher - sa_ptr->acs // authentication cipher + sa_ptr->acs, // authentication cipher + NULL // cam_cookies (not supported in TM functions yet) ); diff --git a/src/src_main/sadb_routine_inmemory.template.c b/src/src_main/sadb_routine_inmemory.template.c index 70b8d424..59bc86cd 100644 --- a/src/src_main/sadb_routine_inmemory.template.c +++ b/src/src_main/sadb_routine_inmemory.template.c @@ -97,7 +97,7 @@ int32_t sadb_config(void) sa[2].est = 1; sa[2].ast = 1; sa[2].ecs_len = 1; - sa[2].ecs = calloc(1, sa[4].ecs_len * sizeof(uint8_t)); + sa[2].ecs = calloc(1, sa[2].ecs_len * sizeof(uint8_t)); *sa[2].ecs = CRYPTO_CIPHER_AES256_GCM; sa[2].shivf_len = 12; sa[2].iv_len = 12; @@ -116,7 +116,7 @@ int32_t sadb_config(void) sa[3].est = 1; sa[3].ast = 1; sa[3].ecs_len = 1; - sa[3].ecs = calloc(1, sa[4].ecs_len * sizeof(uint8_t)); + sa[3].ecs = calloc(1, sa[3].ecs_len * sizeof(uint8_t)); *sa[3].ecs = CRYPTO_CIPHER_AES256_GCM; sa[3].shivf_len = 12; sa[3].iv_len = 12; @@ -160,7 +160,7 @@ int32_t sadb_config(void) sa[5].est = 1; sa[5].ast = 1; sa[5].ecs_len = 1; - sa[5].ecs = calloc(1, sa[4].ecs_len * sizeof(uint8_t)); + sa[5].ecs = calloc(1, sa[5].ecs_len * sizeof(uint8_t)); *sa[5].ecs = CRYPTO_CIPHER_AES256_GCM; sa[5].shivf_len = 12; sa[5].iv_len = 12; @@ -178,7 +178,7 @@ int32_t sadb_config(void) sa[6].est = 1; sa[6].ast = 1; sa[6].ecs_len = 1; - sa[6].ecs = calloc(1, sa[4].ecs_len * sizeof(uint8_t)); + sa[6].ecs = calloc(1, sa[6].ecs_len * sizeof(uint8_t)); *sa[6].ecs = CRYPTO_CIPHER_AES256_GCM; sa[6].shivf_len = 12; sa[6].iv_len = 12; @@ -197,7 +197,7 @@ int32_t sadb_config(void) sa[7].est = 1; sa[7].ast = 1; sa[7].ecs_len = 1; - sa[7].ecs = calloc(1, sa[4].ecs_len * sizeof(uint8_t)); + sa[7].ecs = calloc(1, sa[7].ecs_len * sizeof(uint8_t)); *sa[7].ecs = CRYPTO_CIPHER_AES256_GCM; sa[7].shivf_len = 12; sa[7].iv_len = 12; @@ -246,6 +246,59 @@ int32_t sadb_config(void) sa[9].gvcid_tc_blk.vcid = 0; sa[9].gvcid_tc_blk.mapid = TYPE_TC; + // SA 10 - KEYED; ARSNW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 130 + // SA 10 VC0/1 is now 4-VC0, 7-VC1 + sa[10].spi = 10; + sa[10].ekid = 130; + sa[10].sa_state = SA_OPERATIONAL; + sa[10].est = 1; + sa[10].ast = 1; + sa[10].ecs_len = 1; + sa[10].ecs = calloc(1, sa[10].ecs_len * sizeof(uint8_t)); + *sa[10].ecs = CRYPTO_CIPHER_AES256_GCM; + sa[10].shivf_len = 12; + sa[10].iv_len = 12; + sa[10].stmacf_len = 16; + sa[10].iv = (uint8_t* )calloc(1, sa[10].shivf_len * sizeof(uint8_t)); + *(sa[10].iv + 11) = 0; + sa[10].abm_len = ABM_SIZE; // 20 + sa[10].abm = (uint8_t* )calloc(1, sa[10].abm_len * sizeof(uint8_t)); + sa[10].arsnw_len = 1; + sa[10].arsnw = 5; + sa[10].arsn_len = 0; + sa[10].gvcid_tc_blk.tfvn = 0x00; + sa[10].gvcid_tc_blk.scid = 0x002C; + sa[10].gvcid_tc_blk.vcid = 1; + sa[10].gvcid_tc_blk.mapid = 0x00; + sa[10].ek_ref="kmc/test/key130"; + + // SA 11 - KEYED; ARSNW:5; AES-GCM; IV:00...00; IV-len:12; MAC-len:16; Key-ID: 130 + // SA 11 VC0/1 is now 4-VC0, 7-VC1 + sa[11].spi = 11; + sa[11].ekid = 130; + sa[11].sa_state = SA_KEYED; + sa[11].est = 1; + sa[11].ast = 0; + sa[11].ecs_len = 1; + sa[11].ecs = calloc(1, sa[11].ecs_len * sizeof(uint8_t)); + *sa[11].ecs = CRYPTO_CIPHER_AES256_CBC; + sa[11].shivf_len = 16; + sa[11].iv_len = 16; + sa[11].shplf_len = 1; + sa[11].stmacf_len = 0; + sa[11].iv = (uint8_t* )calloc(1, sa[11].shivf_len * sizeof(uint8_t)); //TODO; Should this be iv_len instead of shiv_len? + *(sa[11].iv + (sa[11].iv_len - 1)) = 0; + sa[11].abm_len = ABM_SIZE; // 20 + sa[11].abm = (uint8_t* )calloc(1, sa[11].abm_len * sizeof(uint8_t)); + sa[11].arsnw_len = 0; + sa[11].arsnw = 5; + sa[11].arsn_len = 0; + sa[11].gvcid_tc_blk.tfvn = 0; + sa[11].gvcid_tc_blk.scid = SCID & 0x3FF; + sa[11].gvcid_tc_blk.vcid = 0; + sa[11].gvcid_tc_blk.mapid = TYPE_TC; + sa[11].ek_ref="kmc/test/key130"; + return status; } @@ -294,6 +347,7 @@ static int32_t sadb_close(void) if(sa[x].iv != NULL) free(sa[x].iv); if(sa[x].abm != NULL) free(sa[x].abm); if(sa[x].arsn != NULL) free(sa[x].arsn); + if(sa[x].acs != NULL) free(sa[x].acs); } return status; } @@ -350,7 +404,7 @@ static int32_t sadb_get_operational_sa_from_gvcid(uint8_t tfvn, uint16_t scid, u return CRYPTO_LIB_ERR_NO_INIT; } - for (i = 0; i < 10; i++) + for (i = 0; i < NUM_SA; i++) { if ((sa[i].gvcid_tc_blk.tfvn == tfvn) && (sa[i].gvcid_tc_blk.scid == scid) && (sa[i].gvcid_tc_blk.vcid == vcid) && (sa[i].sa_state == SA_OPERATIONAL) && @@ -430,7 +484,7 @@ static int32_t sadb_get_operational_sa_from_gvcid(uint8_t tfvn, uint16_t scid, u (sa[i].gvcid_tc_blk.mapid == mapid && sa[i].sa_state != SA_OPERATIONAL)) { #ifdef SA_DEBUG - printf(KRED "A valid but non-operational SA was found.\n" RESET); + printf(KRED "A valid but non-operational SA was found: SPI: %d.\n" RESET, sa[i].spi); #endif status = CRYPTO_LIB_ERR_NO_OPERATIONAL_SA; } diff --git a/src/src_mysql/sadb_routine_mariadb.template.c b/src/src_mysql/sadb_routine_mariadb.template.c index fa98dd47..cab36772 100644 --- a/src/src_mysql/sadb_routine_mariadb.template.c +++ b/src/src_mysql/sadb_routine_mariadb.template.c @@ -62,7 +62,7 @@ static const char* SQL_SADB_UPDATE_IV_ARC_BY_SPI = // sadb_routine mariaDB private helper functions static int32_t parse_sa_from_mysql_query(char* query, SecurityAssociation_t** security_association); static int32_t convert_hexstring_to_byte_array(char* hexstr, uint8_t* byte_array); -static char* convert_byte_array_to_hexstring(void* src_buffer, size_t buffer_length); +static void convert_byte_array_to_hexstring(void* src_buffer, size_t buffer_length, char* dest_str); /* ** Global Variables @@ -172,6 +172,7 @@ static int32_t sadb_close(void) mysql_close(con); con = NULL; } + return CRYPTO_LIB_SUCCESS; } @@ -208,11 +209,18 @@ static int32_t sadb_save_sa(SecurityAssociation_t* sa) } char update_sa_query[2048]; + + char* iv_h = malloc(sa->iv_len * 2 + 1); + convert_byte_array_to_hexstring(sa->iv, sa->iv_len, iv_h); + char* arsn_h = malloc(sa->arsn_len * 2 + 1); + convert_byte_array_to_hexstring(sa->arsn, sa->arsn_len, arsn_h); + snprintf(update_sa_query, sizeof(update_sa_query), SQL_SADB_UPDATE_IV_ARC_BY_SPI, - convert_byte_array_to_hexstring(sa->iv, sa->iv_len), - convert_byte_array_to_hexstring(sa->arsn, sa->arsn_len), sa->spi, sa->gvcid_tc_blk.tfvn, + iv_h, + arsn_h, sa->spi, sa->gvcid_tc_blk.tfvn, sa->gvcid_tc_blk.scid, sa->gvcid_tc_blk.vcid, sa->gvcid_tc_blk.mapid); - + free(iv_h); + free(arsn_h); #ifdef SA_DEBUG fprintf(stderr, "MySQL Insert SA Query: %s \n", update_sa_query); #endif @@ -221,7 +229,6 @@ static int32_t sadb_save_sa(SecurityAssociation_t* sa) if (mysql_query(con, update_sa_query)) { status = finish_with_error(&con, SADB_QUERY_FAILED); - return status; } // todo - if query fails, need to push failure message to error stack instead of just return code. @@ -232,6 +239,14 @@ static int32_t sadb_save_sa(SecurityAssociation_t* sa) free(sa->abm); if (sa->arsn != NULL) free(sa->arsn); + if (sa->ek_ref != NULL) + free(sa->ek_ref); + if (sa->ecs != NULL) + free(sa->ecs); + if (sa->acs != NULL) + free(sa->acs); + if (sa->ak_ref != NULL) + free(sa->ak_ref); free(sa); return status; } @@ -351,7 +366,9 @@ static int32_t parse_sa_from_mysql_query(char* query, SecurityAssociation_t** se sa->ekid = atoi(row[i]); } else // Cryptography Type KMC Crypto Service with PKCS12 String Key References { - sa->ek_ref = row[i]; + sa->ekid = 0; + sa->ek_ref = malloc((strlen(row[i])+1) * sizeof(char)); + memcpy(sa->ek_ref, row[i], strlen(row[i])+1); } continue; } @@ -362,7 +379,8 @@ static int32_t parse_sa_from_mysql_query(char* query, SecurityAssociation_t** se sa->akid = atoi(row[i]); } else // Cryptography Type KMC Crypto Service with PKCS12 String Key References { - sa->ak_ref = row[i]; + sa->ak_ref = malloc((strlen(row[i])+1) * sizeof(char)); + memcpy(sa->ak_ref, row[i], strlen(row[i])+1); } continue; } @@ -493,11 +511,11 @@ static int32_t parse_sa_from_mysql_query(char* query, SecurityAssociation_t** se sa->abm = (uint8_t* )calloc(1, sa->abm_len * sizeof(uint8_t)); sa->ecs = (uint8_t* )calloc(1, sa->ecs_len * sizeof(uint8_t)); sa->acs = (uint8_t* )calloc(1, sa->acs_len * sizeof(uint8_t)); - convert_hexstring_to_byte_array(iv_byte_str, sa->iv); - convert_hexstring_to_byte_array(arc_byte_str, sa->arsn); - convert_hexstring_to_byte_array(abm_byte_str, sa->abm); - convert_hexstring_to_byte_array(ecs_byte_str, sa->ecs); - convert_hexstring_to_byte_array(acs_byte_str, sa->acs); + if(sa->iv_len > 0) convert_hexstring_to_byte_array(iv_byte_str, sa->iv); + if(sa->arsn_len > 0) convert_hexstring_to_byte_array(arc_byte_str, sa->arsn); + if(sa->abm_len > 0) convert_hexstring_to_byte_array(abm_byte_str, sa->abm); + if(sa->ecs_len > 0) convert_hexstring_to_byte_array(ecs_byte_str, sa->ecs); + if(sa->acs_len > 0) convert_hexstring_to_byte_array(acs_byte_str, sa->acs); //arsnw_len is not necessary for mariadb interface, putty dummy/default value for prints. sa->arsnw_len = 1; @@ -528,28 +546,21 @@ static int32_t convert_hexstring_to_byte_array(char* source_str, uint8_t* dest_b return data_len; } -static char* convert_byte_array_to_hexstring(void* src_buffer, size_t buffer_length) +static void convert_byte_array_to_hexstring(void* src_buffer, size_t buffer_length, char* dest_str) { - if (buffer_length == 0) - { // Return empty string (with null char!) if buffer is empty - return ""; - } - unsigned char* bytes = src_buffer; - char* hexstr = malloc(buffer_length * 2 + 1); - - if (src_buffer == NULL) - return NULL; - for (size_t i = 0; i < buffer_length; i++) + if (src_buffer != NULL) { - uint8_t nib1 = (bytes[i] >> 4) & 0x0F; - uint8_t nib2 = (bytes[i]) & 0x0F; - hexstr[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; - hexstr[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA; + for (size_t i = 0; i < buffer_length; i++) + { + uint8_t nib1 = (bytes[i] >> 4) & 0x0F; + uint8_t nib2 = (bytes[i]) & 0x0F; + dest_str[i * 2 + 0] = nib1 < 0xA ? '0' + nib1 : 'A' + nib1 - 0xA; + dest_str[i * 2 + 1] = nib2 < 0xA ? '0' + nib2 : 'A' + nib2 - 0xA; + } + dest_str[buffer_length * 2] = '\0'; } - hexstr[buffer_length * 2] = '\0'; - return hexstr; } static int32_t finish_with_error(MYSQL **con_loc, int err) diff --git a/util/src_util/pt_auth_as.c b/util/src_util/pt_auth_as.c new file mode 100644 index 00000000..7828a089 --- /dev/null +++ b/util/src_util/pt_auth_as.c @@ -0,0 +1,1204 @@ +/* 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 make use of TC_ApplySecurity/TC_ProcessSecurity function on the data with KMC Crypto Service/MariaDB Functionality Enabled. + * BE SURE TO HAVE APPROPRIATE SA's READY FOR EACH SET OF TESTS + **/ +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" +#include "utest.h" + +#include "crypto.h" +#include "shared_util.h" +#include + +#include +#include + +int num_frames_1K = 1; +int num_frames_100 = 1; + +void Write_To_File(uint16_t enc_frame_len, float total_time, char* test_name, int num_frames, int reset) +{ + if(reset == 1) + { + if(access("PERFORMANCE_RESULTS_AAS.csv", F_OK) == 0) + { + int deleted = remove("PERFORMANCE_RESULTS_AAS.csv"); + if(deleted){printf("ERROR Deleting File!\n");} + } + } + + FILE *fp = NULL; + fp = fopen("PERFORMANCE_RESULTS_AAS.csv", "a"); + if (fp != NULL) + { + if(reset ==1) fprintf(fp, "Name of Test,Frames Sent,Bytes per Frame,Total Time,Mbps\n"); + fprintf(fp, "%s,%d,%d,%f,%f\n", test_name, num_frames, enc_frame_len, total_time, (((enc_frame_len * 8 * num_frames)/total_time)/1024/1024)); + } + fclose(fp); + +} + +double Apply_Security_Loop(uint8_t *frame, int frame_length, uint8_t *enc_frame, uint16_t *enc_frame_len, int num_loops) +{ + struct timespec begin, end; + double total_time; + total_time = 0.0; + + frame = frame; + frame_length = frame_length; + enc_frame_len = enc_frame_len; + + int32_t status = CRYPTO_LIB_SUCCESS; + + for(int i = 0; i < num_loops; i++) + { + printf("LOOP NUMBER: %d\n", i+1); + clock_gettime(CLOCK_REALTIME, &begin); + status = Crypto_TC_ApplySecurity(frame, frame_length, &enc_frame, enc_frame_len); + clock_gettime(CLOCK_REALTIME, &end); + free(enc_frame); + + long seconds = end.tv_sec - begin.tv_sec; + long nanoseconds = end.tv_nsec - begin.tv_nsec; + double elapsed = seconds + nanoseconds*1e-9; + + if (status != CRYPTO_LIB_SUCCESS) + { + total_time = -1.0; + printf("ERROR: %d\n", status); + break; + } + + total_time += elapsed; + } + return total_time; +} + +UTEST(PERFORMANCE, LSA_LIBG_AUTH_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_libg, "Auth Only: LSA+LIBG Apply Security SHORT", num_frames_100, 1); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_AUTH_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association; + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_mdb_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_mdb_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_libg, "Auth Only: MDB+LIBG Apply Security SHORT", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_AUTH_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: LSA+KMC Apply Security SHORT", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_AUTH_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: MDB+KMC Apply Security SHORT", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// ***************** MED TESTS 100 **********************// + +UTEST(PERFORMANCE, LSA_LIBG_AUTH_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_libg, "Auth Only: LSA+LIBG Apply Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_AUTH_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_mdb_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_mdb_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_libg, "Auth Only: MDB+LIBG Apply Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_AUTH_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: LSA+KMC Apply Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_AUTH_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: MDB+KMC Apply Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// ******************** LONG TESTS 100 ********************* + +UTEST(PERFORMANCE, LSA_LIBG_AUTH_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_libg, "Auth Only: LSA+LIBG Apply Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_AUTH_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_mdb_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_mdb_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_libg, "Auth Only: MDB+LIBG Apply Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_AUTH_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: LSA+KMC Apply Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_AUTH_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_100); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: MDB+KMC Apply Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// ***************** 1K **************** + +UTEST(PERFORMANCE, LSA_LIBG_AUTH_SHORT_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_libg, "Auth Only: LSA+LIBG Apply Security SHORT", num_frames_1K, 1); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_AUTH_SHORT_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_mdb_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_mdb_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_libg, "Auth Only: MDB+LIBG Apply Security SHORT", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_AUTH_SHORT_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: LSA+KMC Apply Security SHORT", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_AUTH_SHORT_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: MDB+KMC Apply Security SHORT", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// ***************** MED TESTS 1K **********************// + +UTEST(PERFORMANCE, LSA_LIBG_AUTH_MED_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_libg, "Auth Only: LSA+LIBG Apply Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_AUTH_MED_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_mdb_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_mdb_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_libg, "Auth Only: MDB+LIBG Apply Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_AUTH_MED_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: LSA+KMC Apply Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_AUTH_MED_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: MDB+KMC Apply Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// ******************** LONG TESTS 1K ********************* + +UTEST(PERFORMANCE, LSA_LIBG_AUTH_LONG_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_libg, "Auth Only: LSA+LIBG Apply Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_AUTH_LONG_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_mdb_libg = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_mdb_libg < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+LIBG AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_libg)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_libg, "Auth Only: MDB+LIBG Apply Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_AUTH_LONG_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->est = 0; + test_association->arsn_len = 0; + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nLSA+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: LSA+KMC Apply Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_AUTH_LONG_1K) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + Crypto_Init(); + char* data_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* data_b = NULL; + int data_l = 0; + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + hex_conversion(data_h, &data_b, &data_l); + + + float ttl_time_lsa_kmc = Apply_Security_Loop((uint8_t *) data_b, data_l, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + if(ttl_time_lsa_kmc < 0) status = CRYPTO_LIB_ERROR; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("\nMDB+KMC AUTH ONLY Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_kmc)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_kmc, "Auth Only: MDB+KMC Apply Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST_MAIN(); \ No newline at end of file diff --git a/util/src_util/pt_auth_enc_as.c b/util/src_util/pt_auth_enc_as.c new file mode 100644 index 00000000..7c0ad572 --- /dev/null +++ b/util/src_util/pt_auth_enc_as.c @@ -0,0 +1,1049 @@ +/* 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 make use of TC_ApplySecurity/TC_ProcessSecurity function on the data with KMC Crypto Service/MariaDB Functionality Enabled. + * BE SURE TO HAVE APPROPRIATE SA's READY FOR EACH SET OF TESTS + **/ +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" +#include "utest.h" + +#include "crypto.h" +#include "shared_util.h" +#include + +#include +#include + +int num_frames_1K = 1; +int num_frames_100 = 1; + +void Write_To_File(uint16_t enc_frame_len, float total_time, char* test_name, int num_frames, int reset) +{ + if(reset == 1) + { + if(access("PERFORMANCE_RESULTS_AEAS.csv", F_OK) == 0) + { + int deleted = remove("PERFORMANCE_RESULTS_AEAS.csv"); + if(deleted){printf("ERROR Deleting File!\n");} + } + } + + FILE *fp = NULL; + fp = fopen("PERFORMANCE_RESULTS_AEAS.csv", "a"); + if (fp != NULL) + { + if(reset ==1) fprintf(fp, "Name of Test,Frames Sent,Bytes per Frame,Total Time,Mbps\n"); + fprintf(fp, "%s,%d,%d,%f,%f\n", test_name, num_frames, enc_frame_len, total_time, (((enc_frame_len * 8 * num_frames)/total_time)/1024/1024)); + } + fclose(fp); + +} + +double Apply_Security_Loop(uint8_t *frame, int frame_length, uint8_t *enc_frame, uint16_t *enc_frame_len, int num_loops) +{ + struct timespec begin, end; + double total_time; + total_time = 0.0; + + frame = frame; + frame_length = frame_length; + enc_frame_len = enc_frame_len; + + int32_t status = CRYPTO_LIB_SUCCESS; + + for(int i = 0; i < num_loops; i++) + { + printf("LOOP NUMBER: %d\n", i+1); + clock_gettime(CLOCK_REALTIME, &begin); + status = Crypto_TC_ApplySecurity(frame, frame_length, &enc_frame, enc_frame_len); + clock_gettime(CLOCK_REALTIME, &end); + free(enc_frame); + + long seconds = end.tv_sec - begin.tv_sec; + long nanoseconds = end.tv_nsec - begin.tv_nsec; + double elapsed = seconds + nanoseconds*1e-9; + + if (status != CRYPTO_LIB_SUCCESS) + { + total_time = -1.0; + printf("ERROR: %d\n", status); + break; + } + + total_time += elapsed; + } + return total_time; +} + +UTEST(PERFORMANCE, LSA_LIBG_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init_Unit_Test(); + char* raw_tc_sdls_ping_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + + char* raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + float ttl_time_lsa_clib_100 = 0.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; + + SecurityAssociation_t* test_association; + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->arsn_len = 0; + + ttl_time_lsa_clib_100 = + Apply_Security_Loop((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nLSA+LIBG Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_clib_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_clib_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_clib_100, "LSA+LIBG Apply Security SHORT", num_frames_100, 1); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+LIBG Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+LIBG Apply Security SHORT", num_frames_100, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nLSA+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "LSA+KMC Apply Security SHORT", num_frames_100, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_SHORT_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+KMC Apply Security SHORT", num_frames_100, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_LIBG_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init_Unit_Test(); + char* raw_tc_sdls_ping_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + + char* raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + float ttl_time_lsa_clib_100 = 0.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; + + SecurityAssociation_t* test_association; + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->arsn_len = 0; + + ttl_time_lsa_clib_100 = + Apply_Security_Loop((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nLSA+LIBG Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_clib_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_clib_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_clib_100, "LSA+LIBG Apply Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+LIBG Apply Security MED", num_frames_100, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "LSA+KMC Apply Security MED", num_frames_100, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_MED_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+KMC Apply Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_LIBG_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init_Unit_Test(); + char* raw_tc_sdls_ping_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + float ttl_time_lsa_clib_100 = 0.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; + + SecurityAssociation_t* test_association; + // 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->arsn_len = 0; + + ttl_time_lsa_clib_100 = + Apply_Security_Loop((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nLSA+LIBG Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_clib_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_lsa_clib_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_clib_100, "LSA+LIBG Apply Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+LIBG Apply Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "LSA+KMC Apply Security LONG", num_frames_100, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_LONG_100) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_100); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+KMC Apply Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_LIBG_SHORT_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init_Unit_Test(); + char* raw_tc_sdls_ping_h = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + + char* raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + float ttl_time_lsa_clib_100 = 0.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; + + SecurityAssociation_t* test_association; + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->arsn_len = 0; + + ttl_time_lsa_clib_100 = + Apply_Security_Loop((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nLSA+LIBG Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_clib_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_clib_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_clib_100, "LSA+LIBG Apply Security SHORT", num_frames_1K, 0); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_SHORT_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+LIBG Apply Security SHORT", num_frames_1K, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_SHORT_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "LSA+KMC Apply Security SHORT", num_frames_1K, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_SHORT_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C046100ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEFF01C"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+KMC Apply Security SHORT", num_frames_1K, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_LIBG_MED_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init_Unit_Test(); + char* raw_tc_sdls_ping_h = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + + char* raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + float ttl_time_lsa_clib_100 = 0.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; + + SecurityAssociation_t* test_association; + // 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(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->arsn_len = 0; + + ttl_time_lsa_clib_100 = + Apply_Security_Loop((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nLSA+LIBG Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_clib_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_clib_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_clib_100, "LSA+LIBG Apply Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_MED_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+LIBG Apply Security MED", num_frames_1K, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_MED_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_short = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_short, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "LSA+KMC Apply Security MED", num_frames_1K, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_MED_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C05E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b00313011"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+KMC Apply Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_LIBG_LONG_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init_Unit_Test(); + char* raw_tc_sdls_ping_h = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + char* raw_tc_sdls_ping_b = NULL; + int raw_tc_sdls_ping_len = 0; + SadbRoutine sadb_routine = get_sadb_routine_inmemory(); + + float ttl_time_lsa_clib_100 = 0.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; + + SecurityAssociation_t* test_association; + // 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->arsn_len = 0; + + ttl_time_lsa_clib_100 = + Apply_Security_Loop((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nLSA+LIBG Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_clib_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_lsa_clib_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_lsa_clib_100, "LSA+LIBG Apply Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_LONG_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+LIBG Apply Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_LONG_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "LSA+KMC Apply Security LONG", num_frames_1K, 0); + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + //free(cryptography_kmc_crypto_config); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_LONG_1000) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + char* raw_tc_jpl_mmt_scid44_vcid1_long = "202C07E1000080d2c70008197fABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF01234567890b0031626E"; + + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + float ttl_time_mdb_kmc_100 = 0.0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1_long, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + + ttl_time_mdb_kmc_100 = Apply_Security_Loop((uint8_t* )raw_tc_jpl_mmt_scid44_vcid1_expect, raw_tc_jpl_mmt_scid44_vcid1_expect_len, ptr_enc_frame, &enc_frame_len, num_frames_1K); + + printf("\nMDB+KMC Apply Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", enc_frame_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((enc_frame_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + + Write_To_File(enc_frame_len, ttl_time_mdb_kmc_100, "MDB+KMC Apply Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(raw_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST_MAIN(); \ No newline at end of file diff --git a/util/src_util/pt_auth_enc_ps.c b/util/src_util/pt_auth_enc_ps.c new file mode 100644 index 00000000..abc75703 --- /dev/null +++ b/util/src_util/pt_auth_enc_ps.c @@ -0,0 +1,1043 @@ +/* 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 make use of TC_ApplySecurity/TC_ProcessSecurity function on the data with KMC Crypto Service/MariaDB Functionality Enabled. + * BE SURE TO HAVE APPROPRIATE SA's READY FOR EACH SET OF TESTS + **/ +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" +#include "utest.h" + +#include "crypto.h" +#include "shared_util.h" +#include + +#include +#include + +int num_frames_1K = 1; +int num_frames_100 = 1; + +void Write_To_File(uint16_t enc_frame_len, float total_time, char* test_name, int num_frames, int reset) +{ + if(reset == 1) + { + if(access("PERFORMANCE_RESULTS_AEPS.csv", F_OK) == 0) + { + int deleted = remove("PERFORMANCE_RESULTS_AEPS.csv"); + if(deleted){printf("ERROR Deleting File!\n");} + } + } + + FILE *fp = NULL; + fp = fopen("PERFORMANCE_RESULTS_AEPS.csv", "a"); + if (fp != NULL) + { + if(reset ==1) fprintf(fp, "Name of Test,Frames Sent,Bytes per Frame,Total Time,Mbps\n"); + fprintf(fp, "%s,%d,%d,%f,%f\n", test_name, num_frames, enc_frame_len, total_time, (((enc_frame_len * 8 * num_frames)/total_time)/1024/1024)); + } + fclose(fp); + +} + +double Process_Security_Loop(char *data_b, int* data_l, TC_t* processed_frame, int num_loops) +{ + struct timespec begin, end; + double total_time; + total_time = 0.0; + + + int32_t status = CRYPTO_LIB_SUCCESS; + + for(int i = 0; i < num_loops; i++) + { + printf("LOOP NUMBER: %d\n", i+1); + clock_gettime(CLOCK_REALTIME, &begin); + status = Crypto_TC_ProcessSecurity((uint8_t*) data_b, data_l, processed_frame); + clock_gettime(CLOCK_REALTIME, &end); + free(processed_frame->tc_sec_header.iv); + free(processed_frame->tc_sec_header.sn); + free(processed_frame->tc_sec_header.pad); + free(processed_frame->tc_sec_trailer.mac); + + long seconds = end.tv_sec - begin.tv_sec; + long nanoseconds = end.tv_nsec - begin.tv_nsec; + double elapsed = seconds + nanoseconds*1e-9; + + if (status != CRYPTO_LIB_SUCCESS) + { + total_time = -1.0; + printf("ERROR: %d\n", status); + break; + } + + total_time += elapsed; + } + return total_time; +} + +UTEST(PERFORMANCE, LSA_LIBG_SHORT_100) +{ + 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F00000A00000000000000000000006367CCB04793EECE4ECFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DFB63A4245482C24985171000B61A0C7F0386C"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nLSA+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "LSA+LIBG Process Security SHORT", num_frames_100, 1); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_SHORT_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F0000020000000000000000000070E304CDF655C3FE953D03F0C9322A30FC1E9E93A753017B3B7890A0FF7DECFFE57CBBE804F7CB184436CD7F21D92E01586D243D128E195834F3070365D9CE59D7F71F7F71C4E60FA424ADE3C3976200268804BB9CD6027F9BCFA3BF13F126C5565AF370736625F4A32B1B390B11D3"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nMDB+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_mdb_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_libg_100, "MDB+LIBG Process Security SHORT", num_frames_100, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_SHORT_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F00000A00000000000000000000006367CCB04793EECE4ECFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DFB63A4245482C24985171000B61A0C7F0386C"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nLSA+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_kmc_100, "LSA+KMC Process Security SHORT", num_frames_100, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_SHORT_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F0000020000000000000000000071B20E13F2B09519E820E696F04588AACE79E1827D42E5EA66F450E2C4893674185EC19C970BE7CABD06AB8768B04F5A29A1AA58FC539A3010EB674B2FC821441BA36AF225474E8E0998513417C525336E858704588E4F3083EC3EA4245D3C6F1CA5312A20DC3AADC47A0310C7FB09"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nMDB+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_kmc_100, "MDB+KMC Process Security SHORT", num_frames_100, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// MEDIUM TESTS + +UTEST(PERFORMANCE, LSA_LIBG_MED_100) +{ + 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF00000A000000000000000000000063CC818D81B0A3B0B8CFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DF90F3545628E3182AADD4E7084E423A4CCA88F7C0BAE07A50169E687A1D6232EA056F02AD1F8362D9168A239CDF20EA0615E2FF9B3FC9E02852DD335F0ABCEBBC45A037E073A09B3300B3A2275A646209F4F5BF9C95A9D2A20881273A269B5DB614572D1E117CB73587832D3D63ACD4AD8C9C73AE9A0E521A8C85E19D20F9D670E709924849C46D578D91C0790EF998663E03DE0B830360B2C8E14DF8FA33BC0AC0120CCCA5823543E999C48064B140D8034EBB299E238E526B0443C239EE1CBA826BDAA8705DF421B073A08706D38E11DBD988E08EF9A38C4E4E726326FF54DC43AA76B0EAF004973BCDD51265B306D68EF393E6389AE35858D1B619A3B7D6A3656C3F8EA9512FA6685A3F2710A5A6274FCA0B69275339BC09F3349700E4214A275B9362EE08D2E1E6BBFE0D038007470DD17D8133451B027D1C73AA491256489F6FA2B1964BBA4A6746544ABF98C20C9511E5EFF08678A4B04BFBDFDF401D092FEF153DAB01DB3EBBF0C1879758A6485342DF30D84F46059846F2B910AC279437195F7B80DB14495CA46D9BC075A94CEE7F"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nLSA+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "LSA+LIBG Process Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_MED_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF000002000000000000000000007147C4888DE20936C8FFF8772894382BC0EADCFDA9B684BC64250309930E745CB9B300EA84D6C694C8688DCFA9007C276FDC33CDFE2CA12415C359FEDED084549487AA3FD876D04BC947B2EDA171983D7FEE5E627D93ADEF5EA9D9790203E1683B6454AD33614D47903A9D2BD46620FCB1A20DA9138B7A42DDA9F4E5207E3A1A15ECE4C917903B0CACEEED9EC8391AC0010F5BBD309EE3FDD5337BFE51F70DC07EB006B646967269CE93073402CCFB547577AA45CAC5823C35F6F76496F1621252491306EE70089F7DFAE6C1A9E515409E1603F61ADDD12C3BDFF98B2E8595B2CCC0F8339D081380EBBDC295DF274F8C4A02BBE1FC7E405CD54CF11FFA4FF6983A2AD6EAF2EB98EE3418191AE5CFCEBB8653091E4605D017C382990B18E2569A64DE56152CBF5B76D23614E60B449CE24924D549325A0447B88D931C8A0580DD167F5FD1B670A36D965FD3411005C4809F1CF1245C1DA008F1003B7334F933C89DDBF21BC556F8E5F244464A77FFB4320C64DBCD9DF82B3D720A8C9796C3BB8677355A7FF0BDC43D48D8990DC8EA34DA3B796AB0515341522D67FF629C283EA8B71CF261BD2F758443E880A3BEB61C5A839BD702D6D35F7B783B3752D59A1D4A4909A677F88572B84665184AE5149E9492728DA5BBDEA36B6B27E2E6F8194136604C98497056815197F871B8C36B51"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nMDB+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "MDB+LIBG Process Security MED", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_MED_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF00000A000000000000000000000010B7D5264AA13C588EBCE851BB4CF923A94BDCB9E04ABB6688055EE94074AEFF78139C195EFB6D26F9D5DF2A002D4D26F6C1420F623D9EC8AC1B47EF469314F30D2466B5A1F8E4DC1ED2FFDA3B82C6BF6A8CC93B220C9B15A7FDCC1C3D86AEAA7EC2158D603F5FA4E102AE14917E5CF9B4A5EDC23E4EC5E42368C0D8FB1CF9FBCEE6ADC4790ACDE3B91BA41907416B949B7852DBFF244752242405E58A2C9B48D26E428DC89168A7AD434D3800B60192016F7925A27E12EE8F6C58BFC4D5297C39139B4871A2A37A1B5E4669F753A95003569BDD6CA8DB454CB25677D9227D9744EC09391F56A22970580AF34E5743918CF87387918596155B153CC913B588D07E96D765B11703B2D6C5910BFF92042C5ABD4FC474A0E050DACA184FCE4CAF82903F5C2BE659BD8551D1CA66BBA61B3611D4E9DA7E3060400A80F7F9504867F0943BCAC62EA2D07DD1BEE2FBD52439FA45A5F1AB8041D724E6B73A3803AA0B701BA7821B797C30C71C3B3BAF0CFA3C76DBC8323FFB22FF8507C995B5C552883141054E8B0B01594BCA4E6741E6D5DCC5CCA0D5D8A0F07E4771607513AEC948157966E0D0A8B0C8F8AC3D114F1095781B6F29B37655484F8C7C700D41F7B231D2A4637B10EAFDEFA26D133ECE3FDECF73E379BE93E3488F91F82C01031839E851EB324969EA367D98965AC0FA351B"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nLSA+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_kmc_100, "LSA+KMC Process Security MED", num_frames_100, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_MED_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF0000020000000000000000000071B8A9140EDCF90BEB2E84643FB928463D5E7CA5A2CA190B13DFCCBD7C150FC57FCAE7812FD90A672132370F05C11740089A0A263DE28CCF66901393AD69E63E71BF7795929B3F1ECB93D8C73168C84150835C3942C6DE5C015FEEC35DF5227FC068098CFB6307D7D1D9888B81688DFED4D6A16915A636DD6D93C46DE65C869A9E08985D238D5898B07760EAC9DEF46E9F3A38BDF6A28B9185350DB8C0AD33CDA11E6DF84EB849E7288138CEA52F8E9BACB6C461D6E30E365E89697D1FEE1D484452207403A988B643779A07D56A91CFC0C7C197DDC0C68AD837D0FF248AFE3D0F5A46FEB4380EEF796C46D1A279A4D1E12103107FDF84BB1A4FCCF7E56460CEC85F99580597966B5214BBFE22E84E078EFB664D79A98A850F1FC2DDCCD43A92E25D5732C4700F86D2D342A67EBD2363032F7B2E1C1F2D7C003D0590FD4ABD064AE5C8FCFCD656A2AF510223345CC9F2F8837F3060A66F6DAF811E93600D9CB9BC3B3B66EFC395B86DF065C66C9C8A86192092AED70AC44A1D33D219ABE453E47764B78B5ED8689E06FA40A1276874E99560BA983B01B4268C1FD6B7CAA90B5148D2B39E2026C2E6AD56A9071894A7F6FD0BBE91F75519A0ACC72196F3CD72ACACD0820DA674215E80D63D3C9AFE59FFE547AB2F5F7EE16FFF9328EF6473BD7D3121116AD14868BDA4EA305636D744"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nMDB+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_kmc_100, "MDB+KMC Process Security MED", num_frames_100, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// LONG TESTS + +UTEST(PERFORMANCE, LSA_LIBG_LONG_100) +{ + 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF00000A000000000000000000000063CC818D81B0A3B0B8CFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DF90F3545628E3182AADD4E7084E423A4CCA88F7C0BAE07A50169E687A1D6232EA056F02AD1F8362D9168A239CDF20EA0615E2FF9B3FC9E02852DD335F0ABCEBBC45A037E073A09B3300B3A2275A646209F4F5BF9C95A9D2A20881273A269B5DB614572D1E117CB73587832D3D63ACD4AD8C9C73AE9A0E521A8C85E19D20F9D670E709924849C46D578D91C0790EF998663E03DE0B830360B2C8E14DF8FA33BC0AC0120CCCA5823543E999C48064B140D8034EBB299E238E526B0443C239EE1CBA826BDAA8705DF421B073A08706D38E11DBD988E08EF9A38C4E4E726326FF54DC43AA76B0EAF004973BCDD51265B306D68EF393E6389AE35858D1B619A3B7D6A3656C3F8EA9512FA6685A3F2710A5A6274FCA0B69275339BC09F3349700E4214A275B9362EE08D2E1E6BBFE0D038007470DD17D8133451B027D1C73AA491256489F6FA2B1964BBA4A6746544ABF98C20C9511E5EFF08678A4B04BFBDFDF401D092FEF153DAB01DB3EBBF0C1879758A6485342DF30D84F46059846F2B9100CEA4A31F68604E41C4448D56A4174B38CF4BA1B365904A442ABCAE1773DBD4007470E674DD5422C5F6DE74EBB068C1C0EEBB68378BB1CFFBC8893DB94136AA501D30C7AEA14135C95C7FA017A893681DF3696448D7F4523102B0A93D183097560B75145754158C7A77B8CE69A7BC7625E28B59FCAD104FDB619B2BB9D5BA7C621697EBC87F80A7FD43480A46828E433314522B9146FCA62CB2105D1EB22D19F55FAE320ED16D54F150FD20081F1336572D128F6D2803A09E8E35456ECB1D6AE0A3A8ECCE231C4602893171538BF40E5AD79003B2CEFA33FDD83D5A79E0BCF26A25754D1E91B279222393F8BDB7A07A0B5BD15458FAB9FB6138192D43A294A9FDE48F660A497305C49726BB3AF29521F8EB686441C34E2DF0BB5EE4E7B0FA14E0A56879A44D252BDA7939B777B6D283C85522F77202A574C0BA9049A3B9BBD059B2CE6CEAEE88B7C979FCB4333BB39AF5F14CFF8B8E8F477C15F11FC6A89365DD8CD258339AB6B748EAB34F93D007805E904A532E7BB5C90AE88209E6170A6656AFCBA6C9F6F5902A8117694C28BF9CF396648E993F5C59D5C60C5C175EEE70E2EA72EA67E2E6535E56E4F95B03B3B077572C6A021D1F1E54EA83DA804E84CD4EE2368917D367552B102B80A5DA8203F65FA9C1BDB5992C0A75F9FF4B9052CD1A59D1AFFC4F31397702C795E36FD31DB437F2376F0E5C9D451777AF0BF88CFE466EE9F4BF2B7929689EDFB2A529ECF8DAFF177E382"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nLSA+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "LSA+LIBG Process Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_LONG_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF000002000000000000000000007217E059763EF5ADFC613DFAC3D96B906BE1361B1652B72637738C5731EDEF6C0C8EF2A5915169F1C6BBF7755B2D96BEE2F6DBC490A432A1515A89F4164B00020159A9B8646EDA561DB153F0FF6B4FCDE2E1E7FF4A1F3C155742E9298CA19A3F360FCC6CEC154802771F81F7214709DAEFB8D7732D311C2D11BE133D9F4882A82EE5B16A204B102FC8B21E9F3F841589EB85A97ED72781A2A2482A7E81ADB16628D3C7E8B15BDE350A18D9C459A43DE5B048F8C97A92B56E39C05B41AC0119427AE80E5A85FF7D52D25AFFF8B361FE938051399238864B4ED9983BEB4BE0F467530ABA03BF9EAAAAC9899A934E8FDB7DBDF5FE7F6B10B461D88045DEFBE3B872D11DBE975F2EE94A4BE9225653EFE1D6B06CBD6B0987E74B15E348135C41A583A0B5F249B08615FBD9DADFF8C29C9ED9886501B39857A7E2971DE338BC25379CC1E525A1201DE30922C9E3975D7F7E7538FE3D08B9DDF34E7C9DAB5FC961747A4F6F2FAF5E1BEE8F1EBEC4403FE3F6AC90369A04CB64AEBFF8C2457EFB0FE7714F72712C5256CBF18F25BF1AF13F6B639CCBED04CDC6FBA65F09B3ADF0F912995C5AF40157706D7F572C3481AA215F6B1AF5B7DCD519EDCD02EF66358588A756F3AB8C4C7D7B446D425F0DC7D309BADA9078DE415175DDCEDBDEDD6028D67DD4B83DB1D15EAC3F1A8926D11903F1C48484EF953E846203E5EDF6FA62F93D1FCD5E70C3E17017CFB7FC7C9B8A71B4F2B290893E18C7CC2E0BD8CA9FA940D1DC5129B062CBD2FACEDE3D26F5CDB2CC38F013B032835F1E0C6F2898A77DC8859F485FD64CCA79BA79A0911F87F5EA2C1AE5F72D530E8AE17F094A964ABD43C55E7BB7EBD63479C0E1F4014E5BA6B11FBA230A038B9C1470E289555D4313A2C166B34316BDFA84C428F11E61C11B300039B6FAA1D44ABD313ACCAB31DEE5E7D461BE78428EFEBC7DFBC49CBCB5F98DE92F4F07B477A57CB3426D7A66E5A07CC49A1061F8912C68F7C691BEFBD6DF536A9F26AEAEB6968F3F35017760B1840D4CF1E277FE8D9A99326A9FAD68814DE6F929253F2D4F3AF046C578A434DBFEA4B2CC9834633CF5919085126D95F74A444328113408AE6D798FF6961C38822D4523F9AA99C766AE3E5104BE7EE0E027C2F9B73BA07ADA882762FD93F590BF29FD0429BBC83DB4F531C1187B8DECEC0E5171027433358CE6E0AAAF50B59AC32EDE3D5B198B7C6AD462F00F9E7E52A21DE992037FDED63EF5646C236701729096AC704DA4A649C244B24242795DA49B25C34AB8F2E824DE4F42E1D4F06CC79F594DB4F62F31CD928C4CD678451CA7511FBB30F771F45522410586A22321C683F98774C89864E9ADF030216BC083E991CA96E4E751324B7E0AC2775996CDF33A46E09BCC24856D3112D844D337C57B9519F966A4"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nMDB+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "MDB+LIBG Process Security LONG", num_frames_100, 0); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_LONG_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF00000A0000000000000000000000042DD72659FA856512F4C35CA125B94FA62D41A46ECE953F746436776EDBB107E4CB804D02C0A794341D4D7D5A83C8735001B649FABAC5D32D62E64D3A7768CA60B79861A05434DAF398873705D0226717B99079451D42F01FCA9CC895277B705096CA10A9873760CE387FCAB553B93D907F0C299B8427A514C09B36D9008DEFD8C38F7D9E2E7880430D08BFD76C98AE0A1E4A78A5D6E3651C81C418A20C1AA6871BD2C9170A411AB9C00376BC7019E867F7BA0D6A7C28A49E8462216D35C97DA05779F1DEE2A2C1C83E12362A2748E9D7555E8F8CAAAA85611DD3BE5E68C55949329FC64A94804449DA2442406FDFAD6878945EFC27479F4F4DCB82399978F9BAB3D583AC548CEB7EFFEC13D6EE49ECD258781B8F395B9B129F41A72CEDA58D5AE2373EDE39C037042BBFB926DC5C01C92A8D32AF6908754612C2CF6311CAEDF8E78DC3E632C4485D972CF70250A376E687551BB5CE544779AC915CB39ED489B0FA5F7B2948333F5D4DC9CD07D712C764A1085C696473FE62AB77DF0E7E67D91EAF1B1A96F9FE3014B33450B63E6783D2DB965EED2BD26392B221814AA01EEB646B54B2AA45A29F9808283E5FBF04B49C6A3BEE6480E67169825A4E2DA8EB0C7AC690C380107CC888722844DE1C600B2ACFB74ECEB4425B63E8B7AF906540D30333D27BB3DEDAC1A7F04A0584D06EBF0867BC6ADDFBF52B17A7FBE3AD2E814DA4307607DA9C3FC2818A527CDF4E41F4CBF853F25086E90BF8836A9C839830AA9D72283E92A5C5CAACC786CF7FBED1528666FEE7B02CA1ED6F5A05630366AA0DFA37D3B0AB13ADBB2EE053465F7C39A01FF125EADA21619420A89B7FC1706B7E2C6F21ECC923AE9E602FAAD137DBEA66B7CA8C536B00218DC58C398CA465F20DB15438E0D8A2D421AA56FFC7765B7B1903C20312103B86B96B2F8A2F8A72CEBC7D66F86FC2B1EC1662B602C72EEC1C2D6A0B6AF0DF1BC2AFE635C2AB2C083F06D303FE3E45766C80499073177429C7A81EF3883A86FC4271D8F0C91EAB043DCF130CFC2AA1A48C3C20F1ABC1CE1E17B3E64DF5FE4996E285DEC0011A22DAAED5C58529B77E273A2389548748B780271D33A8BB8C380CE1C3CE8F3B2A487DD10D828F3146A2CE61F94CAFF3A5A099A59BF8B59196F271FFF718D097779D56BA71ECFEC97FDDF7BCAC120C264581B3EE45D6C2B01DD0382178134B940D4048847CFB5555A95E33DE7D59C35D41BC37625A64E87AC5F425F7CF88C3D78FF3449464AD4BE53818E8238C9EDB9A1389CAA421A656CDDE86057D08F636AD4462EEBB09B942CE6C86ABA6935DE352AF100257C7B416842C1B4DE70F5DBF55087B297C32A26DC47920B1CD70D1D893EB12703CF04DBD58C4EAE5B5CB674582C69FFD4A06F8491F56DAC15DADFB508EDF1FAA"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nLSA+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_lsa_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_kmc_100, "LSA+KMC Process Security LONG", num_frames_100, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_LONG_100) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF00000200000000000000000000721BAC6AEED1D245278896BFCE8F455FC3DA3DFFFC84F3A42360697AC41AA8387B67F79EE9E336B4F2900A074274C3D380C3076550FF491C2CD0131DE6EB2F2D2E5F1F8222A1421E911AD331872BF8A6A48165A8D4B0126B8F156EA11117F975D7F1289375EEC9972796EAE4554AD30C9EDB323518644DC504F4484BBB8447DDE7464F74BF271002141DCD5D961D2FA774703F4D708D21922D5D322D95F87762F93BC53AB12E7539784A7622502BD8BDDAD8CFF1415AD0CC27834CF6009A32E50AAC9B20612D4848B93689F1283C844739D03F96A81970A473167F37CAA1E7FF66C539285EB79AC17AF709D3FC7395479CAA2724189ED064AFEEE81D28ECD0CDEEA105A7F3D8AC2AC42C5FD4BC25569EDD6A5B9674A1F90034E031DB30EFDE4789BC381BAF2D367165CA77114B2B72155DD90A48BE229AC984A6153C3E78B3A6C453CC7D791A1FDF6982F8530EAE61649B5FB83776CE0F607B438A7FF0C13F79A778B295F020FA993ADA08CCE57CB7B6098CC3ABF96EEBC6313C361F7DD0230B9FDDF04ED5E0D206178B8F63F072D4DE6DE8A60D1D5279E86F1E518FB875EC7F86406C4719BB6C4F8682F7F2693341599587EC884B882252437BDE838C318F13C2474D909331A737F0BB651656DF1CF46DB16B911851B1C2CCE0A03E3D9DF7494B3AFB5C24FC793A9D52144482B8A4FEA666B472CBD6E4B8355F39E02BEF428C5B950571D3D195561E0BD004A9BA089CA0776FC42C60E95CD5B190A12143F71473A352DC64BD8E5796C7ECC88506F0A6BEE0600E922053ADC2CB43EDC6852E52C82C388CE39C1C30F6DC30DD5B67B3A326F099C0FC5B8A3BDDDD3D3E0E136889AE5578FF746C2D81162023DA4F7C7EE2312284BB88CCA91AC5090BA5C2E101234E6D50F642F77960205CB9DDAB3E09FE77997B7CC4BF0BBC5AFC11CFB8E53936AD637101390BEC5B534AA48236DEEAEA2901EC42EA699B3DF1E0F91A533E0D1D76432DEDC1A5A2E6763662BD1E8D29D29FDA13C072C549B2F42B0E3B796981853DD7B776CA27142E60C65FCECD23CE7624CA0D81E966248C4C5D10953A6F742AD27C87A4950B4F172B706F65CCA2EFDA0FA6715BD95DD2841DB5C3262B210BF126A548D09C1EF0F7F83FA379D389C1890804AEE56B1CC7F45BF4422CCA3FC165F36BC5DD3546DB386A3551F0F467377D952C82D2890E70840F738FDC49838B1F30A16D1AF8DFE9E594D88EEBAEAE3B6DBA33F8DC69D95246E43A394E1463D0EFCAD040FE57CE9E95C57098E7EE76C6507889EB94232DA84B11572D462F932C04CCD60E3372024B7978B8960EA9145A297E7DF1114E48E6048CFDD31C1859B1A468991E1E67FAEAD138652A0BAA7A4D99669882357ECC0893E4D96A1E6E3D754CA24D5D997EDB91C647436A18A47377"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_100); + printf("\nMDB+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_100); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_100)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_kmc_100, "MDB+KMC Process Security LONG", num_frames_100, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// *********************************************** 1K Tests *********************************************// + +UTEST(PERFORMANCE, LSA_LIBG_SHORT_1K) +{ + 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F00000A00000000000000000000006367CCB04793EECE4ECFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DFB63A4245482C24985171000B61A0C7F0386C"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nLSA+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "LSA+LIBG Process Security SHORT", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_SHORT_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F0000020000000000000000000070E304CDF655C3FE953D03F0C9322A30FC1E9E93A753017B3B7890A0FF7DECFFE57CBBE804F7CB184436CD7F21D92E01586D243D128E195834F3070365D9CE59D7F71F7F71C4E60FA424ADE3C3976200268804BB9CD6027F9BCFA3BF13F126C5565AF370736625F4A32B1B390B11D3"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nMDB+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_mdb_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_libg_100, "MDB+LIBG Process Security SHORT", num_frames_1K, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_SHORT_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F00000A00000000000000000000006367CCB04793EECE4ECFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DFB63A4245482C24985171000B61A0C7F0386C"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nLSA+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_kmc_100, "LSA+KMC Process Security SHORT", num_frames_1K, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_SHORT_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C047F0000020000000000000000000071B20E13F2B09519E820E696F04588AACE79E1827D42E5EA66F450E2C4893674185EC19C970BE7CABD06AB8768B04F5A29A1AA58FC539A3010EB674B2FC821441BA36AF225474E8E0998513417C525336E858704588E4F3083EC3EA4245D3C6F1CA5312A20DC3AADC47A0310C7FB09"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nMDB+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_kmc_100, "MDB+KMC Process Security SHORT", num_frames_1K, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// MEDIUM TESTS + +UTEST(PERFORMANCE, LSA_LIBG_MED_1K) +{ + 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF00000A000000000000000000000063CC818D81B0A3B0B8CFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DF90F3545628E3182AADD4E7084E423A4CCA88F7C0BAE07A50169E687A1D6232EA056F02AD1F8362D9168A239CDF20EA0615E2FF9B3FC9E02852DD335F0ABCEBBC45A037E073A09B3300B3A2275A646209F4F5BF9C95A9D2A20881273A269B5DB614572D1E117CB73587832D3D63ACD4AD8C9C73AE9A0E521A8C85E19D20F9D670E709924849C46D578D91C0790EF998663E03DE0B830360B2C8E14DF8FA33BC0AC0120CCCA5823543E999C48064B140D8034EBB299E238E526B0443C239EE1CBA826BDAA8705DF421B073A08706D38E11DBD988E08EF9A38C4E4E726326FF54DC43AA76B0EAF004973BCDD51265B306D68EF393E6389AE35858D1B619A3B7D6A3656C3F8EA9512FA6685A3F2710A5A6274FCA0B69275339BC09F3349700E4214A275B9362EE08D2E1E6BBFE0D038007470DD17D8133451B027D1C73AA491256489F6FA2B1964BBA4A6746544ABF98C20C9511E5EFF08678A4B04BFBDFDF401D092FEF153DAB01DB3EBBF0C1879758A6485342DF30D84F46059846F2B910AC279437195F7B80DB14495CA46D9BC075A94CEE7F"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nLSA+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "LSA+LIBG Process Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_MED_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF000002000000000000000000007147C4888DE20936C8FFF8772894382BC0EADCFDA9B684BC64250309930E745CB9B300EA84D6C694C8688DCFA9007C276FDC33CDFE2CA12415C359FEDED084549487AA3FD876D04BC947B2EDA171983D7FEE5E627D93ADEF5EA9D9790203E1683B6454AD33614D47903A9D2BD46620FCB1A20DA9138B7A42DDA9F4E5207E3A1A15ECE4C917903B0CACEEED9EC8391AC0010F5BBD309EE3FDD5337BFE51F70DC07EB006B646967269CE93073402CCFB547577AA45CAC5823C35F6F76496F1621252491306EE70089F7DFAE6C1A9E515409E1603F61ADDD12C3BDFF98B2E8595B2CCC0F8339D081380EBBDC295DF274F8C4A02BBE1FC7E405CD54CF11FFA4FF6983A2AD6EAF2EB98EE3418191AE5CFCEBB8653091E4605D017C382990B18E2569A64DE56152CBF5B76D23614E60B449CE24924D549325A0447B88D931C8A0580DD167F5FD1B670A36D965FD3411005C4809F1CF1245C1DA008F1003B7334F933C89DDBF21BC556F8E5F244464A77FFB4320C64DBCD9DF82B3D720A8C9796C3BB8677355A7FF0BDC43D48D8990DC8EA34DA3B796AB0515341522D67FF629C283EA8B71CF261BD2F758443E880A3BEB61C5A839BD702D6D35F7B783B3752D59A1D4A4909A677F88572B84665184AE5149E9492728DA5BBDEA36B6B27E2E6F8194136604C98497056815197F871B8C36B51"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nMDB+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "MDB+LIBG Process Security MED", num_frames_1K, 0); + + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_MED_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF00000A000000000000000000000010B7D5264AA13C588EBCE851BB4CF923A94BDCB9E04ABB6688055EE94074AEFF78139C195EFB6D26F9D5DF2A002D4D26F6C1420F623D9EC8AC1B47EF469314F30D2466B5A1F8E4DC1ED2FFDA3B82C6BF6A8CC93B220C9B15A7FDCC1C3D86AEAA7EC2158D603F5FA4E102AE14917E5CF9B4A5EDC23E4EC5E42368C0D8FB1CF9FBCEE6ADC4790ACDE3B91BA41907416B949B7852DBFF244752242405E58A2C9B48D26E428DC89168A7AD434D3800B60192016F7925A27E12EE8F6C58BFC4D5297C39139B4871A2A37A1B5E4669F753A95003569BDD6CA8DB454CB25677D9227D9744EC09391F56A22970580AF34E5743918CF87387918596155B153CC913B588D07E96D765B11703B2D6C5910BFF92042C5ABD4FC474A0E050DACA184FCE4CAF82903F5C2BE659BD8551D1CA66BBA61B3611D4E9DA7E3060400A80F7F9504867F0943BCAC62EA2D07DD1BEE2FBD52439FA45A5F1AB8041D724E6B73A3803AA0B701BA7821B797C30C71C3B3BAF0CFA3C76DBC8323FFB22FF8507C995B5C552883141054E8B0B01594BCA4E6741E6D5DCC5CCA0D5D8A0F07E4771607513AEC948157966E0D0A8B0C8F8AC3D114F1095781B6F29B37655484F8C7C700D41F7B231D2A4637B10EAFDEFA26D133ECE3FDECF73E379BE93E3488F91F82C01031839E851EB324969EA367D98965AC0FA351B"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nLSA+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_kmc_100, "LSA+KMC Process Security MED", num_frames_1K, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_MED_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C05FF0000020000000000000000000071B8A9140EDCF90BEB2E84643FB928463D5E7CA5A2CA190B13DFCCBD7C150FC57FCAE7812FD90A672132370F05C11740089A0A263DE28CCF66901393AD69E63E71BF7795929B3F1ECB93D8C73168C84150835C3942C6DE5C015FEEC35DF5227FC068098CFB6307D7D1D9888B81688DFED4D6A16915A636DD6D93C46DE65C869A9E08985D238D5898B07760EAC9DEF46E9F3A38BDF6A28B9185350DB8C0AD33CDA11E6DF84EB849E7288138CEA52F8E9BACB6C461D6E30E365E89697D1FEE1D484452207403A988B643779A07D56A91CFC0C7C197DDC0C68AD837D0FF248AFE3D0F5A46FEB4380EEF796C46D1A279A4D1E12103107FDF84BB1A4FCCF7E56460CEC85F99580597966B5214BBFE22E84E078EFB664D79A98A850F1FC2DDCCD43A92E25D5732C4700F86D2D342A67EBD2363032F7B2E1C1F2D7C003D0590FD4ABD064AE5C8FCFCD656A2AF510223345CC9F2F8837F3060A66F6DAF811E93600D9CB9BC3B3B66EFC395B86DF065C66C9C8A86192092AED70AC44A1D33D219ABE453E47764B78B5ED8689E06FA40A1276874E99560BA983B01B4268C1FD6B7CAA90B5148D2B39E2026C2E6AD56A9071894A7F6FD0BBE91F75519A0ACC72196F3CD72ACACD0820DA674215E80D63D3C9AFE59FFE547AB2F5F7EE16FFF9328EF6473BD7D3121116AD14868BDA4EA305636D744"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nMDB+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_kmc_100, "MDB+KMC Process Security MED", num_frames_1K, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +// LONG TESTS + +UTEST(PERFORMANCE, LSA_LIBG_LONG_1K) +{ + 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF00000A000000000000000000000063CC818D81B0A3B0B8CFAE6926856AB15F27C747E4F00F0314AC3174263FCC8ABEEE245A705DF168E7AF81057111A8776502606B698CA119FFDAEE33FD3027358EC40BC2C166D6E5312BF6A813D97864A037D8E3696EFBF2052228DF90F3545628E3182AADD4E7084E423A4CCA88F7C0BAE07A50169E687A1D6232EA056F02AD1F8362D9168A239CDF20EA0615E2FF9B3FC9E02852DD335F0ABCEBBC45A037E073A09B3300B3A2275A646209F4F5BF9C95A9D2A20881273A269B5DB614572D1E117CB73587832D3D63ACD4AD8C9C73AE9A0E521A8C85E19D20F9D670E709924849C46D578D91C0790EF998663E03DE0B830360B2C8E14DF8FA33BC0AC0120CCCA5823543E999C48064B140D8034EBB299E238E526B0443C239EE1CBA826BDAA8705DF421B073A08706D38E11DBD988E08EF9A38C4E4E726326FF54DC43AA76B0EAF004973BCDD51265B306D68EF393E6389AE35858D1B619A3B7D6A3656C3F8EA9512FA6685A3F2710A5A6274FCA0B69275339BC09F3349700E4214A275B9362EE08D2E1E6BBFE0D038007470DD17D8133451B027D1C73AA491256489F6FA2B1964BBA4A6746544ABF98C20C9511E5EFF08678A4B04BFBDFDF401D092FEF153DAB01DB3EBBF0C1879758A6485342DF30D84F46059846F2B9100CEA4A31F68604E41C4448D56A4174B38CF4BA1B365904A442ABCAE1773DBD4007470E674DD5422C5F6DE74EBB068C1C0EEBB68378BB1CFFBC8893DB94136AA501D30C7AEA14135C95C7FA017A893681DF3696448D7F4523102B0A93D183097560B75145754158C7A77B8CE69A7BC7625E28B59FCAD104FDB619B2BB9D5BA7C621697EBC87F80A7FD43480A46828E433314522B9146FCA62CB2105D1EB22D19F55FAE320ED16D54F150FD20081F1336572D128F6D2803A09E8E35456ECB1D6AE0A3A8ECCE231C4602893171538BF40E5AD79003B2CEFA33FDD83D5A79E0BCF26A25754D1E91B279222393F8BDB7A07A0B5BD15458FAB9FB6138192D43A294A9FDE48F660A497305C49726BB3AF29521F8EB686441C34E2DF0BB5EE4E7B0FA14E0A56879A44D252BDA7939B777B6D283C85522F77202A574C0BA9049A3B9BBD059B2CE6CEAEE88B7C979FCB4333BB39AF5F14CFF8B8E8F477C15F11FC6A89365DD8CD258339AB6B748EAB34F93D007805E904A532E7BB5C90AE88209E6170A6656AFCBA6C9F6F5902A8117694C28BF9CF396648E993F5C59D5C60C5C175EEE70E2EA72EA67E2E6535E56E4F95B03B3B077572C6A021D1F1E54EA83DA804E84CD4EE2368917D367552B102B80A5DA8203F65FA9C1BDB5992C0A75F9FF4B9052CD1A59D1AFFC4F31397702C795E36FD31DB437F2376F0E5C9D451777AF0BF88CFE466EE9F4BF2B7929689EDFB2A529ECF8DAFF177E382"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nLSA+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "LSA+LIBG Process Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(processed_frame); + free(data_b); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_LIBG_LONG_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF000002000000000000000000007217E059763EF5ADFC613DFAC3D96B906BE1361B1652B72637738C5731EDEF6C0C8EF2A5915169F1C6BBF7755B2D96BEE2F6DBC490A432A1515A89F4164B00020159A9B8646EDA561DB153F0FF6B4FCDE2E1E7FF4A1F3C155742E9298CA19A3F360FCC6CEC154802771F81F7214709DAEFB8D7732D311C2D11BE133D9F4882A82EE5B16A204B102FC8B21E9F3F841589EB85A97ED72781A2A2482A7E81ADB16628D3C7E8B15BDE350A18D9C459A43DE5B048F8C97A92B56E39C05B41AC0119427AE80E5A85FF7D52D25AFFF8B361FE938051399238864B4ED9983BEB4BE0F467530ABA03BF9EAAAAC9899A934E8FDB7DBDF5FE7F6B10B461D88045DEFBE3B872D11DBE975F2EE94A4BE9225653EFE1D6B06CBD6B0987E74B15E348135C41A583A0B5F249B08615FBD9DADFF8C29C9ED9886501B39857A7E2971DE338BC25379CC1E525A1201DE30922C9E3975D7F7E7538FE3D08B9DDF34E7C9DAB5FC961747A4F6F2FAF5E1BEE8F1EBEC4403FE3F6AC90369A04CB64AEBFF8C2457EFB0FE7714F72712C5256CBF18F25BF1AF13F6B639CCBED04CDC6FBA65F09B3ADF0F912995C5AF40157706D7F572C3481AA215F6B1AF5B7DCD519EDCD02EF66358588A756F3AB8C4C7D7B446D425F0DC7D309BADA9078DE415175DDCEDBDEDD6028D67DD4B83DB1D15EAC3F1A8926D11903F1C48484EF953E846203E5EDF6FA62F93D1FCD5E70C3E17017CFB7FC7C9B8A71B4F2B290893E18C7CC2E0BD8CA9FA940D1DC5129B062CBD2FACEDE3D26F5CDB2CC38F013B032835F1E0C6F2898A77DC8859F485FD64CCA79BA79A0911F87F5EA2C1AE5F72D530E8AE17F094A964ABD43C55E7BB7EBD63479C0E1F4014E5BA6B11FBA230A038B9C1470E289555D4313A2C166B34316BDFA84C428F11E61C11B300039B6FAA1D44ABD313ACCAB31DEE5E7D461BE78428EFEBC7DFBC49CBCB5F98DE92F4F07B477A57CB3426D7A66E5A07CC49A1061F8912C68F7C691BEFBD6DF536A9F26AEAEB6968F3F35017760B1840D4CF1E277FE8D9A99326A9FAD68814DE6F929253F2D4F3AF046C578A434DBFEA4B2CC9834633CF5919085126D95F74A444328113408AE6D798FF6961C38822D4523F9AA99C766AE3E5104BE7EE0E027C2F9B73BA07ADA882762FD93F590BF29FD0429BBC83DB4F531C1187B8DECEC0E5171027433358CE6E0AAAF50B59AC32EDE3D5B198B7C6AD462F00F9E7E52A21DE992037FDED63EF5646C236701729096AC704DA4A649C244B24242795DA49B25C34AB8F2E824DE4F42E1D4F06CC79F594DB4F62F31CD928C4CD678451CA7511FBB30F771F45522410586A22321C683F98774C89864E9ADF030216BC083E991CA96E4E751324B7E0AC2775996CDF33A46E09BCC24856D3112D844D337C57B9519F966A4"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_libg_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nMDB+LIBG Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tBytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_libg_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_libg_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_libg_100, "MDB+LIBG Process Security LONG", num_frames_1K, 0); + + Crypto_Shutdown(); + free(processed_frame); + free(data_b); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, LSA_KMC_LONG_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF00000A0000000000000000000000042DD72659FA856512F4C35CA125B94FA62D41A46ECE953F746436776EDBB107E4CB804D02C0A794341D4D7D5A83C8735001B649FABAC5D32D62E64D3A7768CA60B79861A05434DAF398873705D0226717B99079451D42F01FCA9CC895277B705096CA10A9873760CE387FCAB553B93D907F0C299B8427A514C09B36D9008DEFD8C38F7D9E2E7880430D08BFD76C98AE0A1E4A78A5D6E3651C81C418A20C1AA6871BD2C9170A411AB9C00376BC7019E867F7BA0D6A7C28A49E8462216D35C97DA05779F1DEE2A2C1C83E12362A2748E9D7555E8F8CAAAA85611DD3BE5E68C55949329FC64A94804449DA2442406FDFAD6878945EFC27479F4F4DCB82399978F9BAB3D583AC548CEB7EFFEC13D6EE49ECD258781B8F395B9B129F41A72CEDA58D5AE2373EDE39C037042BBFB926DC5C01C92A8D32AF6908754612C2CF6311CAEDF8E78DC3E632C4485D972CF70250A376E687551BB5CE544779AC915CB39ED489B0FA5F7B2948333F5D4DC9CD07D712C764A1085C696473FE62AB77DF0E7E67D91EAF1B1A96F9FE3014B33450B63E6783D2DB965EED2BD26392B221814AA01EEB646B54B2AA45A29F9808283E5FBF04B49C6A3BEE6480E67169825A4E2DA8EB0C7AC690C380107CC888722844DE1C600B2ACFB74ECEB4425B63E8B7AF906540D30333D27BB3DEDAC1A7F04A0584D06EBF0867BC6ADDFBF52B17A7FBE3AD2E814DA4307607DA9C3FC2818A527CDF4E41F4CBF853F25086E90BF8836A9C839830AA9D72283E92A5C5CAACC786CF7FBED1528666FEE7B02CA1ED6F5A05630366AA0DFA37D3B0AB13ADBB2EE053465F7C39A01FF125EADA21619420A89B7FC1706B7E2C6F21ECC923AE9E602FAAD137DBEA66B7CA8C536B00218DC58C398CA465F20DB15438E0D8A2D421AA56FFC7765B7B1903C20312103B86B96B2F8A2F8A72CEBC7D66F86FC2B1EC1662B602C72EEC1C2D6A0B6AF0DF1BC2AFE635C2AB2C083F06D303FE3E45766C80499073177429C7A81EF3883A86FC4271D8F0C91EAB043DCF130CFC2AA1A48C3C20F1ABC1CE1E17B3E64DF5FE4996E285DEC0011A22DAAED5C58529B77E273A2389548748B780271D33A8BB8C380CE1C3CE8F3B2A487DD10D828F3146A2CE61F94CAFF3A5A099A59BF8B59196F271FFF718D097779D56BA71ECFEC97FDDF7BCAC120C264581B3EE45D6C2B01DD0382178134B940D4048847CFB5555A95E33DE7D59C35D41BC37625A64E87AC5F425F7CF88C3D78FF3449464AD4BE53818E8238C9EDB9A1389CAA421A656CDDE86057D08F636AD4462EEBB09B942CE6C86ABA6935DE352AF100257C7B416842C1B4DE70F5DBF55087B297C32A26DC47920B1CD70D1D893EB12703CF04DBD58C4EAE5B5CB674582C69FFD4A06F8491F56DAC15DADFB508EDF1FAA"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + // Expose/setup SAs for testing + SecurityAssociation_t* test_association;; + // Deactivate SA 1 + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->sa_state = SA_NONE; + // Activate SA 10 + sadb_routine->sadb_get_sa_from_spi(10, &test_association); + test_association->sa_state = SA_OPERATIONAL; + *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_lsa_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nLSA+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_lsa_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_lsa_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_lsa_kmc_100, "LSA+KMC Process Security LONG", num_frames_1K, 0); + Crypto_Shutdown(); + free(data_b); + free(processed_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(PERFORMANCE, MDB_KMC_LONG_1K) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + char* data_h = "202C07FF00000200000000000000000000721BAC6AEED1D245278896BFCE8F455FC3DA3DFFFC84F3A42360697AC41AA8387B67F79EE9E336B4F2900A074274C3D380C3076550FF491C2CD0131DE6EB2F2D2E5F1F8222A1421E911AD331872BF8A6A48165A8D4B0126B8F156EA11117F975D7F1289375EEC9972796EAE4554AD30C9EDB323518644DC504F4484BBB8447DDE7464F74BF271002141DCD5D961D2FA774703F4D708D21922D5D322D95F87762F93BC53AB12E7539784A7622502BD8BDDAD8CFF1415AD0CC27834CF6009A32E50AAC9B20612D4848B93689F1283C844739D03F96A81970A473167F37CAA1E7FF66C539285EB79AC17AF709D3FC7395479CAA2724189ED064AFEEE81D28ECD0CDEEA105A7F3D8AC2AC42C5FD4BC25569EDD6A5B9674A1F90034E031DB30EFDE4789BC381BAF2D367165CA77114B2B72155DD90A48BE229AC984A6153C3E78B3A6C453CC7D791A1FDF6982F8530EAE61649B5FB83776CE0F607B438A7FF0C13F79A778B295F020FA993ADA08CCE57CB7B6098CC3ABF96EEBC6313C361F7DD0230B9FDDF04ED5E0D206178B8F63F072D4DE6DE8A60D1D5279E86F1E518FB875EC7F86406C4719BB6C4F8682F7F2693341599587EC884B882252437BDE838C318F13C2474D909331A737F0BB651656DF1CF46DB16B911851B1C2CCE0A03E3D9DF7494B3AFB5C24FC793A9D52144482B8A4FEA666B472CBD6E4B8355F39E02BEF428C5B950571D3D195561E0BD004A9BA089CA0776FC42C60E95CD5B190A12143F71473A352DC64BD8E5796C7ECC88506F0A6BEE0600E922053ADC2CB43EDC6852E52C82C388CE39C1C30F6DC30DD5B67B3A326F099C0FC5B8A3BDDDD3D3E0E136889AE5578FF746C2D81162023DA4F7C7EE2312284BB88CCA91AC5090BA5C2E101234E6D50F642F77960205CB9DDAB3E09FE77997B7CC4BF0BBC5AFC11CFB8E53936AD637101390BEC5B534AA48236DEEAEA2901EC42EA699B3DF1E0F91A533E0D1D76432DEDC1A5A2E6763662BD1E8D29D29FDA13C072C549B2F42B0E3B796981853DD7B776CA27142E60C65FCECD23CE7624CA0D81E966248C4C5D10953A6F742AD27C87A4950B4F172B706F65CCA2EFDA0FA6715BD95DD2841DB5C3262B210BF126A548D09C1EF0F7F83FA379D389C1890804AEE56B1CC7F45BF4422CCA3FC165F36BC5DD3546DB386A3551F0F467377D952C82D2890E70840F738FDC49838B1F30A16D1AF8DFE9E594D88EEBAEAE3B6DBA33F8DC69D95246E43A394E1463D0EFCAD040FE57CE9E95C57098E7EE76C6507889EB94232DA84B11572D462F932C04CCD60E3372024B7978B8960EA9145A297E7DF1114E48E6048CFDD31C1859B1A468991E1E67FAEAD138652A0BAA7A4D99669882357ECC0893E4D96A1E6E3D754CA24D5D997EDB91C647436A18A47377"; + int32_t status = Crypto_Init(); + + char* data_b = NULL; + int data_l = 0; + + TC_t* processed_frame; + processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + // Convert hex to binary + hex_conversion(data_h, &data_b, &data_l); + + // Variables for gathering Time data + float ttl_time_mdb_kmc_100 = Process_Security_Loop(data_b, &data_l, processed_frame, num_frames_1K); + printf("\nMDB+KMC Process Security\n"); + printf("\tNumber of Frames Sent: %d\n", num_frames_1K); + printf("\t\tEncrypted Bytes Per Frame: %d\n", processed_frame->tc_pdu_len); + printf("\t\tTotal Time: %f\n", ttl_time_mdb_kmc_100); + printf("\tMbps: %f\n", (((processed_frame->tc_pdu_len * 8 * num_frames_1K)/ttl_time_mdb_kmc_100)/1024/1024)); + printf("\n"); + Write_To_File(processed_frame->tc_pdu_len, ttl_time_mdb_kmc_100, "MDB+KMC Process Security LONG", num_frames_1K, 0); + Crypto_Shutdown(); + free(processed_frame); + free(data_b); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST_MAIN(); \ No newline at end of file diff --git a/util/src_util/pt_gcry_base.c b/util/src_util/pt_gcry_base.c new file mode 100644 index 00000000..dc206e60 --- /dev/null +++ b/util/src_util/pt_gcry_base.c @@ -0,0 +1,206 @@ +/* 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 make use of TC_ApplySecurity/TC_ProcessSecurity function on the data with KMC Crypto Service/MariaDB Functionality Enabled. + * BE SURE TO HAVE APPROPRIATE SA's READY FOR EACH SET OF TESTS + **/ + +#include "utest.h" + +#include +#include +#include + +#include +#include + +#include "crypto.h" +#include "crypto_error.h" + +#include "shared_util.h" + +void random_big_buffer(char* buffer, int32_t buffer_len) +{ + const char* hex_digits = "ABCDEF0123456789"; + for(int i = 0; i < buffer_len; i++) + { + srand(clock()); + int j = (rand() % 16); + //printf("J: %d\n", j); + buffer[i] = hex_digits[j]; + } +} + +UTEST(PERFORMANCE, GCRY_BASE) +{ + int32_t status = Crypto_Init_Unit_Test(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + int32_t big_buffer_len = 1024; + int num_loops = 1000; + int32_t len_data_out = 1024; + + struct timespec begin, end; + double total_time = 0.0; + + for(int i = 0; i < num_loops; i++) + { + char* big_buffer = calloc(1, big_buffer_len * 2 * sizeof(char)); + uint8_t* big_buffer_b = NULL; + int32_t big_buffer_b_len = 0; + //clock_gettime(CLOCK_REALTIME, &begin); + random_big_buffer(big_buffer, (big_buffer_len * 2)); + hex_conversion((char *)big_buffer, (char **)&big_buffer_b, &big_buffer_b_len); + printf("\n"); + for (int i = 0; i < (big_buffer_b_len); i++) + { + printf("%02x", big_buffer_b[i] & 0xff); + } + printf("\nLength: %d\n", big_buffer_b_len); + + gcry_error_t gcry_error = GPG_ERR_NO_ERROR; + gcry_cipher_hd_t tmp_hd; + + char* key_ptr_h = "FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210FEDCBA9876543210"; + char* key_ptr = NULL; + int32_t key_ptr_len = 32; + + hex_conversion(key_ptr_h, &key_ptr, &key_ptr_len); + printf("\nKEY: "); + for (int i = 0; i < key_ptr_len; i++) + { + printf("%02x", (uint8_t)key_ptr[i]); + } + printf("\n"); + + uint8_t* iv = (uint8_t* )calloc(1, 12 * sizeof(uint8_t)); + int32_t iv_len = 16; + printf("IV: "); + for(int i = 0; i < iv_len; i++) + { + printf("%02x ", iv[i]); + } + printf("\n"); + + char* aad_h = "ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF012345"; + uint8_t* aad = NULL; + int32_t aad_len = 30; + hex_conversion(aad_h, (char **) &aad, &aad_len); + printf("\nAAD: "); + for (int i = 0; i < aad_len; i++) + { + printf("%02x", (uint8_t)aad[i]); + } + printf("\n"); + + uint8_t* data_out = calloc(1, len_data_out * sizeof(uint8_t)); + + clock_gettime(CLOCK_REALTIME, &begin); + gcry_error = gcry_cipher_open(&(tmp_hd), GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_GCM, GCRY_CIPHER_NONE); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_open error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + total_time = -1.0; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + } + + gcry_error = gcry_cipher_setkey(tmp_hd, key_ptr, key_ptr_len); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_setkey error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + gcry_cipher_close(tmp_hd); + total_time = -1.0; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + } + + gcry_error = gcry_cipher_setiv(tmp_hd, iv, iv_len); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_setiv error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_LIBGCRYPT_ERROR; + gcry_cipher_close(tmp_hd); + total_time = -1.0; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + } + + 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) + { + printf(KRED "ERROR: gcry_cipher_authenticate error code %d\n" RESET, + gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_AUTHENTICATION_ERROR; + gcry_cipher_close(tmp_hd); + total_time = -1.0; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + } + + + + printf("BIG BUFFER LENGTH: %d\n", big_buffer_len); + gcry_error = gcry_cipher_encrypt(tmp_hd, + data_out, // ciphertext output + len_data_out, // length of data + big_buffer_b, // plaintext input + big_buffer_b_len // in data length + ); + if ((gcry_error & GPG_ERR_CODE_MASK) != GPG_ERR_NO_ERROR) + { + printf(KRED "ERROR: gcry_cipher_encrypt error code %d\n" RESET, gcry_error & GPG_ERR_CODE_MASK); + printf(KRED "Failure: %s/%s\n", gcry_strsource(gcry_error), gcry_strerror(gcry_error)); + status = CRYPTO_LIB_ERR_ENCRYPTION_ERROR; + gcry_cipher_close(tmp_hd); + total_time = -1.0; + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + } + + clock_gettime(CLOCK_REALTIME, &end); + long seconds = end.tv_sec - begin.tv_sec; + long nanoseconds = end.tv_nsec - begin.tv_nsec; + double elapsed = seconds + nanoseconds*1e-9; + + total_time += elapsed; + + printf("Output payload length is %d\n", len_data_out); + printf(KYEL "Printing TC Frame Data after encryption:\n\t"); + for (int j = 0; j < len_data_out; j++) + { + printf("%02x", *(data_out + j)); + } + printf("\n"); + free(big_buffer_b); + free(iv); + free(data_out); + } + printf("Total Frames: %d\n", num_loops); + printf("Bytes per Frame: %d\n", len_data_out); + printf("Total Time: %f\n", total_time); + printf("Mbps: %f\n", (((len_data_out * 8 * num_loops)/total_time)/1024/1024)); + +} + +UTEST_MAIN(); \ No newline at end of file diff --git a/util/src_util/ut_kmc_crypto_cam.c b/util/src_util/ut_kmc_crypto_cam.c new file mode 100644 index 00000000..81097991 --- /dev/null +++ b/util/src_util/ut_kmc_crypto_cam.c @@ -0,0 +1,647 @@ +/* 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 make use of TC_ApplySecurity/TC_ProcessSecurity function on the data with KMC Crypto Service/MariaDB Functionality Enabled. + **/ +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" +#include "utest.h" + +#include "shared_util.h" +#include + +/** + * @brief Unit Test: Nominal Encryption with KMC Crypto Service && JPL Unit Test MariaDB + **/ +UTEST(KMC_CRYPTO_CAM, HAPPY_PATH_APPLY_SEC_ENC_AND_AUTH_KERBEROS_KEYTAB_FILE) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("localhost", "sadb", 3306, CRYPTO_FALSE, 0, NULL, NULL, NULL, NULL, NULL, "sadb_user", + "sadb_password"); + Crypto_Config_Kmc_Crypto_Service("https", "asec-dev-vm18.jpl.nasa.gov", 8443, "crypto-service", + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/ammos-ca-bundle.crt", NULL, + CRYPTO_FALSE, + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-cert.pem", + "PEM", "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-key.pem", + NULL, NULL); +// Crypto_Config_Cam(CAM_ENABLED_TRUE,"/home/isaleh/.cam_cookie_file",NULL,CAM_LOGIN_NONE,"https://asec-dev-vm10.jpl.nasa.gov:443", NULL, NULL); + Crypto_Config_Cam(CAM_ENABLED_TRUE,"/home/isaleh/.cam_cookie_file","/home/isaleh/secret/testuser3300.kt",CAM_LOGIN_KEYTAB_FILE,"https://asec-dev-vm10.jpl.nasa.gov:443", "testuser3300", NULL); +// Crypto_Config_Cam(CAM_ENABLED_TRUE,"/home/isaleh/.cam_cookie_file",NULL,CAM_LOGIN_KERBEROS,"https://asec-dev-vm10.jpl.nasa.gov:443", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 0, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 2, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 3, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + char* raw_tc_jpl_mmt_scid44_vcid1= "202c0408000001bd37"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + printf("Frame before encryption:\n"); + for (int i=0; itc_pdu_len; i++) + { + printf("%02x ", tc_processed_frame->tc_pdu[i]); + } + printf("\n"); + + ASSERT_EQ(0x00,tc_processed_frame->tc_pdu[0]); + ASSERT_EQ( 0x01,tc_processed_frame->tc_pdu[1]); + + Crypto_Shutdown(); + free(enc_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} +/** + * @brief Unit Test: Nominal Encryption with KMC Crypto Service && JPL Unit Test MariaDB + * This doesn't work -- Apply Security Auth Only doesn't return the proper tag. + **/ +UTEST(KMC_CRYPTO_CAM, HAPPY_PATH_PROCESS_SEC_AUTH_ONLY) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("localhost", "sadb", 3306, CRYPTO_FALSE, 0, NULL, NULL, NULL, NULL, NULL, "sadb_user", + "sadb_password"); + Crypto_Config_Kmc_Crypto_Service("https", "asec-dev-vm18.jpl.nasa.gov", 8443, "crypto-service", + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/ammos-ca-bundle.crt", NULL, + CRYPTO_FALSE, + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-cert.pem", + "PEM", "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-key.pem", + NULL, NULL); + Crypto_Config_Cam(CAM_ENABLED_TRUE,"/home/isaleh/.cam_cookie_file",NULL,CAM_LOGIN_NONE,NULL, NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 0, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 1, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 2, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 3, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + char* enc_tc_jpl_mmt_scid44_vcid1= "202C0C2600000400000000000000000000000100016E2051F96CAB186BCE364A65AF599AE52F38"; + char* enc_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int enc_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + + // Data=0001 + // IV=000000000000000000000001 + // AAD=00000000000000000000000000000000000000 + + + TC_t* tc_processed_frame; + tc_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + hex_conversion(enc_tc_jpl_mmt_scid44_vcid1, &enc_tc_jpl_mmt_scid44_vcid1_expect, &enc_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("Encrypted Frame Before Processing:\n"); + for (int i=0; itc_pdu_len; i++) + { + printf("%02x ", tc_processed_frame->tc_pdu[i]); + } + printf("\n"); + + // ASSERT_EQ(0x00,tc_processed_frame->tc_pdu[0]); + // ASSERT_EQ( 0x01,tc_processed_frame->tc_pdu[1]); + + Crypto_Shutdown(); + free(enc_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + // ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(KMC_CRYPTO_CAM, HAPPY_PATH_APPLY_SEC_ENC_AND_AUTH_AESGCM_8BYTE_MAC) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("localhost", "sadb", 3306, CRYPTO_FALSE, 0, NULL, NULL, NULL, NULL, NULL, "sadb_user", + "sadb_password"); + Crypto_Config_Kmc_Crypto_Service("https", "asec-dev-vm18.jpl.nasa.gov", 8443, "crypto-service", + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/ammos-ca-bundle.crt", NULL, + CRYPTO_FALSE, + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-cert.pem", + "PEM", "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-key.pem", + NULL, NULL); + Crypto_Config_Cam(CAM_ENABLED_TRUE,"/home/isaleh/.cam_cookie_file",NULL,CAM_LOGIN_NONE,NULL, NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 11, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + char* raw_tc_jpl_mmt_scid44_vcid1= "202c2c08000001bd37"; + char* raw_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int raw_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + + hex_conversion(raw_tc_jpl_mmt_scid44_vcid1, &raw_tc_jpl_mmt_scid44_vcid1_expect, &raw_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("Frame before encryption:\n"); + for (int i=0; itc_pdu_len; i++) + { + printf("%02x ", tc_processed_frame->tc_pdu[i]); + } + printf("\n"); + + ASSERT_EQ(0x00,tc_processed_frame->tc_pdu[0]); + ASSERT_EQ( 0x01,tc_processed_frame->tc_pdu[1]); + + Crypto_Shutdown(); + free(enc_tc_jpl_mmt_scid44_vcid1_expect); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); +} + +UTEST(KMC_CRYPTO_CAM, UNHAPPY_PATH_INVALID_MAC_PROCESS_SEC_ENC_AND_AUTH_AESGCM_8BYTE_MAC) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("localhost", "sadb", 3306, CRYPTO_FALSE, 0, NULL, NULL, NULL, NULL, NULL, "sadb_user", + "sadb_password"); + Crypto_Config_Kmc_Crypto_Service("https", "asec-dev-vm18.jpl.nasa.gov", 8443, "crypto-service", + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/ammos-ca-bundle.crt", NULL, + CRYPTO_FALSE, + "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-cert.pem", + "PEM", "/home/isaleh/git/KMC/CryptoLib-IbraheemYSaleh/util/etc/local-test-key.pem", + NULL, NULL); + Crypto_Config_Cam(CAM_ENABLED_TRUE,"/home/isaleh/.cam_cookie_file",NULL,CAM_LOGIN_NONE,NULL, NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x002C, 11, TC_HAS_FECF, TC_NO_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + char* enc_tc_jpl_mmt_scid44_vcid1= "202C2C1E000009000000000000000000000001669C5639DCCDEA8C6CE3EEF2"; + char* enc_tc_jpl_mmt_scid44_vcid1_expect = NULL; + int enc_tc_jpl_mmt_scid44_vcid1_expect_len = 0; + + // Data=0001 + // IV=000000000000000000000001 + // AAD=00000000000000000000000000000000000000 + + + TC_t* tc_processed_frame; + tc_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + + hex_conversion(enc_tc_jpl_mmt_scid44_vcid1, &enc_tc_jpl_mmt_scid44_vcid1_expect, &enc_tc_jpl_mmt_scid44_vcid1_expect_len); + + uint8_t* ptr_enc_frame = NULL; + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + printf("Encrypted Frame Before Processing:\n"); + for (int i=0; i +void cleanup_sa(SecurityAssociation_t* test_association) +{ + if (test_association->iv != NULL) + free(test_association->iv); + if (test_association->abm != NULL) + free(test_association->abm); + if (test_association->arsn != NULL) + free(test_association->arsn); + if (test_association->ek_ref != NULL) + free(test_association->ek_ref); + if (test_association->ecs != NULL) + free(test_association->ecs); + if (test_association->acs != NULL) + free(test_association->acs); + if (test_association->ak_ref != NULL) + free(test_association->ak_ref); + + free(test_association); +} + void reload_db(void) { printf("Resetting Database\n"); @@ -74,8 +94,7 @@ UTEST(MARIA_DB, DB_CONNECT) SadbRoutine sadb_routine = get_sadb_routine_mariadb(); //need the sa call - SecurityAssociation_t* test_sa = NULL; - test_sa = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_sa; status = sadb_routine->sadb_get_sa_from_spi(1, &test_sa); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); @@ -84,11 +103,11 @@ UTEST(MARIA_DB, DB_CONNECT) test_sa->iv[11] = 0xAB; status = sadb_routine->sadb_save_sa(test_sa); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); - status = sadb_routine->sadb_get_sa_from_spi(1, &test_sa); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); ASSERT_EQ(test_sa->iv[11] , 0xAB); - Crypto_Shutdown(); + Crypto_Shutdown(); + cleanup_sa(test_sa); } /** @@ -123,21 +142,21 @@ UTEST(MARIA_DB, HAPPY_PATH_ENC) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; status = sadb_routine->sadb_get_sa_from_spi(2, &test_association); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); 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_SUCCESS, return_val); - + cleanup_sa(test_association); status = sadb_routine->sadb_get_sa_from_spi(2, &test_association); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); ASSERT_EQ(test_association->iv[test_association->iv_len - 1], 2); // Verify that IV incremented. free(raw_tc_sdls_ping_b); - free(ptr_enc_frame); - free(test_association); + free(ptr_enc_frame); + cleanup_sa(test_association); Crypto_Shutdown(); } @@ -173,7 +192,7 @@ UTEST(MARIA_DB, HAPPY_PATH_AUTH_ENC) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Expose the SADB Security Association for test edits. sadb_routine->sadb_get_sa_from_spi(3, &test_association); @@ -181,12 +200,14 @@ UTEST(MARIA_DB, HAPPY_PATH_AUTH_ENC) 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_SUCCESS, return_val); - + + cleanup_sa(test_association); status = sadb_routine->sadb_get_sa_from_spi(3, &test_association); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); ASSERT_EQ(test_association->iv[test_association->iv_len - 1], 2); // Verify that IV incremented. - Crypto_Shutdown(); + Crypto_Shutdown(); + cleanup_sa(test_association); free(raw_tc_sdls_ping_b); free(ptr_enc_frame); } @@ -230,7 +251,7 @@ UTEST(MARIA_DB, AUTH_DECRYPTION_TEST) tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(3, &test_association); test_association->iv[test_association->iv_len - 1] = 0; sadb_routine->sadb_save_sa(test_association); @@ -242,6 +263,13 @@ UTEST(MARIA_DB, AUTH_DECRYPTION_TEST) } Crypto_Shutdown(); + free(dec_test_b); + free(enc_test_b); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_sdls_processed_frame); } /** @@ -287,20 +315,30 @@ UTEST(MARIA_DB, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(4, &test_association); return_val = - Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + cleanup_sa(test_association); sadb_routine->sadb_get_sa_from_spi(4, &test_association); for (int i = 0; i < test_association->iv_len; i++) { @@ -309,6 +347,9 @@ UTEST(MARIA_DB, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) } Crypto_Shutdown(); + cleanup_sa(test_association); + free(expected_iv_b); + free(new_iv_b); free(raw_tc_sdls_ping_b); free(ptr_enc_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); @@ -357,20 +398,30 @@ UTEST(MARIA_DB, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(4, &test_association); + memcpy(test_association->iv, new_iv_b, new_iv_len); return_val = - Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = - Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); - + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + cleanup_sa(test_association); sadb_routine->sadb_get_sa_from_spi(4, &test_association); for (int i = 0; i < test_association->iv_len; i++) { @@ -379,6 +430,9 @@ UTEST(MARIA_DB, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) } Crypto_Shutdown(); + cleanup_sa(test_association); + free(expected_iv_b); + free(new_iv_b); free(raw_tc_sdls_ping_b); free(ptr_enc_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); @@ -429,25 +483,43 @@ UTEST(MARIA_DB, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Expose the SADB Security Association for test edits. sadb_routine->sadb_get_sa_from_spi(5, &test_association); 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; + cleanup_sa(test_association); + sadb_routine->sadb_get_sa_from_spi(5, &test_association); 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; + cleanup_sa(test_association); + sadb_routine->sadb_get_sa_from_spi(5, &test_association); 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; + cleanup_sa(test_association); + sadb_routine->sadb_get_sa_from_spi(5, &test_association); 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; + cleanup_sa(test_association); + sadb_routine->sadb_get_sa_from_spi(5, &test_association); 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_SUCCESS,return_val); + + cleanup_sa(test_association); printf("Expected ARSN:\n"); Crypto_hexprint(expected_arsn_b,expected_arsn_len); @@ -462,6 +534,10 @@ UTEST(MARIA_DB, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) } //Must shutdown after checking test_association ARSN since that will get freed! + + cleanup_sa(test_association); + free(expected_arsn_b); + free(new_arsn_b); Crypto_Shutdown(); free(raw_tc_sdls_ping_b); free(ptr_enc_frame); diff --git a/util/src_util/ut_tc_apply.c b/util/src_util/ut_tc_apply.c index 370a079b..dc66c29d 100644 --- a/util/src_util/ut_tc_apply.c +++ b/util/src_util/ut_tc_apply.c @@ -49,6 +49,9 @@ UTEST(TC_APPLY_SECURITY, NO_CRYPTO_INIT) 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); + + char* error_enum = Crypto_Get_Error_Code_Enum_String(return_val); + ASSERT_STREQ("CRYPTO_LIB_ERR_NO_INIT",error_enum); free(raw_tc_sdls_ping_b); Crypto_Shutdown(); } @@ -71,6 +74,10 @@ UTEST(TC_APPLY_SECURITY, NO_CONFIG) 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); + + char* error_enum = Crypto_Get_Error_Code_Enum_String(return_val); + ASSERT_STREQ("CRYPTO_LIB_ERR_NO_CONFIG",error_enum); + free(raw_tc_sdls_ping_b); Crypto_Shutdown(); } @@ -121,7 +128,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Expose the SADB Security Association for test edits. sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; @@ -138,6 +145,43 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC) ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); } +/** + * @brief Unit Test: Nominal Encryption CBC + **/ +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC_CBC) +{ + // Setup & Initialize CryptoLib + Crypto_Init_Unit_Test(); + char* raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + int32_t return_val = CRYPTO_LIB_ERROR; + + SecurityAssociation_t* test_association; + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + 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_SUCCESS, return_val); +} /** * @brief Unit Test: Nominal Authorized Encryption **/ @@ -157,7 +201,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_AUTH_ENC) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Expose the SADB Security Association for test edits. sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; @@ -204,7 +248,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_IV_ROLLOVE int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Expose the SADB Security Association for test edits. sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; @@ -216,13 +260,21 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_IV_ROLLOVE memcpy(test_association->iv + (test_association->iv_len - test_association->shivf_len), new_iv_b, new_iv_len); return_val = - Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); for (int i = 0; i < test_association->iv_len; i++) @@ -234,6 +286,8 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_IV_ROLLOVE Crypto_Shutdown(); free(raw_tc_sdls_ping_b); free(ptr_enc_frame); + free(new_iv_b); + free(expected_iv_b); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); } @@ -243,10 +297,12 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_IV_ROLLOVE UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); 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, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); char* raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; char* raw_tc_sdls_ping_b = NULL; @@ -270,7 +326,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Expose the SADB Security Association for test edits. sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; @@ -282,13 +338,21 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) memcpy(test_association->iv + (test_association->iv_len - test_association->shivf_len), new_iv_b, new_iv_len); return_val = - Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + free(ptr_enc_frame); + ptr_enc_frame = NULL; return_val = Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); for (int i = 0; i < test_association->iv_len; i++) @@ -300,6 +364,8 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) Crypto_Shutdown(); free(raw_tc_sdls_ping_b); free(ptr_enc_frame); + free(new_iv_b); + free(expected_iv_b); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); } @@ -309,7 +375,11 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_STATIC_IV_ROLLOVER) UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); + 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, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); char* raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; char* raw_tc_sdls_ping_b = NULL; @@ -333,7 +403,7 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLO int32_t return_val = CRYPTO_LIB_ERROR; - SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Expose the SADB Security Association for test edits. sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; @@ -344,9 +414,11 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLO test_association->est=0; test_association->ast=1; test_association->ecs_len=1; + free(test_association->ecs); test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_NONE; test_association->acs_len=1; + free(test_association->acs); test_association->acs = calloc(1, test_association->acs_len * sizeof(uint8_t)); *test_association->acs = CRYPTO_MAC_CMAC_AES256; test_association->arsn_len = 3; @@ -359,15 +431,23 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLO 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; 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_SUCCESS,return_val); + free(ptr_enc_frame); + ptr_enc_frame = NULL; 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_SUCCESS,return_val); @@ -382,11 +462,12 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLO printf("[%d] Truth: %02x, Actual: %02x\n", i, expected_arsn_b[i], *(test_association->arsn + i)); ASSERT_EQ(expected_arsn_b[i], *(test_association->arsn + i)); } - //Must shutdown after checking test_association ARSN since that will get freed! Crypto_Shutdown(); free(raw_tc_sdls_ping_b); free(ptr_enc_frame); + free(new_arsn_b); + free(expected_arsn_b); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); } @@ -398,7 +479,12 @@ UTEST(TC_APPLY_SECURITY, HAPPY_PATH_APPLY_NONTRANSMITTED_INCREMENTING_ARSN_ROLLO UTEST(TC_APPLY_SECURITY, BAD_SPACE_CRAFT_ID) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); + 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, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); char* raw_tc_sdls_ping_bad_scid_h = "20010015000080d2c70008197f0b00310000b1fe3128"; char* raw_tc_sdls_ping_bad_scid_b = NULL; int raw_tc_sdls_ping_bad_scid_len = 0; @@ -424,7 +510,12 @@ UTEST(TC_APPLY_SECURITY, BAD_SPACE_CRAFT_ID) UTEST(TC_APPLY_SECURITY, BAD_VIRTUAL_CHANNEL_ID) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); + 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, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); char* raw_tc_sdls_ping_bad_vcid_h = "20032015000080d2c70008197f0b00310000b1fe3128"; char* raw_tc_sdls_ping_bad_vcid_b = NULL; int raw_tc_sdls_ping_bad_vcid_len = 0; @@ -451,7 +542,12 @@ UTEST(TC_APPLY_SECURITY, BAD_VIRTUAL_CHANNEL_ID) UTEST(TC_APPLY_SECURITY, NULL_BUFFER) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); + 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, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); long buffer_size = 0; uint8_t* buffer = NULL; uint16_t buffer_size_i = (uint16_t)buffer_size; @@ -493,8 +589,7 @@ UTEST(TC_APPLY_SECURITY, INVALID_FRAME_SIZE) int test_frame_pt_len, long_frame_pt_len = 0; // Expose/setup SAs for testing - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->arsn_len = 0; test_association->shsnf_len = 0; @@ -517,6 +612,9 @@ UTEST(TC_APPLY_SECURITY, INVALID_FRAME_SIZE) hex_conversion(long_frame_pt_h, (char**) &long_frame_pt_b, &long_frame_pt_len); // Should fail, as frame length violates the spec max status = Crypto_TC_ApplySecurity(long_frame_pt_b, long_frame_pt_len, &ptr_enc_frame, &enc_frame_len); + free(test_frame_pt_b); + free(long_frame_pt_b); + Crypto_Shutdown(); ASSERT_EQ(CRYPTO_LIB_ERR_TC_FRAME_SIZE_EXCEEDS_SPEC_LIMIT, status); } @@ -539,8 +637,7 @@ UTEST(TC_APPLY_SECURITY, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPEC) int test_frame_pt_len = 0; // Expose/setup SAs for testing - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->arsn_len = 0; test_association->shsnf_len = 0; @@ -550,7 +647,7 @@ UTEST(TC_APPLY_SECURITY, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPEC) // Should fail, as frame length violates the managed parameter status = Crypto_TC_ApplySecurity(test_frame_pt_b, test_frame_pt_len, &ptr_enc_frame, &enc_frame_len); ASSERT_EQ(CRYPTO_LIB_ERR_INPUT_FRAME_TOO_SHORT_FOR_TC_STANDARD, status); - + free(test_frame_pt_b); Crypto_Shutdown(); } @@ -573,8 +670,7 @@ UTEST(TC_APPLY_SECURITY, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPECIFIED_FRAME_LENG int test_frame_pt_len = 0; // Expose/setup SAs for testing - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->arsn_len = 0; test_association->shsnf_len = 0; @@ -584,8 +680,223 @@ UTEST(TC_APPLY_SECURITY, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPECIFIED_FRAME_LENG // Should fail, as frame length violates the managed parameter status = Crypto_TC_ApplySecurity(test_frame_pt_b, test_frame_pt_len, &ptr_enc_frame, &enc_frame_len); ASSERT_EQ(CRYPTO_LIB_ERR_INPUT_FRAME_LENGTH_SHORTER_THAN_FRAME_HEADERS_LENGTH, status); + free(test_frame_pt_b); + Crypto_Shutdown(); +} + +/** + * @brief Unit Test: Encryption CBC 1 Byte of padding + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_1BP) +{ + // 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030016000080d2c70008197f0b0031000000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association; + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003002A0000000B00000000000000000000000000000000011C1741A95DE7EF6FCF2B20B6F09E9FD29988"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + //printf("\n"); + + Crypto_Shutdown(); + free(truth_data_b); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC 16 Bytes of padding + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_16BP) +{ + // 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030017000080d2c70008197f0b003100000000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association; + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003003A0000000B00000000000000000000000000000000103970EAE4C05ACD1B0C348FDA174DF73EF0E2D603996C4B78B992CD60918729D3A47A"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + printf("\n"); Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + free(truth_data_b); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); } +/** + * @brief Unit Test: Nominal Encryption CBC + * Frame is max size for this test. Any encrypted data of length greater than 1007 bytes, + * will cause frame length exception. + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_FRAME_MAX) +{ + // 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "200303E3000080d2c70008197f0b003100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b1fed255"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association; + // 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(11, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + 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_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC + * Frame is 1 byte too large for this test. Any encrypted data of length greater than 1007 bytes, + * will cause frame length exception. + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_FRAME_TOO_BIG) +{ + // 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "200303F7000080d2c70008197f0b0031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b1fed255"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association; + // 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(11, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + 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_TC_FRAME_SIZE_EXCEEDS_SPEC_LIMIT, return_val); +} + + UTEST_MAIN(); diff --git a/util/src_util/ut_tc_kmc.c b/util/src_util/ut_tc_kmc.c new file mode 100644 index 00000000..8982f661 --- /dev/null +++ b/util/src_util/ut_tc_kmc.c @@ -0,0 +1,915 @@ +/* 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 Functionality with KMC Service. + **/ + +#include "ut_tc_apply.h" +#include "ut_tc_process.h" +#include "crypto.h" +#include "crypto_error.h" +#include "sadb_routine.h" +#include "utest.h" + +/** + * @brief Unit Test: Nominal Encryption CBC KMC + **/ +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC_CBC_KMC) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003001A0000000B025364F9BC3344AF359DA06CA886746F591C8E"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + //printf("\n"); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC KMC 1 Byte of padding + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_KMC_1BP) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030016000080d2c70008197f0b0031000000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003001A0000000B011C1741A95DE7EF6FCF2B20B6F09E9FD225AD"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + //printf("\n"); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC KMC 16 Bytes of padding + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_KMC_16BP) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030017000080d2c70008197f0b003100000000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003002A0000000B103970EAE4C05ACD1B0C348FDA174DF73EF0E2D603996C4B78B992CD60918729D34FE2"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + printf("\n"); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Nominal Encryption CBC KMC + * Frame is max size for this test. Any encrypted data of length greater than 1007 bytes, + * will cause frame length exception. + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_KMC_FRAME_MAX) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "200303F6000080d2c70008197f0b00310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b1fed255"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + 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_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC KMC + * Frame is 1 byte too large for this test. Any encrypted data of length greater than 1007 bytes, + * will cause frame length exception. + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_KMC_FRAME_TOO_BIG) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "200303F7000080d2c70008197f0b0031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b1fed255"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + 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_TC_FRAME_SIZE_EXCEEDS_SPEC_LIMIT, return_val); +} + +//********************************* Encryption Tests MDB + KMC *******************************************************************// +/** + * @brief Unit Test: Nominal Encryption CBC MDB KMC + **/ +UTEST(TC_APPLY_SECURITY, HAPPY_PATH_ENC_CBC_MDB_KMC) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030015000080d2c70008197f0b00310000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003001A0000000B025364F9BC3344AF359DA06CA886746F591C8E"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + //printf("\n"); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC MDB KMC 1 Byte of padding + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_MDB_KMC_1BP) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030016000080d2c70008197f0b0031000000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003001A0000000B011C1741A95DE7EF6FCF2B20B6F09E9FD225AD"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + //printf("\n"); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC MDB KMC 16 Bytes of padding + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_MDB_KMC_16BP) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "20030017000080d2c70008197f0b003100000000b1fe3128"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + printf("SPI: %d\n", test_association->spi); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + return_val = + Crypto_TC_ApplySecurity((uint8_t* )raw_tc_sdls_ping_b, raw_tc_sdls_ping_len, &ptr_enc_frame, &enc_frame_len); + + char* truth_data_h = "2003002A0000000B103970EAE4C05ACD1B0C348FDA174DF73EF0E2D603996C4B78B992CD60918729D34FE2"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char **)&truth_data_b, &truth_data_l); + //printf("Encrypted Frame:\n"); + for(int i = 0; i < enc_frame_len; i++) + { + //printf("%02x -> %02x ", ptr_enc_frame[i], truth_data_b[i]); + ASSERT_EQ(ptr_enc_frame[i], truth_data_b[i]); + } + printf("\n"); + + Crypto_Shutdown(); + free(raw_tc_sdls_ping_b); + free(ptr_enc_frame); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Nominal Encryption CBC MDB KMC + * Frame is max size for this test. Any encrypted data of length greater than 1007 bytes, + * will cause frame length exception. + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_MDB_KMC_FRAME_MAX) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "200303F6000080d2c70008197f0b00310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b1fed255"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + 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_SUCCESS, return_val); +} + +/** + * @brief Unit Test: Encryption CBC MDB KMC + * Frame is 1 byte too large for this test. Any encrypted data of length greater than 1007 bytes, + * will cause frame length exception. + **/ +UTEST(TC_APPLY_SECURITY, ENC_CBC_MDB_KMC_FRAME_TOO_BIG) +{ + // Setup & Initialize CryptoLib + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_TRUE, TC_HAS_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t return_val = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); + + char* raw_tc_sdls_ping_h = "200303F7000080d2c70008197f0b0031000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b1fed255"; + char* 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_t* ptr_enc_frame = NULL; + uint16_t enc_frame_len = 0; + + SecurityAssociation_t* test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + // 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(11, &test_association); + test_association->sa_state = SA_OPERATIONAL; + test_association->ast = 0; + test_association->arsn_len = 0; + sadb_routine->sadb_get_sa_from_spi(11, &test_association); + 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_TC_FRAME_SIZE_EXCEEDS_SPEC_LIMIT, return_val); +} + + +//******************************************************* Decryption Tests *******************************************************// + +/** + * @brief Unit Test: Nominal Decryption CBC KMC + **/ +UTEST(TC_PROCESS, HAPPY_PATH_DECRYPT_CBC_KMC) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003001A0000000B025364F9BC3344AF359DA06CA886746F591C8E"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b00310000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + Crypto_Shutdown(); +} + +/** + * @brief Unit Test: Decryption CBC KMC with 1 Byte of padding + **/ +UTEST(TC_PROCESS, DECRYPT_CBC_KMC_1B) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003001A0000000B011C1741A95DE7EF6FCF2B20B6F09E9FD225AD"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b0031000000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + Crypto_Shutdown(); +} + +/** + * @brief Unit Test: Decryption CBC KMC with 16 Bytes of padding + **/ +UTEST(TC_PROCESS, DECRYPT_CBC_KMC_16B) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003002A0000000B103970EAE4C05ACD1B0C348FDA174DF73EF0E2D603996C4B78B992CD60918729D34FE2"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b003100000000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + Crypto_Shutdown(); +} + +// *************************************** Decryption + MDB ***********************************************// +/** + * @brief Unit Test: Nominal Decryption CBC MDB KMC + **/ +UTEST(TC_PROCESS, HAPPY_PATH_DECRYPT_CBC_MDB_KMC) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003001A0000000B025364F9BC3344AF359DA06CA886746F591C8E"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b00310000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + Crypto_Shutdown(); +} + +/** + * @brief Unit Test: Decryption CBC MDB KMC with 1 Byte of padding + **/ +UTEST(TC_PROCESS, DECRYPT_CBC_MDB_KMC_1B) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003001A0000000B011C1741A95DE7EF6FCF2B20B6F09E9FD225AD"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b0031000000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + Crypto_Shutdown(); +} + +/** + * @brief Unit Test: Decryption CBC MDB KMC with 16 Bytes of padding + **/ +UTEST(TC_PROCESS, DECRYPT_CBC_MDB_KMC_16B) +{ + Crypto_Config_CryptoLib(SADB_TYPE_MARIADB, CRYPTOGRAPHY_TYPE_KMCCRYPTO, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_MariaDB("client-demo-kmc.example.com","sadb", 3306,CRYPTO_TRUE,CRYPTO_TRUE, "/home/itc/Desktop/CERTS/ammos-ca-bundle.crt", NULL, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "/home/itc/Desktop/CERTS/ammos-client-key.pem",NULL,"robert", NULL); + Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003002A0000000B103970EAE4C05ACD1B0C348FDA174DF73EF0E2D603996C4B78B992CD60918729D34FE2"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association = NULL; + test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b003100000000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + Crypto_Shutdown(); +} + +UTEST_MAIN(); \ No newline at end of file diff --git a/util/src_util/ut_tc_process.c b/util/src_util/ut_tc_process.c index fba4e541..214b744a 100644 --- a/util/src_util/ut_tc_process.c +++ b/util/src_util/ut_tc_process.c @@ -60,8 +60,7 @@ UTEST(TC_PROCESS, EXERCISE_IV) tc_nist_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); // Expose/setup SAs for testing - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Deactivate SA 1 sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; @@ -69,6 +68,8 @@ UTEST(TC_PROCESS, EXERCISE_IV) sadb_routine->sadb_get_sa_from_spi(9, &test_association); test_association->sa_state = SA_OPERATIONAL; sadb_routine->sadb_get_sa_from_spi(9, &test_association); + test_association->ecs_len = 1; + free(test_association->ecs); test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; // Insert key into keyring of SA 9 @@ -88,16 +89,28 @@ UTEST(TC_PROCESS, EXERCISE_IV) printf(KGRN "Checking replay - using previous received IV...\n" RESET); status = Crypto_TC_ProcessSecurity(buffer_replay_b, &buffer_replay_len, tc_nist_processed_frame); ASSERT_EQ(CRYPTO_LIB_ERR_IV_OUTSIDE_WINDOW, status); + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // Expect to fail on counter being too high printf(KGRN "Checking replay - using IV outside the window...\n" RESET); status = Crypto_TC_ProcessSecurity(buffer_outside_window_b, &buffer_outside_window_len, tc_nist_processed_frame); ASSERT_EQ(CRYPTO_LIB_ERR_IV_OUTSIDE_WINDOW, status); + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // Expect success on valid IV printf(KGRN "Checking valid IV... should be able to receive it... \n" RESET); status = Crypto_TC_ProcessSecurity(buffer_good_iv_b, &buffer_good_iv_len, tc_nist_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // Expect success on valid IV within window, but has a gap printf(KGRN "Checking valid IV within window... should be able to receive it... \n" RESET); @@ -115,9 +128,18 @@ UTEST(TC_PROCESS, EXERCISE_IV) } printf("\n"); Crypto_Shutdown(); + free(buffer_replay_b); + free(buffer_outside_window_b); + free(buffer_good_iv_b); + free(buffer_good_iv_with_gap_b); free(ptr_enc_frame); free(buffer_nist_iv_b); free(buffer_nist_key_b); + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_nist_processed_frame); } /** @@ -155,8 +177,7 @@ UTEST(TC_PROCESS, EXERCISE_ARSN) tc_nist_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); // Expose/setup SAs for testing - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; // Deactivate SA 1 sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->sa_state = SA_NONE; @@ -164,8 +185,13 @@ UTEST(TC_PROCESS, EXERCISE_ARSN) sadb_routine->sadb_get_sa_from_spi(9, &test_association); test_association->sa_state = SA_OPERATIONAL; sadb_routine->sadb_get_sa_from_spi(9, &test_association); + free(test_association->ecs); + test_association->ecs_len = 1; + free(test_association->ecs); test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_NONE; + free(test_association->acs); + test_association->acs_len = 1; test_association->acs = calloc(1, test_association->acs_len * sizeof(uint8_t)); *test_association->acs = CRYPTO_MAC_CMAC_AES256; test_association->est = 0; @@ -179,6 +205,7 @@ UTEST(TC_PROCESS, EXERCISE_ARSN) test_association->akid = 136; test_association->ekid = 0; // memset(test_association->abm, 0x00, (test_association->abm_len * sizeof(uint8_t))); + free(test_association->abm); test_association->abm = calloc(1, test_association->abm_len * sizeof(uint8_t)); test_association->stmacf_len = 16; // Insert key into keyring of SA 9 @@ -191,22 +218,34 @@ UTEST(TC_PROCESS, EXERCISE_ARSN) hex_conversion(buffer_good_arsn_with_gap_h, (char**) &buffer_good_arsn_with_gap_b, &buffer_good_arsn_with_gap_len); // Convert/Set input ARSN hex_conversion(buffer_arsn_h, (char**) &buffer_arsn_b, &buffer_arsn_len); + free(test_association->arsn); test_association->arsn = calloc(1, test_association->arsn_len); memcpy(test_association->arsn, buffer_arsn_b, buffer_arsn_len); // Expect to fail on replay printf(KGRN "Checking replay - using previous received ARSN...\n" RESET); status = Crypto_TC_ProcessSecurity(buffer_replay_b, &buffer_replay_len, tc_nist_processed_frame); ASSERT_EQ(CRYPTO_LIB_ERR_ARSN_OUTSIDE_WINDOW, status); - + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // Expect to fail on counter being too high printf(KGRN "Checking replay - using ARSN outside the window...\n" RESET); status = Crypto_TC_ProcessSecurity(buffer_outside_window_b, &buffer_outside_window_len, tc_nist_processed_frame); ASSERT_EQ(CRYPTO_LIB_ERR_ARSN_OUTSIDE_WINDOW, status); + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // Expect success on valid ARSN printf(KGRN "Checking next valid ARSN... should be able to receive it... \n" RESET); status = Crypto_TC_ProcessSecurity(buffer_good_arsn_b, &buffer_good_arsn_len, tc_nist_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // Expect success on valid ARSN within window, but has a gap printf(KGRN "Checking valid ARSN within window... should be able to receive it... \n" RESET); @@ -224,18 +263,29 @@ UTEST(TC_PROCESS, EXERCISE_ARSN) } printf("\n"); Crypto_Shutdown(); + free(tc_nist_processed_frame->tc_sec_header.iv); + free(tc_nist_processed_frame->tc_sec_header.sn); + free(tc_nist_processed_frame->tc_sec_header.pad); + free(tc_nist_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_nist_processed_frame); free(ptr_enc_frame); free(buffer_nist_key_b); + free(buffer_replay_b); + free(buffer_outside_window_b); + free(buffer_good_arsn_b); + free(buffer_good_arsn_with_gap_b); + free(buffer_arsn_b); } UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_STATIC_IV_ROLLOVER) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); 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, SA_INCREMENT_NONTRANSMITTED_IV_FALSE); - + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); SadbRoutine sadb_routine = get_sadb_routine_inmemory(); char* dec_test_fe_h = @@ -252,8 +302,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_STATIC_IV_ROLLOVER) hex_conversion(dec_test_ff_h, (char**) &dec_test_ff_b, &dec_test_ff_len); hex_conversion(dec_test_00_h, (char**) &dec_test_00_b, &dec_test_00_len); - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; int32_t return_val = -1; @@ -264,6 +313,8 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_STATIC_IV_ROLLOVER) // Default SA // Expose SA 1 for testing sadb_routine->sadb_get_sa_from_spi(1, &test_association); + //free(test_association->ecs); + test_association->ecs_len = 1; test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_NONE; @@ -276,6 +327,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_STATIC_IV_ROLLOVER) test_association->gvcid_tc_blk.vcid = 0; test_association->shivf_len = 6; test_association->iv_len = 12; + free(test_association->iv); test_association->iv = calloc(1, test_association->iv_len * sizeof(uint8_t)); // IV = "000000000000FFFFFFFFFFFE" test_association->iv[0] = 0x00; @@ -293,6 +345,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_STATIC_IV_ROLLOVER) test_association->ast = 1; test_association->est = 1; test_association->sa_state = SA_OPERATIONAL; + free(test_association->ecs); test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; @@ -300,9 +353,17 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_STATIC_IV_ROLLOVER) return_val = Crypto_TC_ProcessSecurity(dec_test_fe_b, &dec_test_fe_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); ASSERT_EQ(test_association->iv[11],0xFE); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? return_val = Crypto_TC_ProcessSecurity(dec_test_ff_b, &dec_test_ff_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); ASSERT_EQ(test_association->iv[11],0xFF); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? return_val = Crypto_TC_ProcessSecurity(dec_test_00_b, &dec_test_00_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); for(int i=0; i < test_association->iv_len; i++) @@ -317,19 +378,24 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_STATIC_IV_ROLLOVER) free(dec_test_fe_b); free(dec_test_ff_b); free(dec_test_00_b); - // free(test_association->ecs); + + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? free(tc_sdls_processed_frame); - // sadb_routine->sadb_close(); + } UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); 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, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); - + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); SadbRoutine sadb_routine = get_sadb_routine_inmemory(); char* dec_test_fe_h = @@ -346,8 +412,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) hex_conversion(dec_test_ff_h, (char**) &dec_test_ff_b, &dec_test_ff_len); hex_conversion(dec_test_00_h, (char**) &dec_test_00_b, &dec_test_00_len); - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; int32_t return_val = -1; @@ -358,6 +423,8 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) // Default SA // Expose SA 1 for testing sadb_routine->sadb_get_sa_from_spi(1, &test_association); + free(test_association->ecs); + test_association->ecs_len = 1; test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_NONE; @@ -370,6 +437,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) test_association->gvcid_tc_blk.vcid = 0; test_association->shivf_len = 6; test_association->iv_len = 12; + free(test_association->iv); test_association->iv = calloc(1, test_association->iv_len * sizeof(uint8_t)); // IV = "000000000000FFFFFFFFFFFE" test_association->iv[0] = 0x00; @@ -387,6 +455,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) test_association->ast = 1; test_association->est = 1; test_association->sa_state = SA_OPERATIONAL; + free(test_association->ecs); test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_AES256_GCM; @@ -394,9 +463,17 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) return_val = Crypto_TC_ProcessSecurity(dec_test_fe_b, &dec_test_fe_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); ASSERT_EQ(test_association->iv[11],0xFE); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? return_val = Crypto_TC_ProcessSecurity(dec_test_ff_b, &dec_test_ff_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); ASSERT_EQ(test_association->iv[11],0xFF); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // test_association->iv[5] = 0x01; return_val = Crypto_TC_ProcessSecurity(dec_test_00_b, &dec_test_00_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); @@ -420,18 +497,22 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_IV_ROLLOVER) free(dec_test_fe_b); free(dec_test_ff_b); free(dec_test_00_b); - // free(test_association->ecs); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? free(tc_sdls_processed_frame); - // sadb_routine->sadb_close(); } UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) { // Setup & Initialize CryptoLib - Crypto_Init_Unit_Test(); 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_FALSE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Init(); SadbRoutine sadb_routine = get_sadb_routine_inmemory(); @@ -449,8 +530,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) hex_conversion(dec_test_ff_h, (char**) &dec_test_ff_b, &dec_test_ff_len); hex_conversion(dec_test_00_h, (char**) &dec_test_00_b, &dec_test_00_len); - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; int32_t return_val = -1; @@ -461,6 +541,7 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) // Default SA // Expose SA 1 for testing sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->ecs_len = 1; test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_NONE; @@ -475,13 +556,16 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) test_association->est=0; test_association->ast=1; test_association->ecs_len=1; + free(test_association->ecs); test_association->ecs = calloc(1, test_association->ecs_len * sizeof(uint8_t)); *test_association->ecs = CRYPTO_CIPHER_NONE; test_association->acs_len=1; + free(test_association->acs); test_association->acs = calloc(1, test_association->acs_len * sizeof(uint8_t)); *test_association->acs = CRYPTO_MAC_CMAC_AES256; test_association->arsn_len = 3; test_association->shsnf_len = 2; + free(test_association->arsn); test_association->arsn = calloc(1,test_association->arsn_len); // ARSN = "05FFFD" test_association->arsn[0] = 0x05; @@ -495,9 +579,17 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) return_val = Crypto_TC_ProcessSecurity(dec_test_fe_b, &dec_test_fe_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); ASSERT_EQ(test_association->arsn[2],0xFE); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? return_val = Crypto_TC_ProcessSecurity(dec_test_ff_b, &dec_test_ff_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); ASSERT_EQ(test_association->arsn[2],0xFF); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? // test_association->iv[5] = 0x01; return_val = Crypto_TC_ProcessSecurity(dec_test_00_b, &dec_test_00_len, tc_sdls_processed_frame); ASSERT_EQ(CRYPTO_LIB_SUCCESS, return_val); @@ -512,9 +604,11 @@ UTEST(TC_PROCESS, HAPPY_PATH_PROCESS_NONTRANSMITTED_INCREMENTING_ARSN_ROLLOVER) free(dec_test_fe_b); free(dec_test_ff_b); free(dec_test_00_b); - // free(test_association->ecs); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? free(tc_sdls_processed_frame); - // sadb_routine->sadb_close(); } UTEST(TC_PROCESS, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPEC) @@ -538,8 +632,7 @@ UTEST(TC_PROCESS, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPEC) int test_frame_pt_len = 0; // Expose/setup SAs for testing - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->arsn_len = 0; test_association->shsnf_len = 0; @@ -551,6 +644,12 @@ UTEST(TC_PROCESS, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPEC) ASSERT_EQ(CRYPTO_LIB_ERR_INPUT_FRAME_TOO_SHORT_FOR_TC_STANDARD, status); Crypto_Shutdown(); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_sdls_processed_frame); + free(test_frame_pt_b); } UTEST(TC_PROCESS, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPECIFIED_FRAME_LENGTH_HEADER) @@ -573,8 +672,7 @@ UTEST(TC_PROCESS, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPECIFIED_FRAME_LENGTH_HEAD int test_frame_pt_len = 0; // Expose/setup SAs for testing - SecurityAssociation_t* test_association = NULL; - test_association = malloc(sizeof(SecurityAssociation_t) * sizeof(uint8_t)); + SecurityAssociation_t* test_association; sadb_routine->sadb_get_sa_from_spi(1, &test_association); test_association->arsn_len = 0; test_association->shsnf_len = 0; @@ -586,6 +684,188 @@ UTEST(TC_PROCESS, ERROR_TC_INPUT_FRAME_TOO_SHORT_FOR_SPECIFIED_FRAME_LENGTH_HEAD ASSERT_EQ(CRYPTO_LIB_ERR_INPUT_FRAME_LENGTH_SHORTER_THAN_FRAME_HEADERS_LENGTH, status); Crypto_Shutdown(); + free(test_frame_pt_b); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_sdls_processed_frame); +} + +UTEST(TC_PROCESS, HAPPY_PATH_DECRYPT_CBC) +{ + int32_t status = CRYPTO_LIB_SUCCESS; + 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_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + + status = Crypto_Init(); + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003002A0000000B00000000000000000000000000000000025364F9BC3344AF359DA06CA886746F59A0AB"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b00310000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_sdls_processed_frame); + free(test_frame_pt_b); + free(truth_data_b); + Crypto_Shutdown(); + +} + +/** + * @brief Unit Test: Decryption CBC with 1 Byte of padding + **/ +UTEST(TC_PROCESS, DECRYPT_CBC_1B) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); + + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003002A0000000B00000000000000000000000000000000011C1741A95DE7EF6FCF2B20B6F09E9FD29988"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b0031000000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_sdls_processed_frame); + free(truth_data_b); + Crypto_Shutdown(); +} + +/** + * @brief Unit Test: Decryption CBC with 16 Bytes of padding + **/ +UTEST(TC_PROCESS, DECRYPT_CBC_16B) +{ + Crypto_Config_CryptoLib(SADB_TYPE_INMEMORY, CRYPTOGRAPHY_TYPE_LIBGCRYPT, CRYPTO_TC_CREATE_FECF_TRUE, TC_PROCESS_SDLS_PDUS_FALSE, TC_NO_PUS_HDR, + TC_IGNORE_SA_STATE_FALSE, TC_IGNORE_ANTI_REPLAY_TRUE, TC_UNIQUE_SA_PER_MAP_ID_FALSE, + TC_CHECK_FECF_TRUE, 0x3F, SA_INCREMENT_NONTRANSMITTED_IV_TRUE); +Crypto_Config_Kmc_Crypto_Service("https", "client-demo-kmc.example.com", 8443, "crypto-service","/home/itc/Desktop/CERTS/ammos-ca-bundle.crt",NULL, CRYPTO_FALSE, "/home/itc/Desktop/CERTS/ammos-client-cert.pem", "PEM","/home/itc/Desktop/CERTS/ammos-client-key.pem", NULL, NULL); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 0, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 1, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 2, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + Crypto_Config_Add_Gvcid_Managed_Parameter(0, 0x0003, 3, TC_HAS_FECF, TC_HAS_SEGMENT_HDRS, 1024); + int32_t status = Crypto_Init(); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + + TC_t* tc_sdls_processed_frame; + tc_sdls_processed_frame = malloc(sizeof(uint8_t) * TC_SIZE); + memset(tc_sdls_processed_frame, 0, (sizeof(uint8_t) * TC_SIZE)); + + char* test_frame_pt_h = "2003003A0000000B00000000000000000000000000000000103970EAE4C05ACD1B0C348FDA174DF73EF0E2D603996C4B78B992CD60918729D3A47A"; + uint8_t *test_frame_pt_b = NULL; + int test_frame_pt_len = 0; + + // Expose/setup SAs for testing + SecurityAssociation_t* test_association; + sadb_routine->sadb_get_sa_from_spi(1, &test_association); + test_association->arsn_len = 0; + test_association->shsnf_len = 0; + + // Convert input test frame + hex_conversion(test_frame_pt_h, (char**) &test_frame_pt_b, &test_frame_pt_len); + + status = Crypto_TC_ProcessSecurity(test_frame_pt_b, &test_frame_pt_len, tc_sdls_processed_frame); + + char* truth_data_h = "80d2c70008197f0b003100000000b1fe"; + uint8_t* truth_data_b = NULL; + int truth_data_l = 0; + + hex_conversion(truth_data_h, (char**) &truth_data_b, &truth_data_l); + //printf("Decrypted Frame:\n"); + for(int i = 0; i < tc_sdls_processed_frame->tc_pdu_len; i++) + { + //printf("%02x -> %02x ", tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + ASSERT_EQ(tc_sdls_processed_frame->tc_pdu[i], truth_data_b[i]); + } + //printf("\n"); + + ASSERT_EQ(CRYPTO_LIB_SUCCESS, status); + free(test_frame_pt_b); + free(tc_sdls_processed_frame->tc_sec_header.iv); + free(tc_sdls_processed_frame->tc_sec_header.sn); + free(tc_sdls_processed_frame->tc_sec_header.pad); + free(tc_sdls_processed_frame->tc_sec_trailer.mac); // TODO: Is there a method to free all of this? + free(tc_sdls_processed_frame); + free(truth_data_b); + Crypto_Shutdown(); } UTEST_MAIN();