sata_get_random_base64: fix: don't "overflow" the '\0' terminator

Also don't encode the base64 string with an ending newline.

The '\0' was stored just after out_size bytes in the output buffer, so it wasn't
stored on the TPM NV memory and it cause issues with functions expecting
a null terminated string (ata_security_*).
pull/443/head
Marco Oliverio 2024-01-19 16:34:31 +00:00
parent 4b957bd529
commit 311ed5fa22
1 changed files with 10 additions and 5 deletions

View File

@ -270,20 +270,25 @@ static int get_key_sha256(uint8_t key_slot, uint8_t *hash)
static int sata_get_random_base64(uint8_t *out, int *out_size)
{
uint8_t rand[ATA_SECRET_RANDOM_BYTES];
word32 _out_size;
word32 base_64_len;
int ret;
ret = wolfBoot_get_random(rand, ATA_SECRET_RANDOM_BYTES);
if (ret != 0)
return ret;
_out_size = *out_size;
ret = Base64_Encode(rand, ATA_SECRET_RANDOM_BYTES, out, &_out_size);
base_64_len = *out_size;
ret = Base64_Encode_NoNl(rand, ATA_SECRET_RANDOM_BYTES, out, &base_64_len);
if (ret != 0)
return ret;
/* double check we have a NULL-terminated string */
*out_size = (int)_out_size;
out[*out_size] = '\0';
if ((int)base_64_len < *out_size) {
out[base_64_len] = '\0';
base_64_len += 1;
} else {
out[base_64_len-1] = '\0';
}
*out_size = (int)base_64_len;
return 0;
}