Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/valgrind_ctime_test.c
/********************************************************************** | /********************************************************************** | ||||
* Copyright (c) 2020 Gregory Maxwell * | * Copyright (c) 2020 Gregory Maxwell * | ||||
* Distributed under the MIT software license, see the accompanying * | * Distributed under the MIT software license, see the accompanying * | ||||
* file COPYING or http://www.opensource.org/licenses/mit-license.php.* | * file COPYING or http://www.opensource.org/licenses/mit-license.php.* | ||||
**********************************************************************/ | **********************************************************************/ | ||||
#include <valgrind/memcheck.h> | #include <valgrind/memcheck.h> | ||||
#include "include/secp256k1.h" | #include "include/secp256k1.h" | ||||
#include "util.h" | #include "util.h" | ||||
#if ENABLE_MODULE_ECDH | #if ENABLE_MODULE_ECDH | ||||
# include "include/secp256k1_ecdh.h" | # include "include/secp256k1_ecdh.h" | ||||
#endif | #endif | ||||
#if ENABLE_MODULE_RECOVERY | |||||
# include "include/secp256k1_recovery.h" | |||||
#endif | |||||
#if ENABLE_MODULE_SCHNORR | #if ENABLE_MODULE_SCHNORR | ||||
# include "include/secp256k1_schnorr.h" | # include "include/secp256k1_schnorr.h" | ||||
#endif | #endif | ||||
int main(void) { | int main(void) { | ||||
secp256k1_context* ctx; | secp256k1_context* ctx; | ||||
secp256k1_ecdsa_signature signature; | secp256k1_ecdsa_signature signature; | ||||
secp256k1_pubkey pubkey; | secp256k1_pubkey pubkey; | ||||
size_t siglen = 74; | size_t siglen = 74; | ||||
size_t outputlen = 33; | size_t outputlen = 33; | ||||
int i; | int i; | ||||
int ret; | int ret; | ||||
unsigned char msg[32]; | unsigned char msg[32]; | ||||
unsigned char key[32]; | unsigned char key[32]; | ||||
unsigned char sig[74]; | unsigned char sig[74]; | ||||
unsigned char spubkey[33]; | unsigned char spubkey[33]; | ||||
#if ENABLE_MODULE_RECOVERY | |||||
secp256k1_ecdsa_recoverable_signature recoverable_signature; | |||||
int recid; | |||||
#endif | |||||
if (!RUNNING_ON_VALGRIND) { | if (!RUNNING_ON_VALGRIND) { | ||||
fprintf(stderr, "This test can only usefully be run inside valgrind.\n"); | fprintf(stderr, "This test can only usefully be run inside valgrind.\n"); | ||||
fprintf(stderr, "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n"); | fprintf(stderr, "Usage: libtool --mode=execute valgrind ./valgrind_ctime_test\n"); | ||||
exit(1); | exit(1); | ||||
} | } | ||||
/** In theory, testing with a single secret input should be sufficient: | /** In theory, testing with a single secret input should be sufficient: | ||||
Show All 27 Lines | |||||
#if ENABLE_MODULE_ECDH | #if ENABLE_MODULE_ECDH | ||||
/* Test ECDH. */ | /* Test ECDH. */ | ||||
VALGRIND_MAKE_MEM_UNDEFINED(key, 32); | VALGRIND_MAKE_MEM_UNDEFINED(key, 32); | ||||
ret = secp256k1_ecdh(ctx, msg, &pubkey, key, NULL, NULL); | ret = secp256k1_ecdh(ctx, msg, &pubkey, key, NULL, NULL); | ||||
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret)); | VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret)); | ||||
CHECK(ret == 1); | CHECK(ret == 1); | ||||
#endif | #endif | ||||
#if ENABLE_MODULE_RECOVERY | |||||
/* Test signing a recoverable signature. */ | |||||
VALGRIND_MAKE_MEM_UNDEFINED(key, 32); | |||||
ret = secp256k1_ecdsa_sign_recoverable(ctx, &recoverable_signature, msg, key, NULL, NULL); | |||||
VALGRIND_MAKE_MEM_DEFINED(&recoverable_signature, sizeof(recoverable_signature)); | |||||
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret)); | |||||
CHECK(ret); | |||||
CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, sig, &recid, &recoverable_signature)); | |||||
CHECK(recid >= 0 && recid <= 3); | |||||
#endif | |||||
#if ENABLE_MODULE_SCHNORR | #if ENABLE_MODULE_SCHNORR | ||||
/* Test schnorr signing. */ | /* Test schnorr signing. */ | ||||
VALGRIND_MAKE_MEM_UNDEFINED(key, 32); | VALGRIND_MAKE_MEM_UNDEFINED(key, 32); | ||||
ret = secp256k1_schnorr_sign(ctx, sig, msg, key, NULL, NULL); | ret = secp256k1_schnorr_sign(ctx, sig, msg, key, NULL, NULL); | ||||
VALGRIND_MAKE_MEM_DEFINED(&sig, sizeof(64)); | VALGRIND_MAKE_MEM_DEFINED(&sig, sizeof(64)); | ||||
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret)); | VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret)); | ||||
CHECK(ret); | CHECK(ret); | ||||
#endif | #endif | ||||
Show All 32 Lines |