From 05732c5a5f781d49972659d8f59e5262ff026ce8 Mon Sep 17 00:00:00 2001 From: Luke Dashjr Date: Tue, 1 Sep 2015 05:48:58 +0000 Subject: [PATCH] Callback data: Accept pointers to either const or non-const data --- include/secp256k1.h | 6 +++--- src/modules/recovery/main_impl.h | 2 +- src/modules/schnorr/main_impl.h | 4 ++-- src/secp256k1.c | 10 +++++----- src/tests.c | 6 +++--- src/util.h | 8 ++++++-- 6 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/secp256k1.h b/include/secp256k1.h index 2bc6909..3c53c8f 100644 --- a/include/secp256k1.h +++ b/include/secp256k1.h @@ -94,7 +94,7 @@ typedef int (*secp256k1_nonce_function_t)( const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, - const void *data, + void *data, unsigned int attempt ); @@ -187,7 +187,7 @@ void secp256k1_context_destroy( void secp256k1_context_set_illegal_callback( secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), - void* data + const void* data ) SECP256K1_ARG_NONNULL(1); /** Set a callback function to be called when an internal consistency check @@ -209,7 +209,7 @@ void secp256k1_context_set_illegal_callback( void secp256k1_context_set_error_callback( secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), - void* data + const void* data ) SECP256K1_ARG_NONNULL(1); /** Parse a variable-length public key into the pubkey object. diff --git a/src/modules/recovery/main_impl.h b/src/modules/recovery/main_impl.h index 7f3d8a7..4d74a84 100644 --- a/src/modules/recovery/main_impl.h +++ b/src/modules/recovery/main_impl.h @@ -105,7 +105,7 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context_t* ctx, secp256k1_e secp256k1_scalar_set_b32(&msg, msg32, NULL); while (1) { unsigned char nonce32[32]; - ret = noncefp(nonce32, seckey, msg32, NULL, noncedata, count); + ret = noncefp(nonce32, seckey, msg32, NULL, (void*)noncedata, count); if (!ret) { break; } diff --git a/src/modules/schnorr/main_impl.h b/src/modules/schnorr/main_impl.h index 8548b26..5a337b4 100644 --- a/src/modules/schnorr/main_impl.h +++ b/src/modules/schnorr/main_impl.h @@ -36,7 +36,7 @@ int secp256k1_schnorr_sign(const secp256k1_context_t* ctx, unsigned char *sig64, secp256k1_scalar_set_b32(&sec, seckey, NULL); while (1) { unsigned char nonce32[32]; - ret = noncefp(nonce32, msg32, seckey, secp256k1_schnorr_algo16, noncedata, count); + ret = noncefp(nonce32, msg32, seckey, secp256k1_schnorr_algo16, (void*)noncedata, count); if (!ret) { break; } @@ -107,7 +107,7 @@ int secp256k1_schnorr_generate_nonce_pair(const secp256k1_context_t* ctx, secp25 do { int overflow; - ret = noncefp(privnonce32, sec32, msg32, secp256k1_schnorr_algo16, noncedata, count++); + ret = noncefp(privnonce32, sec32, msg32, secp256k1_schnorr_algo16, (void*)noncedata, count++); if (!ret) { break; } diff --git a/src/secp256k1.c b/src/secp256k1.c index 41b279b..b54b525 100644 --- a/src/secp256k1.c +++ b/src/secp256k1.c @@ -22,7 +22,7 @@ #define ARG_CHECK(cond) do { \ if (EXPECT(!(cond), 0)) { \ - ctx->illegal_callback.fn(#cond, ctx->illegal_callback.data); \ + secp256k1_callback(&ctx->illegal_callback, #cond); \ return 0; \ } \ } while(0) @@ -94,14 +94,14 @@ void secp256k1_context_destroy(secp256k1_context_t* ctx) { free(ctx); } -void secp256k1_context_set_illegal_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) { +void secp256k1_context_set_illegal_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), const void* data) { if (!fun) fun = default_illegal_callback_fn; ctx->illegal_callback.fn = fun; ctx->illegal_callback.data = data; } -void secp256k1_context_set_error_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) { +void secp256k1_context_set_error_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), const void* data) { if (!fun) fun = default_error_callback_fn; ctx->error_callback.fn = fun; @@ -230,7 +230,7 @@ int secp256k1_ecdsa_verify(const secp256k1_context_t* ctx, const secp256k1_ecdsa secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m)); } -static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) { +static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) { unsigned char keydata[112]; int keylen = 64; secp256k1_rfc6979_hmac_sha256_t rng; @@ -285,7 +285,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context_t* ctx, secp256k1_ecdsa_signatu secp256k1_scalar_set_b32(&msg, msg32, NULL); while (1) { unsigned char nonce32[32]; - ret = noncefp(nonce32, msg32, seckey, NULL, noncedata, count); + ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count); if (!ret) { break; } diff --git a/src/tests.c b/src/tests.c index 624f39a..342cb07 100644 --- a/src/tests.c +++ b/src/tests.c @@ -1787,7 +1787,7 @@ void run_ecdsa_sign_verify(void) { } /** Dummy nonce generation function that just uses a precomputed nonce, and fails if it is not accepted. Use only for testing. */ -static int precomputed_nonce_function(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) { +static int precomputed_nonce_function(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) { (void)msg32; (void)key32; (void)algo16; @@ -1795,7 +1795,7 @@ static int precomputed_nonce_function(unsigned char *nonce32, const unsigned cha return (counter == 0); } -static int nonce_function_test_fail(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) { +static int nonce_function_test_fail(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) { /* Dummy nonce generator that has a fatal error on the first counter value. */ if (counter == 0) { return 0; @@ -1803,7 +1803,7 @@ static int nonce_function_test_fail(unsigned char *nonce32, const unsigned char return nonce_function_rfc6979(nonce32, msg32, key32, algo16, data, counter - 1); } -static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) { +static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) { /* Dummy nonce generator that produces unacceptable nonces for the first several counter values. */ if (counter < 3) { memset(nonce32, counter==0 ? 0 : 255, 32); diff --git a/src/util.h b/src/util.h index ce21c42..2f7c1c3 100644 --- a/src/util.h +++ b/src/util.h @@ -17,9 +17,13 @@ typedef struct { void (*fn)(const char *text, void* data); - void* data; + const void* data; } callback_t; +static SECP256K1_INLINE void secp256k1_callback(const callback_t * const cb, const char * const text) { + cb->fn(text, (void*)cb->data); +} + #ifdef DETERMINISTIC #define TEST_FAILURE(msg) do { \ fprintf(stderr, "%s\n", msg); \ @@ -64,7 +68,7 @@ typedef struct { static SECP256K1_INLINE void *checked_malloc(const callback_t* cb, size_t size) { void *ret = malloc(size); if (ret == NULL) { - cb->fn("Out of memory", cb->data); + secp256k1_callback(cb, "Out of memory"); } return ret; }