Skip to content

Commit 6e05287

Browse files
committed
Do signature recovery/verification with 4 possible recid case
1 parent 666d3b5 commit 6e05287

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/tests.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -939,16 +939,17 @@ void run_ecdsa_end_to_end(void) {
939939
}
940940
}
941941

942-
void test_ecdsa_infinity(void) {
942+
/* Tests several edge cases. */
943+
void test_ecdsa_edge_cases(void) {
943944
const unsigned char msg32[32] = {
944945
'T', 'h', 'i', 's', ' ', 'i', 's', ' ',
945946
'a', ' ', 'v', 'e', 'r', 'y', ' ', 's',
946947
'e', 'c', 'r', 'e', 't', ' ', 'm', 'e',
947948
's', 's', 'a', 'g', 'e', '.', '.', '.'
948949
};
949950
const unsigned char sig64[64] = {
950-
// Generated by signing the above message with nonce 'This is the nonce we will use...'
951-
// and secret key 0 (which is not valid), resulting in recid 0.
951+
/* Generated by signing the above message with nonce 'This is the nonce we will use...'
952+
* and secret key 0 (which is not valid), resulting in recid 0. */
952953
0x67, 0xCB, 0x28, 0x5F, 0x9C, 0xD1, 0x94, 0xE8,
953954
0x40, 0xD6, 0x29, 0x39, 0x7A, 0xF5, 0x56, 0x96,
954955
0x62, 0xFD, 0xE4, 0x46, 0x49, 0x99, 0x59, 0x63,
@@ -964,10 +965,32 @@ void test_ecdsa_infinity(void) {
964965
CHECK(secp256k1_ecdsa_recover_compact(msg32, 32, sig64, pubkey, &pubkeylen, 0, 1));
965966
CHECK(!secp256k1_ecdsa_recover_compact(msg32, 32, sig64, pubkey, &pubkeylen, 0, 2));
966967
CHECK(!secp256k1_ecdsa_recover_compact(msg32, 32, sig64, pubkey, &pubkeylen, 0, 3));
968+
969+
/* signature (r,s) = (4,4), which can be recovered with all 4 recids. */
970+
const unsigned char sigb64[64] = {
971+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
972+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
973+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
974+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
975+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
976+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
977+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
978+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
979+
};
980+
unsigned char pubkeyb[33];
981+
int pubkeyblen = 33;
982+
for (int recid = 0; recid < 4; recid++) {
983+
unsigned char sigbder[8] = {0x30, 0x06, 0x02, 0x01, 0x04, 0x02, 0x01, 0x04};
984+
CHECK(secp256k1_ecdsa_recover_compact(msg32, 32, sigb64, pubkeyb, &pubkeyblen, 1, recid));
985+
CHECK(secp256k1_ecdsa_verify(msg32, 32, sigbder, sizeof(sigbder), pubkeyb, pubkeyblen) == 1);
986+
/* Damage signature. */
987+
sigbder[7]++;
988+
CHECK(secp256k1_ecdsa_verify(msg32, 32, sigbder, sizeof(sigbder), pubkeyb, pubkeyblen) == 0);
989+
}
967990
}
968991

969-
void run_ecdsa_infinity(void) {
970-
test_ecdsa_infinity();
992+
void run_ecdsa_edge_cases(void) {
993+
test_ecdsa_edge_cases();
971994
}
972995

973996
#ifdef ENABLE_OPENSSL_TESTS
@@ -1071,7 +1094,7 @@ int main(int argc, char **argv) {
10711094
/* ecdsa tests */
10721095
run_ecdsa_sign_verify();
10731096
run_ecdsa_end_to_end();
1074-
run_ecdsa_infinity();
1097+
run_ecdsa_edge_cases();
10751098
#ifdef ENABLE_OPENSSL_TESTS
10761099
run_ecdsa_openssl();
10771100
#endif

0 commit comments

Comments
 (0)