Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / testsuite / eddsa-verify-test.c
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
36 static void
37 test_eddsa (const struct ecc_curve *ecc,
38             const struct nettle_hash *H,
39             const uint8_t *pub,
40             const struct tstring *msg,
41             const uint8_t *signature)
42 {
43   mp_limb_t *A = xalloc_limbs (ecc_size_a (ecc));
44   mp_limb_t *scratch = xalloc_limbs (_eddsa_verify_itch (ecc));
45   size_t nbytes = 1 + ecc->p.bit_size / 8;
46   uint8_t *cmsg = xalloc (msg->length);
47   uint8_t *csignature = xalloc (2*nbytes);
48   void *ctx = xalloc (H->context_size);
49
50   if (!_eddsa_decompress (ecc, A, pub, scratch))
51     die ("Invalid eddsa public key.\n");
52
53   memcpy (csignature, signature, 2*nbytes);
54   if (!_eddsa_verify (ecc, H, pub, A, ctx,
55                       msg->length, msg->data, csignature, scratch))
56     {
57       fprintf (stderr, "eddsa_verify failed with valid signature.\n");
58     fail:
59       fprintf (stderr, "bit_size = %u\npub = ", ecc->p.bit_size);
60       print_hex (nbytes, pub);
61       fprintf (stderr, "\nmsg = ");
62       tstring_print_hex (msg);
63       fprintf (stderr, "\nsign = ");
64       print_hex (2*nbytes, csignature);
65       fprintf (stderr, "\n");
66       abort();
67     }
68
69   memcpy (csignature, signature, 2*nbytes);
70   csignature[nbytes/3] ^= 0x40;
71   if (_eddsa_verify (ecc, H, pub, A, ctx,
72                      msg->length, msg->data, csignature, scratch))
73     {
74       fprintf (stderr,
75                "ecdsa_verify unexpectedly succeeded with invalid signature r.\n");
76       goto fail;
77     }
78
79   memcpy (csignature, signature, 2*nbytes);
80   csignature[5*nbytes/3] ^= 0x8;
81
82   if (_eddsa_verify (ecc, H, pub, A, ctx,
83                      msg->length, msg->data, csignature, scratch))
84     {
85       fprintf (stderr,
86                "ecdsa_verify unexpectedly succeeded with invalid signature s.\n");
87       goto fail;
88     }
89
90   if (msg->length == 0)
91     {
92       if (_eddsa_verify  (ecc, H, pub, A, ctx,
93                           3, "foo", signature, scratch))
94         {
95           fprintf (stderr,
96                    "ecdsa_verify unexpectedly succeeded with different message.\n");
97           goto fail;
98         }
99     }
100   else
101     {
102       if (_eddsa_verify  (ecc, H, pub, A, ctx,
103                           msg->length - 1, msg->data,
104                           signature, scratch))
105         {
106           fprintf (stderr,
107                    "ecdsa_verify unexpectedly succeeded with truncated message.\n");
108           goto fail;
109         }
110       memcpy (cmsg, msg->data, msg->length);
111       cmsg[2*msg->length / 3] ^= 0x20;
112       if (_eddsa_verify  (ecc, H, pub, A, ctx,
113                           msg->length, cmsg, signature, scratch))
114         {
115           fprintf (stderr,
116                    "ecdsa_verify unexpectedly succeeded with modified message.\n");
117           goto fail;
118         }
119     }
120   free (A);
121   free (scratch);
122   free (cmsg);
123   free (csignature);
124   free (ctx);
125 }
126
127 void
128 test_main (void)
129 {
130   test_eddsa (&_nettle_curve25519, &nettle_sha512,
131               H("d75a980182b10ab7 d54bfed3c964073a"
132                 "0ee172f3daa62325 af021a68f707511a"),
133               SHEX(""),
134               H("e5564300c360ac72 9086e2cc806e828a"
135                 "84877f1eb8e5d974 d873e06522490155"
136                 "5fb8821590a33bac c61e39701cf9b46b"
137                 "d25bf5f0595bbe24 655141438e7a100b"));
138   test_eddsa (&_nettle_curve25519, &nettle_sha512,
139               H("3d4017c3e843895a 92b70aa74d1b7ebc"
140                 "9c982ccf2ec4968c c0cd55f12af4660c"),
141               SHEX("72"),
142               H("92a009a9f0d4cab8 720e820b5f642540"
143                 "a2b27b5416503f8f b3762223ebdb69da"
144                 "085ac1e43e15996e 458f3613d0f11d8c"
145                 "387b2eaeb4302aee b00d291612bb0c00"));
146   test_eddsa (&_nettle_curve25519, &nettle_sha512,
147               H("1ed506485b09a645 0be7c9337d9fe87e"
148                 "f99c96f8bd11cd63 1ca160d0fd73067e"),
149               SHEX("fbed2a7df418ec0e 8036312ec239fcee"
150                    "6ef97dc8c2df1f2e 14adee287808b788"
151                    "a6072143b851d975 c8e8a0299df846b1"
152                    "9113e38cee83da71 ea8e9bd6f57bdcd3"
153                    "557523f4feb616ca a595aea01eb0b3d4"
154                    "90b99b525ea4fbb9 258bc7fbb0deea8f"
155                    "568cb2"),
156               H("cbef65b6f3fd5809 69fc3340cfae4f7c"
157                 "99df1340cce54626 183144ef46887163"
158                 "4b0a5c0033534108 e1c67c0dc99d3014"
159                 "f01084e98c95e101 4b309b1dbb2e6704"));
160 }