xref: /openbmc/linux/crypto/ecdh.c (revision 586b4106)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* ECDH key-agreement protocol
3  *
4  * Copyright (c) 2016, Intel Corporation
5  * Authors: Salvator Benedetto <salvatore.benedetto@intel.com>
6  */
7 
8 #include <linux/module.h>
9 #include <crypto/internal/ecc.h>
10 #include <crypto/internal/kpp.h>
11 #include <crypto/kpp.h>
12 #include <crypto/ecdh.h>
13 #include <linux/scatterlist.h>
14 
15 struct ecdh_ctx {
16 	unsigned int curve_id;
17 	unsigned int ndigits;
18 	u64 private_key[ECC_MAX_DIGITS];
19 };
20 
21 static inline struct ecdh_ctx *ecdh_get_ctx(struct crypto_kpp *tfm)
22 {
23 	return kpp_tfm_ctx(tfm);
24 }
25 
26 static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
27 			   unsigned int len)
28 {
29 	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
30 	struct ecdh params;
31 
32 	if (crypto_ecdh_decode_key(buf, len, &params) < 0 ||
33 	    params.key_size > sizeof(u64) * ctx->ndigits)
34 		return -EINVAL;
35 
36 	memset(ctx->private_key, 0, sizeof(ctx->private_key));
37 
38 	if (!params.key || !params.key_size)
39 		return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
40 				       ctx->private_key);
41 
42 	memcpy(ctx->private_key, params.key, params.key_size);
43 
44 	if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
45 			     ctx->private_key, params.key_size) < 0) {
46 		memzero_explicit(ctx->private_key, params.key_size);
47 		return -EINVAL;
48 	}
49 	return 0;
50 }
51 
52 static int ecdh_compute_value(struct kpp_request *req)
53 {
54 	struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
55 	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
56 	u64 *public_key;
57 	u64 *shared_secret = NULL;
58 	void *buf;
59 	size_t copied, nbytes, public_key_sz;
60 	int ret = -ENOMEM;
61 
62 	nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
63 	/* Public part is a point thus it has both coordinates */
64 	public_key_sz = 2 * nbytes;
65 
66 	public_key = kmalloc(public_key_sz, GFP_KERNEL);
67 	if (!public_key)
68 		return -ENOMEM;
69 
70 	if (req->src) {
71 		shared_secret = kmalloc(nbytes, GFP_KERNEL);
72 		if (!shared_secret)
73 			goto free_pubkey;
74 
75 		/* from here on it's invalid parameters */
76 		ret = -EINVAL;
77 
78 		/* must have exactly two points to be on the curve */
79 		if (public_key_sz != req->src_len)
80 			goto free_all;
81 
82 		copied = sg_copy_to_buffer(req->src,
83 					   sg_nents_for_len(req->src,
84 							    public_key_sz),
85 					   public_key, public_key_sz);
86 		if (copied != public_key_sz)
87 			goto free_all;
88 
89 		ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
90 						ctx->private_key, public_key,
91 						shared_secret);
92 
93 		buf = shared_secret;
94 	} else {
95 		ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
96 				       ctx->private_key, public_key);
97 		buf = public_key;
98 		nbytes = public_key_sz;
99 	}
100 
101 	if (ret < 0)
102 		goto free_all;
103 
104 	/* might want less than we've got */
105 	nbytes = min_t(size_t, nbytes, req->dst_len);
106 	copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
107 								nbytes),
108 				     buf, nbytes);
109 	if (copied != nbytes)
110 		ret = -EINVAL;
111 
112 	/* fall through */
113 free_all:
114 	kfree_sensitive(shared_secret);
115 free_pubkey:
116 	kfree(public_key);
117 	return ret;
118 }
119 
120 static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
121 {
122 	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
123 
124 	/* Public key is made of two coordinates, add one to the left shift */
125 	return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
126 }
127 
128 static int ecdh_nist_p192_init_tfm(struct crypto_kpp *tfm)
129 {
130 	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
131 
132 	ctx->curve_id = ECC_CURVE_NIST_P192;
133 	ctx->ndigits = ECC_CURVE_NIST_P192_DIGITS;
134 
135 	return 0;
136 }
137 
138 static struct kpp_alg ecdh_nist_p192 = {
139 	.set_secret = ecdh_set_secret,
140 	.generate_public_key = ecdh_compute_value,
141 	.compute_shared_secret = ecdh_compute_value,
142 	.max_size = ecdh_max_size,
143 	.init = ecdh_nist_p192_init_tfm,
144 	.base = {
145 		.cra_name = "ecdh-nist-p192",
146 		.cra_driver_name = "ecdh-nist-p192-generic",
147 		.cra_priority = 100,
148 		.cra_module = THIS_MODULE,
149 		.cra_ctxsize = sizeof(struct ecdh_ctx),
150 	},
151 };
152 
153 static int ecdh_nist_p256_init_tfm(struct crypto_kpp *tfm)
154 {
155 	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
156 
157 	ctx->curve_id = ECC_CURVE_NIST_P256;
158 	ctx->ndigits = ECC_CURVE_NIST_P256_DIGITS;
159 
160 	return 0;
161 }
162 
163 static struct kpp_alg ecdh_nist_p256 = {
164 	.set_secret = ecdh_set_secret,
165 	.generate_public_key = ecdh_compute_value,
166 	.compute_shared_secret = ecdh_compute_value,
167 	.max_size = ecdh_max_size,
168 	.init = ecdh_nist_p256_init_tfm,
169 	.base = {
170 		.cra_name = "ecdh-nist-p256",
171 		.cra_driver_name = "ecdh-nist-p256-generic",
172 		.cra_priority = 100,
173 		.cra_module = THIS_MODULE,
174 		.cra_ctxsize = sizeof(struct ecdh_ctx),
175 	},
176 };
177 
178 static int ecdh_nist_p384_init_tfm(struct crypto_kpp *tfm)
179 {
180 	struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
181 
182 	ctx->curve_id = ECC_CURVE_NIST_P384;
183 	ctx->ndigits = ECC_CURVE_NIST_P384_DIGITS;
184 
185 	return 0;
186 }
187 
188 static struct kpp_alg ecdh_nist_p384 = {
189 	.set_secret = ecdh_set_secret,
190 	.generate_public_key = ecdh_compute_value,
191 	.compute_shared_secret = ecdh_compute_value,
192 	.max_size = ecdh_max_size,
193 	.init = ecdh_nist_p384_init_tfm,
194 	.base = {
195 		.cra_name = "ecdh-nist-p384",
196 		.cra_driver_name = "ecdh-nist-p384-generic",
197 		.cra_priority = 100,
198 		.cra_module = THIS_MODULE,
199 		.cra_ctxsize = sizeof(struct ecdh_ctx),
200 	},
201 };
202 
203 static bool ecdh_nist_p192_registered;
204 
205 static int __init ecdh_init(void)
206 {
207 	int ret;
208 
209 	/* NIST p192 will fail to register in FIPS mode */
210 	ret = crypto_register_kpp(&ecdh_nist_p192);
211 	ecdh_nist_p192_registered = ret == 0;
212 
213 	ret = crypto_register_kpp(&ecdh_nist_p256);
214 	if (ret)
215 		goto nist_p256_error;
216 
217 	ret = crypto_register_kpp(&ecdh_nist_p384);
218 	if (ret)
219 		goto nist_p384_error;
220 
221 	return 0;
222 
223 nist_p384_error:
224 	crypto_unregister_kpp(&ecdh_nist_p256);
225 
226 nist_p256_error:
227 	if (ecdh_nist_p192_registered)
228 		crypto_unregister_kpp(&ecdh_nist_p192);
229 	return ret;
230 }
231 
232 static void __exit ecdh_exit(void)
233 {
234 	if (ecdh_nist_p192_registered)
235 		crypto_unregister_kpp(&ecdh_nist_p192);
236 	crypto_unregister_kpp(&ecdh_nist_p256);
237 	crypto_unregister_kpp(&ecdh_nist_p384);
238 }
239 
240 subsys_initcall(ecdh_init);
241 module_exit(ecdh_exit);
242 MODULE_ALIAS_CRYPTO("ecdh");
243 MODULE_LICENSE("GPL");
244 MODULE_DESCRIPTION("ECDH generic algorithm");
245