|
7 | 7 | #ifndef _SECP256K1_MODULE_RECOVERY_TESTS_
|
8 | 8 | #define _SECP256K1_MODULE_RECOVERY_TESTS_
|
9 | 9 |
|
| 10 | +static int recovery_test_nonce_function(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) { |
| 11 | + (void) msg32; |
| 12 | + (void) key32; |
| 13 | + (void) algo16; |
| 14 | + (void) data; |
| 15 | + |
| 16 | + /* On the first run, return 0 to force a second run */ |
| 17 | + if (counter == 0) { |
| 18 | + memset(nonce32, 0, 32); |
| 19 | + return 1; |
| 20 | + } |
| 21 | + /* On the second run, return an overflow to force a third run */ |
| 22 | + if (counter == 1) { |
| 23 | + memset(nonce32, 0xff, 32); |
| 24 | + return 1; |
| 25 | + } |
| 26 | + /* On the next run, return a valid nonce, but flip a coin as to whether or not to fail signing. */ |
| 27 | + memset(nonce32, 1, 32); |
| 28 | + return secp256k1_rand_bits(1); |
| 29 | +} |
| 30 | + |
| 31 | +void test_ecdsa_recovery_api(void) { |
| 32 | + /* Setup contexts that just count errors */ |
| 33 | + secp256k1_context *none = secp256k1_context_create(SECP256K1_CONTEXT_NONE); |
| 34 | + secp256k1_context *sign = secp256k1_context_create(SECP256K1_CONTEXT_SIGN); |
| 35 | + secp256k1_context *vrfy = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY); |
| 36 | + secp256k1_context *both = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); |
| 37 | + secp256k1_pubkey pubkey; |
| 38 | + secp256k1_pubkey recpubkey; |
| 39 | + secp256k1_ecdsa_signature normal_sig; |
| 40 | + secp256k1_ecdsa_recoverable_signature recsig; |
| 41 | + unsigned char privkey[32] = { 1 }; |
| 42 | + unsigned char message[32] = { 2 }; |
| 43 | + int32_t ecount = 0; |
| 44 | + int recid = 0; |
| 45 | + unsigned char sig[74]; |
| 46 | + unsigned char zero_privkey[32] = { 0 }; |
| 47 | + unsigned char over_privkey[32] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 48 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 49 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 50 | + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; |
| 51 | + |
| 52 | + secp256k1_context_set_error_callback(none, counting_illegal_callback_fn, &ecount); |
| 53 | + secp256k1_context_set_error_callback(sign, counting_illegal_callback_fn, &ecount); |
| 54 | + secp256k1_context_set_error_callback(vrfy, counting_illegal_callback_fn, &ecount); |
| 55 | + secp256k1_context_set_error_callback(both, counting_illegal_callback_fn, &ecount); |
| 56 | + secp256k1_context_set_illegal_callback(none, counting_illegal_callback_fn, &ecount); |
| 57 | + secp256k1_context_set_illegal_callback(sign, counting_illegal_callback_fn, &ecount); |
| 58 | + secp256k1_context_set_illegal_callback(vrfy, counting_illegal_callback_fn, &ecount); |
| 59 | + secp256k1_context_set_illegal_callback(both, counting_illegal_callback_fn, &ecount); |
| 60 | + |
| 61 | + /* Construct and verify corresponding public key. */ |
| 62 | + CHECK(secp256k1_ec_seckey_verify(ctx, privkey) == 1); |
| 63 | + CHECK(secp256k1_ec_pubkey_create(ctx, &pubkey, privkey) == 1); |
| 64 | + |
| 65 | + /* Check bad contexts and NULLs for signing */ |
| 66 | + ecount = 0; |
| 67 | + CHECK(secp256k1_ecdsa_sign_recoverable(none, &recsig, message, privkey, NULL, NULL) == 0); |
| 68 | + CHECK(ecount == 1); |
| 69 | + CHECK(secp256k1_ecdsa_sign_recoverable(sign, &recsig, message, privkey, NULL, NULL) == 1); |
| 70 | + CHECK(ecount == 1); |
| 71 | + CHECK(secp256k1_ecdsa_sign_recoverable(vrfy, &recsig, message, privkey, NULL, NULL) == 0); |
| 72 | + CHECK(ecount == 2); |
| 73 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, &recsig, message, privkey, NULL, NULL) == 1); |
| 74 | + CHECK(ecount == 2); |
| 75 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, NULL, message, privkey, NULL, NULL) == 0); |
| 76 | + CHECK(ecount == 3); |
| 77 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, &recsig, NULL, privkey, NULL, NULL) == 0); |
| 78 | + CHECK(ecount == 4); |
| 79 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, &recsig, message, NULL, NULL, NULL) == 0); |
| 80 | + CHECK(ecount == 5); |
| 81 | + /* This will fail or succeed randomly, and in either case will not ARG_CHECK failure */ |
| 82 | + secp256k1_ecdsa_sign_recoverable(both, &recsig, message, privkey, recovery_test_nonce_function, NULL); |
| 83 | + CHECK(ecount == 5); |
| 84 | + /* These will all fail, but not in ARG_CHECK way */ |
| 85 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, &recsig, message, zero_privkey, NULL, NULL) == 0); |
| 86 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, &recsig, message, over_privkey, NULL, NULL) == 0); |
| 87 | + /* This one will succeed. */ |
| 88 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, &recsig, message, privkey, NULL, NULL) == 1); |
| 89 | + CHECK(ecount == 5); |
| 90 | + |
| 91 | + /* Check signing with a goofy nonce function */ |
| 92 | + |
| 93 | + /* Check bad contexts and NULLs for recovery */ |
| 94 | + ecount = 0; |
| 95 | + CHECK(secp256k1_ecdsa_recover(none, &recpubkey, &recsig, message) == 0); |
| 96 | + CHECK(ecount == 1); |
| 97 | + CHECK(secp256k1_ecdsa_recover(sign, &recpubkey, &recsig, message) == 0); |
| 98 | + CHECK(ecount == 2); |
| 99 | + CHECK(secp256k1_ecdsa_recover(vrfy, &recpubkey, &recsig, message) == 1); |
| 100 | + CHECK(ecount == 2); |
| 101 | + CHECK(secp256k1_ecdsa_recover(both, &recpubkey, &recsig, message) == 1); |
| 102 | + CHECK(ecount == 2); |
| 103 | + CHECK(secp256k1_ecdsa_recover(both, NULL, &recsig, message) == 0); |
| 104 | + CHECK(ecount == 3); |
| 105 | + CHECK(secp256k1_ecdsa_recover(both, &recpubkey, NULL, message) == 0); |
| 106 | + CHECK(ecount == 4); |
| 107 | + CHECK(secp256k1_ecdsa_recover(both, &recpubkey, &recsig, NULL) == 0); |
| 108 | + CHECK(ecount == 5); |
| 109 | + |
| 110 | + /* Check NULLs for conversion */ |
| 111 | + CHECK(secp256k1_ecdsa_sign(both, &normal_sig, message, privkey, NULL, NULL) == 1); |
| 112 | + ecount = 0; |
| 113 | + CHECK(secp256k1_ecdsa_recoverable_signature_convert(both, NULL, &recsig) == 0); |
| 114 | + CHECK(ecount == 1); |
| 115 | + CHECK(secp256k1_ecdsa_recoverable_signature_convert(both, &normal_sig, NULL) == 0); |
| 116 | + CHECK(ecount == 2); |
| 117 | + CHECK(secp256k1_ecdsa_recoverable_signature_convert(both, &normal_sig, &recsig) == 1); |
| 118 | + |
| 119 | + /* Check NULLs for de/serialization */ |
| 120 | + CHECK(secp256k1_ecdsa_sign_recoverable(both, &recsig, message, privkey, NULL, NULL) == 1); |
| 121 | + ecount = 0; |
| 122 | + CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(both, NULL, &recid, &recsig) == 0); |
| 123 | + CHECK(ecount == 1); |
| 124 | + CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(both, sig, NULL, &recsig) == 0); |
| 125 | + CHECK(ecount == 2); |
| 126 | + CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(both, sig, &recid, NULL) == 0); |
| 127 | + CHECK(ecount == 3); |
| 128 | + CHECK(secp256k1_ecdsa_recoverable_signature_serialize_compact(both, sig, &recid, &recsig) == 1); |
| 129 | + |
| 130 | + CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(both, NULL, sig, recid) == 0); |
| 131 | + CHECK(ecount == 4); |
| 132 | + CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(both, &recsig, NULL, recid) == 0); |
| 133 | + CHECK(ecount == 5); |
| 134 | + CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(both, &recsig, sig, -1) == 0); |
| 135 | + CHECK(ecount == 6); |
| 136 | + CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(both, &recsig, sig, 5) == 0); |
| 137 | + CHECK(ecount == 7); |
| 138 | + /* overflow in signature will fail but not affect ecount */ |
| 139 | + memcpy(sig, over_privkey, 32); |
| 140 | + CHECK(secp256k1_ecdsa_recoverable_signature_parse_compact(both, &recsig, sig, recid) == 0); |
| 141 | + CHECK(ecount == 7); |
| 142 | + |
| 143 | + /* cleanup */ |
| 144 | + secp256k1_context_destroy(none); |
| 145 | + secp256k1_context_destroy(sign); |
| 146 | + secp256k1_context_destroy(vrfy); |
| 147 | + secp256k1_context_destroy(both); |
| 148 | +} |
| 149 | + |
10 | 150 | void test_ecdsa_recovery_end_to_end(void) {
|
11 | 151 | unsigned char extra[32] = {0x00};
|
12 | 152 | unsigned char privkey[32];
|
@@ -241,6 +381,9 @@ void test_ecdsa_recovery_edge_cases(void) {
|
241 | 381 |
|
242 | 382 | void run_recovery_tests(void) {
|
243 | 383 | int i;
|
| 384 | + for (i = 0; i < count; i++) { |
| 385 | + test_ecdsa_recovery_api(); |
| 386 | + } |
244 | 387 | for (i = 0; i < 64*count; i++) {
|
245 | 388 | test_ecdsa_recovery_end_to_end();
|
246 | 389 | }
|
|
0 commit comments