Skip to content

Commit f587617

Browse files
committed
prevent optimization in algorithms
Signed-off-by: Harshil Jani <[email protected]>
1 parent 1b21aa5 commit f587617

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

examples/ecdh.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919

2020
int main(void) {
21-
unsigned char seckey1[32];
22-
unsigned char seckey2[32];
21+
volatile unsigned char seckey1[32];
22+
volatile unsigned char seckey2[32];
2323
unsigned char compressed_pubkey1[33];
2424
unsigned char compressed_pubkey2[33];
25-
unsigned char shared_secret1[32];
26-
unsigned char shared_secret2[32];
25+
volatile unsigned char shared_secret1[32];
26+
volatile unsigned char shared_secret2[32];
2727
unsigned char randomize[32];
2828
int return_val;
2929
size_t len;
@@ -112,7 +112,7 @@ int main(void) {
112112
* example through "out of bounds" array access (see Heartbleed), Or the OS
113113
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
114114
*
115-
* TODO: Prevent these writes from being optimized out, as any good compiler
115+
* Here we are preventing these writes from being optimized out, as any good compiler
116116
* will remove any writes that aren't used. */
117117
memset(seckey1, 0, sizeof(seckey1));
118118
memset(seckey2, 0, sizeof(seckey2));

examples/ecdsa.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ int main(void) {
2929
0x61, 0x2B, 0x1F, 0xCE, 0x77, 0xC8, 0x69, 0x34,
3030
0x5B, 0xFC, 0x94, 0xC7, 0x58, 0x94, 0xED, 0xD3,
3131
};
32-
unsigned char seckey[32];
32+
volatile unsigned char seckey[32];
3333
unsigned char randomize[32];
3434
unsigned char compressed_pubkey[33];
3535
unsigned char serialized_signature[64];
@@ -125,7 +125,7 @@ int main(void) {
125125
* example through "out of bounds" array access (see Heartbleed), Or the OS
126126
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
127127
*
128-
* TODO: Prevent these writes from being optimized out, as any good compiler
128+
* Here we are preventing these writes from being optimized out, as any good compiler
129129
* will remove any writes that aren't used. */
130130
memset(seckey, 0, sizeof(seckey));
131131

examples/schnorr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main(void) {
2121
unsigned char msg[12] = "Hello World!";
2222
unsigned char msg_hash[32];
2323
unsigned char tag[17] = "my_fancy_protocol";
24-
unsigned char seckey[32];
24+
volatile unsigned char seckey[32];
2525
unsigned char randomize[32];
2626
unsigned char auxiliary_rand[32];
2727
unsigned char serialized_pubkey[32];
@@ -140,7 +140,7 @@ int main(void) {
140140
* example through "out of bounds" array access (see Heartbleed), Or the OS
141141
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
142142
*
143-
* TODO: Prevent these writes from being optimized out, as any good compiler
143+
* Here we are preventing these writes from being optimized out, as any good compiler
144144
* will remove any writes that aren't used. */
145145
memset(seckey, 0, sizeof(seckey));
146146

0 commit comments

Comments
 (0)