Skip to content

Commit dd08f03

Browse files
committed
OpenSSL/EC tests
1 parent da3038c commit dd08f03

File tree

3 files changed

+119
-8
lines changed

3 files changed

+119
-8
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ bench: $(FILES) src/bench.c $(OBJS)
2727
$(CC) -fPIC -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DNDEBUG -O2 src/bench.c $(OBJS) $(LDFLAGS_EXTRA) -o bench
2828

2929
tests: $(FILES) src/tests.c $(OBJS)
30-
$(CC) -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DVERIFY -fstack-protector-all -O2 -ggdb3 src/tests.c $(OBJS) $(LDFLAGS_EXTRA) -o tests
30+
$(CC) -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) $(CFLAGS_TEST_EXTRA) -DVERIFY -fstack-protector-all -O2 -ggdb3 src/tests.c $(OBJS) $(LDFLAGS_EXTRA) $(LDFLAGS_TEST_EXTRA) -o tests
3131

3232
coverage: $(FILES) src/tests.c $(OBJS)
3333
rm -rf tests.gcno tests.gcda tests_cov
34-
$(CC) -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) -DVERIFY --coverage -O0 -g src/tests.c $(OBJS) $(LDFLAGS_EXTRA) -o tests_cov
34+
$(CC) -std=c99 $(CFLAGS) $(CFLAGS_EXTRA) $(CFLAGS_TEST_EXTRA) -DVERIFY --coverage -O0 -g src/tests.c $(OBJS) $(LDFLAGS_EXTRA) $(LDFLAGS_TEST_EXTRA) -o tests_cov
3535
rm -rf lcov
3636
mkdir -p lcov
3737
cd lcov; lcov --directory ../ --zerocounters

configure

+31
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ if [ "$?" = 0 ]; then
4949
HAVE_OPENSSL=1
5050
fi
5151

52+
# test openssl/EC
53+
HAVE_OPENSSL_EC=0
54+
if [ "$HAVE_OPENSSL" = "1" ]; then
55+
$CC $CFLAGS -std=c99 -x c - -o /dev/null -lcrypto <<EOF
56+
#include <openssl/ec.h>
57+
#include <openssl/ecdsa.h>
58+
#include <openssl/obj_mac.h>
59+
int main() {
60+
EC_KEY *eckey = EC_KEY_new_by_curve_name(NID_secp256k1);
61+
ECDSA_sign(0, NULL, 0, NULL, NULL, eckey);
62+
ECDSA_verify(0, NULL, 0, NULL, 0, eckey);
63+
EC_KEY_free(eckey);
64+
return 0;
65+
}
66+
EOF
67+
if [ "$?" = 0 ]; then
68+
HAVE_OPENSSL_EC=1
69+
fi
70+
fi
71+
5272
# test gmp
5373
HAVE_GMP=0
5474
$CC $CFLAGS -std=c99 -x c - -o /dev/null -lgmp 2>/dev/null <<EOF
@@ -127,10 +147,21 @@ if [ "$LINK_GMP" = "1" ]; then
127147
fi
128148
if [ "$LINK_OPENSSL" = "1" ]; then
129149
LDFLAGS_EXTRA="-lcrypto"
150+
else
151+
if [ "$HAVE_OPENSSL_EC" = "1" ]; then
152+
LDFLAGS_TEST_EXTRA="-lcrypto"
153+
fi
154+
fi
155+
156+
CFLAGS_TEST_EXTRA=""
157+
if [ "$HAVE_OPENSSL_EC" = "1" ]; then
158+
CFLAGS_TEST_EXTRA="-DENABLE_OPENSSL_TESTS"
130159
fi
131160

132161
echo "CC=$CC" > config.mk
133162
echo "YASM=$YASM" >>config.mk
134163
echo "CFLAGS_EXTRA=$CFLAGS_EXTRA" >> config.mk
164+
echo "CFLAGS_TEST_EXTRA=$CFLAGS_TEST_EXTRA" >> config.mk
135165
echo "LDFLAGS_EXTRA=$LDFLAGS_EXTRA" >> config.mk
166+
echo "LDFLAGS_TEST_EXTRA=$LDFLAGS_TEST_EXTRA" >> config.mk
136167
echo "USE_ASM=$USE_ASM" >>config.mk

src/tests.c

+86-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
#include "impl/ecdsa.h"
88
#include "impl/util.h"
99

10+
#ifdef ENABLE_OPENSSL_TESTS
11+
#include "openssl/bn.h"
12+
#include "openssl/ec.h"
13+
#include "openssl/ecdsa.h"
14+
#include "openssl/obj_mac.h"
15+
#endif
16+
1017
static int count = 100;
1118

1219
/***** NUM TESTS *****/
@@ -330,28 +337,33 @@ void run_wnaf() {
330337
secp256k1_num_free(&n);
331338
}
332339

