Skip to content

Commit 10109b0

Browse files
committed
Fix Armv8.1-M x4 xor_bytes alignment fallback
Fixes #1759. Signed-off-by: Brendan Moran <brendan.moran@arm.com>
1 parent 9b69e04 commit 10109b0

4 files changed

Lines changed: 60 additions & 0 deletions

File tree

dev/fips202/armv81m/mve.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ static MLK_INLINE int mlk_keccakf1600_xor_bytes_x4_native(
4949
const uint8_t *data2, const uint8_t *data3, unsigned offset,
5050
unsigned length)
5151
{
52+
unsigned offset_mod8 = offset & 7u;
53+
unsigned byte_prefix = 0;
54+
unsigned main_length;
55+
56+
if (offset_mod8 != 0u)
57+
{
58+
byte_prefix = 8u - offset_mod8;
59+
if (byte_prefix > length)
60+
{
61+
byte_prefix = length;
62+
}
63+
}
64+
65+
main_length = length - byte_prefix;
66+
if (main_length >= 8u &&
67+
((((uintptr_t)data0 + byte_prefix) |
68+
((uintptr_t)data1 + byte_prefix) |
69+
((uintptr_t)data2 + byte_prefix) |
70+
((uintptr_t)data3 + byte_prefix)) &
71+
3u) != 0u)
72+
{
73+
return MLK_NATIVE_FUNC_FALLBACK;
74+
}
75+
5276
mlk_keccak_f1600_x4_state_xor_bytes(state, data0, data1, data2, data3, offset,
5377
length);
5478
return MLK_NATIVE_FUNC_SUCCESS;

mlkem/src/fips202/native/armv81m/mve.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,30 @@ static MLK_INLINE int mlk_keccakf1600_xor_bytes_x4_native(
4949
const uint8_t *data2, const uint8_t *data3, unsigned offset,
5050
unsigned length)
5151
{
52+
unsigned offset_mod8 = offset & 7u;
53+
unsigned byte_prefix = 0;
54+
unsigned main_length;
55+
56+
if (offset_mod8 != 0u)
57+
{
58+
byte_prefix = 8u - offset_mod8;
59+
if (byte_prefix > length)
60+
{
61+
byte_prefix = length;
62+
}
63+
}
64+
65+
main_length = length - byte_prefix;
66+
if (main_length >= 8u &&
67+
((((uintptr_t)data0 + byte_prefix) |
68+
((uintptr_t)data1 + byte_prefix) |
69+
((uintptr_t)data2 + byte_prefix) |
70+
((uintptr_t)data3 + byte_prefix)) &
71+
3u) != 0u)
72+
{
73+
return MLK_NATIVE_FUNC_FALLBACK;
74+
}
75+
5276
mlk_keccak_f1600_x4_state_xor_bytes(state, data0, data1, data2, data3, offset,
5377
length);
5478
return MLK_NATIVE_FUNC_SUCCESS;

test/baremetal/platform/m33-an524/exec_wrapper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ def err(msg, **kwargs):
4545
result = subprocess.run(qemu_cmd, encoding="utf-8", capture_output=True)
4646
finally:
4747
os.unlink(args_file)
48+
49+
output = result.stdout + result.stderr
4850
if result.returncode != 0:
4951
err("FAIL!")
5052
err(f"{qemu_cmd} failed with error code {result.returncode}")
5153
err(result.stderr)
5254
exit(1)
5355

56+
if "ERROR" in output or "FAIL:" in output or "FAILED" in output:
57+
err(output, end="")
58+
exit(1)
59+
5460
for line in result.stdout.splitlines():
5561
print(line)

test/baremetal/platform/m55-an547/exec_wrapper.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ def err(msg, **kwargs):
3838
result = subprocess.run(qemu_cmd, encoding="utf-8", capture_output=True)
3939
finally:
4040
os.unlink(args_file)
41+
42+
output = result.stdout + result.stderr
4143
if result.returncode != 0:
4244
err("FAIL!")
4345
err(f"{qemu_cmd} failed with error code {result.returncode}")
4446
err(result.stderr)
4547
exit(1)
4648

49+
if "ERROR" in output or "FAIL:" in output or "FAILED" in output:
50+
err(output, end="")
51+
exit(1)
52+
4753
for line in result.stdout.splitlines():
4854
print(line)

0 commit comments

Comments
 (0)