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
test_g(const uint8_t * s,const uint8_t * r)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
test_a(const uint8_t * s,const uint8_t * b,const uint8_t * r)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
test_main(void)76 test_main (void)
77 {
78   /* From RFC 7748. */
79   test_g (H("77076d0a7318a57d3c16c17251b26645"
80 	    "df4c2f87ebc0992ab177fba51db92c2a"),
81 	  H("8520f0098930a754748b7ddcb43ef75a"
82 	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"));
83   test_g (H("5dab087e624a8a4b79e17f8b83800ee6"
84 	    "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
85 	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
86 	    "3f8343c85b78674dadfc7e146f882b4f"));
87 
88   test_a (H("77076d0a7318a57d3c16c17251b26645"
89 	    "df4c2f87ebc0992ab177fba51db92c2a"),
90 	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
91 	    "3f8343c85b78674dadfc7e146f882b4f"),
92 	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
93 	    "e07e21c947d19e3376f09b3c1e161742"));
94 
95   test_a (H("5dab087e624a8a4b79e17f8b83800ee6"
96 	    "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
97 	  H("8520f0098930a754748b7ddcb43ef75a"
98 	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"),
99 	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
100 	    "e07e21c947d19e3376f09b3c1e161742"));
101 
102   /* Check that the least significant three bits (first octet) of the
103      scalar are ignored by mul_g. */
104   test_g (H("70076d0a7318a57d3c16c17251b26645"
105 	    "df4c2f87ebc0992ab177fba51db92c2a"),
106 	  H("8520f0098930a754748b7ddcb43ef75a"
107 	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"));
108   /* Check that the most significant two bits (last octet) of the
109      scalar are ignored by mul_g. */
110   test_g (H("5dab087e624a8a4b79e17f8b83800ee6"
111 	    "6f3bb1292618b6fd1c2f8b27ff88e02b"),
112 	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
113 	    "3f8343c85b78674dadfc7e146f882b4f"));
114 
115   /* Check that the least significant three bits (first octet) of the
116      scalar are ignored by mul_a. */
117   test_a (H("5aab087e624a8a4b79e17f8b83800ee6"
118 	    "6f3bb1292618b6fd1c2f8b27ff88e0eb"),
119 	  H("8520f0098930a754748b7ddcb43ef75a"
120 	    "0dbf3a0d26381af4eba4a98eaa9b4e6a"),
121 	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
122 	    "e07e21c947d19e3376f09b3c1e161742"));
123 
124   /* Check that the most significant two bits (last octet) of the
125      scalar are ignored by mul_g. */
126   test_a (H("77076d0a7318a57d3c16c17251b26645"
127 	    "df4c2f87ebc0992ab177fba51db92cea"),
128 	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
129 	    "3f8343c85b78674dadfc7e146f882b4f"),
130 	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
131 	    "e07e21c947d19e3376f09b3c1e161742"));
132 
133   /* Check that the most significant bit (last octet) of the x
134      coordinate is ignored. */
135   test_a (H("77076d0a7318a57d3c16c17251b26645"
136 	    "df4c2f87ebc0992ab177fba51db92c2a"),
137 	  H("de9edb7d7b7dc1b4d35b61c2ece43537"
138 	    "3f8343c85b78674dadfc7e146f882bcf"),
139 	  H("4a5d9d5ba4ce2de1728e3bf480350f25"
140 	    "e07e21c947d19e3376f09b3c1e161742"));
141 }
142