1 #include "testutils.h"
2
3 static void
test_ecdsa(const struct ecc_curve * ecc,const char * xs,const char * ys,struct tstring * h,const char * r,const char * s)4 test_ecdsa (const struct ecc_curve *ecc,
5 /* Public key */
6 const char *xs, const char *ys,
7 /* Hash */
8 struct tstring *h,
9 /* Valid signature */
10 const char *r, const char *s)
11 {
12 struct ecc_point pub;
13 struct dsa_signature signature;
14 mpz_t x, y;
15
16 ecc_point_init (&pub, ecc);
17 dsa_signature_init (&signature);
18
19 mpz_init_set_str (x, xs, 16);
20 mpz_init_set_str (y, ys, 16);
21
22 if (!ecc_point_set (&pub, x, y))
23 die ("ecc_point_set failed.\n");
24
25 mpz_set_str (signature.r, r, 16);
26 mpz_set_str (signature.s, s, 16);
27
28 if (!ecdsa_verify (&pub, h->length, h->data, &signature))
29 {
30 fprintf (stderr, "ecdsa_verify failed with valid signature.\n");
31 fail:
32 fprintf (stderr, "bit_size = %u\nx = ", ecc->p.bit_size);
33 mpz_out_str (stderr, 16, x);
34 fprintf (stderr, "\ny = ");
35 mpz_out_str (stderr, 16, y);
36 fprintf (stderr, "\ndigest ");
37 print_hex (h->length, h->data);
38 fprintf (stderr, "r = ");
39 mpz_out_str (stderr, 16, signature.r);
40 fprintf (stderr, "\ns = ");
41 mpz_out_str (stderr, 16, signature.s);
42 fprintf (stderr, "\n");
43 abort();
44 }
45
46 mpz_combit (signature.r, ecc->p.bit_size / 3);
47 if (ecdsa_verify (&pub, h->length, h->data, &signature))
48 {
49 fprintf (stderr, "ecdsa_verify unexpectedly succeeded with invalid signature.\n");
50 goto fail;
51 }
52 mpz_combit (signature.r, ecc->p.bit_size / 3);
53
54 mpz_combit (signature.s, 4*ecc->p.bit_size / 5);
55 if (ecdsa_verify (&pub, h->length, h->data, &signature))
56 {
57 fprintf (stderr, "ecdsa_verify unexpectedly succeeded with invalid signature.\n");
58 goto fail;
59 }
60 mpz_combit (signature.s, 4*ecc->p.bit_size / 5);
61
62 h->data[2*h->length / 3] ^= 0x40;
63 if (ecdsa_verify (&pub, h->length, h->data, &signature))
64 {
65 fprintf (stderr, "ecdsa_verify unexpectedly succeeded with invalid signature.\n");
66 goto fail;
67 }
68 h->data[2*h->length / 3] ^= 0x40;
69 if (!ecdsa_verify (&pub, h->length, h->data, &signature))
70 {
71 fprintf (stderr, "ecdsa_verify failed, internal testsuite error.\n");
72 goto fail;
73 }
74
75 ecc_point_clear (&pub);
76 dsa_signature_clear (&signature);
77 mpz_clear (x);
78 mpz_clear (y);
79 }
80
81 void
test_main(void)82 test_main (void)
83 {
84 /* From RFC 4754 */
85 test_ecdsa (&_nettle_secp_256r1,
86 "2442A5CC 0ECD015F A3CA31DC 8E2BBC70"
87 "BF42D60C BCA20085 E0822CB0 4235E970", /* x */
88
89 "6FC98BD7 E50211A4 A27102FA 3549DF79"
90 "EBCB4BF2 46B80945 CDDFE7D5 09BBFD7D", /* y */
91
92 SHEX("BA7816BF 8F01CFEA 414140DE 5DAE2223"
93 "B00361A3 96177A9C B410FF61 F20015AD"), /* h */
94
95 "CB28E099 9B9C7715 FD0A80D8 E47A7707"
96 "9716CBBF 917DD72E 97566EA1 C066957C", /* r */
97 "86FA3BB4 E26CAD5B F90B7F81 899256CE"
98 "7594BB1E A0C89212 748BFF3B 3D5B0315"); /* s */
99
100 test_ecdsa (&_nettle_secp_384r1,
101 "96281BF8 DD5E0525 CA049C04 8D345D30"
102 "82968D10 FEDF5C5A CA0C64E6 465A97EA"
103 "5CE10C9D FEC21797 41571072 1F437922", /* x */
104
105 "447688BA 94708EB6 E2E4D59F 6AB6D7ED"
106 "FF9301D2 49FE49C3 3096655F 5D502FAD"
107 "3D383B91 C5E7EDAA 2B714CC9 9D5743CA", /* y */
108
109 SHEX("CB00753F 45A35E8B B5A03D69 9AC65007"
110 "272C32AB 0EDED163 1A8B605A 43FF5BED"
111 "8086072B A1E7CC23 58BAECA1 34C825A7"), /* h */
112
113 "FB017B91 4E291494 32D8BAC2 9A514640"
114 "B46F53DD AB2C6994 8084E293 0F1C8F7E"
115 "08E07C9C 63F2D21A 07DCB56A 6AF56EB3", /* r */
116 "B263A130 5E057F98 4D38726A 1B468741"
117 "09F417BC A112674C 528262A4 0A629AF1"
118 "CBB9F516 CE0FA7D2 FF630863 A00E8B9F"); /* s*/
119
120 test_ecdsa (&_nettle_secp_521r1,
121 "0151518F 1AF0F563 517EDD54 85190DF9"
122 "5A4BF57B 5CBA4CF2 A9A3F647 4725A35F"
123 "7AFE0A6D DEB8BEDB CD6A197E 592D4018"
124 "8901CECD 650699C9 B5E456AE A5ADD190"
125 "52A8", /* x */
126
127 "006F3B14 2EA1BFFF 7E2837AD 44C9E4FF"
128 "6D2D34C7 3184BBAD 90026DD5 E6E85317"
129 "D9DF45CA D7803C6C 20035B2F 3FF63AFF"
130 "4E1BA64D 1C077577 DA3F4286 C58F0AEA"
131 "E643", /* y */
132
133 SHEX("DDAF35A1 93617ABA CC417349 AE204131"
134 "12E6FA4E 89A97EA2 0A9EEEE6 4B55D39A"
135 "2192992A 274FC1A8 36BA3C23 A3FEEBBD"
136 "454D4423 643CE80E 2A9AC94F A54CA49F"), /* h */
137
138 "0154FD38 36AF92D0 DCA57DD5 341D3053"
139 "988534FD E8318FC6 AAAAB68E 2E6F4339"
140 "B19F2F28 1A7E0B22 C269D93C F8794A92"
141 "78880ED7 DBB8D936 2CAEACEE 54432055"
142 "2251", /* r */
143 "017705A7 030290D1 CEB605A9 A1BB03FF"
144 "9CDD521E 87A696EC 926C8C10 C8362DF4"
145 "97536710 1F67D1CF 9BCCBF2F 3D239534"
146 "FA509E70 AAC851AE 01AAC68D 62F86647"
147 "2660"); /* s */
148
149 test_ecdsa (&_nettle_curve25519,
150 /* Public key corresponding to the key in ecdsa-sign-test */
151 "59f8f317fd5f4e82 c02f8d4dec665fe1"
152 "230f83b8572638e1 b2ac34a30028e24d", /* x */
153 "1902a72dc1a6525a 811b9c1845978d56"
154 "fd97dce5e278ebdd ec695349d7e41498", /* y */
155 SHEX("e99df2a098c3c590 ea1e1db6d9547339"
156 "ae760d5331496119 5d967fd881e3b0f5"), /* h */
157 " 515c3a485f57432 0daf3353a0d08110"
158 "64157c556296de09 4132f74865961b37", /* r */
159 " 78f23367291b01 3fc430fb09322d95"
160 "4384723649868d8e 88effc7ac8b141d7"); /* s */
161 }
162