Bitcoin ABC 0.32.12
P2P Digital Currency
valgrind_ctime_test.c
Go to the documentation of this file.
1/***********************************************************************
2 * Copyright (c) 2020 Gregory Maxwell *
3 * Distributed under the MIT software license, see the accompanying *
4 * file COPYING or https://www.opensource.org/licenses/mit-license.php.*
5 ***********************************************************************/
6
7#include <valgrind/memcheck.h>
8#include <stdio.h>
9
10#include "../include/secp256k1.h"
11#include "assumptions.h"
12#include "util.h"
13
14#ifdef ENABLE_MODULE_ECDH
15# include "../include/secp256k1_ecdh.h"
16#endif
17
18#ifdef ENABLE_MODULE_RECOVERY
19# include "../include/secp256k1_recovery.h"
20#endif
21
22#ifdef ENABLE_MODULE_SCHNORR
24#endif
25
26#ifdef ENABLE_MODULE_EXTRAKEYS
27# include "../include/secp256k1_extrakeys.h"
28#endif
29
30#ifdef ENABLE_MODULE_SCHNORRSIG
31#include "../include/secp256k1_schnorrsig.h"
32#endif
33
34void run_tests(secp256k1_context *ctx, unsigned char *key);
35
36int main(void) {
38 unsigned char key[32];
39 int ret, i;
40
41 if (!RUNNING_ON_VALGRIND) {
42 fprintf(stderr, "This test can only usefully be run inside valgrind.\n");
43 fprintf(stderr, "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n");
44 return 1;
45 }
50 for (i = 0; i < 32; i++) {
51 key[i] = i + 65;
52 }
53
54 run_tests(ctx, key);
55
56 /* Test context randomisation. Do this last because it leaves the context
57 * tainted. */
58 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
60 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
61 CHECK(ret);
62
64 return 0;
65}
66
67void run_tests(secp256k1_context *ctx, unsigned char *key) {
69 secp256k1_pubkey pubkey;
70 size_t siglen = 74;
71 size_t outputlen = 33;
72 int i;
73 int ret;
74 unsigned char msg[32];
75 unsigned char sig[74];
76 unsigned char spubkey[33];
77#ifdef ENABLE_MODULE_RECOVERY
78 secp256k1_ecdsa_recoverable_signature recoverable_signature;
79 int recid;
80#endif
81#ifdef ENABLE_MODULE_EXTRAKEYS
82 secp256k1_keypair keypair;
83#endif
84
85 for (i = 0; i < 32; i++) {
86 msg[i] = i + 1;
87 }
88
89 /* Test keygen. */
90 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
91 ret = secp256k1_ec_pubkey_create(ctx, &pubkey, key);
92 VALGRIND_MAKE_MEM_DEFINED(&pubkey, sizeof(secp256k1_pubkey));
93 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
94 CHECK(ret);
95 CHECK(secp256k1_ec_pubkey_serialize(ctx, spubkey, &outputlen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
96
97 /* Test signing. */
98 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
99 ret = secp256k1_ecdsa_sign(ctx, &signature, msg, key, NULL, NULL);
100 VALGRIND_MAKE_MEM_DEFINED(&signature, sizeof(secp256k1_ecdsa_signature));
101 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
102 CHECK(ret);
104
105#ifdef ENABLE_MODULE_ECDH
106 /* Test ECDH. */
107 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
108 ret = secp256k1_ecdh(ctx, msg, &pubkey, key, NULL, NULL);
109 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
110 CHECK(ret == 1);
111#endif
112
113#ifdef ENABLE_MODULE_RECOVERY
114 /* Test signing a recoverable signature. */
115 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
116 ret = secp256k1_ecdsa_sign_recoverable(ctx, &recoverable_signature, msg, key, NULL, NULL);
117 VALGRIND_MAKE_MEM_DEFINED(&recoverable_signature, sizeof(recoverable_signature));
118 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
119 CHECK(ret);
120 CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &recoverable_signature));
121 CHECK(recid >= 0 && recid <= 3);
122#endif
123
124#ifdef ENABLE_MODULE_SCHNORR
125 /* Test schnorr signing. */
126 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
127 ret = secp256k1_schnorr_sign(ctx, sig, msg, key, NULL, NULL);
128 VALGRIND_MAKE_MEM_DEFINED(&sig, sizeof(64));
129 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
130 CHECK(ret);
131#endif
132
133 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
135 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
136 CHECK(ret == 1);
137
138 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
140 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
141 CHECK(ret == 1);
142
143 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
144 VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
145 ret = secp256k1_ec_seckey_tweak_add(ctx, key, msg);
146 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
147 CHECK(ret == 1);
148
149 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
150 VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
151 ret = secp256k1_ec_seckey_tweak_mul(ctx, key, msg);
152 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
153 CHECK(ret == 1);
154
155 /* Test keypair_create and keypair_xonly_tweak_add. */
156#ifdef ENABLE_MODULE_EXTRAKEYS
157 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
158 ret = secp256k1_keypair_create(ctx, &keypair, key);
159 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
160 CHECK(ret == 1);
161
162 /* The tweak is not treated as a secret in keypair_tweak_add */
163 VALGRIND_MAKE_MEM_DEFINED(msg, 32);
164 ret = secp256k1_keypair_xonly_tweak_add(ctx, &keypair, msg);
165 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
166 CHECK(ret == 1);
167
168 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
169 VALGRIND_MAKE_MEM_UNDEFINED(&keypair, sizeof(keypair));
170 ret = secp256k1_keypair_sec(ctx, key, &keypair);
171 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
172 CHECK(ret == 1);
173#endif
174
175#ifdef ENABLE_MODULE_SCHNORRSIG
176 VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
177 ret = secp256k1_keypair_create(ctx, &keypair, key);
178 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
179 CHECK(ret == 1);
180 ret = secp256k1_schnorrsig_sign32(ctx, sig, msg, &keypair, NULL);
181 VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
182 CHECK(ret == 1);
183#endif
184}
secp256k1_context * ctx
Definition: bench_impl.h:13
SchnorrSig sig
Definition: processor.cpp:523
#define CHECK(cond)
Definition: util.h:81
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:176
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by multiplying it by a tweak.
Definition: secp256k1.c:696
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Randomizes the context to provide enhanced protection against side-channel leakage.
Definition: secp256k1.c:743
#define SECP256K1_CONTEXT_DECLASSIFY
Definition: secp256k1.h:210
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a secret key in place.
Definition: secp256k1.c:606
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:282
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an ECDSA secret key.
Definition: secp256k1.c:565
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:137
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create an ECDSA signature.
Definition: secp256k1.c:550
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition: secp256k1.c:588
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:213
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in DER format.
Definition: secp256k1.c:392
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by adding tweak to it.
Definition: secp256k1.c:652
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdh(const secp256k1_context *ctx, unsigned char *output, const secp256k1_pubkey *pubkey, const unsigned char *seckey, secp256k1_ecdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Compute an EC Diffie-Hellman secret in constant time.
Definition: main_impl.h:29
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_sec(const secp256k1_context *ctx, unsigned char *seckey, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Get the secret key from a keypair.
Definition: main_impl.h:213
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a secret key.
Definition: main_impl.h:195
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a keypair by adding tweak32 to the secret key and updating the public key accordingly.
Definition: main_impl.h:254
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in compact format (64 bytes + recovery id).
Definition: main_impl.h:60
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a recoverable ECDSA signature.
Definition: main_impl.h:123
SECP256K1_API int secp256k1_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a signature using a custom EC-Schnorr-SHA256 construction.
Definition: main_impl.h:32
SECP256K1_API int secp256k1_schnorrsig_sign32(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a Schnorr signature.
Definition: main_impl.h:195
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:87
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:74
int main(void)
void run_tests(secp256k1_context *ctx, unsigned char *key)