-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepare moving verify.c functions to backend
Currently, mlkem-native has the `verify`, `cmov`, and `cmov_int16` functions in the verify.c function similar as the reference implementation. By putting them in a separate compilation unit, we stop the compiler from turning this code into variable time code. While being portable, this has various downside: - If LTO were to be used, this can lead to timing side-channels. We stop that by not using LTO for verify.c in our own build, but we have no control over how other people compile our code - this feels dangerous. - If these functions are in a separte compilation unit, the compiler cannot inline them. This is very expensive particularly for `cmov_int16`. By writing that gadget in inline assembly, we can allow the compiler to inline these calls, but still guarantee that they execute in constant-time. This commit prepares the switch to a native implementation of the verify.c functions in a architecture-specific header file. For now these are not implemented in inline assembly as I wanted to gather feedback on this first. Signed-off-by: Matthias J. Kannwischer <[email protected]>
- Loading branch information
1 parent
36a2397
commit dbef242
Showing
7 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2024 The mlkem-native project authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#ifndef VERIFY_AARCH64_H | ||
#define VERIFY_AARCH64_H | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
static inline int verify_native(const uint8_t *a, const uint8_t *b, | ||
const size_t len) | ||
{ | ||
// TODO: replace this with inline asm | ||
uint8_t r = 0; | ||
uint64_t u; | ||
|
||
// Switch to a _signed_ ilen value, so that our loop counter | ||
// can also be signed, and thus (i - 1) in the loop invariant | ||
// can yield -1 as required. | ||
const int ilen = (int)len; | ||
|
||
for (int i = 0; i < ilen; i++) | ||
{ | ||
r |= a[i] ^ b[i]; | ||
} | ||
|
||
u = (-(uint64_t)r) >> 63; | ||
return (int)u; | ||
} | ||
|
||
static inline void cmov_native(uint8_t *r, const uint8_t *x, size_t len, | ||
uint8_t b) | ||
{ | ||
// TODO: replace this with inline asm | ||
size_t i; | ||
|
||
b = (-b) & 0xFF; | ||
for (i = 0; i < len; i++) | ||
{ | ||
r[i] ^= b & (r[i] ^ x[i]); | ||
} | ||
} | ||
|
||
|
||
static inline void cmov_int16_native(int16_t *r, const int16_t v, | ||
const uint16_t b) | ||
{ | ||
// TODO: replace this with inline asm | ||
// b == 0 => mask = 0x0000 | ||
// b == 1 => mask = 0xFFFF | ||
const uint16_t mask = -b; | ||
|
||
// mask == 0x0000 => *r == (*r ^ 0x0000) == *r | ||
// mask == 0xFFFF => *r == (*r ^ (*r ^ v)) == (*r ^ *r) ^ v == 0 ^ v == v | ||
*r ^= mask & ((*r) ^ v); | ||
} | ||
|
||
#endif /* VERIFY_AARCH64_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2024 The mlkem-native project authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
#ifndef VERIFY_X86_64_H | ||
#define VERIFY_X86_64_H | ||
|
||
#include <stddef.h> | ||
#include <stdint.h> | ||
|
||
static inline int verify_native(const uint8_t *a, const uint8_t *b, | ||
const size_t len) | ||
{ | ||
// TODO: replace this with inline asm | ||
uint8_t r = 0; | ||
uint64_t u; | ||
|
||
// Switch to a _signed_ ilen value, so that our loop counter | ||
// can also be signed, and thus (i - 1) in the loop invariant | ||
// can yield -1 as required. | ||
const int ilen = (int)len; | ||
|
||
for (int i = 0; i < ilen; i++) | ||
{ | ||
r |= a[i] ^ b[i]; | ||
} | ||
|
||
u = (-(uint64_t)r) >> 63; | ||
return (int)u; | ||
} | ||
|
||
static inline void cmov_native(uint8_t *r, const uint8_t *x, size_t len, | ||
uint8_t b) | ||
{ | ||
// TODO: replace this with inline asm | ||
size_t i; | ||
|
||
b = (-b) & 0xFF; | ||
for (i = 0; i < len; i++) | ||
{ | ||
r[i] ^= b & (r[i] ^ x[i]); | ||
} | ||
} | ||
|
||
|
||
static inline void cmov_int16_native(int16_t *r, const int16_t v, | ||
const uint16_t b) | ||
{ | ||
// TODO: replace this with inline asm | ||
// b == 0 => mask = 0x0000 | ||
// b == 1 => mask = 0xFFFF | ||
const uint16_t mask = -b; | ||
|
||
// mask == 0x0000 => *r == (*r ^ 0x0000) == *r | ||
// mask == 0xFFFF => *r == (*r ^ (*r ^ v)) == (*r ^ *r) ^ v == 0 ^ v == v | ||
*r ^= mask & ((*r) ^ v); | ||
} | ||
|
||
#endif /* VERIFY_X86_64_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters