iPXE
x25519_test.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26/** @file
27 *
28 * X25519 key exchange test
29 *
30 * Full key exchange test vectors are taken from RFC7748.
31 *
32 */
33
34/* Forcibly enable assertions */
35#undef NDEBUG
36
37#include <stdint.h>
38#include <string.h>
39#include <ipxe/x25519.h>
40#include <ipxe/test.h>
41#include "exchange_test.h"
42
43/** Define inline multiplicand */
44#define MULTIPLICAND(...) { __VA_ARGS__ }
45
46/** Define inline multiplier */
47#define MULTIPLIER(...) { __VA_ARGS__ }
48
49/** Define inline invertend */
50#define INVERTEND(...) { __VA_ARGS__ }
51
52/** Define inline base point */
53#define BASE(...) { __VA_ARGS__ }
54
55/** Define inline scalar multiple */
56#define SCALAR(...) { __VA_ARGS__ }
57
58/** Define inline expected result */
59#define EXPECTED(...) { __VA_ARGS__ }
60
61/** An X25519 multiplication self-test */
63 /** Multiplicand */
64 const void *multiplicand;
65 /** Length of multiplicand */
67 /** Multiplier */
68 const void *multiplier;
69 /** Length of multiplier */
71 /** Expected result */
72 const void *expected;
73 /** Length of expected result */
75};
76
77/**
78 * Define an X25519 multiplication test
79 *
80 * @v name Test name
81 * @v MULTIPLICAND 258-bit multiplicand
82 * @v MULTIPLIER 258-bit multiplier
83 * @v EXPECTED 255-bit expected result
84 * @ret test X25519 multiplication test
85 */
86#define X25519_MULTIPLY_TEST( name, MULTIPLICAND, MULTIPLIER, \
87 EXPECTED ) \
88 static const uint8_t name ## _multiplicand[] = MULTIPLICAND; \
89 static const uint8_t name ## _multiplier[] = MULTIPLIER; \
90 static const uint8_t name ## _expected[] = EXPECTED; \
91 static struct x25519_multiply_test name = { \
92 .multiplicand = name ## _multiplicand, \
93 .multiplicand_len = sizeof ( name ## _multiplicand ), \
94 .multiplier = name ## _multiplier, \
95 .multiplier_len = sizeof ( name ## _multiplier ), \
96 .expected = name ## _expected, \
97 .expected_len = sizeof ( name ## _expected ), \
98 }
99
100/** An X25519 multiplicative inversion self-test */
102 /** Invertend */
103 const void *invertend;
104 /** Length of invertend */
106 /** Expected result */
107 const void *expected;
108 /** Length of expected result */
110};
111
112/**
113 * Define an X25519 multiplicative inversion test
114 *
115 * @v name Test name
116 * @v INVERTEND 258-bit invertend
117 * @v EXPECTED 255-bit expected result
118 * @ret test X25519 multiplicative inversion test
119 */
120#define X25519_INVERT_TEST( name, INVERTEND, EXPECTED ) \
121 static const uint8_t name ## _invertend[] = INVERTEND; \
122 static const uint8_t name ## _expected[] = EXPECTED; \
123 static struct x25519_invert_test name = { \
124 .invertend = name ## _invertend, \
125 .invertend_len = sizeof ( name ## _invertend ), \
126 .expected = name ## _expected, \
127 .expected_len = sizeof ( name ## _expected ), \
128 }
129
130/** An X25519 key exchange self-test */
132 /** Base */
134 /** Scalar */
136 /** Expected result */
138 /** Number of iterations */
139 unsigned int count;
140 /** Key exchange is expected to fail (i.e. produce all-zeroes) */
141 int fail;
142};
143
144/**
145 * Define an X25519 key exchange test
146 *
147 * @v name Test name
148 * @v COUNT Number of iterations
149 * @v FAIL Expected failure status
150 * @v BASE Base point
151 * @v SCALAR Scalar multiple
152 * @v EXPECTED Expected result
153 * @ret test X25519 key exchange test
154 */
155#define X25519_KEY_TEST( name, COUNT, FAIL, BASE, SCALAR, EXPECTED ) \
156 static struct x25519_key_test name = { \
157 .count = COUNT, \
158 .fail = FAIL, \
159 .base = { .raw = BASE }, \
160 .scalar = { .raw = SCALAR }, \
161 .expected = { .raw = EXPECTED }, \
162 }
163
164/**
165 * Report an X25519 multiplication test result
166 *
167 * @v test X25519 multiplication test
168 * @v file Test code file
169 * @v line Test code line
170 */
172 const char *file, unsigned int line ) {
173 union x25519_oct258 multiplicand;
175 union x25519_quad257 expected;
176 union x25519_quad257 actual;
177
178 /* Construct big integers */
179 bigint_init ( &multiplicand.value, test->multiplicand,
180 test->multiplicand_len );
181 DBGC ( test, "X25519 multiplicand:\n" );
182 DBGC_HDA ( test, 0, &multiplicand, sizeof ( multiplicand ) );
183 bigint_init ( &multiplier.value, test->multiplier,
184 test->multiplier_len );
185 DBGC ( test, "X25519 multiplier:\n" );
186 DBGC_HDA ( test, 0, &multiplier, sizeof ( multiplier ) );
187 bigint_init ( &expected.value, test->expected, test->expected_len );
188 DBGC ( test, "X25519 expected product:\n" );
189 DBGC_HDA ( test, 0, &expected, sizeof ( expected ) );
190
191 /* Perform multiplication */
192 x25519_multiply ( &multiplicand, &multiplier, &actual );
193
194 /* Reduce result to allow for comparison */
195 x25519_reduce ( &actual );
196 DBGC ( test, "X25519 actual product:\n" );
197 DBGC_HDA ( test, 0, &actual, sizeof ( actual ) );
198
199 /* Compare against expected result */
200 okx ( memcmp ( &actual, &expected, sizeof ( expected ) ) == 0,
201 file, line );
202}
203#define x25519_multiply_ok( test ) \
204 x25519_multiply_okx ( test, __FILE__, __LINE__ )
205
206/**
207 * Report an X25519 multiplicative inversion test result
208 *
209 * @v test X25519 multiplicative inversion test
210 * @v file Test code file
211 * @v line Test code line
212 */
214 const char *file, unsigned int line ) {
215 static const uint8_t one[] = { 1 };
216 union x25519_oct258 invertend;
217 union x25519_quad257 expected;
218 union x25519_quad257 actual;
220 union x25519_quad257 identity;
221
222 /* Construct big integers */
223 bigint_init ( &invertend.value, test->invertend, test->invertend_len );
224 DBGC ( test, "X25519 invertend:\n" );
225 DBGC_HDA ( test, 0, &invertend, sizeof ( invertend ) );
226 bigint_init ( &expected.value, test->expected, test->expected_len );
227 DBGC ( test, "X25519 expected inverse:\n" );
228 DBGC_HDA ( test, 0, &expected, sizeof ( expected ) );
229 bigint_init ( &identity.value, one, sizeof ( one ) );
230
231 /* Perform inversion */
232 x25519_invert ( &invertend, &actual );
233
234 /* Multiply invertend by inverse */
235 x25519_multiply ( &invertend, &actual.oct258, &product );
236
237 /* Reduce results to allow for comparison */
238 x25519_reduce ( &actual );
239 DBGC ( test, "X25519 actual inverse:\n" );
240 DBGC_HDA ( test, 0, &actual, sizeof ( actual ) );
242 DBGC ( test, "X25519 actual product:\n" );
243 DBGC_HDA ( test, 0, &product, sizeof ( product ) );
244
245 /* Compare against expected results */
246 okx ( memcmp ( &actual, &expected, sizeof ( expected ) ) == 0,
247 file, line );
248 okx ( memcmp ( &product, &identity, sizeof ( identity ) ) == 0,
249 file, line );
250}
251#define x25519_invert_ok( test ) \
252 x25519_invert_okx ( test, __FILE__, __LINE__ )
253
254/**
255 * Report an X25519 key exchange test result
256 *
257 * @v test X25519 key exchange test
258 * @v file Test code file
259 * @v line Test code line
260 */
261static void x25519_key_okx ( struct x25519_key_test *test,
262 const char *file, unsigned int line ) {
263 struct x25519_value base;
264 struct x25519_value scalar;
265 struct x25519_value actual;
266 unsigned int i;
267
268 /* Construct input values */
269 memcpy ( &base, &test->base, sizeof ( test->base ) );
270 memcpy ( &scalar, &test->scalar, sizeof ( test->scalar ) );
271 DBGC ( test, "X25519 base:\n" );
272 DBGC_HDA ( test, 0, &base, sizeof ( base ) );
273 DBGC ( test, "X25519 scalar:\n" );
274 DBGC_HDA ( test, 0, &scalar, sizeof ( scalar ) );
275 DBGC ( test, "X25519 expected result (x%d):\n", test->count );
276 DBGC_HDA ( test, 0, &test->expected, sizeof ( test->expected ) );
277
278 /* Calculate key */
279 for ( i = 0 ; i < test->count ; i++ ) {
280 x25519_key ( &base, &scalar, &actual );
281 if ( test->fail ) {
282 okx ( x25519_is_zero ( &actual ), file, line );
283 } else {
284 okx ( ( ! x25519_is_zero ( &actual ) ), file, line );
285 }
286 memcpy ( &base, &scalar, sizeof ( base ) );
287 memcpy ( &scalar, &actual, sizeof ( scalar ) );
288 }
289 DBGC ( test, "X25519 actual result (x%d):\n", test->count );
290 DBGC_HDA ( test, 0, &actual, sizeof ( actual ) );
291
292 /* Compare against expected result */
293 okx ( memcmp ( &actual, &test->expected,
294 sizeof ( test->expected ) ) == 0, file, line );
295}
296#define x25519_key_ok( test ) \
297 x25519_key_okx ( test, __FILE__, __LINE__ )
298
299/* Test multiplying small numbers */
300X25519_MULTIPLY_TEST ( multiply_small, MULTIPLICAND ( 6 ),
301 MULTIPLIER ( 9 ), EXPECTED ( 6 * 9 ) );
302
303/* Test exact multiple of field prime */
304X25519_MULTIPLY_TEST ( multiply_k_p,
305 MULTIPLICAND ( 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
306 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
307 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
308 0xff, 0xff, 0xff, 0xff, 0xed ),
309 MULTIPLIER ( 0x00, 0xe8, 0x0d, 0x83, 0xd4, 0xe9, 0x1e, 0xdd, 0x7a,
310 0x45, 0x14, 0x87, 0xb7, 0xfc, 0x62, 0x54, 0x1f, 0xb2,
311 0x97, 0x24, 0xde, 0xfa, 0xd3, 0xe7, 0x3e, 0x83, 0x93,
312 0x60, 0xbc, 0x20, 0x97, 0x9b, 0x22 ),
313 EXPECTED ( 0x00 ) );
314
315/* 0x0223b8c1e9392456de3eb13b9046685257bdd640fb06671ad11c80317fa3b1799d *
316 * 0x006c031199972a846916419f828b9d2434e465e150bd9c66b3ad3c2d6d1a3d1fa7 =
317 * 0x1ba87e982f7c477616b4d5136ba54733e40081c1c2e27d864aa178ce893d1297 (mod p)
318 */
320 MULTIPLICAND ( 0x02, 0x23, 0xb8, 0xc1, 0xe9, 0x39, 0x24, 0x56, 0xde,
321 0x3e, 0xb1, 0x3b, 0x90, 0x46, 0x68, 0x52, 0x57, 0xbd,
322 0xd6, 0x40, 0xfb, 0x06, 0x67, 0x1a, 0xd1, 0x1c, 0x80,
323 0x31, 0x7f, 0xa3, 0xb1, 0x79, 0x9d ),
324 MULTIPLIER ( 0x00, 0x6c, 0x03, 0x11, 0x99, 0x97, 0x2a, 0x84, 0x69,
325 0x16, 0x41, 0x9f, 0x82, 0x8b, 0x9d, 0x24, 0x34, 0xe4,
326 0x65, 0xe1, 0x50, 0xbd, 0x9c, 0x66, 0xb3, 0xad, 0x3c,
327 0x2d, 0x6d, 0x1a, 0x3d, 0x1f, 0xa7 ),
328 EXPECTED ( 0x1b, 0xa8, 0x7e, 0x98, 0x2f, 0x7c, 0x47, 0x76, 0x16, 0xb4,
329 0xd5, 0x13, 0x6b, 0xa5, 0x47, 0x33, 0xe4, 0x00, 0x81, 0xc1,
330 0xc2, 0xe2, 0x7d, 0x86, 0x4a, 0xa1, 0x78, 0xce, 0x89, 0x3d,
331 0x12, 0x97 ) );
332
333/* 0x008fadc1a606cb0fb39a1de644815ef6d13b8faa1837f8a88b17fc695a07a0ca6e *
334 * 0x0196da1dac72ff5d2a386ecbe06b65a6a48b8148f6b38a088ca65ed389b74d0fb1 =
335 * 0x351f7bf75ef580249ed6f9ff3996463b0730a1d49b5d36b863e192591157e950 (mod p)
336 */
338 MULTIPLICAND ( 0x00, 0x8f, 0xad, 0xc1, 0xa6, 0x06, 0xcb, 0x0f, 0xb3,
339 0x9a, 0x1d, 0xe6, 0x44, 0x81, 0x5e, 0xf6, 0xd1, 0x3b,
340 0x8f, 0xaa, 0x18, 0x37, 0xf8, 0xa8, 0x8b, 0x17, 0xfc,
341 0x69, 0x5a, 0x07, 0xa0, 0xca, 0x6e ),
342 MULTIPLIER ( 0x01, 0x96, 0xda, 0x1d, 0xac, 0x72, 0xff, 0x5d, 0x2a,
343 0x38, 0x6e, 0xcb, 0xe0, 0x6b, 0x65, 0xa6, 0xa4, 0x8b,
344 0x81, 0x48, 0xf6, 0xb3, 0x8a, 0x08, 0x8c, 0xa6, 0x5e,
345 0xd3, 0x89, 0xb7, 0x4d, 0x0f, 0xb1 ),
346 EXPECTED ( 0x35, 0x1f, 0x7b, 0xf7, 0x5e, 0xf5, 0x80, 0x24, 0x9e, 0xd6,
347 0xf9, 0xff, 0x39, 0x96, 0x46, 0x3b, 0x07, 0x30, 0xa1, 0xd4,
348 0x9b, 0x5d, 0x36, 0xb8, 0x63, 0xe1, 0x92, 0x59, 0x11, 0x57,
349 0xe9, 0x50 ) );
350
351/* 0x016c307511b2b9437a28df6ec4ce4a2bbdc241330b01a9e71fde8a774bcf36d58b *
352 * 0x0117be31111a2a73ed562b0f79c37459eef50bea63371ecd7b27cd813047229389 =
353 * 0x6b43b5185965f8f0920f31ae1b2cefadd7b078fecf68dbeaa17b9c385b558329 (mod p)
354 */
356 MULTIPLICAND ( 0x01, 0x6c, 0x30, 0x75, 0x11, 0xb2, 0xb9, 0x43, 0x7a,
357 0x28, 0xdf, 0x6e, 0xc4, 0xce, 0x4a, 0x2b, 0xbd, 0xc2,
358 0x41, 0x33, 0x0b, 0x01, 0xa9, 0xe7, 0x1f, 0xde, 0x8a,
359 0x77, 0x4b, 0xcf, 0x36, 0xd5, 0x8b ),
360 MULTIPLIER ( 0x01, 0x17, 0xbe, 0x31, 0x11, 0x1a, 0x2a, 0x73, 0xed,
361 0x56, 0x2b, 0x0f, 0x79, 0xc3, 0x74, 0x59, 0xee, 0xf5,
362 0x0b, 0xea, 0x63, 0x37, 0x1e, 0xcd, 0x7b, 0x27, 0xcd,
363 0x81, 0x30, 0x47, 0x22, 0x93, 0x89 ),
364 EXPECTED ( 0x6b, 0x43, 0xb5, 0x18, 0x59, 0x65, 0xf8, 0xf0, 0x92, 0x0f,
365 0x31, 0xae, 0x1b, 0x2c, 0xef, 0xad, 0xd7, 0xb0, 0x78, 0xfe,
366 0xcf, 0x68, 0xdb, 0xea, 0xa1, 0x7b, 0x9c, 0x38, 0x5b, 0x55,
367 0x83, 0x29 ) );
368
369/* 0x020b1f9163ce9ff57f43b7a3a69a8dca03580d7b71d8f564135be6128e18c26797 *
370 * 0x018d5288f1142c3fe860e7a113ec1b8ca1f91e1d4c1ff49b7889463e85759cde66 =
371 * 0x28a77d3c8a14323d63b288dbd40315b3f192b8485d86a02cb87d3dfb7a0b5447 (mod p)
372 */
374 MULTIPLICAND ( 0x02, 0x0b, 0x1f, 0x91, 0x63, 0xce, 0x9f, 0xf5, 0x7f,
375 0x43, 0xb7, 0xa3, 0xa6, 0x9a, 0x8d, 0xca, 0x03, 0x58,
376 0x0d, 0x7b, 0x71, 0xd8, 0xf5, 0x64, 0x13, 0x5b, 0xe6,
377 0x12, 0x8e, 0x18, 0xc2, 0x67, 0x97 ),
378 MULTIPLIER ( 0x01, 0x8d, 0x52, 0x88, 0xf1, 0x14, 0x2c, 0x3f, 0xe8,
379 0x60, 0xe7, 0xa1, 0x13, 0xec, 0x1b, 0x8c, 0xa1, 0xf9,
380 0x1e, 0x1d, 0x4c, 0x1f, 0xf4, 0x9b, 0x78, 0x89, 0x46,
381 0x3e, 0x85, 0x75, 0x9c, 0xde, 0x66 ),
382 EXPECTED ( 0x28, 0xa7, 0x7d, 0x3c, 0x8a, 0x14, 0x32, 0x3d, 0x63, 0xb2,
383 0x88, 0xdb, 0xd4, 0x03, 0x15, 0xb3, 0xf1, 0x92, 0xb8, 0x48,
384 0x5d, 0x86, 0xa0, 0x2c, 0xb8, 0x7d, 0x3d, 0xfb, 0x7a, 0x0b,
385 0x54, 0x47 ) );
386
387/* 0x023139d32c93cd59bf5c941cf0dc98d2c1e2acf72f9e574f7aa0ee89aed453dd32 *
388 * 0x03146d3f31fc377a4c4a15544dc5e7ce8a3a578a8ea9488d990bbb259911ce5dd2 =
389 * 0x4bdb7a35c0a5182000aa67554741e88cfdf460a78c6fae07adf83d2f005d2767 (mod p)
390 */
392 MULTIPLICAND ( 0x02, 0x31, 0x39, 0xd3, 0x2c, 0x93, 0xcd, 0x59, 0xbf,
393 0x5c, 0x94, 0x1c, 0xf0, 0xdc, 0x98, 0xd2, 0xc1, 0xe2,
394 0xac, 0xf7, 0x2f, 0x9e, 0x57, 0x4f, 0x7a, 0xa0, 0xee,
395 0x89, 0xae, 0xd4, 0x53, 0xdd, 0x32 ),
396 MULTIPLIER ( 0x03, 0x14, 0x6d, 0x3f, 0x31, 0xfc, 0x37, 0x7a, 0x4c,
397 0x4a, 0x15, 0x54, 0x4d, 0xc5, 0xe7, 0xce, 0x8a, 0x3a,
398 0x57, 0x8a, 0x8e, 0xa9, 0x48, 0x8d, 0x99, 0x0b, 0xbb,
399 0x25, 0x99, 0x11, 0xce, 0x5d, 0xd2 ),
400 EXPECTED ( 0x4b, 0xdb, 0x7a, 0x35, 0xc0, 0xa5, 0x18, 0x20, 0x00, 0xaa,
401 0x67, 0x55, 0x47, 0x41, 0xe8, 0x8c, 0xfd, 0xf4, 0x60, 0xa7,
402 0x8c, 0x6f, 0xae, 0x07, 0xad, 0xf8, 0x3d, 0x2f, 0x00, 0x5d,
403 0x27, 0x67 ) );
404
405/* 0x01d58842dea2bc372f7412b29347294739614ff3d719db3ad0ddd1dfb23b982ef8 ^ -1 =
406 * 0x093ff51750809d181a9a5481c564e37cff618def8ec45f464b1a6e24f8b826bd (mod p)
407 */
409 INVERTEND ( 0x01, 0xd5, 0x88, 0x42, 0xde, 0xa2, 0xbc, 0x37, 0x2f,
410 0x74, 0x12, 0xb2, 0x93, 0x47, 0x29, 0x47, 0x39, 0x61,
411 0x4f, 0xf3, 0xd7, 0x19, 0xdb, 0x3a, 0xd0, 0xdd, 0xd1,
412 0xdf, 0xb2, 0x3b, 0x98, 0x2e, 0xf8 ),
413 EXPECTED ( 0x09, 0x3f, 0xf5, 0x17, 0x50, 0x80, 0x9d, 0x18, 0x1a, 0x9a,
414 0x54, 0x81, 0xc5, 0x64, 0xe3, 0x7c, 0xff, 0x61, 0x8d, 0xef,
415 0x8e, 0xc4, 0x5f, 0x46, 0x4b, 0x1a, 0x6e, 0x24, 0xf8, 0xb8,
416 0x26, 0xbd ) );
417
418/* 0x02efc89849b3aa7efe4458a885ab9099a435a240ae5af305535ec42e0829a3b2e9 ^ -1 =
419 * 0x591607b163e89d0ac33a62c881e984a25d3826e3db5ce229af240dc58e5b579a (mod p)
420 */
422 INVERTEND ( 0x02, 0xef, 0xc8, 0x98, 0x49, 0xb3, 0xaa, 0x7e, 0xfe,
423 0x44, 0x58, 0xa8, 0x85, 0xab, 0x90, 0x99, 0xa4, 0x35,
424 0xa2, 0x40, 0xae, 0x5a, 0xf3, 0x05, 0x53, 0x5e, 0xc4,
425 0x2e, 0x08, 0x29, 0xa3, 0xb2, 0xe9 ),
426 EXPECTED ( 0x59, 0x16, 0x07, 0xb1, 0x63, 0xe8, 0x9d, 0x0a, 0xc3, 0x3a,
427 0x62, 0xc8, 0x81, 0xe9, 0x84, 0xa2, 0x5d, 0x38, 0x26, 0xe3,
428 0xdb, 0x5c, 0xe2, 0x29, 0xaf, 0x24, 0x0d, 0xc5, 0x8e, 0x5b,
429 0x57, 0x9a ) );
430
431/* 0x003eabedcbbaa80dd488bd64072bcfbe01a28defe39bf0027312476f57a5e5a5ab ^ -1 =
432 * 0x7d87c2e565b27c5038181a0a7cae9ebe826c8afc1f77128a4d62cce96d2759a2 (mod p)
433 */
435 INVERTEND ( 0x00, 0x3e, 0xab, 0xed, 0xcb, 0xba, 0xa8, 0x0d, 0xd4,
436 0x88, 0xbd, 0x64, 0x07, 0x2b, 0xcf, 0xbe, 0x01, 0xa2,
437 0x8d, 0xef, 0xe3, 0x9b, 0xf0, 0x02, 0x73, 0x12, 0x47,
438 0x6f, 0x57, 0xa5, 0xe5, 0xa5, 0xab ),
439 EXPECTED ( 0x7d, 0x87, 0xc2, 0xe5, 0x65, 0xb2, 0x7c, 0x50, 0x38, 0x18,
440 0x1a, 0x0a, 0x7c, 0xae, 0x9e, 0xbe, 0x82, 0x6c, 0x8a, 0xfc,
441 0x1f, 0x77, 0x12, 0x8a, 0x4d, 0x62, 0xcc, 0xe9, 0x6d, 0x27,
442 0x59, 0xa2 ) );
443
444/* 0x008e944239b02b61c4a3d70628ece66fa2fd5166e6451b4cf36123fdf77656af72 ^ -1 =
445 * 0x08e96161a0eee1b29af396f154950d5c715dc61aff66ee97377ab22adf3321d7 (mod p)
446 */
448 INVERTEND ( 0x00, 0x8e, 0x94, 0x42, 0x39, 0xb0, 0x2b, 0x61, 0xc4,
449 0xa3, 0xd7, 0x06, 0x28, 0xec, 0xe6, 0x6f, 0xa2, 0xfd,
450 0x51, 0x66, 0xe6, 0x45, 0x1b, 0x4c, 0xf3, 0x61, 0x23,
451 0xfd, 0xf7, 0x76, 0x56, 0xaf, 0x72 ),
452 EXPECTED ( 0x08, 0xe9, 0x61, 0x61, 0xa0, 0xee, 0xe1, 0xb2, 0x9a, 0xf3,
453 0x96, 0xf1, 0x54, 0x95, 0x0d, 0x5c, 0x71, 0x5d, 0xc6, 0x1a,
454 0xff, 0x66, 0xee, 0x97, 0x37, 0x7a, 0xb2, 0x2a, 0xdf, 0x33,
455 0x21, 0xd7 ) );
456
457/* 0x00d261a7ab3aa2e4f90e51f30dc6a7ee39c4b032ccd7c524a55304317faf42e12f ^ -1 =
458 * 0x0738781c0aeabfbe6e840c85bd30996ef71bc54988ce16cedd5ab4f30c281597 (mod p)
459 */
461 INVERTEND ( 0x00, 0xd2, 0x61, 0xa7, 0xab, 0x3a, 0xa2, 0xe4, 0xf9,
462 0x0e, 0x51, 0xf3, 0x0d, 0xc6, 0xa7, 0xee, 0x39, 0xc4,
463 0xb0, 0x32, 0xcc, 0xd7, 0xc5, 0x24, 0xa5, 0x53, 0x04,
464 0x31, 0x7f, 0xaf, 0x42, 0xe1, 0x2f ),
465 EXPECTED ( 0x07, 0x38, 0x78, 0x1c, 0x0a, 0xea, 0xbf, 0xbe, 0x6e, 0x84,
466 0x0c, 0x85, 0xbd, 0x30, 0x99, 0x6e, 0xf7, 0x1b, 0xc5, 0x49,
467 0x88, 0xce, 0x16, 0xce, 0xdd, 0x5a, 0xb4, 0xf3, 0x0c, 0x28,
468 0x15, 0x97 ) );
469
470/* Base: 0xe6db6867583030db3594c1a424b15f7c726624ec26b3353b10a903a6d0ab1c4c
471 * Scalar: 0xa546e36bf0527c9d3b16154b82465edd62144c0ac1fc5a18506a2244ba449ac4
472 * Result: 0xc3da55379de9c6908e94ea4df28d084f32eccf03491c71f754b4075577a28552
473 */
474X25519_KEY_TEST ( rfc7748_1, 1, 0,
475 BASE ( 0xe6, 0xdb, 0x68, 0x67, 0x58, 0x30, 0x30, 0xdb, 0x35, 0x94,
476 0xc1, 0xa4, 0x24, 0xb1, 0x5f, 0x7c, 0x72, 0x66, 0x24, 0xec,
477 0x26, 0xb3, 0x35, 0x3b, 0x10, 0xa9, 0x03, 0xa6, 0xd0, 0xab,
478 0x1c, 0x4c ),
479 SCALAR ( 0xa5, 0x46, 0xe3, 0x6b, 0xf0, 0x52, 0x7c, 0x9d, 0x3b, 0x16,
480 0x15, 0x4b, 0x82, 0x46, 0x5e, 0xdd, 0x62, 0x14, 0x4c, 0x0a,
481 0xc1, 0xfc, 0x5a, 0x18, 0x50, 0x6a, 0x22, 0x44, 0xba, 0x44,
482 0x9a, 0xc4 ),
483 EXPECTED ( 0xc3, 0xda, 0x55, 0x37, 0x9d, 0xe9, 0xc6, 0x90, 0x8e, 0x94,
484 0xea, 0x4d, 0xf2, 0x8d, 0x08, 0x4f, 0x32, 0xec, 0xcf, 0x03,
485 0x49, 0x1c, 0x71, 0xf7, 0x54, 0xb4, 0x07, 0x55, 0x77, 0xa2,
486 0x85, 0x52 ) );
487
488/* Base: 0xe5210f12786811d3f4b7959d0538ae2c31dbe7106fc03c3efc4cd549c715a493
489 * Scalar: 0x4b66e9d4d1b4673c5ad22691957d6af5c11b6421e0ea01d42ca4169e7918ba0d
490 * Result: 0x95cbde9476e8907d7aade45cb4b873f88b595a68799fa152e6f8f7647aac7957
491 */
492X25519_KEY_TEST ( rfc7748_2, 1, 0,
493 BASE ( 0xe5, 0x21, 0x0f, 0x12, 0x78, 0x68, 0x11, 0xd3, 0xf4, 0xb7,
494 0x95, 0x9d, 0x05, 0x38, 0xae, 0x2c, 0x31, 0xdb, 0xe7, 0x10,
495 0x6f, 0xc0, 0x3c, 0x3e, 0xfc, 0x4c, 0xd5, 0x49, 0xc7, 0x15,
496 0xa4, 0x93 ),
497 SCALAR ( 0x4b, 0x66, 0xe9, 0xd4, 0xd1, 0xb4, 0x67, 0x3c, 0x5a, 0xd2,
498 0x26, 0x91, 0x95, 0x7d, 0x6a, 0xf5, 0xc1, 0x1b, 0x64, 0x21,
499 0xe0, 0xea, 0x01, 0xd4, 0x2c, 0xa4, 0x16, 0x9e, 0x79, 0x18,
500 0xba, 0x0d ),
501 EXPECTED ( 0x95, 0xcb, 0xde, 0x94, 0x76, 0xe8, 0x90, 0x7d, 0x7a, 0xad,
502 0xe4, 0x5c, 0xb4, 0xb8, 0x73, 0xf8, 0x8b, 0x59, 0x5a, 0x68,
503 0x79, 0x9f, 0xa1, 0x52, 0xe6, 0xf8, 0xf7, 0x64, 0x7a, 0xac,
504 0x79, 0x57 ) );
505
506/* Base: 0x0900000000000000000000000000000000000000000000000000000000000000
507 * Scalar: 0x0900000000000000000000000000000000000000000000000000000000000000
508 * Result: 0x422c8e7a6227d7bca1350b3e2bb7279f7897b87bb6854b783c60e80311ae3079
509 */
510X25519_KEY_TEST ( rfc7748_3, 1, 0,
511 BASE ( 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
512 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
513 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
514 0x00, 0x00 ),
515 SCALAR ( 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
516 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
517 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
518 0x00, 0x00 ),
519 EXPECTED ( 0x42, 0x2c, 0x8e, 0x7a, 0x62, 0x27, 0xd7, 0xbc, 0xa1, 0x35,
520 0x0b, 0x3e, 0x2b, 0xb7, 0x27, 0x9f, 0x78, 0x97, 0xb8, 0x7b,
521 0xb6, 0x85, 0x4b, 0x78, 0x3c, 0x60, 0xe8, 0x03, 0x11, 0xae,
522 0x30, 0x79 ) );
523
524/* Base: 0x0900000000000000000000000000000000000000000000000000000000000000
525 * Scalar: 0x0900000000000000000000000000000000000000000000000000000000000000
526 * Result: 0xb1a5a73158904c020866c13939dd7e1aa26852ee1d2609c92e5a8f1debe2150a
527 * (after 100 iterations)
528 *
529 * RFC7748 gives test vectors for 1000 and 1000000 iterations with
530 * these starting values. This test case stops after 100 iterations
531 * to avoid a pointlessly slow test cycle in the common case of
532 * running tests under Valgrind.
533 */
534X25519_KEY_TEST ( rfc7748_4_100, 100, 0,
535 BASE ( 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
536 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
537 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
538 0x00, 0x00 ),
539 SCALAR ( 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
540 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
541 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
542 0x00, 0x00 ),
543 EXPECTED ( 0xb1, 0xa5, 0xa7, 0x31, 0x58, 0x90, 0x4c, 0x02, 0x08, 0x66,
544 0xc1, 0x39, 0x39, 0xdd, 0x7e, 0x1a, 0xa2, 0x68, 0x52, 0xee,
545 0x1d, 0x26, 0x09, 0xc9, 0x2e, 0x5a, 0x8f, 0x1d, 0xeb, 0xe2,
546 0x15, 0x0a ) );
547
548/* Base: 2^255 - 19 + 1 (deliberately malicious public key)
549 * Scalar: 0x000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f
550 * Result: Failure (all zeros)
551 */
552X25519_KEY_TEST ( malicious, 1, 1,
553 BASE ( 0xee, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
554 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
555 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
556 0xff, 0x7f ),
557 SCALAR ( 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
558 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x01, 0x02, 0x03,
559 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
560 0x0e, 0x0f ),
561 EXPECTED ( 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
562 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
563 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
564 0x00, 0x00 ) );
565
566/* Private: 0x77076d0a7318a57d3c16c17251b26645df4c2f87ebc0992ab177fba51db92c2a
567 * Partner: 0xde9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f
568 * Public: 0x8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a
569 * Shared: 0x4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742
570 */
572 PRIVATE ( 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16,
573 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87,
574 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9,
575 0x2c, 0x2a ),
576 PARTNER ( 0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4, 0xd3, 0x5b,
577 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37, 0x3f, 0x83, 0x43, 0xc8,
578 0x5b, 0x78, 0x67, 0x4d, 0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88,
579 0x2b, 0x4f ),
580 PUBLIC ( 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b,
581 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d,
582 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b,
583 0x4e, 0x6a ),
584 SHARED ( 0x4a, 0x5d, 0x9d, 0x5b, 0xa4, 0xce, 0x2d, 0xe1, 0x72, 0x8e,
585 0x3b, 0xf4, 0x80, 0x35, 0x0f, 0x25, 0xe0, 0x7e, 0x21, 0xc9,
586 0x47, 0xd1, 0x9e, 0x33, 0x76, 0xf0, 0x9b, 0x3c, 0x1e, 0x16,
587 0x17, 0x42 ) );
588
589/* Private: 0x5dab087e624a8a4b79e17f8b83800ee66f3bb1292618b6fd1c2f8b27ff88e0eb
590 * Partner: 0x8520f0098930a754748b7ddcb43ef75a0dbf3a0d26381af4eba4a98eaa9b4e6a
591 * Public: 0xde9edb7d7b7dc1b4d35b61c2ece435373f8343c85b78674dadfc7e146f882b4f
592 * Shared: 0x4a5d9d5ba4ce2de1728e3bf480350f25e07e21c947d19e3376f09b3c1e161742
593 */
595 PRIVATE ( 0x5d, 0xab, 0x08, 0x7e, 0x62, 0x4a, 0x8a, 0x4b, 0x79, 0xe1,
596 0x7f, 0x8b, 0x83, 0x80, 0x0e, 0xe6, 0x6f, 0x3b, 0xb1, 0x29,
597 0x26, 0x18, 0xb6, 0xfd, 0x1c, 0x2f, 0x8b, 0x27, 0xff, 0x88,
598 0xe0, 0xeb ),
599 PARTNER ( 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b,
600 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d,
601 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b,
602 0x4e, 0x6a ),
603 PUBLIC ( 0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4, 0xd3, 0x5b,
604 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37, 0x3f, 0x83, 0x43, 0xc8,
605 0x5b, 0x78, 0x67, 0x4d, 0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88,
606 0x2b, 0x4f ),
607 SHARED ( 0x4a, 0x5d, 0x9d, 0x5b, 0xa4, 0xce, 0x2d, 0xe1, 0x72, 0x8e,
608 0x3b, 0xf4, 0x80, 0x35, 0x0f, 0x25, 0xe0, 0x7e, 0x21, 0xc9,
609 0x47, 0xd1, 0x9e, 0x33, 0x76, 0xf0, 0x9b, 0x3c, 0x1e, 0x16,
610 0x17, 0x42 ) );
611
612/* Private: 0x106221fe5694a710d6e147696c5d5b93d6887d584f24f228182ebe1b1d2db85d
613 * Partner: 0xffffff0f000000ffffff0f000000ffffff0f000000ffffff0f000000ffffff0f
614 * Public: 0xecba343f4a89013fea83e869ea3f7a670715d833ab0ec43adb3acd4b48229607
615 * Shared: 0x5e64924b91873b499a5402fa64337c65d4b2ed54beeb3fa5d7347809e43aef1c
616 */
618 PRIVATE ( 0x10, 0x62, 0x21, 0xfe, 0x56, 0x94, 0xa7, 0x10, 0xd6, 0xe1,
619 0x47, 0x69, 0x6c, 0x5d, 0x5b, 0x93, 0xd6, 0x88, 0x7d, 0x58,
620 0x4f, 0x24, 0xf2, 0x28, 0x18, 0x2e, 0xbe, 0x1b, 0x1d, 0x2d,
621 0xb8, 0x5d ),
622 PARTNER ( 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
623 0x0f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00,
624 0x00, 0xff, 0xff, 0xff, 0x0f, 0x00, 0x00, 0x00, 0xff, 0xff,
625 0xff, 0x0f ),
626 PUBLIC ( 0xec, 0xba, 0x34, 0x3f, 0x4a, 0x89, 0x01, 0x3f, 0xea, 0x83,
627 0xe8, 0x69, 0xea, 0x3f, 0x7a, 0x67, 0x07, 0x15, 0xd8, 0x33,
628 0xab, 0x0e, 0xc4, 0x3a, 0xdb, 0x3a, 0xcd, 0x4b, 0x48, 0x22,
629 0x96, 0x07 ),
630 SHARED ( 0x5e, 0x64, 0x92, 0x4b, 0x91, 0x87, 0x3b, 0x49, 0x9a, 0x54,
631 0x02, 0xfa, 0x64, 0x33, 0x7c, 0x65, 0xd4, 0xb2, 0xed, 0x54,
632 0xbe, 0xeb, 0x3f, 0xa5, 0xd7, 0x34, 0x78, 0x09, 0xe4, 0x3a,
633 0xef, 0x1c ) );
634
635/* Private: 0xe0f978dfcd3a8f1a5093418de54136a584c20b7b349afdf6c0520886f95b1272
636 * Partner: 0xe0eb7a7c3b41b8ae1656e3faf19fc46ada098deb9c32b1fd866205165f49b800
637 * Public: 0x124e0f1487c9b38f1c7fd4a6518a47699d158d44cc18665c7bcca03bdc726e5d
638 * Shared: Failure (all zeros)
639 */
641 PRIVATE ( 0xe0, 0xf9, 0x78, 0xdf, 0xcd, 0x3a, 0x8f, 0x1a, 0x50, 0x93,
642 0x41, 0x8d, 0xe5, 0x41, 0x36, 0xa5, 0x84, 0xc2, 0x0b, 0x7b,
643 0x34, 0x9a, 0xfd, 0xf6, 0xc0, 0x52, 0x08, 0x86, 0xf9, 0x5b,
644 0x12, 0x72 ),
645 PARTNER ( 0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56,
646 0xe3, 0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb,
647 0x9c, 0x32, 0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49,
648 0xb8, 0x00 ),
649 PUBLIC ( 0x12, 0x4e, 0x0f, 0x14, 0x87, 0xc9, 0xb3, 0x8f, 0x1c, 0x7f,
650 0xd4, 0xa6, 0x51, 0x8a, 0x47, 0x69, 0x9d, 0x15, 0x8d, 0x44,
651 0xcc, 0x18, 0x66, 0x5c, 0x7b, 0xcc, 0xa0, 0x3b, 0xdc, 0x72,
652 0x6e, 0x5d ),
653 SHARED_FAIL );
654
655/* Private: 0x60a3a4f130b98a5be4b1cedb7cb85584a3520e142d474dc9ccb909a073a9767f
656 * Partner: 0x8d612c5831aa64b057300e7e310f3aa332af34066fefcab2b089c9592878f832
657 * Public: 0x7c6ccba92ff00a6e83382cd06b9ec9e6581eafe3c243f0c52cf68e067843e37a
658 * Shared: 0xe3ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff7f
659 */
661 PRIVATE ( 0x60, 0xa3, 0xa4, 0xf1, 0x30, 0xb9, 0x8a, 0x5b, 0xe4, 0xb1,
662 0xce, 0xdb, 0x7c, 0xb8, 0x55, 0x84, 0xa3, 0x52, 0x0e, 0x14,
663 0x2d, 0x47, 0x4d, 0xc9, 0xcc, 0xb9, 0x09, 0xa0, 0x73, 0xa9,
664 0x76, 0x7f ),
665 PARTNER ( 0x8d, 0x61, 0x2c, 0x58, 0x31, 0xaa, 0x64, 0xb0, 0x57, 0x30,
666 0x0e, 0x7e, 0x31, 0x0f, 0x3a, 0xa3, 0x32, 0xaf, 0x34, 0x06,
667 0x6f, 0xef, 0xca, 0xb2, 0xb0, 0x89, 0xc9, 0x59, 0x28, 0x78,
668 0xf8, 0x32 ),
669 PUBLIC ( 0x7c, 0x6c, 0xcb, 0xa9, 0x2f, 0xf0, 0x0a, 0x6e, 0x83, 0x38,
670 0x2c, 0xd0, 0x6b, 0x9e, 0xc9, 0xe6, 0x58, 0x1e, 0xaf, 0xe3,
671 0xc2, 0x43, 0xf0, 0xc5, 0x2c, 0xf6, 0x8e, 0x06, 0x78, 0x43,
672 0xe3, 0x7a ),
673 SHARED ( 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
674 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
675 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
676 0xff, 0x7f ) );
677
678/**
679 * Perform X25519 self-tests
680 *
681 */
682static void x25519_test_exec ( void ) {
683
684 /* Perform multiplication tests */
685 x25519_multiply_ok ( &multiply_small );
686 x25519_multiply_ok ( &multiply_k_p );
687 x25519_multiply_ok ( &multiply_1 );
688 x25519_multiply_ok ( &multiply_2 );
689 x25519_multiply_ok ( &multiply_3 );
690 x25519_multiply_ok ( &multiply_4 );
691 x25519_multiply_ok ( &multiply_5 );
692
693 /* Perform multiplicative inversion tests */
694 x25519_invert_ok ( &invert_1 );
695 x25519_invert_ok ( &invert_2 );
696 x25519_invert_ok ( &invert_3 );
697 x25519_invert_ok ( &invert_4 );
698 x25519_invert_ok ( &invert_5 );
699
700 /* Perform key exchange tests */
701 x25519_key_ok ( &rfc7748_1 );
702 x25519_key_ok ( &rfc7748_2 );
703 x25519_key_ok ( &rfc7748_3 );
704 x25519_key_ok ( &rfc7748_4_100 );
705 x25519_key_ok ( &malicious );
706
707 /* Perform key exchange tests via generic key exchange algorithm */
708 exchange_ok ( &rfc7748_alice );
709 exchange_ok ( &rfc7748_bob );
710 exchange_ok ( &wycheproof_49 );
711 exchange_ok ( &wycheproof_63 );
712 exchange_ok ( &wycheproof_112 );
713}
714
715/** X25519 self-test */
716struct self_test x25519_test __self_test = {
717 .name = "x25519",
718 .exec = x25519_test_exec,
719};
#define BASE
Definition 3c595.h:69
unsigned char uint8_t
Definition stdint.h:10
static const uint32_t multiplier
Port multiplier number.
Definition bigint.h:195
#define EXPECTED(...)
Define inline expected result point.
#define SCALAR(...)
Define inline scalar multiple.
static int test
Definition epic100.c:73
#define PUBLIC(...)
Define inline expected public key.
#define EXCHANGE_TEST(name, EXCHANGE, PRIVATE, PARTNER, PUBLIC, SHARED)
Define a key exchange test.
#define PRIVATE(...)
Define inline private key.
#define SHARED(...)
Define inline expected shared secret.
#define exchange_ok(test)
Report a key exchange test result.
#define SHARED_FAIL
Define inline expected failure result.
#define PARTNER(...)
Define inline partner public key.
#define DBGC(...)
Definition compiler.h:530
#define DBGC_HDA(...)
Definition compiler.h:531
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:921
#define bigint_init(value, data, len)
Initialise big integer.
Definition bigint.h:63
uint8_t product
Product string.
Definition smbios.h:5
String functions.
void * memcpy(void *dest, const void *src, size_t len) __nonnull
uint32_t base
Base.
Definition librm.h:3
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition string.c:115
A self-test set.
Definition test.h:15
An X25519 multiplicative inversion self-test.
const void * expected
Expected result.
size_t invertend_len
Length of invertend.
const void * invertend
Invertend.
size_t expected_len
Length of expected result.
An X25519 key exchange self-test.
int fail
Key exchange is expected to fail (i.e.
struct x25519_value base
Base.
struct x25519_value scalar
Scalar.
unsigned int count
Number of iterations.
struct x25519_value expected
Expected result.
An X25519 multiplication self-test.
Definition x25519_test.c:62
const void * multiplicand
Multiplicand.
Definition x25519_test.c:64
size_t multiplier_len
Length of multiplier.
Definition x25519_test.c:70
const void * multiplier
Multiplier.
Definition x25519_test.c:68
size_t multiplicand_len
Length of multiplicand.
Definition x25519_test.c:66
size_t expected_len
Length of expected result.
Definition x25519_test.c:74
const void * expected
Expected result.
Definition x25519_test.c:72
An X25519 32-byte value.
Definition x25519.h:78
Self-test infrastructure.
#define okx(success, file, line)
Report test result.
Definition test.h:44
#define __self_test
Declare a self-test.
Definition test.h:32
An X25519 unsigned 258-bit integer.
Definition x25519.h:46
x25519_t value
Big integer value.
Definition x25519.h:48
An X25519 unsigned 257-bit integer.
Definition x25519.h:65
x25519_t value
Big integer value.
Definition x25519.h:67
const union x25519_oct258 oct258
X25519 unsigned 258-bit integer.
Definition x25519.h:74
int x25519_is_zero(const struct x25519_value *value)
Check if X25519 value is zero.
Definition x25519.c:793
void x25519_multiply(const union x25519_oct258 *multiplicand, const union x25519_oct258 *multiplier, union x25519_quad257 *result)
Multiply big integers modulo field prime.
Definition x25519.c:427
void x25519_reduce(union x25519_quad257 *value)
Reduce big integer to canonical range.
Definition x25519.c:586
void x25519_key(const struct x25519_value *base, const struct x25519_value *scalar, struct x25519_value *result)
Calculate X25519 key.
Definition x25519.c:808
void x25519_invert(const union x25519_oct258 *invertend, union x25519_quad257 *result)
Compute multiplicative inverse.
Definition x25519.c:529
struct exchange_algorithm x25519_algorithm
X25519 key exchange algorithm.
Definition x25519.c:875
X25519 key exchange.
#define INVERTEND(...)
Define inline invertend.
Definition x25519_test.c:50
#define X25519_KEY_TEST(name, COUNT, FAIL, BASE, SCALAR, EXPECTED)
Define an X25519 key exchange test.
#define X25519_MULTIPLY_TEST(name, MULTIPLICAND, MULTIPLIER, EXPECTED)
Define an X25519 multiplication test.
Definition x25519_test.c:86
#define x25519_key_ok(test)
static void x25519_multiply_okx(struct x25519_multiply_test *test, const char *file, unsigned int line)
Report an X25519 multiplication test result.
#define MULTIPLIER(...)
Define inline multiplier.
Definition x25519_test.c:47
#define X25519_INVERT_TEST(name, INVERTEND, EXPECTED)
Define an X25519 multiplicative inversion test.
static void x25519_key_okx(struct x25519_key_test *test, const char *file, unsigned int line)
Report an X25519 key exchange test result.
static void x25519_invert_okx(struct x25519_invert_test *test, const char *file, unsigned int line)
Report an X25519 multiplicative inversion test result.
#define x25519_invert_ok(test)
#define MULTIPLICAND(...)
Define inline multiplicand.
Definition x25519_test.c:44
#define x25519_multiply_ok(test)
static void x25519_test_exec(void)
Perform X25519 self-tests.