Skip to content

Commit 9240aa7

Browse files
committed
Side channel: ARMv8 Neon code requires cmov optimization blocker
Signed-off-by: Stephan Mueller <smueller@chronox.de>
1 parent ea24307 commit 9240aa7

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

internal/api/null_buffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern "C" {
3030
extern const uint8_t null_buffer[];
3131

3232
extern volatile int16_t optimization_blocker_int16;
33+
extern volatile int8_t optimization_blocker_int8;
3334

3435
#ifdef __cplusplus
3536
}

internal/api/sidechannel_resistantce.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define SIDECHANNEL_RESISTANCE_H
2929

3030
#include "ext_headers.h"
31+
#include "null_buffer.h"
3132

3233
#ifdef __cplusplus
3334
extern "C" {
@@ -47,10 +48,29 @@ extern "C" {
4748
static inline void cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b)
4849
{
4950
size_t i;
51+
uint8_t opt_blocker;
52+
53+
/*
54+
* Goal: increment variable only depending on a given condition without
55+
* the use of a branching operation which alters the timing behavior
56+
* depending on the condition. As the condition here depends on
57+
* secret data (the buf variable), the code has to ensure that no
58+
* branching is used to have time-invariant code. This solution
59+
* below also shall ensure that the compiler cannot optimize this code
60+
* such that it brings back the branching.
61+
*
62+
* (condition ^ opt_blocker) can be any value at run-time to the
63+
* compiler, making it impossible to skip the computation (except the
64+
* compiler would care to create a branch for opt_blocker to be either
65+
* 0 or 1, which would be extremely unlikely). Yet the volatile
66+
* variable has to be loaded only once at the beginning of the function
67+
* call.
68+
*/
69+
opt_blocker = (uint8_t)optimization_blocker_int8;
5070

5171
b = -b;
5272
for (i = 0; i < len; i++)
53-
r[i] ^= b & (r[i] ^ x[i]);
73+
r[i] ^= (b & (r[i] ^ x[i])) ^ opt_blocker;
5474
}
5575

5676
/**

internal/src/null_buffer.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@
2222
const uint8_t null_buffer[LC_NULL_BUFFER_SIZE] = { 0 };
2323

2424
volatile int16_t optimization_blocker_int16 = 0;
25+
volatile int8_t optimization_blocker_int8 = 0;

0 commit comments

Comments
 (0)