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