Skip to content

Commit

Permalink
[#196] Improve cyc. comp.
Browse files Browse the repository at this point in the history
  • Loading branch information
rjbrown6 committed Feb 22, 2024
1 parent 11a14ef commit 08b2646
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 97 deletions.
104 changes: 37 additions & 67 deletions src/core/crypto_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,99 +133,69 @@ char *crypto_enum_errlist_crypto_cam[] =
(char*) "CAM_KEYTAB_FILE_KINIT_FAILURE",
};

/*
** @brief: Helper Function. Get specific error code, given code, allowable max, and valid string expansion
** @param: int32_t, int32_t, char*
* @return: char*
*/
char* Crypto_Get_Crypto_Error_Code_String(int32_t crypto_error_code, int32_t crypto_error_code_max, char* valid_output_string)
{
if(crypto_error_code < crypto_error_code_max)
{
return CRYPTO_UNDEFINED_ERROR;
}
return valid_output_string;
}

/*
** @brief: Helper Function. Get specific error code, given code, allowable max, and valid string expansion
** @param: int32_t, int32_t, char*
* @return: char*
*/
char* Crypto_Get_Error_Code_String(int32_t crypto_error_code, int32_t crypto_error_code_max, char* valid_output_string)
{
if(crypto_error_code > crypto_error_code_max)
{
return CRYPTO_UNDEFINED_ERROR;
}
return valid_output_string;
}

/*
** @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)
{
char* return_string = CRYPTO_UNDEFINED_ERROR;
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];
}

return_string = Crypto_Get_Error_Code_String(crypto_error_code, 610, 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];
}
return_string = Crypto_Get_Error_Code_String(crypto_error_code, 515, 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];
}

return_string = Crypto_Get_Error_Code_String(crypto_error_code, 402, 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_sa_mariadb[crypto_error_code % 300];
}

return_string = Crypto_Get_Error_Code_String(crypto_error_code, 303, crypto_enum_errlist_sa_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_sa_if[crypto_error_code % 200];
}
return_string = Crypto_Get_Error_Code_String(crypto_error_code, 201, crypto_enum_errlist_sa_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;
return_string = Crypto_Get_Error_Code_String(crypto_error_code, 103, crypto_enum_errlist_config[crypto_error_code % 100]);
}
else if(crypto_error_code <= 0) // Cryptolib Core Error Codes
{
if(crypto_error_code < -45)
{
return CRYPTO_UNDEFINED_ERROR;
}
else
{
return crypto_enum_errlist_core[(crypto_error_code * (-1))];
}
}
else
{
return CRYPTO_UNDEFINED_ERROR;
return_string = Crypto_Get_Crypto_Error_Code_String(crypto_error_code, -45, crypto_enum_errlist_core[(crypto_error_code * (-1))]);
}
return return_string;
}
65 changes: 35 additions & 30 deletions src/crypto/kmc/base64url.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,40 @@ void base64urlEncode(const void* input, size_t inputLen, char_t* output,
}


void base64urlDecode_rempadding(size_t inputLen, uint32_t value, size_t *n, uint8_t* p)
{
//All trailing pad characters are omitted in Base64url
if((inputLen % 4) == 2)
{
//The last block contains only 1 byte
if(p != NULL)
{
//Decode the last byte
p[*n] = (value >> 4) & 0xFF;
}

//Adjust the length of the decoded data
*n = *n + 1;
}
else if((inputLen % 4) == 3)
{
//The last block contains only 2 bytes
if(p != NULL)
{
//Decode the last two bytes
p[*n] = (value >> 10) & 0xFF;
p[*n + 1] = (value >> 2) & 0xFF;
}

//Adjust the length of the decoded data
*n = *n + 2;
}
else
{
//No pad characters in this case
}
}

/**
* @brief Base64url decoding algorithm
* @param[in] input Base64url-encoded string
Expand Down Expand Up @@ -252,36 +286,7 @@ int32_t base64urlDecode(const char_t* input, size_t inputLen, void* output,
//Check status code
if(!error)
{
//All trailing pad characters are omitted in Base64url
if((inputLen % 4) == 2)
{
//The last block contains only 1 byte
if(p != NULL)
{
//Decode the last byte
p[n] = (value >> 4) & 0xFF;
}

//Adjust the length of the decoded data
n++;
}
else if((inputLen % 4) == 3)
{
//The last block contains only 2 bytes
if(p != NULL)
{
//Decode the last two bytes
p[n] = (value >> 10) & 0xFF;
p[n + 1] = (value >> 2) & 0xFF;
}

//Adjust the length of the decoded data
n += 2;
}
else
{
//No pad characters in this case
}
base64urlDecode_rempadding(inputLen,value, n, p);
}

//Total number of bytes that have been written
Expand Down

0 comments on commit 08b2646

Please sign in to comment.