Skip to content

Commit 27674f4

Browse files
committed
Add secure_erase function to clear secrets
Signed-off-by: Harshil Jani <[email protected]>
1 parent f587617 commit 27674f4

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

examples/ecdh.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@
1616

1717
#include "random.h"
1818

19+
void secure_erase(void *buf, size_t size) {
20+
volatile char *vbuf = (volatile char *)buf;
21+
size_t i;
22+
for (i = 0; i < size; ++i) {
23+
vbuf[i] = 0;
24+
}
25+
}
1926

2027
int main(void) {
21-
volatile unsigned char seckey1[32];
22-
volatile unsigned char seckey2[32];
28+
unsigned char seckey1[32];
29+
unsigned char seckey2[32];
2330
unsigned char compressed_pubkey1[33];
2431
unsigned char compressed_pubkey2[33];
25-
volatile unsigned char shared_secret1[32];
26-
volatile unsigned char shared_secret2[32];
32+
unsigned char shared_secret1[32];
33+
unsigned char shared_secret2[32];
2734
unsigned char randomize[32];
2835
int return_val;
2936
size_t len;
@@ -114,10 +121,10 @@ int main(void) {
114121
*
115122
* Here we are preventing these writes from being optimized out, as any good compiler
116123
* will remove any writes that aren't used. */
117-
memset(seckey1, 0, sizeof(seckey1));
118-
memset(seckey2, 0, sizeof(seckey2));
119-
memset(shared_secret1, 0, sizeof(shared_secret1));
120-
memset(shared_secret2, 0, sizeof(shared_secret2));
124+
secure_erase(seckey1,sizeof(seckey1));
125+
secure_erase(seckey2,sizeof(seckey2));
126+
secure_erase(shared_secret1,sizeof(shared_secret1));
127+
secure_erase(shared_secret2,sizeof(shared_secret2));
121128

122129
return 0;
123130
}

examples/ecdsa.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515

1616
#include "random.h"
1717

18-
18+
void secure_erase(void *buf, size_t size) {
19+
volatile char *vbuf = (volatile char *)buf;
20+
size_t i;
21+
for (i = 0; i < size; ++i) {
22+
vbuf[i] = 0;
23+
}
24+
}
1925

2026
int main(void) {
2127
/* Instead of signing the message directly, we must sign a 32-byte hash.
@@ -29,7 +35,7 @@ int main(void) {
2935
0x61, 0x2B, 0x1F, 0xCE, 0x77, 0xC8, 0x69, 0x34,
3036
0x5B, 0xFC, 0x94, 0xC7, 0x58, 0x94, 0xED, 0xD3,
3137
};
32-
volatile unsigned char seckey[32];
38+
unsigned char seckey[32];
3339
unsigned char randomize[32];
3440
unsigned char compressed_pubkey[33];
3541
unsigned char serialized_signature[64];
@@ -127,7 +133,7 @@ int main(void) {
127133
*
128134
* Here we are preventing these writes from being optimized out, as any good compiler
129135
* will remove any writes that aren't used. */
130-
memset(seckey, 0, sizeof(seckey));
136+
secure_erase(seckey,sizeof(seckey));
131137

132138
return 0;
133139
}

examples/schnorr.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,19 @@
1717

1818
#include "random.h"
1919

20+
void secure_erase(void *buf, size_t size) {
21+
volatile char *vbuf = (volatile char *)buf;
22+
size_t i;
23+
for (i = 0; i < size; ++i) {
24+
vbuf[i] = 0;
25+
}
26+
}
27+
2028
int main(void) {
2129
unsigned char msg[12] = "Hello World!";
2230
unsigned char msg_hash[32];
2331
unsigned char tag[17] = "my_fancy_protocol";
24-
volatile unsigned char seckey[32];
32+
unsigned char seckey[32];
2533
unsigned char randomize[32];
2634
unsigned char auxiliary_rand[32];
2735
unsigned char serialized_pubkey[32];
@@ -142,7 +150,6 @@ int main(void) {
142150
*
143151
* Here we are preventing these writes from being optimized out, as any good compiler
144152
* will remove any writes that aren't used. */
145-
memset(seckey, 0, sizeof(seckey));
146-
153+
secure_erase(seckey,sizeof(seckey));
147154
return 0;
148155
}

0 commit comments

Comments
 (0)