File Index
Files and directories
Sign in
FILE

curve25519.c

File preview with syntax highlighting.

linux-6.19.14/lib/crypto/curve25519.c C
 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 * 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
19static const u8 curve25519_null_point[CURVE25519_KEY_SIZE] __aligned(32) = { 0 };
20static 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
25static 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
32static 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
39bool __must_check
40curve25519(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}
48EXPORT_SYMBOL(curve25519);
49
50bool __must_check
51curve25519_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}
60EXPORT_SYMBOL(curve25519_generate_public);
61
62#ifdef curve25519_mod_init_arch
63static int __init curve25519_mod_init(void)
64{
65	curve25519_mod_init_arch();
66	return 0;
67}
68subsys_initcall(curve25519_mod_init);
69
70static void __exit curve25519_mod_exit(void)
71{
72}
73module_exit(curve25519_mod_exit);
74#endif
75
76MODULE_LICENSE("GPL v2");
77MODULE_DESCRIPTION("Curve25519 algorithm");
78MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
~/ Read-only file preview.