Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

196 reducing cyclomatic complexity #232

Merged
merged 30 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
08b2646
[nasa/cryptolib#196] Improve cyc. comp.
rjbrown6 Feb 22, 2024
acb4f73
Refactor get SA Service Type
rjbrown6 Feb 26, 2024
f206784
Refactor get SA Service Type
rjbrown6 Feb 26, 2024
c402e6d
WIP - Committing to migrate VMS
rjbrown6 Feb 29, 2024
ff72a58
[nasa/cryptolib#196] TC_Process Refactor
Mar 5, 2024
7bbcf78
[nasa/cryptolib#196] TC_Process Refactor Header prototype updates
rjbrown6 Mar 5, 2024
18c6a76
[nasa/cryptolib#196] WIP: TC_Apply Refactor Refactor and Fix for Stat…
rjbrown6 Mar 6, 2024
cb27f71
[nasa/cryptolib#196] WIP: TC_Apply Refactor
rjbrown6 Mar 6, 2024
6e6436d
[nasa/cryptolib#196] TC_Apply Refactor
rjbrown6 Mar 15, 2024
69846e9
[nasa/cryptolib#196] cryptography_aead_encrypt refactor
rjbrown6 Mar 15, 2024
50dbcb9
[nasa/cryptolib#196] standalone tm_process refactor
rjbrown6 Mar 15, 2024
a028378
[nasa/cryptolib#196] Crypto_Check_Anti_Replay refactor
rjbrown6 Mar 15, 2024
972d481
[nasa/cryptolib#196] sa_get_operational_sa_from_gvcid refactor
rjbrown6 Mar 15, 2024
922a5aa
[nasa/cryptolib#196] Crypto_TM_ApplySecurity Refactor
rjbrown6 Mar 15, 2024
70d0849
[nasa/cryptolib#196] WIP: Crypto_TM_ApplySecurity Refactor
rjbrown6 Mar 15, 2024
76ef337
[nasa/cryptolib#196] Cyclomatic Complexity Refactoring
rjbrown6 Mar 15, 2024
ad9100f
Revert "[nasa/cryptolib#196] Cyclomatic Complexity Refactoring"
rjbrown2 Mar 25, 2024
f59847c
Revert "[nasa/cryptolib#196] WIP: Crypto_TM_ApplySecurity Refactor"
rjbrown2 Mar 25, 2024
a3c2f59
Revert "[nasa/cryptolib#196] Crypto_TM_ApplySecurity Refactor"
rjbrown2 Mar 25, 2024
b01824a
[nasa/cryptolib#196] Unused Variable Fixes
rjbrown6 Mar 26, 2024
dff75ab
[nasa/cryptolib#196] Unused Variable Fixes - misplaced i
rjbrown6 Mar 26, 2024
ebb8033
[nasa/cryptolib#196] KMC Build fix - missing & for pointer
rjbrown6 Mar 26, 2024
d8bbe63
[nasa/cryptolib#196] Fix varable names to lowercase
rjbrown6 Mar 26, 2024
8e163bb
[nasa/cryptolib#196] Refactor for single return - Check Anti Replay
rjbrown6 Mar 26, 2024
bd84fd2
[nasa/cryptolib#196] Fix Enum String Max to appropriate value
rjbrown6 Mar 26, 2024
26729a4
[nasa/cryptolib#196] Remove Comments, update variable to lowercase
rjbrown6 Mar 26, 2024
687addd
[nasa/cryptolib#196] Update Doxygen boilerplate, add prototypes to cr…
rjbrown6 Mar 26, 2024
620748e
[nasa/cryptolib#196] WIP: Update Doxygen boilerplate, add prototypes …
rjbrown6 Mar 26, 2024
9e424b5
[nasa/cryptolib#196] Update Doxygen boilerplate, add prototypes to cr…
rjbrown6 Mar 27, 2024
0dd2483
Minor copy/paste updates in crypto.c
dccutrig Mar 28, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions include/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,52 @@ extern int32_t Crypto_TC_ProcessSecurity(uint8_t* ingest, int *len_ingest, TC_t*
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);

int32_t Crypto_TC_Get_SA_Service_Type(uint8_t* sa_service_type, SecurityAssociation_t* sa_ptr);
int32_t Crypto_TC_Parse_Check_FECF(uint8_t* ingest, int* len_ingest, TC_t* tc_sdls_processed_frame);
int32_t Crypto_TC_Nontransmitted_IV_Increment(SecurityAssociation_t* sa_ptr, TC_t* tc_sdls_processed_frame);
int32_t Crypto_TC_Nontransmitted_SN_Increment(SecurityAssociation_t* sa_ptr, TC_t* tc_sdls_processed_frame);
int32_t Crypto_TC_Check_ACS_Keylen(crypto_key_t* akp, SecurityAssociation_t* sa_ptr);
int32_t Crypto_TC_Check_ECS_Keylen(crypto_key_t* ekp, SecurityAssociation_t* sa_ptr);
void Crypto_TC_Safe_Free_Ptr(uint8_t* ptr);
int32_t Crypto_TC_Do_Decrypt(uint8_t sa_service_type, uint8_t ecs_is_aead_algorithm, crypto_key_t* ekp, SecurityAssociation_t* sa_ptr, uint8_t* aad, TC_t* tc_sdls_processed_frame, uint8_t* ingest, uint16_t tc_enc_payload_start_index, uint16_t aad_len, char* cam_cookies, crypto_key_t* akp, uint8_t segment_hdr_len);
int32_t Crypto_TC_Process_Sanity_Check(int* len_ingest);
int32_t Crypto_TC_Prep_AAD(TC_t* tc_sdls_processed_frame, uint8_t fecf_len, uint8_t sa_service_type, uint8_t ecs_is_aead_algorithm, uint16_t* aad_len, SecurityAssociation_t* sa_ptr, uint8_t segment_hdr_len, uint8_t* ingest, uint8_t** aad);
int32_t Crypto_TC_Get_Keys(crypto_key_t** ekp, crypto_key_t** akp, SecurityAssociation_t* sa_ptr);
int32_t Crypto_TC_Check_IV_ARSN(SecurityAssociation_t* sa_ptr,TC_t* tc_sdls_processed_frame);
uint32_t Crypto_TC_Sanity_Validations(TC_t* tc_sdls_processed_frame, SecurityAssociation_t** sa_ptr);
void Crypto_TC_Get_Ciper_Mode_TCP(uint8_t sa_service_type, uint32_t* encryption_cipher, uint8_t* ecs_is_aead_algorithm, SecurityAssociation_t* sa_ptr);
int32_t Crypto_TC_Get_Ciper_Mode_TCA(uint8_t sa_service_type, uint32_t* encryption_cipher, uint8_t* ecs_is_aead_algorithm, SecurityAssociation_t* sa_ptr);
void Crypto_TC_Calc_Lengths(uint8_t* fecf_len, uint8_t* segment_hdr_len);
void Crypto_TC_Set_Segment_Header(TC_t* tc_sdls_processed_frame, uint8_t* ingest, int* byte_idx);
rjbrown2 marked this conversation as resolved.
Show resolved Hide resolved
int32_t Crypto_TC_Check_CMD_Frame_Flag(uint8_t header_cc);
int32_t Crypto_TC_Validate_SA_Service_Type(uint8_t sa_service_type);
int32_t Crypto_TC_Handle_Enc_Padding(uint8_t sa_service_type, uint32_t* pkcs_padding, uint16_t* p_enc_frame_len, uint16_t* new_enc_frame_header_field_length, uint16_t tf_payload_len, SecurityAssociation_t* sa_ptr);
int32_t Crypto_TC_Frame_Validation(uint16_t* p_enc_frame_len);
int32_t Crypto_TC_Accio_Buffer(uint8_t** p_new_enc_frame, uint16_t* p_enc_frame_len);
int32_t Crypto_TC_ACS_Algo_Check(SecurityAssociation_t* sa_ptr);
int32_t Crypto_TC_Check_IV_Setup(SecurityAssociation_t* sa_ptr, uint8_t* p_new_enc_frame, uint16_t *index);
int32_t Crypto_TC_Do_Encrypt_PLAINTEXT(uint8_t sa_service_type, SecurityAssociation_t* sa_ptr, uint16_t* mac_loc, uint16_t tf_payload_len, uint8_t segment_hdr_len, uint8_t* p_new_enc_frame, crypto_key_t* ekp, uint8_t** aad, uint8_t ecs_is_aead_algorithm, uint16_t *index_p, const uint8_t* p_in_frame, char* cam_cookies, uint32_t pkcs_padding);
void Crypto_TC_Do_Encrypt_NONPLAINTEXT(uint8_t sa_service_type, SecurityAssociation_t* sa_ptr);
int32_t Crypto_TC_Do_Encrypt(uint8_t sa_service_type, SecurityAssociation_t* sa_ptr, uint16_t* mac_loc, uint16_t tf_payload_len, uint8_t segment_hdr_len, uint8_t* p_new_enc_frame, crypto_key_t* ekp, uint8_t** aad, uint8_t ecs_is_aead_algorithm, uint16_t *index_p, const uint8_t* p_in_frame, char* cam_cookies, uint32_t pkcs_padding, uint16_t new_enc_frame_header_field_length, uint16_t* new_fecf);
int32_t Crypto_TC_Check_Init_Setup(uint16_t in_frame_length);
int32_t Crypto_TC_Sanity_Setup(const uint8_t* p_in_frame, const uint16_t in_frame_length);
int32_t Crytpo_TC_Validate_TC_Temp_Header(const uint16_t in_frame_length, TC_FramePrimaryHeader_t temp_tc_header, const uint8_t* p_in_frame, uint8_t* map_id, uint8_t* segmentation_hdr, SecurityAssociation_t** sa_ptr);
int32_t Crypto_TC_Finalize_Frame_Setup(uint8_t sa_service_type, uint32_t* pkcs_padding, uint16_t* p_enc_frame_len, uint16_t* new_enc_frame_header_field_length, uint16_t tf_payload_len, SecurityAssociation_t** sa_ptr, uint8_t** p_new_enc_frame);
void Crypto_TC_Handle_Padding(uint32_t pkcs_padding, SecurityAssociation_t* sa_ptr, uint8_t* p_new_enc_frame, uint16_t* index);
int32_t Crypto_TC_Set_IV(SecurityAssociation_t* sa_ptr, uint8_t* p_new_enc_frame, uint16_t* index);




// Telemetry (TM)
extern int32_t Crypto_TM_ApplySecurity(uint8_t* pTfBuffer);
extern int32_t Crypto_TM_ProcessSecurity(uint8_t* p_ingest, uint16_t len_ingest, uint8_t** pp_processed_frame, uint16_t *p_decrypted_length);
// Advanced Orbiting Systems (AOS)
extern int32_t Crypto_AOS_ApplySecurity(uint8_t* pTfBuffer);
extern int32_t Crypto_AOS_ProcessSecurity(uint8_t* p_ingest, uint16_t len_ingest, uint8_t** pp_processed_frame, uint16_t* p_decrypted_length);


// Crypo Error Support Functions
extern char* Crypto_Get_Error_Code_Enum_String(int32_t crypto_error_code);

Expand Down Expand Up @@ -138,6 +177,10 @@ int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t *sa_ptr, uint8_t *arsn, u
int32_t Crypto_Get_ECS_Algo_Keylen(uint8_t algo);
int32_t Crypto_Get_ACS_Algo_Keylen(uint8_t algo);

int32_t Crypto_Check_Anti_Replay_Verify_Pointers(SecurityAssociation_t* sa_ptr, uint8_t* arsn, uint8_t* iv);
int32_t Crypto_Check_Anti_Replay_ARSNW(SecurityAssociation_t* sa_ptr, uint8_t* arsn, int8_t* arsn_valid);
int32_t Crypto_Check_Anti_Replay_GCM(SecurityAssociation_t* sa_ptr, uint8_t* iv, int8_t* iv_valid);

// Key Management Functions
int32_t Crypto_Key_OTAR(void);
int32_t Crypto_Key_update(uint8_t state);
Expand Down
1 change: 1 addition & 0 deletions include/crypto_error.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
#define CRYPTO_LIB_ERR_MC_INIT (-48)
#define CRYPTO_LIB_ERR_INPUT_FRAME_TOO_SHORT_FOR_AOS_STANDARD (-49)
#define CRYPTO_LIB_ERR_TC_ENUM_USED_FOR_AOS_CONFIG (-50)
#define CRYPTO_LIB_ERR_INVALID_SA_SERVICE_TYPE (-51)

extern char *crypto_enum_errlist_core[];
extern char *crypto_enum_errlist_config[];
Expand Down
137 changes: 104 additions & 33 deletions src/core/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -806,30 +806,45 @@ int32_t Crypto_Process_Extended_Procedure_Pdu(TC_t* tc_sdls_processed_frame, uin
return status;
} // End Process SDLS PDU

/*
** @brief: Check IVs and ARSNs to ensure within valid positive window if applicable
*/
int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t* sa_ptr, uint8_t* arsn, uint8_t* iv)

/**
* @brief Function: Crypto_Check_Anti_Replay_Verify_Pointers
* Sanity Check, validates pointers, verifies non-null
* @param sa_ptr: SecurityAssociation_t*
* @param arsn: uint8_t*
* @param iv: uint8_t*
**/
int32_t Crypto_Check_Anti_Replay_Verify_Pointers(SecurityAssociation_t* sa_ptr, uint8_t* arsn, uint8_t* iv)
{
int32_t status = CRYPTO_LIB_SUCCESS;
int8_t IV_VALID = -1;
int8_t ARSN_VALID = -1;

// Check for NULL pointers
if (sa_ptr == NULL) // #177 - Modification made per suggestion of 'Spicydll' - prevents null dereference
{
return CRYPTO_LIB_ERR_NULL_SA;
status = CRYPTO_LIB_ERR_NULL_SA;
return status;
}
if (arsn == NULL && sa_ptr->arsn_len > 0)
{
return CRYPTO_LIB_ERR_NULL_ARSN;
status = CRYPTO_LIB_ERR_NULL_ARSN;
return status;
}
if (iv == NULL && sa_ptr->shivf_len > 0 && crypto_config.cryptography_type != CRYPTOGRAPHY_TYPE_KMCCRYPTO)
{
return CRYPTO_LIB_ERR_NULL_IV;
status = CRYPTO_LIB_ERR_NULL_IV;
return status;
}

// If sequence number field is greater than zero, check for replay
return status;
}

/**
* @brief Function: Crypto_Check_Anti_Replay_ARSNW
* Sanity Check, validates ARSN within window
* @param sa_ptr: SecurityAssociation_t*
* @param arsn: uint8_t*
* @param arsn_valid: uint8_t*
**/
int32_t Crypto_Check_Anti_Replay_ARSNW(SecurityAssociation_t* sa_ptr, uint8_t* arsn, int8_t* arsn_valid)
{
int32_t status = CRYPTO_LIB_SUCCESS;
if (sa_ptr->shsnf_len > 0)
{
// Check Sequence Number is in ARSNW
Expand All @@ -855,11 +870,23 @@ int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t* sa_ptr, uint8_t* arsn, u
// Valid ARSN received, increment stored value
else
{
ARSN_VALID = CRYPTO_TRUE;
*arsn_valid = CRYPTO_TRUE;
// memcpy(sa_ptr->arsn, arsn, sa_ptr->arsn_len);
}
}
// If IV is greater than zero and using GCM, check for replay
return status;
}

/**
* @brief Function: Crypto_Check_Anti_Replay_GCM
* Sanity Check, validates IV within window
* @param sa_ptr: SecurityAssociation_t*
* @param iv: uint8_t*
* @param iv_valid: uint8_t*
**/
int32_t Crypto_Check_Anti_Replay_GCM(SecurityAssociation_t* sa_ptr, uint8_t* iv, int8_t* iv_valid)
{
int32_t status = CRYPTO_LIB_SUCCESS;
if ((sa_ptr->iv_len > 0) && (sa_ptr->ecs == CRYPTO_CIPHER_AES256_GCM))
{
// Check IV is in ARSNW
Expand Down Expand Up @@ -893,20 +920,46 @@ int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t* sa_ptr, uint8_t* arsn, u
// Valid IV received, increment stored value
else
{
IV_VALID = CRYPTO_TRUE;
*iv_valid = CRYPTO_TRUE;
// memcpy(sa_ptr->iv, iv, sa_ptr->iv_len);
}
}
// IV length is greater than zero, but not using an incrementing IV as in GCM
// we can't verify this internally as Crpytolib doesn't track previous IVs
// or generate random ones
// else{}
return status;
}

/**
* @brief Function: Crypto_Check_Anti_Replay
* Verifies data within window.
* @param sa_ptr: SecurityAssociation_t*
* @param arsn: uint8_t*
* @param iv: uint8_t*
**/
int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t* sa_ptr, uint8_t* arsn, uint8_t* iv)
{
int32_t status = CRYPTO_LIB_SUCCESS;
int8_t iv_valid = -1;
int8_t arsn_valid = -1;

// Check for NULL pointers
status = Crypto_Check_Anti_Replay_Verify_Pointers(sa_ptr, arsn, iv);

// If sequence number field is greater than zero, check for replay
if(status == CRYPTO_LIB_SUCCESS)
{
status = Crypto_Check_Anti_Replay_ARSNW(sa_ptr, arsn, &arsn_valid);
}

// If IV is greater than zero and using GCM, check for replay
if(status == CRYPTO_LIB_SUCCESS)
{
status = Crypto_Check_Anti_Replay_GCM(sa_ptr, iv, &iv_valid);
}

// For GCM specifically, if have a valid IV...
if ((sa_ptr->ecs == CRYPTO_CIPHER_AES256_GCM) && (IV_VALID == CRYPTO_TRUE))
if ((sa_ptr->ecs == CRYPTO_CIPHER_AES256_GCM) && (iv_valid == CRYPTO_TRUE))
{
// Using ARSN? Need to be valid to increment both
if (sa_ptr->arsn_len > 0 && ARSN_VALID == CRYPTO_TRUE)
if (sa_ptr->arsn_len > 0 && arsn_valid == CRYPTO_TRUE)
{
memcpy(sa_ptr->iv, iv, sa_ptr->iv_len);
memcpy(sa_ptr->arsn, arsn, sa_ptr->arsn_len);
Expand All @@ -919,18 +972,25 @@ int32_t Crypto_Check_Anti_Replay(SecurityAssociation_t* sa_ptr, uint8_t* arsn, u
}

// If not GCM, and ARSN is valid - can incrmeent it
if (sa_ptr->ecs != CRYPTO_CIPHER_AES256_GCM && ARSN_VALID == CRYPTO_TRUE)
if (sa_ptr->ecs != CRYPTO_CIPHER_AES256_GCM && arsn_valid == CRYPTO_TRUE)
{
memcpy(sa_ptr->arsn, arsn, sa_ptr->arsn_len);
}

if(status != CRYPTO_LIB_SUCCESS)
{
// Log error if it happened
mc_if->mc_log(status);
}

return status;
}

/*
** @brief: For a given algorithm, return the associated key length in bytes
** @param: algo
*/
/**
* @brief: Function: Crypto_Get_ECS_Algo_Keylen
* For a given algorithm, return the associated key length in bytes
* @param algo: uint8_t
**/
int32_t Crypto_Get_ECS_Algo_Keylen(uint8_t algo)
{
int32_t retval = -1;
Expand All @@ -953,10 +1013,11 @@ 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
*/
/**
* @brief: Function: Crypto_Get_ACS_Algo_Keylen
* For a given algorithm, return the associated key length in bytes
* @param algo: uint8_t
**/
int32_t Crypto_Get_ACS_Algo_Keylen(uint8_t algo)
{
int32_t retval = -1;
Expand All @@ -979,6 +1040,11 @@ int32_t Crypto_Get_ACS_Algo_Keylen(uint8_t algo)
return retval;
}

/**
* @brief: Function: Crypto_Get_Security_Header_Length
* Return Security Header Length
* @param sa_ptr: SecurityAssociation_t*
**/
int32_t Crypto_Get_Security_Header_Length(SecurityAssociation_t* sa_ptr)
{
/* Narrator's Note: Leaving this here for future work
Expand All @@ -1002,12 +1068,17 @@ int32_t Crypto_Get_Security_Header_Length(SecurityAssociation_t* sa_ptr)
return securityHeaderLength;
}

/**
* @brief: Function: Crypto_Get_Security_Trailer_Length
* Return Security Trailer Length
* @param sa_ptr: SecurityAssociation_t*
**/
int32_t Crypto_Get_Security_Trailer_Length(SecurityAssociation_t* sa_ptr)
{
if (!sa_ptr)
{
#ifdef DEBUG
printf(KRED "Get_Trailer_Header_Length passed Null SA!\n" RESET);
printf(KRED "Get_Trailer_Trailer_Length passed Null SA!\n" RESET);
#endif
return CRYPTO_LIB_ERR_NULL_SA;
}
Expand All @@ -1017,4 +1088,4 @@ int32_t Crypto_Get_Security_Trailer_Length(SecurityAssociation_t* sa_ptr)

return securityTrailerLength;

}
}
Loading
Loading