Skip to content

Commit 6245eec

Browse files
committed
ctime_test: move context randomization test to the end
1 parent 24d1656 commit 6245eec

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/valgrind_ctime_test.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,49 @@
2525
#include "include/secp256k1_schnorrsig.h"
2626
#endif
2727

28+
void run_tests(secp256k1_context *ctx, unsigned char *key);
29+
2830
int main(void) {
2931
secp256k1_context* ctx;
32+
unsigned char key[32];
33+
int ret, i;
34+
35+
if (!RUNNING_ON_VALGRIND) {
36+
fprintf(stderr, "This test can only usefully be run inside valgrind.\n");
37+
fprintf(stderr, "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n");
38+
exit(1);
39+
}
40+
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN
41+
| SECP256K1_CONTEXT_VERIFY
42+
| SECP256K1_CONTEXT_DECLASSIFY);
43+
/** In theory, testing with a single secret input should be sufficient:
44+
* If control flow depended on secrets the tool would generate an error.
45+
*/
46+
for (i = 0; i < 32; i++) {
47+
key[i] = i + 65;
48+
}
49+
50+
run_tests(ctx, key);
51+
52+
/* Test context randomisation. Do this last because it leaves the context
53+
* tainted. */
54+
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
55+
ret = secp256k1_context_randomize(ctx, key);
56+
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
57+
CHECK(ret);
58+
59+
secp256k1_context_destroy(ctx);
60+
return 1;
61+
}
62+
63+
void run_tests(secp256k1_context *ctx, unsigned char *key) {
3064
secp256k1_ecdsa_signature signature;
3165
secp256k1_pubkey pubkey;
3266
size_t siglen = 74;
3367
size_t outputlen = 33;
3468
int i;
3569
int ret;
3670
unsigned char msg[32];
37-
unsigned char key[32];
3871
unsigned char sig[74];
3972
unsigned char spubkey[33];
4073
#ifdef ENABLE_MODULE_RECOVERY
@@ -45,26 +78,10 @@ int main(void) {
4578
secp256k1_keypair keypair;
4679
#endif
4780

48-
if (!RUNNING_ON_VALGRIND) {
49-
fprintf(stderr, "This test can only usefully be run inside valgrind.\n");
50-
fprintf(stderr, "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n");
51-
exit(1);
52-
}
53-
54-
/** In theory, testing with a single secret input should be sufficient:
55-
* If control flow depended on secrets the tool would generate an error.
56-
*/
57-
for (i = 0; i < 32; i++) {
58-
key[i] = i + 65;
59-
}
6081
for (i = 0; i < 32; i++) {
6182
msg[i] = i + 1;
6283
}
6384

64-
ctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN
65-
| SECP256K1_CONTEXT_VERIFY
66-
| SECP256K1_CONTEXT_DECLASSIFY);
67-
6885
/* Test keygen. */
6986
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
7087
ret = secp256k1_ec_pubkey_create(ctx, &pubkey, key);
@@ -122,12 +139,6 @@ int main(void) {
122139
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
123140
CHECK(ret == 1);
124141

125-
/* Test context randomisation. Do this last because it leaves the context tainted. */
126-
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
127-
ret = secp256k1_context_randomize(ctx, key);
128-
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
129-
CHECK(ret);
130-
131142
/* Test keypair_create and keypair_xonly_tweak_add. */
132143
#ifdef ENABLE_MODULE_EXTRAKEYS
133144
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
@@ -157,7 +168,4 @@ int main(void) {
157168
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
158169
CHECK(ret == 1);
159170
#endif
160-
161-
secp256k1_context_destroy(ctx);
162-
return 0;
163171
}

0 commit comments

Comments
 (0)