Browse Source

Callback data: Accept pointers to either const or non-const data

master
Luke Dashjr 7 years ago
parent
commit
05732c5a5f
  1. 6
      include/secp256k1.h
  2. 2
      src/modules/recovery/main_impl.h
  3. 4
      src/modules/schnorr/main_impl.h
  4. 10
      src/secp256k1.c
  5. 6
      src/tests.c
  6. 8
      src/util.h

6
include/secp256k1.h

@ -94,7 +94,7 @@ typedef int (*secp256k1_nonce_function_t)( @@ -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( @@ -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( @@ -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.

2
src/modules/recovery/main_impl.h

@ -105,7 +105,7 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context_t* ctx, secp256k1_e @@ -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;
}

4
src/modules/schnorr/main_impl.h

@ -36,7 +36,7 @@ int secp256k1_schnorr_sign(const secp256k1_context_t* ctx, unsigned char *sig64, @@ -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 @@ -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;
}

10
src/secp256k1.c

@ -22,7 +22,7 @@ @@ -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) { @@ -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 @@ -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 @@ -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;
}

6
src/tests.c

@ -1787,7 +1787,7 @@ void run_ecdsa_sign_verify(void) { @@ -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 @@ -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 @@ -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);

8
src/util.h

@ -17,9 +17,13 @@ @@ -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 { @@ -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;
}

Loading…
Cancel
Save