xref: /OK3568_Linux_fs/kernel/arch/arm/crypto/curve25519-glue.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0 OR MIT
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Based on public domain code from Daniel J. Bernstein and Peter Schwabe. This
6*4882a593Smuzhiyun  * began from SUPERCOP's curve25519/neon2/scalarmult.s, but has subsequently been
7*4882a593Smuzhiyun  * manually reworked for use in kernel space.
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <asm/hwcap.h>
11*4882a593Smuzhiyun #include <asm/neon.h>
12*4882a593Smuzhiyun #include <asm/simd.h>
13*4882a593Smuzhiyun #include <crypto/internal/kpp.h>
14*4882a593Smuzhiyun #include <crypto/internal/simd.h>
15*4882a593Smuzhiyun #include <linux/types.h>
16*4882a593Smuzhiyun #include <linux/module.h>
17*4882a593Smuzhiyun #include <linux/init.h>
18*4882a593Smuzhiyun #include <linux/jump_label.h>
19*4882a593Smuzhiyun #include <linux/scatterlist.h>
20*4882a593Smuzhiyun #include <crypto/curve25519.h>
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun asmlinkage void curve25519_neon(u8 mypublic[CURVE25519_KEY_SIZE],
23*4882a593Smuzhiyun 				const u8 secret[CURVE25519_KEY_SIZE],
24*4882a593Smuzhiyun 				const u8 basepoint[CURVE25519_KEY_SIZE]);
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_neon);
27*4882a593Smuzhiyun 
curve25519_arch(u8 out[CURVE25519_KEY_SIZE],const u8 scalar[CURVE25519_KEY_SIZE],const u8 point[CURVE25519_KEY_SIZE])28*4882a593Smuzhiyun void curve25519_arch(u8 out[CURVE25519_KEY_SIZE],
29*4882a593Smuzhiyun 		     const u8 scalar[CURVE25519_KEY_SIZE],
30*4882a593Smuzhiyun 		     const u8 point[CURVE25519_KEY_SIZE])
31*4882a593Smuzhiyun {
32*4882a593Smuzhiyun 	if (static_branch_likely(&have_neon) && crypto_simd_usable()) {
33*4882a593Smuzhiyun 		kernel_neon_begin();
34*4882a593Smuzhiyun 		curve25519_neon(out, scalar, point);
35*4882a593Smuzhiyun 		kernel_neon_end();
36*4882a593Smuzhiyun 	} else {
37*4882a593Smuzhiyun 		curve25519_generic(out, scalar, point);
38*4882a593Smuzhiyun 	}
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun EXPORT_SYMBOL(curve25519_arch);
41*4882a593Smuzhiyun 
curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],const u8 secret[CURVE25519_KEY_SIZE])42*4882a593Smuzhiyun void curve25519_base_arch(u8 pub[CURVE25519_KEY_SIZE],
43*4882a593Smuzhiyun 			  const u8 secret[CURVE25519_KEY_SIZE])
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun 	return curve25519_arch(pub, secret, curve25519_base_point);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun EXPORT_SYMBOL(curve25519_base_arch);
48*4882a593Smuzhiyun 
curve25519_set_secret(struct crypto_kpp * tfm,const void * buf,unsigned int len)49*4882a593Smuzhiyun static int curve25519_set_secret(struct crypto_kpp *tfm, const void *buf,
50*4882a593Smuzhiyun 				 unsigned int len)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	u8 *secret = kpp_tfm_ctx(tfm);
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 	if (!len)
55*4882a593Smuzhiyun 		curve25519_generate_secret(secret);
56*4882a593Smuzhiyun 	else if (len == CURVE25519_KEY_SIZE &&
57*4882a593Smuzhiyun 		 crypto_memneq(buf, curve25519_null_point, CURVE25519_KEY_SIZE))
58*4882a593Smuzhiyun 		memcpy(secret, buf, CURVE25519_KEY_SIZE);
59*4882a593Smuzhiyun 	else
60*4882a593Smuzhiyun 		return -EINVAL;
61*4882a593Smuzhiyun 	return 0;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
curve25519_compute_value(struct kpp_request * req)64*4882a593Smuzhiyun static int curve25519_compute_value(struct kpp_request *req)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
67*4882a593Smuzhiyun 	const u8 *secret = kpp_tfm_ctx(tfm);
68*4882a593Smuzhiyun 	u8 public_key[CURVE25519_KEY_SIZE];
69*4882a593Smuzhiyun 	u8 buf[CURVE25519_KEY_SIZE];
70*4882a593Smuzhiyun 	int copied, nbytes;
71*4882a593Smuzhiyun 	u8 const *bp;
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	if (req->src) {
74*4882a593Smuzhiyun 		copied = sg_copy_to_buffer(req->src,
75*4882a593Smuzhiyun 					   sg_nents_for_len(req->src,
76*4882a593Smuzhiyun 							    CURVE25519_KEY_SIZE),
77*4882a593Smuzhiyun 					   public_key, CURVE25519_KEY_SIZE);
78*4882a593Smuzhiyun 		if (copied != CURVE25519_KEY_SIZE)
79*4882a593Smuzhiyun 			return -EINVAL;
80*4882a593Smuzhiyun 		bp = public_key;
81*4882a593Smuzhiyun 	} else {
82*4882a593Smuzhiyun 		bp = curve25519_base_point;
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	curve25519_arch(buf, secret, bp);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	/* might want less than we've got */
88*4882a593Smuzhiyun 	nbytes = min_t(size_t, CURVE25519_KEY_SIZE, req->dst_len);
89*4882a593Smuzhiyun 	copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
90*4882a593Smuzhiyun 								nbytes),
91*4882a593Smuzhiyun 				     buf, nbytes);
92*4882a593Smuzhiyun 	if (copied != nbytes)
93*4882a593Smuzhiyun 		return -EINVAL;
94*4882a593Smuzhiyun 	return 0;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun 
curve25519_max_size(struct crypto_kpp * tfm)97*4882a593Smuzhiyun static unsigned int curve25519_max_size(struct crypto_kpp *tfm)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	return CURVE25519_KEY_SIZE;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun static struct kpp_alg curve25519_alg = {
103*4882a593Smuzhiyun 	.base.cra_name		= "curve25519",
104*4882a593Smuzhiyun 	.base.cra_driver_name	= "curve25519-neon",
105*4882a593Smuzhiyun 	.base.cra_priority	= 200,
106*4882a593Smuzhiyun 	.base.cra_module	= THIS_MODULE,
107*4882a593Smuzhiyun 	.base.cra_ctxsize	= CURVE25519_KEY_SIZE,
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun 	.set_secret		= curve25519_set_secret,
110*4882a593Smuzhiyun 	.generate_public_key	= curve25519_compute_value,
111*4882a593Smuzhiyun 	.compute_shared_secret	= curve25519_compute_value,
112*4882a593Smuzhiyun 	.max_size		= curve25519_max_size,
113*4882a593Smuzhiyun };
114*4882a593Smuzhiyun 
mod_init(void)115*4882a593Smuzhiyun static int __init mod_init(void)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	if (elf_hwcap & HWCAP_NEON) {
118*4882a593Smuzhiyun 		static_branch_enable(&have_neon);
119*4882a593Smuzhiyun 		return IS_REACHABLE(CONFIG_CRYPTO_KPP) ?
120*4882a593Smuzhiyun 			crypto_register_kpp(&curve25519_alg) : 0;
121*4882a593Smuzhiyun 	}
122*4882a593Smuzhiyun 	return 0;
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun 
mod_exit(void)125*4882a593Smuzhiyun static void __exit mod_exit(void)
126*4882a593Smuzhiyun {
127*4882a593Smuzhiyun 	if (IS_REACHABLE(CONFIG_CRYPTO_KPP) && elf_hwcap & HWCAP_NEON)
128*4882a593Smuzhiyun 		crypto_unregister_kpp(&curve25519_alg);
129*4882a593Smuzhiyun }
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun module_init(mod_init);
132*4882a593Smuzhiyun module_exit(mod_exit);
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("curve25519");
135*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("curve25519-neon");
136*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
137