iPXE
|
X25519 key exchange. More...
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <ipxe/init.h>
#include <ipxe/crypto.h>
#include <ipxe/x25519.h>
Go to the source code of this file.
Data Structures | |
union | x25519_multiply_step1 |
X25519 multiplication step 1 result. More... | |
union | x25519_multiply_step2 |
X25519 multiplication step 2 result. More... | |
union | x25519_multiply_step3 |
X25519 multiplication step 3 result. More... | |
union | x25519_multiply_workspace |
X25519 multiplication temporary working space. More... | |
struct | x25519_projective |
An X25519 elliptic curve point in projective coordinates. More... | |
struct | x25519_step |
An X25519 Montgomery ladder step. More... | |
Macros | |
#define | X25519_REDUCE_256 38 |
X25519 reduction constant. More... | |
Functions | |
FILE_LICENCE (GPL2_OR_LATER_OR_UBDL) | |
static | bigint_t (bigint_required_size(sizeof(x25519_reduce_256_raw))) |
Reduction constant (used during multiplication) More... | |
static void | x25519_init_constants (void) |
Initialise constants. More... | |
struct init_fn x25519_init_fn | __init_fn (INIT_NORMAL) |
Initialisation function. More... | |
static void | x25519_add (const union x25519_quad257 *augend, const union x25519_quad257 *addend, union x25519_oct258 *result) |
Add big integers modulo field prime. More... | |
static void | x25519_subtract (const union x25519_quad257 *minuend, const union x25519_quad257 *subtrahend, union x25519_oct258 *result) |
Subtract big integers modulo field prime. More... | |
void | x25519_multiply (const union x25519_oct258 *multiplicand, const union x25519_oct258 *multiplier, union x25519_quad257 *result) |
Multiply big integers modulo field prime. More... | |
void | x25519_invert (const union x25519_oct258 *invertend, union x25519_quad257 *result) |
Compute multiplicative inverse. More... | |
static void | x25519_reduce_by (const x25519_t *subtrahend, x25519_t *value) |
Reduce big integer via conditional subtraction. More... | |
void | x25519_reduce (union x25519_quad257 *value) |
Reduce big integer to canonical range. More... | |
static void | x25519_step (const union x25519_quad257 *base, int bit, struct x25519_step *step) |
Compute next step of the Montgomery ladder. More... | |
static void | x25519_ladder (const union x25519_quad257 *base, struct x25519_value *scalar, union x25519_quad257 *result) |
Multiply X25519 elliptic curve point. More... | |
static void | x25519_reverse (struct x25519_value *value) |
Reverse X25519 value endianness. More... | |
int | x25519_key (const struct x25519_value *base, const struct x25519_value *scalar, struct x25519_value *result) |
Calculate X25519 key. More... | |
static int | x25519_curve_multiply (const void *base, const void *scalar, void *result) |
Multiply scalar by curve point. More... | |
Variables | |
static const uint8_t | x25519_p_raw [] |
Constant p=2^255-19 (the finite field prime) More... | |
static x25519_t | x25519_p |
Constant p=2^255-19 (the finite field prime) More... | |
static x25519_t | x25519_2p |
Constant 2p=2^256-38. More... | |
static x25519_t | x25519_4p |
Constant 4p=2^257-76. More... | |
static const uint8_t | x25519_reduce_256_raw [] = { X25519_REDUCE_256 } |
Reduction constant (used during multiplication) More... | |
static union x25519_oct258 | x25519_121665 |
Constant 121665 (used in the Montgomery ladder) More... | |
static struct x25519_value | x25519_generator |
Constant g=9 (the group generator) More... | |
struct elliptic_curve | x25519_curve |
X25519 elliptic curve. More... | |
X25519 key exchange.
This implementation is inspired by and partially based upon the paper "Implementing Curve25519/X25519: A Tutorial on Elliptic Curve Cryptography" by Martin Kleppmann, available for download from https://www.cl.cam.ac.uk/teaching/2122/Crypto/curve25519.pdf
The underlying modular addition, subtraction, and multiplication operations are completely redesigned for substantially improved efficiency compared to the TweetNaCl implementation studied in that paper.
TweetNaCl iPXE --------- ----
Storage size of each big integer 128 40 (in bytes)
Stack usage for key exchange 1144 360 (in bytes, large objects only)
Cost of big integer addition 16 5 (in number of 64-bit additions)
Cost of big integer multiplication 273 31 (in number of 64-bit multiplications)
The implementation is constant-time (provided that the underlying big integer operations are also constant-time).
Definition in file x25519.c.
#define X25519_REDUCE_256 38 |
X25519 reduction constant.
The X25519 field prime is p=2^255-19. This gives us:
p = 2^255 - 19 2^255 = p + 19 2^255 = 19 (mod p) k * 2^255 = k * 19 (mod p)
We can therefore reduce a value modulo p by taking the high-order bits of the value from bit 255 and above, multiplying by 19, and adding this to the low-order 255 bits of the value.
This would be cumbersome to do in practice since it would require partitioning the value at a 255-bit boundary (and hence would require some shifting and masking operations). However, we can note that:
k * 2^255 = k * 19 (mod p)
k * 2 * 2^255 = k * 2 * 19 (mod p) k * 2^256 = k * 38 (mod p)
We can therefore simplify the reduction to taking the high order bits of the value from bit 256 and above, multiplying by 38, and adding this to the low-order 256 bits of the value.
Since 256 will inevitably be a multiple of the big integer element size (typically 32 or 64 bits), this avoids the need to perform any shifting or masking operations.
FILE_LICENCE | ( | GPL2_OR_LATER_OR_UBDL | ) |
|
static |
|
static |
Initialise constants.
Definition at line 313 of file x25519.c.
References bigint_add, bigint_copy, bigint_init, x25519_oct258::value, x25519_121665, x25519_2p, x25519_4p, x25519_p, x25519_p_raw, and x25519_reduce_256_raw.
struct init_fn x25519_init_fn __init_fn | ( | INIT_NORMAL | ) |
Initialisation function.
|
inlinestatic |
Add big integers modulo field prime.
augend | Big integer to add |
addend | Big integer to add |
result | Big integer to hold result (may overlap augend) |
Definition at line 348 of file x25519.c.
References bigint_add, bigint_copy, build_assert, x25519_quad257::oct258, result, x25519_oct258::value, and x25519_quad257::value.
Referenced by x25519_step().
|
inlinestatic |
Subtract big integers modulo field prime.
minuend | Big integer from which to subtract |
subtrahend | Big integer to subtract |
result | Big integer to hold result (may overlap minuend) |
Definition at line 381 of file x25519.c.
References bigint_add, bigint_copy, bigint_subtract, build_assert, x25519_quad257::oct258, result, subtrahend, x25519_oct258::value, and x25519_4p.
Referenced by x25519_step().
void x25519_multiply | ( | const union x25519_oct258 * | multiplicand, |
const union x25519_oct258 * | multiplier, | ||
union x25519_quad257 * | result | ||
) |
Multiply big integers modulo field prime.
multiplicand | Big integer to be multiplied |
multiplier | Big integer to be multiplied |
result | Big integer to hold result (may overlap either input) |
Definition at line 425 of file x25519.c.
References bigint_add, bigint_grow, bigint_multiply, memset(), multiplier, x25519_multiply_step1::parts, x25519_multiply_step2::parts, result, static_assert, tmp, and x25519_oct258::value.
Referenced by x25519_invert(), x25519_invert_okx(), x25519_ladder(), x25519_multiply_okx(), and x25519_step().
void x25519_invert | ( | const union x25519_oct258 * | invertend, |
union x25519_quad257 * | result | ||
) |
Compute multiplicative inverse.
invertend | Big integer to be inverted |
result | Big integer to hold result (may not overlap input) |
Definition at line 527 of file x25519.c.
References assert(), bigint_copy, result, x25519_oct258::value, and x25519_multiply().
Referenced by x25519_invert_okx(), and x25519_ladder().
|
static |
Reduce big integer via conditional subtraction.
subtrahend | Big integer to subtract |
value | Big integer to be subtracted from, if possible |
Definition at line 565 of file x25519.c.
References bigint_copy, bigint_subtract, bigint_swap, subtrahend, tmp, and value.
Referenced by x25519_reduce().
void x25519_reduce | ( | union x25519_quad257 * | value | ) |
Reduce big integer to canonical range.
value | Big integer to be reduced |
Definition at line 584 of file x25519.c.
References value, x25519_2p, x25519_p, and x25519_reduce_by().
Referenced by x25519_invert_okx(), x25519_ladder(), and x25519_multiply_okx().
|
static |
Compute next step of the Montgomery ladder.
base | Base point |
bit | Bit value |
step | Ladder step |
Definition at line 616 of file x25519.c.
References base, bigint_swap, bit, c, container_of, x25519_quad257::oct258, step(), value, x25519_quad257::value, x25519_121665, x25519_add(), x25519_multiply(), and x25519_subtract().
Referenced by x25519_ladder().
|
static |
Multiply X25519 elliptic curve point.
base | Base point |
scalar | Scalar multiple |
result | Point to hold result (may overlap base point) |
Definition at line 738 of file x25519.c.
References base, bigint_copy, bigint_init, bit, x25519_value::raw, result, step(), tmp, x25519_invert(), x25519_multiply(), x25519_reduce(), and x25519_step().
Referenced by x25519_key().
|
static |
int x25519_key | ( | const struct x25519_value * | base, |
const struct x25519_value * | scalar, | ||
struct x25519_value * | result | ||
) |
Calculate X25519 key.
base | Base point |
scalar | Scalar multiple |
result | Point to hold result (may overlap base point) |
rc | Return status code |
Definition at line 793 of file x25519.c.
References base, bigint_done, bigint_init, bigint_is_zero, EPERM, memcpy(), result, tmp, x25519_quad257::value, x25519_ladder(), and x25519_reverse().
Referenced by x25519_curve_multiply(), and x25519_key_okx().
|
static |
Multiply scalar by curve point.
base | Base point (or NULL to use generator) |
scalar | Scalar multiple |
result | Result point to fill in |
rc | Return status code |
Definition at line 829 of file x25519.c.
References base, result, x25519_generator, and x25519_key().
|
static |
Constant p=2^255-19 (the finite field prime)
Definition at line 275 of file x25519.c.
Referenced by x25519_init_constants().
|
static |
Constant p=2^255-19 (the finite field prime)
Definition at line 283 of file x25519.c.
Referenced by x25519_init_constants(), and x25519_reduce().
|
static |
Constant 2p=2^256-38.
Definition at line 286 of file x25519.c.
Referenced by x25519_init_constants(), and x25519_reduce().
|
static |
Constant 4p=2^257-76.
Definition at line 289 of file x25519.c.
Referenced by x25519_init_constants(), and x25519_subtract().
|
static |
Reduction constant (used during multiplication)
Definition at line 292 of file x25519.c.
Referenced by x25519_init_constants().
|
static |
Constant 121665 (used in the Montgomery ladder)
Definition at line 302 of file x25519.c.
Referenced by x25519_init_constants(), and x25519_step().
|
static |
Constant g=9 (the group generator)
Definition at line 305 of file x25519.c.
Referenced by x25519_curve_multiply().
struct elliptic_curve x25519_curve |
X25519 elliptic curve.