mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-02 07:20:26 +00:00
Test fixes for big-endian
Signed-off-by: Dave Rodgman <dave.rodgman@arm.com>
This commit is contained in:
parent
6d3ec55849
commit
9dc8b6a6a2
@ -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) {
|
if (mbedtls_test_unhexify(raw, sizeof(raw), hex_string, &olen) != 0) {
|
||||||
return 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;
|
*result = 0;
|
||||||
for (size_t i = 0; i < olen; i++) {
|
for (size_t i = 0; i < olen; i++) {
|
||||||
if (MBEDTLS_IS_BIG_ENDIAN) {
|
if (MBEDTLS_IS_BIG_ENDIAN) {
|
||||||
@ -57,38 +71,28 @@ void mbedtls_unaligned_access(int size, int offset)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Generate expected result */
|
/* Define expected result by manually aligning the raw bytes, and
|
||||||
uint64_t expected = 0;
|
* reading back with a normal pointer access. */
|
||||||
for (uint8_t i = 0; i < 8; i++) {
|
uint64_t raw_aligned = 0;
|
||||||
uint8_t shift;
|
uint8_t *e8 = (uint8_t *) &raw_aligned;
|
||||||
if (MBEDTLS_IS_BIG_ENDIAN) {
|
uint8_t *r8 = ((uint8_t *) &raw) + offset;
|
||||||
/*
|
/* Make aligned copy */
|
||||||
* Similar to little-endian case described below, but the shift needs
|
for (int i = 0; i < size / 8; i++) {
|
||||||
* to be inverted
|
e8[i] = r8[i];
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
/* Make a 16/32/64 byte read from the aligned location, and copy to expected */
|
||||||
/* Mask out excess bits from expected result */
|
uint64_t expected = 0;
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case 16:
|
case 16:
|
||||||
expected &= 0xffff;
|
uint16_t *e16 = (uint16_t *) &raw_aligned;
|
||||||
|
expected = *e16;
|
||||||
break;
|
break;
|
||||||
case 32:
|
case 32:
|
||||||
expected &= 0xffffffff;
|
uint32_t *e32 = (uint32_t *) &raw_aligned;
|
||||||
|
expected = *e32;
|
||||||
|
break;
|
||||||
|
case 64:
|
||||||
|
expected = raw_aligned;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user