iPXE
x25519.h
Go to the documentation of this file.
1 #ifndef _IPXE_X25519_H
2 #define _IPXE_X25519_H
3 
4 /** @file
5  *
6  * X25519 key exchange
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <ipxe/bigint.h>
14 #include <ipxe/crypto.h>
15 
16 /** X25519 unsigned big integer size
17  *
18  * X25519 uses the finite field of integers modulo the prime
19  * p=2^255-19. The canonical representations of integers in this
20  * field therefore require only 255 bits.
21  *
22  * For internal calculations we use big integers containing up to 267
23  * bits, since this ends up allowing us to avoid some unnecessary (and
24  * expensive) intermediate reductions modulo p.
25  */
26 #define X25519_SIZE bigint_required_size ( ( 267 /* bits */ + 7 ) / 8 )
27 
28 /** An X25519 unsigned big integer used in internal calculations */
29 typedef bigint_t ( X25519_SIZE ) x25519_t;
30 
31 /** An X25519 unsigned 258-bit integer
32  *
33  * This is an unsigned integer N in the finite field of integers
34  * modulo the prime p=2^255-19.
35  *
36  * In this representation, N is encoded as any big integer that is in
37  * the same congruence class as N (i.e that has the same value as N
38  * modulo p) and that lies within the 258-bit range [0,8p-1].
39  *
40  * This type can be used as an input for multiplication (but not for
41  * addition or subtraction).
42  *
43  * Addition or subtraction will produce an output of this type.
44  */
46  /** Big integer value */
47  x25519_t value;
48 };
49 
50 /** An X25519 unsigned 257-bit integer
51  *
52  * This is an unsigned integer N in the finite field of integers
53  * modulo the prime p=2^255-19.
54  *
55  * In this representation, N is encoded as any big integer that is in
56  * the same congruence class as N (i.e that has the same value as N
57  * modulo p) and that lies within the 257-bit range [0,4p-1].
58  *
59  * This type can be used as an input for addition, subtraction, or
60  * multiplication.
61  *
62  * Multiplication will produce an output of this type.
63  */
65  /** Big integer value */
66  x25519_t value;
67  /** X25519 unsigned 258-bit integer
68  *
69  * Any value in the range [0,4p-1] is automatically also
70  * within the range [0,8p-1] and so may be consumed as an
71  * unsigned 258-bit integer.
72  */
73  const union x25519_oct258 oct258;
74 };
75 
76 /** An X25519 32-byte value */
77 struct x25519_value {
78  /** Raw value */
79  uint8_t raw[32];
80 };
81 
82 extern void x25519_multiply ( const union x25519_oct258 *multiplicand,
83  const union x25519_oct258 *multiplier,
84  union x25519_quad257 *result );
85 extern void x25519_invert ( const union x25519_oct258 *invertend,
86  union x25519_quad257 *result );
87 extern void x25519_reduce ( union x25519_quad257 *value );
88 extern int x25519_key ( const struct x25519_value *base,
89  const struct x25519_value *scalar,
90  struct x25519_value *result );
91 
92 extern struct elliptic_curve x25519_curve;
93 
94 #endif /* _IPXE_X25519_H */
static const void const void void * result
Definition: crypto.h:335
const union x25519_oct258 oct258
X25519 unsigned 258-bit integer.
Definition: x25519.h:73
An X25519 unsigned 257-bit integer.
Definition: x25519.h:64
static const void const void * scalar
Definition: crypto.h:335
An X25519 unsigned 258-bit integer.
Definition: x25519.h:45
Cryptographic API.
static const void * base
Base address.
Definition: crypto.h:335
uint8_t multiplier
Port multiplier number.
Definition: edd.h:32
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
Big integer support.
int x25519_key(const struct x25519_value *base, const struct x25519_value *scalar, struct x25519_value *result)
Calculate X25519 key.
Definition: x25519.c:793
x25519_t value
Big integer value.
Definition: x25519.h:66
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
unsigned char uint8_t
Definition: stdint.h:10
void x25519_reduce(union x25519_quad257 *value)
Reduce big integer to canonical range.
Definition: x25519.c:584
An elliptic curve.
Definition: crypto.h:199
#define X25519_SIZE
X25519 unsigned big integer size.
Definition: x25519.h:26
uint8_t raw[32]
Raw value.
Definition: x25519.h:79
struct elliptic_curve x25519_curve
X25519 elliptic curve.
Definition: x25519.c:840
void x25519_invert(const union x25519_oct258 *invertend, union x25519_quad257 *result)
Compute multiplicative inverse.
Definition: x25519.c:527
An X25519 32-byte value.
Definition: x25519.h:77
typedef bigint_t(X25519_SIZE) x25519_t
An X25519 unsigned big integer used in internal calculations.
x25519_t value
Big integer value.
Definition: x25519.h:47