Apply %restore_fcommon macro for Address Sanitizer
[platform/upstream/nettle.git] / testsuite / curve25519-dh-test.c
1 /* curve25519-dh-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 "curve25519.h"
35
36 static void
37 test_g (const uint8_t *s, const uint8_t *r)
38 {
39   uint8_t p[CURVE25519_SIZE];
40   curve25519_mul_g (p, s);
41   if (!MEMEQ (CURVE25519_SIZE, p, r))
42     {
43       printf ("curve25519_mul_g failure:\ns = ");
44       print_hex (CURVE25519_SIZE, s);
45       printf ("\np = ");
46       print_hex (CURVE25519_SIZE, p);
47       printf (" (bad)\nr = ");
48       print_hex (CURVE25519_SIZE, r);
49       printf (" (expected)\n");
50       abort ();
51     }
52 }
53
54 static void
55 test_a (const uint8_t *s, const uint8_t *b, const uint8_t *r)
56 {
57   uint8_t p[CURVE25519_SIZE];
58   curve25519_mul (p, s, b);
59     
60   if (!MEMEQ (CURVE25519_SIZE, p, r))
61     {
62       printf ("curve25519_mul failure:\ns = ");
63       print_hex (CURVE25519_SIZE, s);
64       printf ("\nb = ");
65       print_hex (CURVE25519_SIZE, b);
66       printf ("\np = ");
67       print_hex (CURVE25519_SIZE, p);
68       printf (" (bad)\nr = ");
69       print_hex (CURVE25519_SIZE, r);
70       printf (" (expected)\n");
71       abort ();
72     }
73 }
74
75 void
76 test_main (void)
77 {
78   /* From draft-turner-thecurve25519function-00 (same also in
79      draft-josefsson-tls-curve25519-05, but the latter uses different
80      endianness). */
81   test_g (H("77076d0a7318a57d3c16c17251b26645"
82             "df4c2f87ebc0992ab177fba51db92c2a"),
83           H("8520f0098930a754748b7ddcb43ef75a"
84             "0dbf3a0d26381af4eba4a98eaa9b4e6a"));
85   test_g (H("5dab087e624a8a4b79e17f8b83800ee6"
86             "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
87           H("de9edb7d7b7dc1b4d35b61c2ece43537"
88             "3f8343c85b78674dadfc7e146f882b4f"));
89
90   test_a (H("77076d0a7318a57d3c16c17251b26645"
91             "df4c2f87ebc0992ab177fba51db92c2a"),
92           H("de9edb7d7b7dc1b4d35b61c2ece43537"
93             "3f8343c85b78674dadfc7e146f882b4f"),
94           H("4a5d9d5ba4ce2de1728e3bf480350f25"
95             "e07e21c947d19e3376f09b3c1e161742"));
96
97   test_a (H("5dab087e624a8a4b79e17f8b83800ee6"
98             "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
99           H("8520f0098930a754748b7ddcb43ef75a"
100             "0dbf3a0d26381af4eba4a98eaa9b4e6a"),
101           H("4a5d9d5ba4ce2de1728e3bf480350f25"
102             "e07e21c947d19e3376f09b3c1e161742"));
103 }