|
7 | 7 | #ifndef SECP256K1_MODULE_ECDH_TESTS_H
|
8 | 8 | #define SECP256K1_MODULE_ECDH_TESTS_H
|
9 | 9 |
|
| 10 | +static int ecdh_hash_function_test_xpassthru(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) { |
| 11 | + (void)x; |
| 12 | + (void)y; |
| 13 | + (void)data; |
| 14 | + memcpy(output, x, 32); |
| 15 | + return 1; |
| 16 | +} |
| 17 | + |
10 | 18 | static int ecdh_hash_function_test_fail(unsigned char *output, const unsigned char *x, const unsigned char *y, void *data) {
|
11 | 19 | (void)output;
|
12 | 20 | (void)x;
|
@@ -142,11 +150,53 @@ static void test_result_basepoint(void) {
|
142 | 150 | }
|
143 | 151 | }
|
144 | 152 |
|
| 153 | +static void test_ecdh_wycheproof(void) { |
| 154 | +#include "../../wycheproof/ecdh_secp256k1_test.h" |
| 155 | + int t; |
| 156 | + for (t = 0; t < SECP256K1_ECDH_WYCHEPROOF_NUMBER_TESTVECTORS; t++) { |
| 157 | + int parsed_ok; |
| 158 | + secp256k1_pubkey point; |
| 159 | + const unsigned char *pk; |
| 160 | + const unsigned char *sk; |
| 161 | + const unsigned char *expected_shared_secret; |
| 162 | + unsigned char output_ecdh[65] = { 0 }; |
| 163 | + |
| 164 | + int expected_result; |
| 165 | + int actual; |
| 166 | + |
| 167 | + memset(&point, 0, sizeof(point)); |
| 168 | + pk = &wycheproof_ecdh_public_keys[testvectors[t].pk_offset]; |
| 169 | + parsed_ok = secp256k1_ec_pubkey_parse(CTX, &point, pk, testvectors[t].pk_len); |
| 170 | + |
| 171 | + expected_result = testvectors[t].expected_result; |
| 172 | + |
| 173 | + /* fail if public key is valid, but doesn't parse */ |
| 174 | + CHECK(parsed_ok || expected_result == 0); |
| 175 | + |
| 176 | + if (!parsed_ok && expected_result == 0) { |
| 177 | + continue; |
| 178 | + } |
| 179 | + |
| 180 | + sk = &wycheproof_ecdh_private_keys[testvectors[t].sk_offset]; |
| 181 | + CHECK(testvectors[t].sk_len == 32); |
| 182 | + |
| 183 | + actual = secp256k1_ecdh(CTX, output_ecdh, &point, sk, ecdh_hash_function_test_xpassthru, NULL); |
| 184 | + expected_shared_secret = &wycheproof_ecdh_shared_secrets[testvectors[t].shared_offset]; |
| 185 | + |
| 186 | + CHECK(actual == expected_result); |
| 187 | + if (expected_result == 0) { |
| 188 | + CHECK(testvectors[t].shared_len == 0); |
| 189 | + } |
| 190 | + CHECK(secp256k1_memcmp_var(output_ecdh, expected_shared_secret, testvectors[t].shared_len) == 0); |
| 191 | + } |
| 192 | +} |
| 193 | + |
145 | 194 | static void run_ecdh_tests(void) {
|
146 | 195 | test_ecdh_api();
|
147 | 196 | test_ecdh_generator_basepoint();
|
148 | 197 | test_bad_scalar();
|
149 | 198 | test_result_basepoint();
|
| 199 | + test_ecdh_wycheproof(); |
150 | 200 | }
|
151 | 201 |
|
152 | 202 | #endif /* SECP256K1_MODULE_ECDH_TESTS_H */
|
0 commit comments