cfb8: Fix decrypt path
[gd/nettle] / testsuite / eddsa-sign-test.c
1 /* eddsa-sign-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
37 static void
38 test_eddsa_sign (const struct ecc_curve *ecc,
39                  const struct nettle_hash *H,
40                  const struct tstring *public,
41                  const struct tstring *private,
42                  const struct tstring *msg,
43                  const struct tstring *ref)
44 {
45   mp_limb_t *scratch = xalloc_limbs (_eddsa_sign_itch (ecc));
46   size_t nbytes = 1 + ecc->p.bit_size / 8;
47   uint8_t *signature = xalloc (2*nbytes);
48   void *ctx = xalloc (H->context_size);
49   uint8_t *public_out = xalloc (nbytes);
50   uint8_t *digest = xalloc (2*nbytes);
51   const uint8_t *k1 = digest + nbytes;
52   mp_limb_t *k2 = xalloc_limbs (ecc->p.size);
53
54   ASSERT (public->length == nbytes);
55   ASSERT (private->length == nbytes);
56   ASSERT (ref->length == 2*nbytes);
57
58   _eddsa_expand_key (ecc, H, ctx, private->data,
59                      digest, k2);
60   _eddsa_public_key (ecc, k2, public_out, scratch);
61
62   if (!MEMEQ (nbytes, public_out, public->data))
63     {
64       fprintf (stderr, "Bad public key from _eddsa_expand_key + _eddsa_public_key.\n");
65       fprintf (stderr, "got:");
66       print_hex (nbytes, public_out);
67       fprintf (stderr, "\nref:");
68       tstring_print_hex (public);
69       fprintf (stderr, "\n");
70       abort ();
71     }
72   H->update (ctx, nbytes, k1);
73   
74   _eddsa_sign (ecc, H, public->data, ctx, k2,
75                msg->length, msg->data, signature, scratch);
76
77   if (!MEMEQ (2*nbytes, signature, ref->data))
78     {
79       fprintf (stderr, "Bad _eddsa_sign output.\n");
80       fprintf (stderr, "Public key:");
81       tstring_print_hex (public);
82       fprintf (stderr, "\nPrivate key:");
83       tstring_print_hex (private);
84       fprintf (stderr, "\nk2:");
85       mpn_out_str (stderr, 16, k2, ecc->p.size);
86       fprintf (stderr, "\nMessage (length %u):", (unsigned) msg->length);
87       tstring_print_hex (msg);      
88       fprintf (stderr, "\ngot:");
89       print_hex (2*nbytes, signature);
90       fprintf (stderr, "\nref:");
91       tstring_print_hex (ref);
92       fprintf (stderr, "\n");
93       abort ();
94     }
95   
96   free (scratch);
97   free (signature);
98   free (ctx);
99   free (digest);
100   free (k2);
101   free (public_out);
102 }
103
104 void test_main (void)
105 {
106   /* Based on a few of the test vectors at
107      http://ed25519.cr.yp.to/python/sign.input */
108   test_eddsa_sign (&_nettle_curve25519, &nettle_sha512,
109                    SHEX("d75a980182b10ab7 d54bfed3c964073a"
110                         "0ee172f3daa62325 af021a68f707511a"),
111                    SHEX("9d61b19deffd5a60 ba844af492ec2cc4"
112                         "4449c5697b326919 703bac031cae7f60"),
113                    SHEX(""),
114                    SHEX("e5564300c360ac72 9086e2cc806e828a"
115                         "84877f1eb8e5d974 d873e06522490155"
116                         "5fb8821590a33bac c61e39701cf9b46b"
117                         "d25bf5f0595bbe24 655141438e7a100b"));
118   test_eddsa_sign (&_nettle_curve25519, &nettle_sha512,
119                    SHEX("3d4017c3e843895a 92b70aa74d1b7ebc"
120                         "9c982ccf2ec4968c c0cd55f12af4660c"),
121                    SHEX("4ccd089b28ff96da 9db6c346ec114e0f"
122                         "5b8a319f35aba624 da8cf6ed4fb8a6fb"),
123                    SHEX("72"),
124                    SHEX("92a009a9f0d4cab8 720e820b5f642540"
125                         "a2b27b5416503f8f b3762223ebdb69da"
126                         "085ac1e43e15996e 458f3613d0f11d8c"
127                         "387b2eaeb4302aee b00d291612bb0c00"));
128   test_eddsa_sign (&_nettle_curve25519, &nettle_sha512,
129                    SHEX("1ed506485b09a645 0be7c9337d9fe87e"
130                         "f99c96f8bd11cd63 1ca160d0fd73067e"),
131                    SHEX("f215d34fe2d757cf f9cf5c05430994de"
132                         "587987ce45cb0459 f61ec6c825c62259"),
133                    SHEX("fbed2a7df418ec0e 8036312ec239fcee"
134                         "6ef97dc8c2df1f2e 14adee287808b788"
135                         "a6072143b851d975 c8e8a0299df846b1"
136                         "9113e38cee83da71 ea8e9bd6f57bdcd3"
137                         "557523f4feb616ca a595aea01eb0b3d4"
138                         "90b99b525ea4fbb9 258bc7fbb0deea8f"
139                         "568cb2"),
140                    SHEX("cbef65b6f3fd5809 69fc3340cfae4f7c"
141                         "99df1340cce54626 183144ef46887163"
142                         "4b0a5c0033534108 e1c67c0dc99d3014"
143                         "f01084e98c95e101 4b309b1dbb2e6704"));
144 }