File tree Expand file tree Collapse file tree 5 files changed +42
-17
lines changed Expand file tree Collapse file tree 5 files changed +42
-17
lines changed Original file line number Diff line number Diff line change @@ -69,7 +69,7 @@ noinst_HEADERS += contrib/lax_der_parsing.h
69
69
noinst_HEADERS += contrib/lax_der_parsing.c
70
70
noinst_HEADERS += contrib/lax_der_privatekey_parsing.h
71
71
noinst_HEADERS += contrib/lax_der_privatekey_parsing.c
72
- noinst_HEADERS += examples/random .h
72
+ noinst_HEADERS += examples/examples_util .h
73
73
74
74
PRECOMPUTED_LIB = libsecp256k1_precomputed.la
75
75
noinst_LTLIBRARIES = $(PRECOMPUTED_LIB )
Original file line number Diff line number Diff line change 14
14
#include <secp256k1.h>
15
15
#include <secp256k1_ecdh.h>
16
16
17
- #include "random.h"
18
-
17
+ #include "examples_util.h"
19
18
20
19
int main (void ) {
21
20
unsigned char seckey1 [32 ];
@@ -112,12 +111,12 @@ int main(void) {
112
111
* example through "out of bounds" array access (see Heartbleed), Or the OS
113
112
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
114
113
*
115
- * TODO: Prevent these writes from being optimized out, as any good compiler
114
+ * Here we are preventing these writes from being optimized out, as any good compiler
116
115
* 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 ));
116
+ secure_erase (seckey1 , sizeof (seckey1 ));
117
+ secure_erase (seckey2 , sizeof (seckey2 ));
118
+ secure_erase (shared_secret1 , sizeof (shared_secret1 ));
119
+ secure_erase (shared_secret2 , sizeof (shared_secret2 ));
121
120
122
121
return 0 ;
123
122
}
Original file line number Diff line number Diff line change 13
13
14
14
#include <secp256k1.h>
15
15
16
- #include "random.h"
17
-
18
-
16
+ #include "examples_util.h"
19
17
20
18
int main (void ) {
21
19
/* Instead of signing the message directly, we must sign a 32-byte hash.
@@ -133,9 +131,9 @@ int main(void) {
133
131
* example through "out of bounds" array access (see Heartbleed), Or the OS
134
132
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
135
133
*
136
- * TODO: Prevent these writes from being optimized out, as any good compiler
134
+ * Here we are preventing these writes from being optimized out, as any good compiler
137
135
* will remove any writes that aren't used. */
138
- memset (seckey , 0 , sizeof (seckey ));
136
+ secure_erase (seckey , sizeof (seckey ));
139
137
140
138
return 0 ;
141
139
}
Original file line number Diff line number Diff line change @@ -71,3 +71,32 @@ static void print_hex(unsigned char* data, size_t size) {
71
71
}
72
72
printf ("\n" );
73
73
}
74
+
75
+ #if defined(_MSC_VER )
76
+ // For SecureZeroMemory
77
+ #include <Windows.h>
78
+ #endif
79
+ /* Cleanses memory to prevent leaking sensitive info. Won't be optimized out. */
80
+ static SECP256K1_INLINE void secure_erase (void * ptr , size_t len ) {
81
+ #if defined(_MSC_VER )
82
+ /* SecureZeroMemory is guaranteed not to be optimized out by MSVC. */
83
+ SecureZeroMemory (ptr , len );
84
+ #elif defined(__GNUC__ )
85
+ /* We use a memory barrier that scares the compiler away from optimizing out the memset.
86
+ *
87
+ * Quoting Adam Langley <agl@google.com> in commit ad1907fe73334d6c696c8539646c21b11178f20f
88
+ * in BoringSSL (ISC License):
89
+ * As best as we can tell, this is sufficient to break any optimisations that
90
+ * might try to eliminate "superfluous" memsets.
91
+ * This method used in memzero_explicit() the Linux kernel, too. Its advantage is that it is
92
+ * pretty efficient, because the compiler can still implement the memset() efficently,
93
+ * just not remove it entirely. See "Dead Store Elimination (Still) Considered Harmful" by
94
+ * Yang et al. (USENIX Security 2017) for more background.
95
+ */
96
+ memset (ptr , 0 , len );
97
+ __asm__ __volatile__("" : : "r" (ptr ) : "memory" );
98
+ #else
99
+ void * (* volatile const volatile_memset )(void * , int , size_t ) = memset ;
100
+ volatile_memset (ptr , 0 , len );
101
+ #endif
102
+ }
Original file line number Diff line number Diff line change 15
15
#include <secp256k1_extrakeys.h>
16
16
#include <secp256k1_schnorrsig.h>
17
17
18
- #include "random .h"
18
+ #include "examples_util .h"
19
19
20
20
int main (void ) {
21
21
unsigned char msg [12 ] = "Hello World!" ;
@@ -149,9 +149,8 @@ int main(void) {
149
149
* example through "out of bounds" array access (see Heartbleed), Or the OS
150
150
* swapping them to disk. Hence, we overwrite the secret key buffer with zeros.
151
151
*
152
- * TODO: Prevent these writes from being optimized out, as any good compiler
152
+ * Here we are preventing these writes from being optimized out, as any good compiler
153
153
* will remove any writes that aren't used. */
154
- memset (seckey , 0 , sizeof (seckey ));
155
-
154
+ secure_erase (seckey , sizeof (seckey ));
156
155
return 0 ;
157
156
}
You can’t perform that action at this time.
0 commit comments