Skip to content

Commit

Permalink
AMMOSGH-XX: update base64 calculations to use exact sizes instead of …
Browse files Browse the repository at this point in the history
…wild over-compensations
  • Loading branch information
IbraheemYSaleh committed Apr 14, 2022
1 parent de7cec4 commit 095e13f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
6 changes: 6 additions & 0 deletions src/src_cryptography/src_kmc_crypto_service/base64url.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ int32_t base64urlDecode(const char_t* input, size_t inputLen, void* output,
#define ERROR_INVALID_CHARACTER 23
#define NO_ERROR 0

// https://stackoverflow.com/questions/13378815/base64-length-calculation
// calculate the size of 'output' buffer required for a 'input' buffer of length x during Base64 encoding operation
#define B64ENCODE_OUT_SAFESIZE(x) ((((x) + 3 - 1)/3) * 4 + 1)

// calculate the size of 'output' buffer required for a 'input' buffer of length x during Base64 decoding operation
#define B64DECODE_OUT_SAFESIZE(x) (((x)*3)/4)

//C++ guard
#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out,

// Base64 URL encode IV for KMC REST Encrypt
// Not needed for CMAC/HMAC (only supported auth ciphers now)
// char* iv_base64 = (char*)calloc(1,iv_len*4);
// char* iv_base64 = (char*)calloc(1,B64ENCODE_OUT_SAFESIZE(iv_len)+1);
// base64urlEncode(iv,iv_len,iv_base64,NULL);

uint8_t* auth_payload = aad;
Expand Down Expand Up @@ -482,7 +482,8 @@ static int32_t cryptography_authenticate(uint8_t* data_out, size_t len_data_out,

/* JSON Response Handling End */

uint8_t* icv_decoded = malloc(strlen(icv_base64) + 1);
// https://stackoverflow.com/questions/13378815/base64-length-calculation
uint8_t* icv_decoded = calloc(1,B64DECODE_OUT_SAFESIZE(strlen(icv_base64)) + 1);
size_t icv_decoded_len = 0;
base64urlDecode(icv_base64,strlen(icv_base64),icv_decoded, &icv_decoded_len);
#ifdef DEBUG
Expand Down Expand Up @@ -539,7 +540,7 @@ static int32_t cryptography_validate_authentication(uint8_t* data_out, size_t le
size_t auth_payload_len = aad_len;

// Base64 URL encode MAC for KMC REST Encrypt
char* mac_base64 = (char*)calloc(1,mac_size*4);
char* mac_base64 = (char*)calloc(1,B64ENCODE_OUT_SAFESIZE(mac_size) + 1);
base64urlEncode(mac,mac_size,mac_base64,NULL);
#ifdef DEBUG
printf("MAC Base64 URL Encoded: %s\n",mac_base64);
Expand Down Expand Up @@ -721,7 +722,7 @@ static int32_t cryptography_aead_encrypt(uint8_t* data_out, size_t len_data_out,
configure_curl_connect_opts(curl);

// Base64 URL encode IV for KMC REST Encrypt
char* iv_base64 = (char*)calloc(1,iv_len*4);
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;
Expand Down Expand Up @@ -977,7 +978,7 @@ static int32_t cryptography_aead_decrypt(uint8_t* data_out, size_t len_data_out,
configure_curl_connect_opts(curl);

// Base64 URL encode IV for KMC REST Encrypt
char* iv_base64 = (char*)calloc(1,iv_len*4);
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;
Expand Down

0 comments on commit 095e13f

Please sign in to comment.