|
7 | 7 | #ifndef _SECP256K1_MODULE_ECDH_TESTS_ |
8 | 8 | #define _SECP256K1_MODULE_ECDH_TESTS_ |
9 | 9 |
|
| 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 | + |
10 | 39 | void test_ecdh_generator_basepoint(void) { |
11 | 40 | unsigned char s_one[32] = { 0 }; |
12 | 41 | secp256k1_pubkey point[2]; |
@@ -68,6 +97,7 @@ void test_bad_scalar(void) { |
68 | 97 | } |
69 | 98 |
|
70 | 99 | void run_ecdh_tests(void) { |
| 100 | + test_ecdh_api(); |
71 | 101 | test_ecdh_generator_basepoint(); |
72 | 102 | test_bad_scalar(); |
73 | 103 | } |
|
0 commit comments