2020-12-29 17:15:51 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* Copyright (c) 2020 Gregory Maxwell *
|
|
|
|
* Distributed under the MIT software license, see the accompanying *
|
|
|
|
* file COPYING or https://www.opensource.org/licenses/mit-license.php.*
|
|
|
|
***********************************************************************/
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
#include <valgrind/memcheck.h>
|
2021-06-14 14:55:38 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "../include/secp256k1.h"
|
2020-08-26 17:35:27 +00:00
|
|
|
#include "assumptions.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2020-09-15 01:39:26 +00:00
|
|
|
#ifdef ENABLE_MODULE_ECDH
|
2023-03-17 05:22:01 +00:00
|
|
|
# include "../include/rustsecp256k1_v0_8_1_ecdh.h"
|
2020-08-26 17:35:27 +00:00
|
|
|
#endif
|
|
|
|
|
2020-09-15 01:39:26 +00:00
|
|
|
#ifdef ENABLE_MODULE_RECOVERY
|
2023-03-17 05:22:01 +00:00
|
|
|
# include "../include/rustsecp256k1_v0_8_1_recovery.h"
|
2020-09-15 01:39:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENABLE_MODULE_EXTRAKEYS
|
2023-03-17 05:22:01 +00:00
|
|
|
# include "../include/rustsecp256k1_v0_8_1_extrakeys.h"
|
2020-09-15 01:39:26 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENABLE_MODULE_SCHNORRSIG
|
2021-06-14 14:55:38 +00:00
|
|
|
#include "../include/secp256k1_schnorrsig.h"
|
2020-08-26 17:35:27 +00:00
|
|
|
#endif
|
|
|
|
|
2023-03-17 05:22:01 +00:00
|
|
|
void run_tests(rustsecp256k1_v0_8_1_context *ctx, unsigned char *key);
|
2021-06-14 14:55:38 +00:00
|
|
|
|
2020-08-26 17:35:27 +00:00
|
|
|
int main(void) {
|
2023-03-17 05:22:01 +00:00
|
|
|
rustsecp256k1_v0_8_1_context* ctx;
|
2021-06-14 14:55:38 +00:00
|
|
|
unsigned char key[32];
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
if (!RUNNING_ON_VALGRIND) {
|
|
|
|
fprintf(stderr, "This test can only usefully be run inside valgrind.\n");
|
|
|
|
fprintf(stderr, "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2023-03-17 05:22:01 +00:00
|
|
|
ctx = rustsecp256k1_v0_8_1_context_create(SECP256K1_CONTEXT_DECLASSIFY);
|
2021-06-14 14:55:38 +00:00
|
|
|
/** In theory, testing with a single secret input should be sufficient:
|
|
|
|
* If control flow depended on secrets the tool would generate an error.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
key[i] = i + 65;
|
|
|
|
}
|
|
|
|
|
|
|
|
run_tests(ctx, key);
|
|
|
|
|
|
|
|
/* Test context randomisation. Do this last because it leaves the context
|
|
|
|
* tainted. */
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_context_randomize(ctx, key);
|
2021-06-14 14:55:38 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret);
|
|
|
|
|
2023-03-17 05:22:01 +00:00
|
|
|
rustsecp256k1_v0_8_1_context_destroy(ctx);
|
2021-06-14 14:55:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-17 05:22:01 +00:00
|
|
|
void run_tests(rustsecp256k1_v0_8_1_context *ctx, unsigned char *key) {
|
|
|
|
rustsecp256k1_v0_8_1_ecdsa_signature signature;
|
|
|
|
rustsecp256k1_v0_8_1_pubkey pubkey;
|
2020-08-26 17:35:27 +00:00
|
|
|
size_t siglen = 74;
|
|
|
|
size_t outputlen = 33;
|
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
unsigned char msg[32];
|
|
|
|
unsigned char sig[74];
|
|
|
|
unsigned char spubkey[33];
|
2020-09-15 01:39:26 +00:00
|
|
|
#ifdef ENABLE_MODULE_RECOVERY
|
2023-03-17 05:22:01 +00:00
|
|
|
rustsecp256k1_v0_8_1_ecdsa_recoverable_signature recoverable_signature;
|
2020-08-26 17:35:27 +00:00
|
|
|
int recid;
|
|
|
|
#endif
|
2020-09-15 01:39:26 +00:00
|
|
|
#ifdef ENABLE_MODULE_EXTRAKEYS
|
2023-03-17 05:22:01 +00:00
|
|
|
rustsecp256k1_v0_8_1_keypair keypair;
|
2020-09-15 01:39:26 +00:00
|
|
|
#endif
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 32; i++) {
|
|
|
|
msg[i] = i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Test keygen. */
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ec_pubkey_create(ctx, &pubkey, key);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&pubkey, sizeof(rustsecp256k1_v0_8_1_pubkey));
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret);
|
2023-03-17 05:22:01 +00:00
|
|
|
CHECK(rustsecp256k1_v0_8_1_ec_pubkey_serialize(ctx, spubkey, &outputlen, &pubkey, SECP256K1_EC_COMPRESSED) == 1);
|
2020-08-26 17:35:27 +00:00
|
|
|
|
|
|
|
/* Test signing. */
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ecdsa_sign(ctx, &signature, msg, key, NULL, NULL);
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&signature, sizeof(rustsecp256k1_v0_8_1_ecdsa_signature));
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret);
|
2023-03-17 05:22:01 +00:00
|
|
|
CHECK(rustsecp256k1_v0_8_1_ecdsa_signature_serialize_der(ctx, sig, &siglen, &signature));
|
2020-08-26 17:35:27 +00:00
|
|
|
|
2020-09-15 01:39:26 +00:00
|
|
|
#ifdef ENABLE_MODULE_ECDH
|
2020-08-26 17:35:27 +00:00
|
|
|
/* Test ECDH. */
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ecdh(ctx, msg, &pubkey, key, NULL, NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
#endif
|
|
|
|
|
2020-09-15 01:39:26 +00:00
|
|
|
#ifdef ENABLE_MODULE_RECOVERY
|
2020-08-26 17:35:27 +00:00
|
|
|
/* Test signing a recoverable signature. */
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ecdsa_sign_recoverable(ctx, &recoverable_signature, msg, key, NULL, NULL);
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&recoverable_signature, sizeof(recoverable_signature));
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret);
|
2023-03-17 05:22:01 +00:00
|
|
|
CHECK(rustsecp256k1_v0_8_1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &recoverable_signature));
|
2020-08-26 17:35:27 +00:00
|
|
|
CHECK(recid >= 0 && recid <= 3);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ec_seckey_verify(ctx, key);
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ec_seckey_negate(ctx, key);
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ec_seckey_tweak_add(ctx, key, msg);
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(msg, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_ec_seckey_tweak_mul(ctx, key, msg);
|
2020-08-26 17:35:27 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
|
2020-09-15 01:39:26 +00:00
|
|
|
/* Test keypair_create and keypair_xonly_tweak_add. */
|
|
|
|
#ifdef ENABLE_MODULE_EXTRAKEYS
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_keypair_create(ctx, &keypair, key);
|
2020-09-15 01:39:26 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
|
|
|
|
/* The tweak is not treated as a secret in keypair_tweak_add */
|
|
|
|
VALGRIND_MAKE_MEM_DEFINED(msg, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_keypair_xonly_tweak_add(ctx, &keypair, msg);
|
2021-06-14 14:55:38 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(&keypair, sizeof(keypair));
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_keypair_sec(ctx, key, &keypair);
|
2020-09-15 01:39:26 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef ENABLE_MODULE_SCHNORRSIG
|
|
|
|
VALGRIND_MAKE_MEM_UNDEFINED(key, 32);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_keypair_create(ctx, &keypair, key);
|
2020-09-15 01:39:26 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
2023-03-17 05:22:01 +00:00
|
|
|
ret = rustsecp256k1_v0_8_1_schnorrsig_sign32(ctx, sig, msg, &keypair, NULL);
|
2020-09-15 01:39:26 +00:00
|
|
|
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
|
|
|
|
CHECK(ret == 1);
|
|
|
|
#endif
|
2020-08-26 17:35:27 +00:00
|
|
|
}
|