diff --git a/src/core/crypto_error.c b/src/core/crypto_error.c index f049de06..a211ea4d 100644 --- a/src/core/crypto_error.c +++ b/src/core/crypto_error.c @@ -133,6 +133,34 @@ 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 @@ -140,92 +168,34 @@ char *crypto_enum_errlist_crypto_cam[] = */ 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; } \ No newline at end of file diff --git a/src/crypto/kmc/base64url.c b/src/crypto/kmc/base64url.c index 0092a2df..e1c53cc5 100644 --- a/src/crypto/kmc/base64url.c +++ b/src/crypto/kmc/base64url.c @@ -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 @@ -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