iPXE
x25519.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 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /** @file
27  *
28  * X25519 key exchange
29  *
30  * This implementation is inspired by and partially based upon the
31  * paper "Implementing Curve25519/X25519: A Tutorial on Elliptic Curve
32  * Cryptography" by Martin Kleppmann, available for download from
33  * https://www.cl.cam.ac.uk/teaching/2122/Crypto/curve25519.pdf
34  *
35  * The underlying modular addition, subtraction, and multiplication
36  * operations are completely redesigned for substantially improved
37  * efficiency compared to the TweetNaCl implementation studied in that
38  * paper.
39  *
40  * TweetNaCl iPXE
41  * --------- ----
42  *
43  * Storage size of each big integer 128 40
44  * (in bytes)
45  *
46  * Stack usage for key exchange 1144 360
47  * (in bytes, large objects only)
48  *
49  * Cost of big integer addition 16 5
50  * (in number of 64-bit additions)
51  *
52  * Cost of big integer multiplication 273 31
53  * (in number of 64-bit multiplications)
54  *
55  * The implementation is constant-time (provided that the underlying
56  * big integer operations are also constant-time).
57  */
58 
59 #include <stdint.h>
60 #include <string.h>
61 #include <assert.h>
62 #include <errno.h>
63 #include <ipxe/init.h>
64 #include <ipxe/crypto.h>
65 #include <ipxe/x25519.h>
66 
67 /** X25519 reduction constant
68  *
69  * The X25519 field prime is p=2^255-19. This gives us:
70  *
71  * p = 2^255 - 19
72  * 2^255 = p + 19
73  * 2^255 = 19 (mod p)
74  * k * 2^255 = k * 19 (mod p)
75  *
76  * We can therefore reduce a value modulo p by taking the high-order
77  * bits of the value from bit 255 and above, multiplying by 19, and
78  * adding this to the low-order 255 bits of the value.
79  *
80  * This would be cumbersome to do in practice since it would require
81  * partitioning the value at a 255-bit boundary (and hence would
82  * require some shifting and masking operations). However, we can
83  * note that:
84  *
85  * k * 2^255 = k * 19 (mod p)
86  * k * 2 * 2^255 = k * 2 * 19 (mod p)
87  * k * 2^256 = k * 38 (mod p)
88  *
89  * We can therefore simplify the reduction to taking the high order
90  * bits of the value from bit 256 and above, multiplying by 38, and
91  * adding this to the low-order 256 bits of the value.
92  *
93  * Since 256 will inevitably be a multiple of the big integer element
94  * size (typically 32 or 64 bits), this avoids the need to perform any
95  * shifting or masking operations.
96  */
97 #define X25519_REDUCE_256 38
98 
99 /** X25519 multiplication step 1 result
100  *
101  * Step 1 of X25519 multiplication is to compute the product of two
102  * X25519 unsigned 258-bit integers.
103  *
104  * Both multiplication inputs are limited to 258 bits, and so the
105  * product will have at most 516 bits.
106  */
108  /** Raw product
109  *
110  * Big integer multiplication produces a result with a number
111  * of elements equal to the sum of the number of elements in
112  * each input.
113  */
115  /** Partition into low-order and high-order bits
116  *
117  * Reduction modulo p requires separating the low-order 256
118  * bits from the remaining high-order bits.
119  *
120  * Since the value will never exceed 516 bits (see above),
121  * there will be at most 260 high-order bits.
122  */
123  struct {
124  /** Low-order 256 bits */
125  bigint_t ( bigint_required_size ( ( 256 /* bits */ + 7 ) / 8 ) )
126  low_256bit;
127  /** High-order 260 bits */
128  bigint_t ( bigint_required_size ( ( 260 /* bits */ + 7 ) / 8 ) )
129  high_260bit;
130  } __attribute__ (( packed )) parts;
131 };
132 
133 /** X25519 multiplication step 2 result
134  *
135  * Step 2 of X25519 multiplication is to multiply the high-order 260
136  * bits from step 1 with the 6-bit reduction constant 38, and to add
137  * this to the low-order 256 bits from step 1.
138  *
139  * The multiplication inputs are limited to 260 and 6 bits
140  * respectively, and so the product will have at most 266 bits. After
141  * adding the low-order 256 bits from step 1, the result will have at
142  * most 267 bits.
143  */
145  /** Raw product
146  *
147  * Big integer multiplication produces a result with a number
148  * of elements equal to the sum of the number of elements in
149  * each input.
150  */
151  bigint_t ( bigint_required_size ( ( 260 /* bits */ + 7 ) / 8 ) +
152  bigint_required_size ( ( 6 /* bits */ + 7 ) / 8 ) ) product;
153  /** Big integer value
154  *
155  * The value will never exceed 267 bits (see above), and so
156  * may be consumed as a normal X25519 big integer.
157  */
158  x25519_t value;
159  /** Partition into low-order and high-order bits
160  *
161  * Reduction modulo p requires separating the low-order 256
162  * bits from the remaining high-order bits.
163  *
164  * Since the value will never exceed 267 bits (see above),
165  * there will be at most 11 high-order bits.
166  */
167  struct {
168  /** Low-order 256 bits */
169  bigint_t ( bigint_required_size ( ( 256 /* bits */ + 7 ) / 8 ) )
170  low_256bit;
171  /** High-order 11 bits */
172  bigint_t ( bigint_required_size ( ( 11 /* bits */ + 7 ) / 8 ) )
173  high_11bit;
174  } __attribute__ (( packed )) parts;
175 };
176 
177 /** X25519 multiplication step 3 result
178  *
179  * Step 3 of X25519 multiplication is to multiply the high-order 11
180  * bits from step 2 with the 6-bit reduction constant 38, and to add
181  * this to the low-order 256 bits from step 2.
182  *
183  * The multiplication inputs are limited to 11 and 6 bits
184  * respectively, and so the product will have at most 17 bits. After
185  * adding the low-order 256 bits from step 2, the result will have at
186  * most 257 bits.
187  */
189  /** Raw product
190  *
191  * Big integer multiplication produces a result with a number
192  * of elements equal to the sum of the number of elements in
193  * each input.
194  */
195  bigint_t ( bigint_required_size ( ( 11 /* bits */ + 7 ) / 8 ) +
196  bigint_required_size ( ( 6 /* bits */ + 7 ) / 8 ) ) product;
197  /** Big integer value
198  *
199  * The value will never exceed 267 bits (see above), and so
200  * may be consumed as a normal X25519 big integer.
201  */
202  x25519_t value;
203 };
204 
205 /** X25519 multiplication temporary working space
206  *
207  * We overlap the buffers used by each step of the multiplication
208  * calculation to reduce the total stack space required:
209  *
210  * |--------------------------------------------------------|
211  * | <- pad -> | <------------ step 1 result -------------> |
212  * | | <- low 256 bits -> | <-- high 260 bits --> |
213  * | <------- step 2 result ------> | <-- step 3 result --> |
214  * |--------------------------------------------------------|
215  */
217  /** Step 1 result */
218  struct {
219  /** Padding to avoid collision between steps 1 and 2
220  *
221  * The step 2 multiplication consumes the high 260
222  * bits of step 1, and so the step 2 multiplication
223  * result must not overlap this portion of the step 1
224  * result.
225  */
226  uint8_t pad[ sizeof ( union x25519_multiply_step2 ) -
228  parts.high_260bit ) ];
229  /** Step 1 result */
231  } __attribute__ (( packed ));
232  /** Steps 2 and 3 results */
233  struct {
234  /** Step 2 result */
236  /** Step 3 result */
238  } __attribute__ (( packed ));
239 };
240 
241 /** An X25519 elliptic curve point in projective coordinates
242  *
243  * A point (x,y) on the Montgomery curve used in X25519 is represented
244  * using projective coordinates (X/Z,Y/Z) so that intermediate
245  * calculations may be performed on both numerator and denominator
246  * separately, with the division step performed only once at the end
247  * of the calculation.
248  *
249  * The group operation calculation is performed using a Montgomery
250  * ladder as:
251  *
252  * X[2i] = ( X[i]^2 - Z[i]^2 )^2
253  * X[2i+1] = ( X[i] * X[i+1] - Z[i] * Z[i+1] )^2
254  * Z[2i] = 4 * X[i] * Z[i] * ( X[i]^2 + A * X[i] * Z[i] + Z[i]^2 )
255  * Z[2i+1] = X[0] * ( X[i] * Z[i+1] - X[i+1] * Z[i] ) ^ 2
256  *
257  * It is therefore not necessary to store (or use) the value of Y.
258  */
260  /** X coordinate */
262  /** Z coordinate */
264 };
265 
266 /** An X25519 Montgomery ladder step */
267 struct x25519_step {
268  /** X[n]/Z[n] */
270  /** X[n+1]/Z[n+1] */
272 };
273 
274 /** Constant p=2^255-19 (the finite field prime) */
275 static const uint8_t x25519_p_raw[] = {
276  0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
277  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
278  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
279  0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xed
280 };
281 
282 /** Constant p=2^255-19 (the finite field prime) */
283 static x25519_t x25519_p;
284 
285 /** Constant 2p=2^256-38 */
286 static x25519_t x25519_2p;
287 
288 /** Constant 4p=2^257-76 */
289 static x25519_t x25519_4p;
290 
291 /** Reduction constant (used during multiplication) */
293 
294 /** Reduction constant (used during multiplication) */
296  x25519_reduce_256;
297 
298 /** Constant 121665 (used in the Montgomery ladder) */
299 static const uint8_t x25519_121665_raw[] = { 0x01, 0xdb, 0x41 };
300 
301 /** Constant 121665 (used in the Montgomery ladder) */
303 
304 /** Constant g=9 (the group generator) */
306  .raw = { 9, }
307 };
308 
309 /**
310  * Initialise constants
311  *
312  */
313 static void x25519_init_constants ( void ) {
314 
315  /* Construct constant p */
316  bigint_init ( &x25519_p, x25519_p_raw, sizeof ( x25519_p_raw ) );
317 
318  /* Construct constant 2p */
321 
322  /* Construct constant 4p */
325 
326  /* Construct reduction constant */
327  bigint_init ( &x25519_reduce_256, x25519_reduce_256_raw,
328  sizeof ( x25519_reduce_256_raw ) );
329 
330  /* Construct constant 121665 */
331  bigint_init ( &x25519_121665.value, x25519_121665_raw,
332  sizeof ( x25519_121665_raw ) );
333 }
334 
335 /** Initialisation function */
336 struct init_fn x25519_init_fn __init_fn ( INIT_NORMAL ) = {
338 };
339 
340 /**
341  * Add big integers modulo field prime
342  *
343  * @v augend Big integer to add
344  * @v addend Big integer to add
345  * @v result Big integer to hold result (may overlap augend)
346  */
347 static inline __attribute__ (( always_inline )) void
348 x25519_add ( const union x25519_quad257 *augend,
349  const union x25519_quad257 *addend,
350  union x25519_oct258 *result ) {
351  int copy;
352 
353  /* Copy augend if necessary */
354  copy = ( result != &augend->oct258 );
355  build_assert ( __builtin_constant_p ( copy ) );
356  if ( copy ) {
357  build_assert ( result != &addend->oct258 );
358  bigint_copy ( &augend->oct258.value, &result->value );
359  }
360 
361  /* Perform addition
362  *
363  * Both inputs are in the range [0,4p-1] and the resulting
364  * sum is therefore in the range [0,8p-2].
365  *
366  * This range lies within the range [0,8p-1] and the result is
367  * therefore a valid X25519 unsigned 258-bit integer, as
368  * required.
369  */
370  bigint_add ( &addend->value, &result->value );
371 }
372 
373 /**
374  * Subtract big integers modulo field prime
375  *
376  * @v minuend Big integer from which to subtract
377  * @v subtrahend Big integer to subtract
378  * @v result Big integer to hold result (may overlap minuend)
379  */
380 static inline __attribute__ (( always_inline )) void
381 x25519_subtract ( const union x25519_quad257 *minuend,
382  const union x25519_quad257 *subtrahend,
383  union x25519_oct258 *result ) {
384  int copy;
385 
386  /* Copy minuend if necessary */
387  copy = ( result != &minuend->oct258 );
388  build_assert ( __builtin_constant_p ( copy ) );
389  if ( copy ) {
390  build_assert ( result != &subtrahend->oct258 );
391  bigint_copy ( &minuend->oct258.value, &result->value );
392  }
393 
394  /* Perform subtraction
395  *
396  * Both inputs are in the range [0,4p-1] and the resulting
397  * difference is therefore in the range [1-4p,4p-1].
398  *
399  * This range lies partially outside the range [0,8p-1] and
400  * the result is therefore not yet a valid X25519 unsigned
401  * 258-bit integer.
402  */
403  bigint_subtract ( &subtrahend->value, &result->value );
404 
405  /* Add constant multiple of field prime p
406  *
407  * Add the constant 4p to the result. This brings the result
408  * within the range [1,8p-1] (without changing the value
409  * modulo p).
410  *
411  * This range lies within the range [0,8p-1] and the result is
412  * therefore now a valid X25519 unsigned 258-bit integer, as
413  * required.
414  */
415  bigint_add ( &x25519_4p, &result->value );
416 }
417 
418 /**
419  * Multiply big integers modulo field prime
420  *
421  * @v multiplicand Big integer to be multiplied
422  * @v multiplier Big integer to be multiplied
423  * @v result Big integer to hold result (may overlap either input)
424  */
425 void x25519_multiply ( const union x25519_oct258 *multiplicand,
426  const union x25519_oct258 *multiplier,
427  union x25519_quad257 *result ) {
429  union x25519_multiply_step1 *step1 = &tmp.step1;
430  union x25519_multiply_step2 *step2 = &tmp.step2;
431  union x25519_multiply_step3 *step3 = &tmp.step3;
432 
433  /* Step 1: perform raw multiplication
434  *
435  * step1 = multiplicand * multiplier
436  *
437  * Both inputs are 258-bit numbers and the step 1 result is
438  * therefore 258+258=516 bits.
439  */
440  static_assert ( sizeof ( step1->product ) >= sizeof ( step1->parts ) );
441  bigint_multiply ( &multiplicand->value, &multiplier->value,
442  &step1->product );
443 
444  /* Step 2: reduce high-order 516-256=260 bits of step 1 result
445  *
446  * Use the identity 2^256=38 (mod p) to reduce the high-order
447  * bits of the step 1 result. We split the 516-bit result
448  * from step 1 into its low-order 256 bits and high-order 260
449  * bits:
450  *
451  * step1 = step1(low 256 bits) + step1(high 260 bits) * 2^256
452  *
453  * and then perform the calculation:
454  *
455  * step2 = step1 (mod p)
456  * = step1(low 256 bits) + step1(high 260 bits) * 2^256 (mod p)
457  * = step1(low 256 bits) + step1(high 260 bits) * 38 (mod p)
458  *
459  * There are 6 bits in the constant value 38. The step 2
460  * multiplication product will therefore have 260+6=266 bits,
461  * and the step 2 result (after the addition) will therefore
462  * have 267 bits.
463  */
464  static_assert ( sizeof ( step2->product ) >= sizeof ( step2->value ) );
465  static_assert ( sizeof ( step2->product ) >= sizeof ( step2->parts ) );
466  bigint_grow ( &step1->parts.low_256bit, &result->value );
467  bigint_multiply ( &step1->parts.high_260bit, &x25519_reduce_256,
468  &step2->product );
469  bigint_add ( &result->value, &step2->value );
470 
471  /* Step 3: reduce high-order 267-256=11 bits of step 2 result
472  *
473  * Use the identity 2^256=38 (mod p) again to reduce the
474  * high-order bits of the step 2 result. As before, we split
475  * the 267-bit result from step 2 into its low-order 256 bits
476  * and high-order 11 bits:
477  *
478  * step2 = step2(low 256 bits) + step2(high 11 bits) * 2^256
479  *
480  * and then perform the calculation:
481  *
482  * step3 = step2 (mod p)
483  * = step2(low 256 bits) + step2(high 11 bits) * 2^256 (mod p)
484  * = step2(low 256 bits) + step2(high 11 bits) * 38 (mod p)
485  *
486  * There are 6 bits in the constant value 38. The step 3
487  * multiplication product will therefore have 11+6=19 bits,
488  * and the step 3 result (after the addition) will therefore
489  * have 257 bits.
490  *
491  * A loose upper bound for the step 3 result (after the
492  * addition) is given by:
493  *
494  * step3 < ( 2^256 - 1 ) + ( 2^19 - 1 )
495  * < ( 2^257 - 2^256 - 1 ) + ( 2^19 - 1 )
496  * < ( 2^257 - 76 ) - 2^256 + 2^19 + 74
497  * < 4 * ( 2^255 - 19 ) - 2^256 + 2^19 + 74
498  * < 4p - 2^256 + 2^19 + 74
499  *
500  * and so the step 3 result is strictly less than 4p, and
501  * therefore lies within the range [0,4p-1].
502  */
503  memset ( &step3->value, 0, sizeof ( step3->value ) );
504  bigint_grow ( &step2->parts.low_256bit, &result->value );
505  bigint_multiply ( &step2->parts.high_11bit, &x25519_reduce_256,
506  &step3->product );
507  bigint_add ( &step3->value, &result->value );
508 
509  /* Step 1 calculates the product of the input operands, and
510  * each subsequent step reduces the number of bits in the
511  * result while preserving this value (modulo p). The final
512  * result is therefore equal to the product of the input
513  * operands (modulo p), as required.
514  *
515  * The step 3 result lies within the range [0,4p-1] and the
516  * final result is therefore a valid X25519 unsigned 257-bit
517  * integer, as required.
518  */
519 }
520 
521 /**
522  * Compute multiplicative inverse
523  *
524  * @v invertend Big integer to be inverted
525  * @v result Big integer to hold result (may not overlap input)
526  */
527 void x25519_invert ( const union x25519_oct258 *invertend,
528  union x25519_quad257 *result ) {
529  int i;
530 
531  /* Sanity check */
532  assert ( invertend != &result->oct258 );
533 
534  /* Calculate inverse as x^(-1)=x^(p-2) where p is the field prime
535  *
536  * The field prime is p=2^255-19 and so:
537  *
538  * p - 2 = 2^255 - 21
539  * = (2^255 - 1) - 2^4 - 2^2
540  *
541  * i.e. p-2 is a 254-bit number in which all bits are set
542  * apart from bit 2 and bit 4.
543  *
544  * We use the square-and-multiply method to compute x^(p-2).
545  */
546  bigint_copy ( &invertend->value, &result->value );
547  for ( i = 253 ; i >= 0 ; i-- ) {
548 
549  /* Square running total */
550  x25519_multiply ( &result->oct258, &result->oct258, result );
551 
552  /* For each set bit in the exponent, multiply by invertend */
553  if ( ( i != 2 ) && ( i != 4 ) ) {
554  x25519_multiply ( invertend, &result->oct258, result );
555  }
556  }
557 }
558 
559 /**
560  * Reduce big integer via conditional subtraction
561  *
562  * @v subtrahend Big integer to subtract
563  * @v value Big integer to be subtracted from, if possible
564  */
565 static void x25519_reduce_by ( const x25519_t *subtrahend, x25519_t *value ) {
566  unsigned int max_bit = ( ( 8 * sizeof ( *value ) ) - 1 );
567  x25519_t tmp;
568 
569  /* Conditionally subtract subtrahend
570  *
571  * Subtract the subtrahend, discarding the result (in constant
572  * time) if the subtraction underflows.
573  */
574  bigint_copy ( value, &tmp );
576  bigint_swap ( value, &tmp, bigint_bit_is_set ( value, max_bit ) );
577 }
578 
579 /**
580  * Reduce big integer to canonical range
581  *
582  * @v value Big integer to be reduced
583  */
585 
586  /* Conditionally subtract 2p
587  *
588  * Subtract twice the field prime, discarding the result (in
589  * constant time) if the subtraction underflows.
590  *
591  * The input value is in the range [0,4p-1]. After this
592  * conditional subtraction, the value is in the range
593  * [0,2p-1].
594  */
595  x25519_reduce_by ( &x25519_2p, &value->value );
596 
597  /* Conditionally subtract p
598  *
599  * Subtract the field prime, discarding the result (in
600  * constant time) if the subtraction underflows.
601  *
602  * The value is already in the range [0,2p-1]. After this
603  * conditional subtraction, the value is in the range [0,p-1]
604  * and is therefore the canonical representation.
605  */
606  x25519_reduce_by ( &x25519_p, &value->value );
607 }
608 
609 /**
610  * Compute next step of the Montgomery ladder
611  *
612  * @v base Base point
613  * @v bit Bit value
614  * @v step Ladder step
615  */
616 static void x25519_step ( const union x25519_quad257 *base, int bit,
617  struct x25519_step *step ) {
618  union x25519_quad257 *a = &step->x_n.X;
619  union x25519_quad257 *b = &step->x_n1.X;
620  union x25519_quad257 *c = &step->x_n.Z;
621  union x25519_quad257 *d = &step->x_n1.Z;
622  union x25519_oct258 e;
623  union x25519_quad257 f;
624  union x25519_oct258 *v1_e;
625  union x25519_oct258 *v2_a;
626  union x25519_oct258 *v3_c;
627  union x25519_oct258 *v4_b;
628  union x25519_quad257 *v5_d;
629  union x25519_quad257 *v6_f;
630  union x25519_quad257 *v7_a;
631  union x25519_quad257 *v8_c;
632  union x25519_oct258 *v9_e;
633  union x25519_oct258 *v10_a;
634  union x25519_quad257 *v11_b;
635  union x25519_oct258 *v12_c;
636  union x25519_quad257 *v13_a;
637  union x25519_oct258 *v14_a;
638  union x25519_quad257 *v15_c;
639  union x25519_quad257 *v16_a;
640  union x25519_quad257 *v17_d;
641  union x25519_quad257 *v18_b;
642 
643  /* See the referenced paper "Implementing Curve25519/X25519: A
644  * Tutorial on Elliptic Curve Cryptography" for the reasoning
645  * behind this calculation.
646  */
647 
648  /* Reuse storage locations for intermediate results where possible */
649  v1_e = &e;
650  v2_a = container_of ( &a->value, union x25519_oct258, value );
651  v3_c = container_of ( &c->value, union x25519_oct258, value );
652  v4_b = container_of ( &b->value, union x25519_oct258, value );
653  v5_d = d;
654  v6_f = &f;
655  v7_a = a;
656  v8_c = c;
657  v9_e = &e;
658  v10_a = container_of ( &a->value, union x25519_oct258, value );
659  v11_b = b;
660  v12_c = container_of ( &c->value, union x25519_oct258, value );
661  v13_a = a;
662  v14_a = container_of ( &a->value, union x25519_oct258, value );
663  v15_c = c;
664  v16_a = a;
665  v17_d = d;
666  v18_b = b;
667 
668  /* Select inputs */
669  bigint_swap ( &a->value, &b->value, bit );
670  bigint_swap ( &c->value, &d->value, bit );
671 
672  /* v1 = a + c */
673  x25519_add ( a, c, v1_e );
674 
675  /* v2 = a - c */
676  x25519_subtract ( a, c, v2_a );
677 
678  /* v3 = b + d */
679  x25519_add ( b, d, v3_c );
680 
681  /* v4 = b - d */
682  x25519_subtract ( b, d, v4_b );
683 
684  /* v5 = v1^2 = (a + c)^2 = a^2 + 2ac + c^2 */
685  x25519_multiply ( v1_e, v1_e, v5_d );
686 
687  /* v6 = v2^2 = (a - c)^2 = a^2 - 2ac + c^2 */
688  x25519_multiply ( v2_a, v2_a, v6_f );
689 
690  /* v7 = v3 * v2 = (b + d) * (a - c) = ab - bc + ad - cd */
691  x25519_multiply ( v3_c, v2_a, v7_a );
692 
693  /* v8 = v4 * v1 = (b - d) * (a + c) = ab + bc - ad - cd */
694  x25519_multiply ( v4_b, v1_e, v8_c );
695 
696  /* v9 = v7 + v8 = 2 * (ab - cd) */
697  x25519_add ( v7_a, v8_c, v9_e );
698 
699  /* v10 = v7 - v8 = 2 * (ad - bc) */
700  x25519_subtract ( v7_a, v8_c, v10_a );
701 
702  /* v11 = v10^2 = 4 * (ad - bc)^2 */
703  x25519_multiply ( v10_a, v10_a, v11_b );
704 
705  /* v12 = v5 - v6 = (a + c)^2 - (a - c)^2 = 4ac */
706  x25519_subtract ( v5_d, v6_f, v12_c );
707 
708  /* v13 = v12 * 121665 = 486660ac = (A-2) * ac */
709  x25519_multiply ( v12_c, &x25519_121665, v13_a );
710 
711  /* v14 = v13 + v5 = (A-2) * ac + a^2 + 2ac + c^2 = a^2 + A * ac + c^2 */
712  x25519_add ( v13_a, v5_d, v14_a );
713 
714  /* v15 = v12 * v14 = 4ac * (a^2 + A * ac + c^2) */
715  x25519_multiply ( v12_c, v14_a, v15_c );
716 
717  /* v16 = v5 * v6 = (a + c)^2 * (a - c)^2 = (a^2 - c^2)^2 */
718  x25519_multiply ( &v5_d->oct258, &v6_f->oct258, v16_a );
719 
720  /* v17 = v11 * base = 4 * base * (ad - bc)^2 */
721  x25519_multiply ( &v11_b->oct258, &base->oct258, v17_d );
722 
723  /* v18 = v9^2 = 4 * (ab - cd)^2 */
724  x25519_multiply ( v9_e, v9_e, v18_b );
725 
726  /* Select outputs */
727  bigint_swap ( &a->value, &b->value, bit );
728  bigint_swap ( &c->value, &d->value, bit );
729 }
730 
731 /**
732  * Multiply X25519 elliptic curve point
733  *
734  * @v base Base point
735  * @v scalar Scalar multiple
736  * @v result Point to hold result (may overlap base point)
737  */
738 static void x25519_ladder ( const union x25519_quad257 *base,
739  struct x25519_value *scalar,
740  union x25519_quad257 *result ) {
741  static const uint8_t zero[] = { 0 };
742  static const uint8_t one[] = { 1 };
743  struct x25519_step step;
744  union x25519_quad257 *tmp;
745  int bit;
746  int i;
747 
748  /* Initialise ladder */
749  bigint_init ( &step.x_n.X.value, one, sizeof ( one ) );
750  bigint_init ( &step.x_n.Z.value, zero, sizeof ( zero ) );
751  bigint_copy ( &base->value, &step.x_n1.X.value );
752  bigint_init ( &step.x_n1.Z.value, one, sizeof ( one ) );
753 
754  /* Use ladder */
755  for ( i = 254 ; i >= 0 ; i-- ) {
756  bit = ( ( scalar->raw[ i / 8 ] >> ( i % 8 ) ) & 1 );
757  x25519_step ( base, bit, &step );
758  }
759 
760  /* Convert back to affine coordinate */
761  tmp = &step.x_n1.X;
762  x25519_invert ( &step.x_n.Z.oct258, tmp );
763  x25519_multiply ( &step.x_n.X.oct258, &tmp->oct258, result );
764  x25519_reduce ( result );
765 }
766 
767 /**
768  * Reverse X25519 value endianness
769  *
770  * @v value Value to reverse
771  */
772 static void x25519_reverse ( struct x25519_value *value ) {
773  uint8_t *low = value->raw;
774  uint8_t *high = &value->raw[ sizeof ( value->raw ) - 1 ];
775  uint8_t tmp;
776 
777  /* Reverse bytes */
778  do {
779  tmp = *low;
780  *low = *high;
781  *high = tmp;
782  } while ( ++low < --high );
783 }
784 
785 /**
786  * Calculate X25519 key
787  *
788  * @v base Base point
789  * @v scalar Scalar multiple
790  * @v result Point to hold result (may overlap base point)
791  * @ret rc Return status code
792  */
793 int x25519_key ( const struct x25519_value *base,
794  const struct x25519_value *scalar,
795  struct x25519_value *result ) {
796  struct x25519_value *tmp = result;
797  union x25519_quad257 point;
798 
799  /* Reverse base point and clear high bit as required by RFC7748 */
800  memcpy ( tmp, base, sizeof ( *tmp ) );
801  x25519_reverse ( tmp );
802  tmp->raw[0] &= 0x7f;
803  bigint_init ( &point.value, tmp->raw, sizeof ( tmp->raw ) );
804 
805  /* Clamp scalar as required by RFC7748 */
806  memcpy ( tmp, scalar, sizeof ( *tmp ) );
807  tmp->raw[0] &= 0xf8;
808  tmp->raw[31] |= 0x40;
809 
810  /* Multiply elliptic curve point */
811  x25519_ladder ( &point, tmp, &point );
812 
813  /* Reverse result */
814  bigint_done ( &point.value, result->raw, sizeof ( result->raw ) );
816 
817  /* Fail if result was all zeros (as required by RFC8422) */
818  return ( bigint_is_zero ( &point.value ) ? -EPERM : 0 );
819 }
820 
821 /**
822  * Multiply scalar by curve point
823  *
824  * @v base Base point (or NULL to use generator)
825  * @v scalar Scalar multiple
826  * @v result Result point to fill in
827  * @ret rc Return status code
828  */
829 static int x25519_curve_multiply ( const void *base, const void *scalar,
830  void *result ) {
831 
832  /* Use base point if applicable */
833  if ( ! base )
835 
836  return x25519_key ( base, scalar, result );
837 }
838 
839 /** X25519 elliptic curve */
841  .name = "x25519",
842  .keysize = sizeof ( struct x25519_value ),
843  .multiply = x25519_curve_multiply,
844 };
union x25519_multiply_step1 __attribute__
bigint_t(bigint_required_size((260+7)/8)+bigint_required_size((6+7)/8))
Raw product.
Definition: x25519.c:151
uint32_t c
Definition: md4.c:30
static void x25519_step(const union x25519_quad257 *base, int bit, struct x25519_step *step)
Compute next step of the Montgomery ladder.
Definition: x25519.c:616
int x25519_key(const struct x25519_value *base, const struct x25519_value *scalar, struct x25519_value *result)
Calculate X25519 key.
Definition: x25519.c:793
void x25519_reduce(union x25519_quad257 *value)
Reduce big integer to canonical range.
Definition: x25519.c:584
uint32_t low
Low 16 bits of address.
Definition: myson.h:19
static const void const void void * result
Definition: crypto.h:335
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:425
void(* initialise)(void)
Definition: init.h:15
const union x25519_oct258 oct258
X25519 unsigned 258-bit integer.
Definition: x25519.h:73
static unsigned int unsigned int bit
Definition: bigint.h:208
struct elliptic_curve x25519_curve
X25519 elliptic curve.
Definition: x25519.c:840
Error codes.
static void x25519_init_constants(void)
Initialise constants.
Definition: x25519.c:313
An X25519 unsigned 257-bit integer.
Definition: x25519.h:64
static const uint8_t x25519_reduce_256_raw[]
Reduction constant (used during multiplication)
Definition: x25519.c:292
An X25519 Montgomery ladder step.
Definition: x25519.c:267
static const void const void * scalar
Definition: crypto.h:335
struct x25519_projective x_n
X[n]/Z[n].
Definition: x25519.c:269
#define bigint_grow(source, dest)
Grow big integer.
Definition: bigint.h:161
union x25519_multiply_step3 step3
Step 3 result.
Definition: x25519.c:237
An X25519 unsigned 258-bit integer.
Definition: x25519.h:45
#define bigint_init(value, data, len)
Initialise big integer.
Definition: bigint.h:50
Cryptographic API.
uint32_t zero
Must be zero.
Definition: ntlm.h:24
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
#define static_assert(x)
Assert a condition at build time.
Definition: assert.h:65
union x25519_quad257 Z
Z coordinate.
Definition: x25519.c:263
#define bigint_is_zero(value)
Test if big integer is equal to zero.
Definition: bigint.h:118
static const void * base
Base address.
Definition: crypto.h:335
uint8_t multiplier
Port multiplier number.
Definition: edd.h:32
uint32_t a
Definition: md4.c:28
static x25519_t x25519_4p
Constant 4p=2^257-76.
Definition: x25519.c:289
static x25519_t x25519_p
Constant p=2^255-19 (the finite field prime)
Definition: x25519.c:283
static __always_inline off_t userptr_t subtrahend
Definition: efi_uaccess.h:61
unsigned long tmp
Definition: linux_pci.h:53
static bigint_t(bigint_required_size(sizeof(x25519_reduce_256_raw)))
Reduction constant (used during multiplication)
Definition: x25519.c:295
#define INIT_NORMAL
Normal initialisation.
Definition: init.h:30
uint32_t e
Definition: sha1.c:32
void * memcpy(void *dest, const void *src, size_t len) __nonnull
static void x25519_reduce_by(const x25519_t *subtrahend, x25519_t *value)
Reduce big integer via conditional subtraction.
Definition: x25519.c:565
static void x25519_ladder(const union x25519_quad257 *base, struct x25519_value *scalar, union x25519_quad257 *result)
Multiply X25519 elliptic curve point.
Definition: x25519.c:738
An initialisation function.
Definition: init.h:14
static void x25519_subtract(const union x25519_quad257 *minuend, const union x25519_quad257 *subtrahend, union x25519_oct258 *result)
Subtract big integers modulo field prime.
Definition: x25519.c:381
Assertions.
X25519 multiplication step 2 result.
Definition: x25519.c:144
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
X25519 key exchange.
uint8_t pad[sizeof(union x25519_multiply_step2) - offsetof(union x25519_multiply_step1, parts.high_260bit)]
Padding to avoid collision between steps 1 and 2.
Definition: x25519.c:228
bigint_t(X25519_SIZE+X25519_SIZE) product
Raw product.
#define bigint_copy(source, dest)
Copy big integer.
Definition: bigint.h:187
static void x25519_add(const union x25519_quad257 *augend, const union x25519_quad257 *addend, union x25519_oct258 *result)
Add big integers modulo field prime.
Definition: x25519.c:348
#define build_assert(condition)
Assert a condition at build time (after dead code elimination)
Definition: assert.h:76
static x25519_t x25519_2p
Constant 2p=2^256-38.
Definition: x25519.c:286
struct x25519_multiply_step1::@432 parts
Partition into low-order and high-order bits.
x25519_t value
Big integer value.
Definition: x25519.h:66
static void x25519_reverse(struct x25519_value *value)
Reverse X25519 value endianness.
Definition: x25519.c:772
#define bigint_done(value, out, len)
Finalise big integer.
Definition: bigint.h:63
const char * name
Curve name.
Definition: crypto.h:201
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
uint32_t high
High 32 bits of address.
Definition: myson.h:20
#define bigint_required_size(len)
Determine number of elements required for a big-integer type.
Definition: bigint.h:30
union x25519_quad257 X
X coordinate.
Definition: x25519.c:261
X25519 multiplication step 3 result.
Definition: x25519.c:188
unsigned char uint8_t
Definition: stdint.h:10
#define X25519_REDUCE_256
X25519 reduction constant.
Definition: x25519.c:97
X25519 multiplication step 1 result.
Definition: x25519.c:107
#define bigint_swap(first, second, swap)
Conditionally swap big integers (in constant time)
Definition: bigint.h:199
static const uint8_t x25519_p_raw[]
Constant p=2^255-19 (the finite field prime)
Definition: x25519.c:275
X25519 multiplication temporary working space.
Definition: x25519.c:216
static int x25519_curve_multiply(const void *base, const void *scalar, void *result)
Multiply scalar by curve point.
Definition: x25519.c:829
#define EPERM
Operation not permitted.
Definition: errno.h:614
union x25519_multiply_step2 step2
Step 2 result.
Definition: x25519.c:235
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
An elliptic curve.
Definition: crypto.h:199
#define X25519_SIZE
X25519 unsigned big integer size.
Definition: x25519.h:26
static struct x25519_value x25519_generator
Constant g=9 (the group generator)
Definition: x25519.c:305
struct x25519_projective x_n1
X[n+1]/Z[n+1].
Definition: x25519.c:271
void step(void)
Single-step a single process.
Definition: process.c:98
An X25519 elliptic curve point in projective coordinates.
Definition: x25519.c:259
uint32_t b
Definition: md4.c:29
uint32_t d
Definition: md4.c:31
#define bigint_multiply(multiplicand, multiplier, result)
Multiply big integers.
Definition: bigint.h:212
uint8_t raw[32]
Raw value.
Definition: x25519.h:79
uint32_t f
Definition: sha256.c:33
uint8_t product
Product string.
Definition: smbios.h:16
void x25519_invert(const union x25519_oct258 *invertend, union x25519_quad257 *result)
Compute multiplicative inverse.
Definition: x25519.c:527
struct init_fn x25519_init_fn __init_fn(INIT_NORMAL)
Initialisation function.
#define bigint_bit_is_set(value, bit)
Test if bit is set in big integer.
Definition: bigint.h:141
union x25519_multiply_step1 step1
Step 1 result.
Definition: x25519.c:230
#define bigint_subtract(subtrahend, value)
Subtract big integers.
Definition: bigint.h:85
#define bigint_add(addend, value)
Add big integers.
Definition: bigint.h:74
String functions.
static union x25519_oct258 x25519_121665
Constant 121665 (used in the Montgomery ladder)
Definition: x25519.c:302
An X25519 32-byte value.
Definition: x25519.h:77
x25519_t value
Big integer value.
Definition: x25519.h:47
void * memset(void *dest, int character, size_t len) __nonnull