Skip to content

Commit 0ed42a6

Browse files
zx2c4herbertx
authored andcommitted
crypto: curve25519 - generic C library implementations
This contains two formally verified C implementations of the Curve25519 scalar multiplication function, one for 32-bit systems, and one for 64-bit systems whose compiler supports efficient 128-bit integer types. Not only are these implementations formally verified, but they are also the fastest available C implementations. They have been modified to be friendly to kernel space and to be generally less horrendous looking, but still an effort has been made to retain their formally verified characteristic, and so the C might look slightly unidiomatic. The 64-bit version comes from HACL*: https://github.com/project-everest/hacl-star The 32-bit version comes from Fiat: https://github.com/mit-plv/fiat-crypto Information: https://cr.yp.to/ecdh.html Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> [ardb: - move from lib/zinc to lib/crypto - replace .c #includes with Kconfig based object selection - drop simd handling and simplify support for per-arch versions ] Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent ed0356e commit 0ed42a6

6 files changed

Lines changed: 1778 additions & 0 deletions

File tree

‎include/crypto/curve25519.h‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2+
/*
3+
* Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4+
*/
5+
6+
#ifndef CURVE25519_H
7+
#define CURVE25519_H
8+
9+
#include <crypto/algapi.h> // For crypto_memneq.
10+
#include <linux/types.h>
11+
#include <linux/random.h>
12+
13+
enum curve25519_lengths {
14+
CURVE25519_KEY_SIZE = 32
15+
};
16+
17+
extern const u8 curve25519_null_point[];
18+
extern const u8 curve25519_base_point[];
19+
20+
void curve25519_generic(u8 out[CURVE25519_KEY_SIZE],
21+
const u8 scalar[CURVE25519_KEY_SIZE],
22+
const u8 point[CURVE25519_KEY_SIZE]);
23+
24+
void curve25519_arch(u8 out[CURVE25519_KEY_SIZE],
25+
const u8 scalar[CURVE25519_KEY_SIZE],
26+
const u8 point[CURVE25519_KEY_SIZE]);
27+
28+
void curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
29+
const u8 secret[CURVE25519_KEY_SIZE]);
30+
31+
static inline
32+
bool __must_check curve25519(u8 mypublic[CURVE25519_KEY_SIZE],
33+
const u8 secret[CURVE25519_KEY_SIZE],
34+
const u8 basepoint[CURVE25519_KEY_SIZE])
35+
{
36+
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CURVE25519))
37+
curve25519_arch(mypublic, secret, basepoint);
38+
else
39+
curve25519_generic(mypublic, secret, basepoint);
40+
return crypto_memneq(mypublic, curve25519_null_point,
41+
CURVE25519_KEY_SIZE);
42+
}
43+
44+
static inline bool
45+
__must_check curve25519_generate_public(u8 pub[CURVE25519_KEY_SIZE],
46+
const u8 secret[CURVE25519_KEY_SIZE])
47+
{
48+
if (unlikely(!crypto_memneq(secret, curve25519_null_point,
49+
CURVE25519_KEY_SIZE)))
50+
return false;
51+
52+
if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_CURVE25519))
53+
curve25519_base_arch(pub, secret);
54+
else
55+
curve25519_generic(pub, secret, curve25519_base_point);
56+
return crypto_memneq(pub, curve25519_null_point, CURVE25519_KEY_SIZE);
57+
}
58+
59+
static inline void curve25519_clamp_secret(u8 secret[CURVE25519_KEY_SIZE])
60+
{
61+
secret[0] &= 248;
62+
secret[31] = (secret[31] & 127) | 64;
63+
}
64+
65+
static inline void curve25519_generate_secret(u8 secret[CURVE25519_KEY_SIZE])
66+
{
67+
get_random_bytes_wait(secret, CURVE25519_KEY_SIZE);
68+
curve25519_clamp_secret(secret);
69+
}
70+
71+
#endif /* CURVE25519_H */

‎lib/crypto/Kconfig‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,31 @@ config CRYPTO_LIB_CHACHA
5959
by either the generic implementation or an arch-specific one, if one
6060
is available and enabled.
6161

62+
config CRYPTO_ARCH_HAVE_LIB_CURVE25519
63+
tristate
64+
help
65+
Declares whether the architecture provides an arch-specific
66+
accelerated implementation of the Curve25519 library interface,
67+
either builtin or as a module.
68+
69+
config CRYPTO_LIB_CURVE25519_GENERIC
70+
tristate
71+
help
72+
This symbol can be depended upon by arch implementations of the
73+
Curve25519 library interface that require the generic code as a
74+
fallback, e.g., for SIMD implementations. If no arch specific
75+
implementation is enabled, this implementation serves the users
76+
of CRYPTO_LIB_CURVE25519.
77+
78+
config CRYPTO_LIB_CURVE25519
79+
tristate "Curve25519 scalar multiplication library"
80+
depends on CRYPTO_ARCH_HAVE_LIB_CURVE25519 || !CRYPTO_ARCH_HAVE_LIB_CURVE25519
81+
select CRYPTO_LIB_CURVE25519_GENERIC if CRYPTO_ARCH_HAVE_LIB_CURVE25519=n
82+
help
83+
Enable the Curve25519 library interface. This interface may be
84+
fulfilled by either the generic implementation or an arch-specific
85+
one, if one is available and enabled.
86+
6287
config CRYPTO_LIB_DES
6388
tristate
6489

‎lib/crypto/Makefile‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ libblake2s-generic-y += blake2s-generic.o
1616
obj-$(CONFIG_CRYPTO_LIB_BLAKE2S) += libblake2s.o
1717
libblake2s-y += blake2s.o
1818

19+
obj-$(CONFIG_CRYPTO_LIB_CURVE25519_GENERIC) += libcurve25519.o
20+
libcurve25519-y := curve25519-fiat32.o
21+
libcurve25519-$(CONFIG_ARCH_SUPPORTS_INT128) := curve25519-hacl64.o
22+
libcurve25519-y += curve25519.o
23+
1924
obj-$(CONFIG_CRYPTO_LIB_DES) += libdes.o
2025
libdes-y := des.o
2126

0 commit comments

Comments
 (0)