Bitcoin ABC  0.22.13
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 http://www.opensource.org/licenses/mit-license.php.*
5  **********************************************************************/
6 
7 #include <valgrind/memcheck.h>
8 #include "include/secp256k1.h"
9 #include "assumptions.h"
10 #include "util.h"
11 
12 #ifdef ENABLE_MODULE_ECDH
13 # include "include/secp256k1_ecdh.h"
14 #endif
15 
16 #ifdef ENABLE_MODULE_RECOVERY
18 #endif
19 
20 #ifdef ENABLE_MODULE_SCHNORR
22 #endif
23 
24 #ifdef ENABLE_MODULE_EXTRAKEYS
26 #endif
27 
28 #ifdef ENABLE_MODULE_SCHNORRSIG
30 #endif
31 
32 int main(void) {
34  secp256k1_ecdsa_signature signature;
35  secp256k1_pubkey pubkey;
36  size_t siglen = 74;
37  size_t outputlen = 33;
38  int i;
39  int ret;
40  unsigned char msg[32];
41  unsigned char key[32];
42  unsigned char sig[74];
43  unsigned char spubkey[33];
44 #ifdef ENABLE_MODULE_RECOVERY
45  secp256k1_ecdsa_recoverable_signature recoverable_signature;
46  int recid;
47 #endif
48 #ifdef ENABLE_MODULE_EXTRAKEYS
49  secp256k1_keypair keypair;
50 #endif
51 
52  if (!RUNNING_ON_VALGRIND) {
53  fprintf(stderr, "This test can only usefully be run inside valgrind.\n");
54  fprintf(stderr, "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n");
55  exit(1);
56  }
57 
61  for (i = 0; i < 32; i++) {
62  key[i] = i + 65;
63  }
64  for (i = 0; i < 32; i++) {
65  msg[i] = i + 1;
66  }
67 
71 
72  /* Test keygen. */
73  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
74  ret = secp256k1_ec_pubkey_create(ctx, &pubkey, key);
75  VALGRIND_MAKE_MEM_DEFINED(&pubkey, sizeof(secp256k1_pubkey));
76  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
77  CHECK(ret);
78  CHECK(secp256k1_ec_pubkey_serialize(ctx, spubkey, &outputlen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
79 
80  /* Test signing. */
81  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
82  ret = secp256k1_ecdsa_sign(ctx, &signature, msg, key, NULL, NULL);
83  VALGRIND_MAKE_MEM_DEFINED(&signature, sizeof(secp256k1_ecdsa_signature));
84  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
85  CHECK(ret);
86  CHECK(secp256k1_ecdsa_signature_serialize_der(ctx, sig, &siglen, &signature));
87 
88 #ifdef ENABLE_MODULE_ECDH
89  /* Test ECDH. */
90  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
91  ret = secp256k1_ecdh(ctx, msg, &pubkey, key, NULL, NULL);
92  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
93  CHECK(ret == 1);
94 #endif
95 
96 #ifdef ENABLE_MODULE_RECOVERY
97  /* Test signing a recoverable signature. */
98  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
99  ret = secp256k1_ecdsa_sign_recoverable(ctx, &recoverable_signature, msg, key, NULL, NULL);
100  VALGRIND_MAKE_MEM_DEFINED(&recoverable_signature, sizeof(recoverable_signature));
101  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
102  CHECK(ret);
103  CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &recoverable_signature));
104  CHECK(recid >= 0 && recid <= 3);
105 #endif
106 
107 #if ENABLE_MODULE_SCHNORR
108  /* Test schnorr signing. */
109  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
110  ret = secp256k1_schnorr_sign(ctx, sig, msg, key, NULL, NULL);
111  VALGRIND_MAKE_MEM_DEFINED(&sig, sizeof(64));
112  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
113  CHECK(ret);
114 #endif
115 
116  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
117  ret = secp256k1_ec_seckey_verify(ctx, key);
118  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
119  CHECK(ret == 1);
120 
121  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
122  ret = secp256k1_ec_seckey_negate(ctx, key);
123  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
124  CHECK(ret == 1);
125 
126  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
127  VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
128  ret = secp256k1_ec_seckey_tweak_add(ctx, key, msg);
129  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
130  CHECK(ret == 1);
131 
132  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
133  VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
134  ret = secp256k1_ec_seckey_tweak_mul(ctx, key, msg);
135  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
136  CHECK(ret == 1);
137 
138  /* Test context randomisation. Do this last because it leaves the context tainted. */
139  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
140  ret = secp256k1_context_randomize(ctx, key);
141  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
142  CHECK(ret);
143 
144  /* Test keypair_create and keypair_xonly_tweak_add. */
145 #ifdef ENABLE_MODULE_EXTRAKEYS
146  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
147  ret = secp256k1_keypair_create(ctx, &keypair, key);
148  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
149  CHECK(ret == 1);
150 
151  /* The tweak is not treated as a secret in keypair_tweak_add */
152  VALGRIND_MAKE_MEM_DEFINED(msg, 32);
153  ret = secp256k1_keypair_xonly_tweak_add(ctx, &keypair, msg);
154  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
155  CHECK(ret == 1);
156 #endif
157 
158 #ifdef ENABLE_MODULE_SCHNORRSIG
159  VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
160  ret = secp256k1_keypair_create(ctx, &keypair, key);
161  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
162  CHECK(ret == 1);
163  ret = secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, NULL);
164  VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
165  CHECK(ret == 1);
166 #endif
167 
169  return 0;
170 }
std::array< uint8_t, 64 > sig
Definition: processor.cpp:250
SECP256K1_API int secp256k1_schnorr_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, 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:33
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
Definition: secp256k1.c:731
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:171
int main(void)
secp256k1_context * ctx
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
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:592
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:220
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:297
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:171
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:197
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:574
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:176
#define SECP256K1_CONTEXT_DECLASSIFY
Definition: secp256k1.h:172
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:551
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msg32, 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:536
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msg32, 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
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:80
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
#define CHECK(cond)
Definition: util.h:53
Opaque data structure that holds a keypair consisting of a secret and a public key.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak) 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:683
#define SECP256K1_CONTEXT_VERIFY
Flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context...
Definition: secp256k1.h:170
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 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:381
SECP256K1_API int secp256k1_schnorrsig_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, secp256k1_nonce_function_hardened noncefp, void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a Schnorr signature.
Definition: main_impl.h:127
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by adding tweak to it.
Definition: secp256k1.c:638
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:153
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:67