340+
void random_sign(secp256k1_ecdsa_sig_t *sig, const secp256k1_num_t *key, const secp256k1_num_t *msg, int *recid) {
341+
secp256k1_num_t nonce;
342+
secp256k1_num_init(&nonce);
343+
do {
344+
random_num_order_test(&nonce);
345+
} while(!secp256k1_ecdsa_sig_sign(sig, key, msg, &nonce, recid));
346+
secp256k1_num_free(&nonce);
347+
}
348+
333349
void test_ecdsa_sign_verify() {
334350
const secp256k1_ge_consts_t *c = secp256k1_ge_consts;
335-
secp256k1_num_t msg, key, nonce;
351+
secp256k1_num_t msg, key;
336352
secp256k1_num_init(&msg);
337353
random_num_order_test(&msg);
338354
secp256k1_num_init(&key);
339355
random_num_order_test(&key);
340-
secp256k1_num_init(&nonce);
341356
secp256k1_gej_t pubj; secp256k1_ecmult_gen(&pubj, &key);
342357
secp256k1_ge_t pub; secp256k1_ge_set_gej(&pub, &pubj);
343358
secp256k1_ecdsa_sig_t sig;
344359
secp256k1_ecdsa_sig_init(&sig);
345-
do {
346-
random_num_order_test(&nonce);
347-
} while(!secp256k1_ecdsa_sig_sign(&sig, &key, &msg, &nonce, NULL));
360+
random_sign(&sig, &key, &msg, NULL);
348361
assert(secp256k1_ecdsa_sig_verify(&sig, &pub, &msg));
349362
secp256k1_num_inc(&msg);
350363
assert(!secp256k1_ecdsa_sig_verify(&sig, &pub, &msg));
351364
secp256k1_ecdsa_sig_free(&sig);
352365
secp256k1_num_free(&msg);
353366
secp256k1_num_free(&key);
354-
secp256k1_num_free(&nonce);
355367
}
356368

357369
void run_ecdsa_sign_verify() {
@@ -360,10 +372,75 @@ void run_ecdsa_sign_verify() {
360372
}
361373
}
362374

375+
#ifdef ENABLE_OPENSSL_TESTS
376+
EC_KEY *get_openssl_key(const secp256k1_num_t *key) {
377+
EC_KEY *ec_key = EC_KEY_new_by_curve_name(NID_secp256k1);
378+
BN_CTX *ctx = BN_CTX_new();
379+
BN_CTX_start(ctx);
380+
BIGNUM *priv_key = BN_CTX_get(ctx);
381+
unsigned char keyb[32];
382+
secp256k1_num_get_bin(keyb, 32, key);
383+
BN_bin2bn(keyb, 32, priv_key);
384+
const EC_GROUP *group = EC_KEY_get0_group(ec_key);
385+
EC_POINT *pub_key = EC_POINT_new(group);
386+
EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx);
387+
EC_KEY_set_private_key(ec_key, priv_key);
388+
EC_KEY_set_public_key(ec_key, pub_key);
389+
EC_POINT_free(pub_key);
390+
BN_CTX_end(ctx);
391+
BN_CTX_free(ctx);
392+
return ec_key;
393+
}
394+
395+
void test_ecdsa_openssl() {
396+
const secp256k1_ge_consts_t *c = secp256k1_ge_consts;
397+
secp256k1_num_t key, msg;
398+
secp256k1_num_init(&msg);
399+
unsigned char message[32];
400+
secp256k1_rand256_test(message);
401+
secp256k1_num_set_bin(&msg, message, 32);
402+
secp256k1_num_init(&key);
403+
random_num_order_test(&key);
404+
secp256k1_gej_t qj;
405+
secp256k1_ecmult_gen(&qj, &key);
406+
secp256k1_ge_t q;
407+
secp256k1_ge_set_gej(&q, &qj);
408+
EC_KEY *ec_key = get_openssl_key(&key);
409+
assert(ec_key);
410+
unsigned char signature[80];
411+
int sigsize = 80;
412+
assert(ECDSA_sign(0, message, sizeof(message), signature, &sigsize, ec_key));
413+
secp256k1_ecdsa_sig_t sig;
414+
secp256k1_ecdsa_sig_init(&sig);
415+
assert(secp256k1_ecdsa_sig_parse(&sig, signature, sigsize));
416+
assert(secp256k1_ecdsa_sig_verify(&sig, &q, &msg));
417+
secp256k1_num_inc(&sig.r);
418+
assert(!secp256k1_ecdsa_sig_verify(&sig, &q, &msg));
419+
420+
random_sign(&sig, &key, &msg, NULL);
421+
sigsize = 80;
422+
assert(secp256k1_ecdsa_sig_serialize(signature, &sigsize, &sig));
423+
assert(ECDSA_verify(0, message, sizeof(message), signature, sigsize, ec_key) == 1);
424+
425+
secp256k1_ecdsa_sig_free(&sig);
426+
EC_KEY_free(ec_key);
427+
secp256k1_num_free(&key);
428+
secp256k1_num_free(&msg);
429+
}
430+
431+
void run_ecdsa_openssl() {
432+
for (int i=0; i<10*count; i++) {
433+
test_ecdsa_openssl();
434+
}
435+
}
436+
#endif
437+
363438
int main(int argc, char **argv) {
364439
if (argc > 1)
365440
count = strtol(argv[1], NULL, 0)*47;
366441

442+
printf("test count = %i\n", count);
443+
367444
// initialize
368445
secp256k1_fe_start();
369446
secp256k1_ge_start();
@@ -379,6 +456,9 @@ int main(int argc, char **argv) {
379456

380457
// ecdsa tests
381458
run_ecdsa_sign_verify();
459+
#ifdef ENABLE_OPENSSL_TESTS
460+
run_ecdsa_openssl();
461+
#endif
382462

383463
// shutdown
384464
secp256k1_ecmult_stop();

0 commit comments

Comments
 (0)