1 /* eddsa-verify-test.c
2 
3    Copyright (C) 2014 Niels Möller
4 
5    This file is part of GNU Nettle.
6 
7    GNU Nettle is free software: you can redistribute it and/or
8    modify it under the terms of either:
9 
10      * the GNU Lesser General Public License as published by the Free
11        Software Foundation; either version 3 of the License, or (at your
12        option) any later version.
13 
14    or
15 
16      * the GNU General Public License as published by the Free
17        Software Foundation; either version 2 of the License, or (at your
18        option) any later version.
19 
20    or both in parallel, as here.
21 
22    GNU Nettle is distributed in the hope that it will be useful,
23    but WITHOUT ANY WARRANTY; without even the implied warranty of
24    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25    General Public License for more details.
26 
27    You should have received copies of the GNU General Public License and
28    the GNU Lesser General Public License along with this program.  If
29    not, see http://www.gnu.org/licenses/.
30 */
31 
32 #include "testutils.h"
33 
34 #include "eddsa.h"
35 #include "eddsa-internal.h"
36 #include "sha3.h"
37 
38 static void
test_eddsa(const struct ecc_curve * ecc,const struct ecc_eddsa * eddsa,void * ctx,const uint8_t * pub,const struct tstring * msg,const uint8_t * signature)39 test_eddsa (const struct ecc_curve *ecc,
40 	    const struct ecc_eddsa *eddsa,
41 	    void *ctx,
42 	    const uint8_t *pub,
43 	    const struct tstring *msg,
44 	    const uint8_t *signature)
45 {
46   mp_limb_t *A = xalloc_limbs (ecc_size_a (ecc));
47   mp_limb_t *scratch = xalloc_limbs (_eddsa_verify_itch (ecc));
48   size_t nbytes = 1 + ecc->p.bit_size / 8;
49   uint8_t *cmsg = xalloc (msg->length);
50   uint8_t *csignature = xalloc (2*nbytes);
51 
52   if (!_eddsa_decompress (ecc, A, pub, scratch))
53     die ("Invalid eddsa public key.\n");
54 
55   memcpy (csignature, signature, 2*nbytes);
56   if (!_eddsa_verify (ecc, eddsa, pub, A, ctx,
57 		      msg->length, msg->data, csignature, scratch))
58     {
59       fprintf (stderr, "eddsa_verify failed with valid signature.\n");
60     fail:
61       fprintf (stderr, "bit_size = %u\npub = ", ecc->p.bit_size);
62       print_hex (nbytes, pub);
63       fprintf (stderr, "\nmsg = ");
64       tstring_print_hex (msg);
65       fprintf (stderr, "\nsign = ");
66       print_hex (2*nbytes, csignature);
67       fprintf (stderr, "\n");
68       abort();
69     }
70 
71   memcpy (csignature, signature, 2*nbytes);
72   csignature[nbytes/3] ^= 0x40;
73   if (_eddsa_verify (ecc, eddsa, pub, A, ctx,
74 		     msg->length, msg->data, csignature, scratch))
75     {
76       fprintf (stderr,
77 	       "ecdsa_verify unexpectedly succeeded with invalid signature r.\n");
78       goto fail;
79     }
80 
81   memcpy (csignature, signature, 2*nbytes);
82   csignature[5*nbytes/3] ^= 0x8;
83 
84   if (_eddsa_verify (ecc, eddsa, pub, A, ctx,
85 		     msg->length, msg->data, csignature, scratch))
86     {
87       fprintf (stderr,
88 	       "ecdsa_verify unexpectedly succeeded with invalid signature s.\n");
89       goto fail;
90     }
91 
92   if (msg->length == 0)
93     {
94       if (_eddsa_verify  (ecc, eddsa, pub, A, ctx,
95 			  LDATA("foo"), signature, scratch))
96 	{
97 	  fprintf (stderr,
98 		   "ecdsa_verify unexpectedly succeeded with different message.\n");
99 	  goto fail;
100 	}
101     }
102   else
103     {
104       if (_eddsa_verify  (ecc, eddsa, pub, A, ctx,
105 			  msg->length - 1, msg->data,
106 			  signature, scratch))
107 	{
108 	  fprintf (stderr,
109 		   "ecdsa_verify unexpectedly succeeded with truncated message.\n");
110 	  goto fail;
111 	}
112       memcpy (cmsg, msg->data, msg->length);
113       cmsg[2*msg->length / 3] ^= 0x20;
114       if (_eddsa_verify  (ecc, eddsa, pub, A, ctx,
115 			  msg->length, cmsg, signature, scratch))
116 	{
117 	  fprintf (stderr,
118 		   "ecdsa_verify unexpectedly succeeded with modified message.\n");
119 	  goto fail;
120 	}
121     }
122   free (A);
123   free (scratch);
124   free (cmsg);
125   free (csignature);
126 }
127 
128 static void
test_ed25519(const uint8_t * pub,const struct tstring * msg,const uint8_t * signature)129 test_ed25519 (const uint8_t *pub,
130 	      const struct tstring *msg,
131 	      const uint8_t *signature)
132 {
133   struct sha512_ctx ctx;
134 
135   sha512_init (&ctx);
136   test_eddsa (&_nettle_curve25519, &_nettle_ed25519_sha512, &ctx,
137 	      pub, msg, signature);
138 }
139 
140 static void
test_ed448(const uint8_t * pub,const struct tstring * msg,const uint8_t * signature)141 test_ed448 (const uint8_t *pub,
142 	    const struct tstring *msg,
143 	    const uint8_t *signature)
144 {
145   struct sha3_256_ctx ctx;
146 
147   sha3_256_init (&ctx);
148   test_eddsa (&_nettle_curve448, &_nettle_ed448_shake256, &ctx,
149 	      pub, msg, signature);
150 }
151 
152 void
test_main(void)153 test_main (void)
154 {
155   test_ed25519 (H("d75a980182b10ab7 d54bfed3c964073a"
156 		  "0ee172f3daa62325 af021a68f707511a"),
157 		SHEX(""),
158 		H("e5564300c360ac72 9086e2cc806e828a"
159 		  "84877f1eb8e5d974 d873e06522490155"
160 		  "5fb8821590a33bac c61e39701cf9b46b"
161 		  "d25bf5f0595bbe24 655141438e7a100b"));
162   test_ed25519 (H("3d4017c3e843895a 92b70aa74d1b7ebc"
163 		  "9c982ccf2ec4968c c0cd55f12af4660c"),
164 		SHEX("72"),
165 		H("92a009a9f0d4cab8 720e820b5f642540"
166 		  "a2b27b5416503f8f b3762223ebdb69da"
167 		  "085ac1e43e15996e 458f3613d0f11d8c"
168 		  "387b2eaeb4302aee b00d291612bb0c00"));
169   test_ed25519 (H("1ed506485b09a645 0be7c9337d9fe87e"
170 		  "f99c96f8bd11cd63 1ca160d0fd73067e"),
171 		SHEX("fbed2a7df418ec0e 8036312ec239fcee"
172 		     "6ef97dc8c2df1f2e 14adee287808b788"
173 		     "a6072143b851d975 c8e8a0299df846b1"
174 		     "9113e38cee83da71 ea8e9bd6f57bdcd3"
175 		     "557523f4feb616ca a595aea01eb0b3d4"
176 		     "90b99b525ea4fbb9 258bc7fbb0deea8f"
177 		     "568cb2"),
178 		H("cbef65b6f3fd5809 69fc3340cfae4f7c"
179 		  "99df1340cce54626 183144ef46887163"
180 		  "4b0a5c0033534108 e1c67c0dc99d3014"
181 		  "f01084e98c95e101 4b309b1dbb2e6704"));
182   /* Based on a few of the test vectors from RFC 8032 */
183   test_ed448 (H("5fd7449b59b461fd 2ce787ec616ad46a"
184 		"1da1342485a70e1f 8a0ea75d80e96778"
185 		"edf124769b46c706 1bd6783df1e50f6c"
186 		"d1fa1abeafe82561 80"),
187 	      SHEX(""),
188 	      H("533a37f6bbe45725 1f023c0d88f976ae"
189 		"2dfb504a843e34d2 074fd823d41a591f"
190 		"2b233f034f628281 f2fd7a22ddd47d78"
191 		"28c59bd0a21bfd39 80ff0d2028d4b18a"
192 		"9df63e006c5d1c2d 345b925d8dc00b41"
193 		"04852db99ac5c7cd da8530a113a0f4db"
194 		"b61149f05a736326 8c71d95808ff2e65"
195 		"2600"));
196   test_ed448 (H("43ba28f430cdff45 6ae531545f7ecd0a"
197 		"c834a55d9358c037 2bfa0c6c6798c086"
198 		"6aea01eb00742802 b8438ea4cb82169c"
199 		"235160627b4c3a94 80"),
200 	      SHEX("03"),
201 	      H("26b8f91727bd6289 7af15e41eb43c377"
202 		"efb9c610d48f2335 cb0bd0087810f435"
203 		"2541b143c4b981b7 e18f62de8ccdf633"
204 		"fc1bf037ab7cd779 805e0dbcc0aae1cb"
205 		"cee1afb2e027df36 bc04dcecbf154336"
206 		"c19f0af7e0a64729 05e799f1953d2a0f"
207 		"f3348ab21aa4adaf d1d234441cf807c0"
208 		"3a00"));
209   test_ed448 (H("df9705f58edbab80 2c7f8363cfe5560a"
210 		"b1c6132c20a9f1dd 163483a26f8ac53a"
211 		"39d6808bf4a1dfbd 261b099bb03b3fb5"
212 		"0906cb28bd8a081f 00"),
213 	      SHEX("bd0f6a3747cd561b dddf4640a332461a"
214 		   "4a30a12a434cd0bf 40d766d9c6d458e5"
215 		   "512204a30c17d1f5 0b5079631f64eb31"
216 		   "12182da300583546 1113718d1a5ef944"),
217 	      H("554bc2480860b49e ab8532d2a533b7d5"
218 		"78ef473eeb58c98b b2d0e1ce488a98b1"
219 		"8dfde9b9b90775e6 7f47d4a1c3482058"
220 		"efc9f40d2ca033a0 801b63d45b3b722e"
221 		"f552bad3b4ccb667 da350192b61c508c"
222 		"f7b6b5adadc2c8d9 a446ef003fb05cba"
223 		"5f30e88e36ec2703 b349ca229c267083"
224 		"3900"));
225 }
226