| 1 |
// SPDX-License-Identifier: GPL-2.0 OR MIT |
| 2 |
/* |
| 3 |
* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved. |
| 4 |
* |
| 5 |
* This is an implementation of the Curve25519 ECDH algorithm, using either an |
| 6 |
* architecture-optimized implementation or a generic implementation. The |
| 7 |
* generic implementation is either 32-bit, or 64-bit with 128-bit integers, |
| 8 |
* depending on what is supported by the target compiler. |
| 9 |
* |
| 10 |
* Information: https://cr.yp.to/ecdh.html |
| 11 |
*/ |
| 12 |
|
| 13 |
#include <crypto/curve25519.h> |
| 14 |
#include <crypto/utils.h> |
| 15 |
#include <linux/export.h> |
| 16 |
#include <linux/init.h> |
| 17 |
#include <linux/module.h> |
| 18 |
|
| 19 |
static const u8 curve25519_null_point[CURVE25519_KEY_SIZE] __aligned(32) = { 0 }; |
| 20 |
static const u8 curve25519_base_point[CURVE25519_KEY_SIZE] __aligned(32) = { 9 }; |
| 21 |
|
| 22 |
#ifdef CONFIG_CRYPTO_LIB_CURVE25519_ARCH |
| 23 |
#include "curve25519.h" /* $(SRCARCH)/curve25519.h */ |
| 24 |
#else |
| 25 |
static void curve25519_arch(u8 mypublic[CURVE25519_KEY_SIZE], |
| 26 |
const u8 secret[CURVE25519_KEY_SIZE], |
| 27 |
const u8 basepoint[CURVE25519_KEY_SIZE]) |
| 28 |
{ |
| 29 |
curve25519_generic(mypublic, secret, basepoint); |
| 30 |
} |
| 31 |
|
| 32 |
static void curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE], |
| 33 |
const u8 secret[CURVE25519_KEY_SIZE]) |
| 34 |
{ |
| 35 |
curve25519_generic(pub, secret, curve25519_base_point); |
| 36 |
} |
| 37 |
#endif |
| 38 |
|
| 39 |
bool __must_check |
| 40 |
curve25519(u8 mypublic[CURVE25519_KEY_SIZE], |
| 41 |
const u8 secret[CURVE25519_KEY_SIZE], |
| 42 |
const u8 basepoint[CURVE25519_KEY_SIZE]) |
| 43 |
{ |
| 44 |
curve25519_arch(mypublic, secret, basepoint); |
| 45 |
return crypto_memneq(mypublic, curve25519_null_point, |
| 46 |
CURVE25519_KEY_SIZE); |
| 47 |
} |
| 48 |
EXPORT_SYMBOL(curve25519); |
| 49 |
|
| 50 |
bool __must_check |
| 51 |
curve25519_generate_public(u8 pub[CURVE25519_KEY_SIZE], |
| 52 |
const u8 secret[CURVE25519_KEY_SIZE]) |
| 53 |
{ |
| 54 |
if (unlikely(!crypto_memneq(secret, curve25519_null_point, |
| 55 |
CURVE25519_KEY_SIZE))) |
| 56 |
return false; |
| 57 |
curve25519_base_arch(pub, secret); |
| 58 |
return crypto_memneq(pub, curve25519_null_point, CURVE25519_KEY_SIZE); |
| 59 |
} |
| 60 |
EXPORT_SYMBOL(curve25519_generate_public); |
| 61 |
|
| 62 |
#ifdef curve25519_mod_init_arch |
| 63 |
static int __init curve25519_mod_init(void) |
| 64 |
{ |
| 65 |
curve25519_mod_init_arch(); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
subsys_initcall(curve25519_mod_init); |
| 69 |
|
| 70 |
static void __exit curve25519_mod_exit(void) |
| 71 |
{ |
| 72 |
} |
| 73 |
module_exit(curve25519_mod_exit); |
| 74 |
#endif |
| 75 |
|
| 76 |
MODULE_LICENSE("GPL v2"); |
| 77 |
MODULE_DESCRIPTION("Curve25519 algorithm"); |
| 78 |
MODULE_AUTHOR("Jason A. Donenfeld <[email protected]>"); |