EDITED by TheMatjaz on 2023-07-26 for formatting clarity.
When I execute offline testing cases of test_encrypt_offline
in the test_aead128_encryption
, the 1024th test case fails every time. I discovered that once the length of the _CT
field is greater than 46, this problem will occur. I located this problem and found vecs_aead_next
in the test_encrypt_offline
, after executing fscan_ciphertext
, the first two bytes of testcase.key
will be overwritten to 0, causing the key to be modified, resulting in an error in the generated ciphertext. This issue is due to vecs_aead_t
. The length of the ciphertext field in the structure vecs_aead_t
is 48. Once it overflows, it will overwrite the next field (key
), causing this error.
To address this issue from the root cause, this modification method is to fix fsca_variable_hexbytes
, so the bytes are modified by not immediately filling the read ciphertext into bytes after fscanf
, but by first caching the read 1-byte ciphertext and ensuring that the last byte of the ciphertext is read before writing the data to bytes.
Source original is:
static vecs_err_t fscan_variable_hexbytes(FILE* const handle,
uint8_t* bytes,
size_t* amount)
{
size_t i = 0;
while (1)
{
if (i >= VECS_MAX_HEXBYTES_LEN)
{
return VECS_FORMAT_TOO_LARGE_HEXBYTES;
}
const int bytes_read = fscanf(handle, " %2hhx ", bytes++);
if (bytes_read != 1)
{
break;
}
i++;
}
*amount = i;
return VECS_OK;
}
Source after fix is:
static vecs_err_t fscan_variable_hexbytes(FILE* const handle,
uint8_t* bytes,
size_t* amount)
{
size_t i = 0;
uint8_t bytes_t;
while (1)
{
if (i >= VECS_MAX_HEXBYTES_LEN)
{
return VECS_FORMAT_TOO_LARGE_HEXBYTES;
}
const int bytes_read = fscanf(handle, " %2hhx ", &bytes_t);
if (bytes_read != 1)
{
break;
}
*bytes++ = bytes_t;
i++;
}
*amount = i;
return VECS_OK;
}