Skip to content

Commit 6f8ae2f

Browse files
committed
ecdh: test NULL-checking of arguments
Boosts the ECDH module to 100% coverage
1 parent 25e3cfb commit 6f8ae2f

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/modules/ecdh/main_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ int secp256k1_ecdh(const secp256k1_context* ctx, unsigned char *result, const se
1616
secp256k1_gej res;
1717
secp256k1_ge pt;
1818
secp256k1_scalar s;
19+
VERIFY_CHECK(ctx != NULL);
1920
ARG_CHECK(result != NULL);
2021
ARG_CHECK(point != NULL);
2122
ARG_CHECK(scalar != NULL);
22-
(void)ctx;
2323

2424
secp256k1_pubkey_load(ctx, &pt, point);
2525
secp256k1_scalar_set_b32(&s, scalar, &overflow);

src/modules/ecdh/tests_impl.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,35 @@
77
#ifndef _SECP256K1_MODULE_ECDH_TESTS_
88
#define _SECP256K1_MODULE_ECDH_TESTS_
99

10+
void test_ecdh_api(void) {
11+
/* Setup context that just counts errors */
12+
secp256k1_context *tctx = secp256k1_context_create(SECP256K1_CONTEXT_SIGN);
13+
secp256k1_pubkey point;
14+
unsigned char res[32];
15+
unsigned char s_one[32] = { 0 };
16+
int32_t ecount = 0;
17+
s_one[31] = 1;
18+
19+
secp256k1_context_set_error_callback(tctx, counting_illegal_callback_fn, &ecount);
20+
secp256k1_context_set_illegal_callback(tctx, counting_illegal_callback_fn, &ecount);
21+
CHECK(secp256k1_ec_pubkey_create(tctx, &point, s_one) == 1);
22+
23+
/* Check all NULLs are detected */
24+
CHECK(secp256k1_ecdh(tctx, res, &point, s_one) == 1);
25+
CHECK(ecount == 0);
26+
CHECK(secp256k1_ecdh(tctx, NULL, &point, s_one) == 0);
27+
CHECK(ecount == 1);
28+
CHECK(secp256k1_ecdh(tctx, res, NULL, s_one) == 0);
29+
CHECK(ecount == 2);
30+
CHECK(secp256k1_ecdh(tctx, res, &point, NULL) == 0);
31+
CHECK(ecount == 3);
32+
CHECK(secp256k1_ecdh(tctx, res, &point, s_one) == 1);
33+
CHECK(ecount == 3);
34+
35+
/* Cleanup */
36+
secp256k1_context_destroy(tctx);
37+
}
38+
1039
void test_ecdh_generator_basepoint(void) {
1140
unsigned char s_one[32] = { 0 };
1241
secp256k1_pubkey point[2];
@@ -68,6 +97,7 @@ void test_bad_scalar(void) {
6897
}
6998

7099
void run_ecdh_tests(void) {
100+
test_ecdh_api();
71101
test_ecdh_generator_basepoint();
72102
test_bad_scalar();
73103
}

0 commit comments

Comments
 (0)