Test fixes for big-endian

Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
Dave Rodgman 2023-04-13 12:53:35 +01:00
parent 6d3ec55849
commit 9dc8b6a6a2

View File

@ -17,6 +17,20 @@ int parse_hex_string(char *hex_string, uint64_t *result)
if (mbedtls_test_unhexify(raw, sizeof(raw), hex_string, &olen) != 0) {
return 0;
}
/* If < 8 bytes, shift right and pad with leading zeros for big-endian */
if (MBEDTLS_IS_BIG_ENDIAN) {
if (olen < 8) {
int offset = 8 - olen;
for (int i = olen - 1; i >= 0; i--) {
raw[i + offset] = raw[i];
}
for (int i = 0; i < offset; i++) {
raw[i] = 0;
}
}
}
*result = 0;
for (size_t i = 0; i < olen; i++) {
if (MBEDTLS_IS_BIG_ENDIAN) {
@ -57,38 +71,28 @@ void mbedtls_unaligned_access(int size, int offset)
break;
}
/* Generate expected result */
uint64_t expected = 0;
for (uint8_t i = 0; i < 8; i++) {
uint8_t shift;
if (MBEDTLS_IS_BIG_ENDIAN) {
/*
* Similar to little-endian case described below, but the shift needs
* to be inverted
*/
shift = 7 - (i * 8);
} else {
/* example for offset == 1:
* expected = (( 1 + 0 ) << (0 * 8)) | (( 1 + 1 ) << (1 * 8)) | (( 1 + 2 ) << (2 * 8)))
* = (1 << 0) | (2 << 8) | (3 << 16) ...
* = 0x0807060504030201
* x = { 0, 1, 2, 3, ... }
* ie expected is the value that would be read from x on a LE system, when
* byte swapping is not performed
*/
shift = i * 8;
}
uint64_t b = offset + i;
expected |= b << shift;
/* Define expected result by manually aligning the raw bytes, and
* reading back with a normal pointer access. */
uint64_t raw_aligned = 0;
uint8_t *e8 = (uint8_t *) &raw_aligned;
uint8_t *r8 = ((uint8_t *) &raw) + offset;
/* Make aligned copy */
for (int i = 0; i < size / 8; i++) {
e8[i] = r8[i];
}
/* Mask out excess bits from expected result */
/* Make a 16/32/64 byte read from the aligned location, and copy to expected */
uint64_t expected = 0;
switch (size) {
case 16:
expected &= 0xffff;
uint16_t *e16 = (uint16_t *) &raw_aligned;
expected = *e16;
break;
case 32:
expected &= 0xffffffff;
uint32_t *e32 = (uint32_t *) &raw_aligned;
expected = *e32;
break;
case 64:
expected = raw_aligned;
break;
}