2 * ECDH helper functions - KPP wrappings
4 * Copyright (C) 2017 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation;
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 * SOFTWARE IS DISCLAIMED.
23 #include "ecdh_helper.h"
25 #include <linux/scatterlist.h>
26 #include <crypto/ecdh.h>
28 struct ecdh_completion {
29 struct completion completion;
33 static void ecdh_complete(struct crypto_async_request *req, int err)
35 struct ecdh_completion *res = req->data;
37 if (err == -EINPROGRESS)
41 complete(&res->completion);
44 static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
48 for (i = 0; i < ndigits; i++)
49 out[i] = __swab64(in[ndigits - 1 - i]);
52 /* compute_ecdh_secret() - function assumes that the private key was
54 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
55 * @public_key: pair's ecc public key.
56 * secret: memory where the ecdh computed shared secret will be saved.
58 * Return: zero on success; error code in case of error.
60 int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
63 struct kpp_request *req;
65 struct ecdh_completion result;
66 struct scatterlist src, dst;
69 tmp = kmalloc(64, GFP_KERNEL);
73 req = kpp_request_alloc(tfm, GFP_KERNEL);
79 init_completion(&result.completion);
81 swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
82 swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
84 sg_init_one(&src, tmp, 64);
85 sg_init_one(&dst, secret, 32);
86 kpp_request_set_input(req, &src, 64);
87 kpp_request_set_output(req, &dst, 32);
88 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
89 ecdh_complete, &result);
90 err = crypto_kpp_compute_shared_secret(req);
91 if (err == -EINPROGRESS) {
92 wait_for_completion(&result.completion);
96 pr_err("alg: ecdh: compute shared secret failed. err %d\n",
101 swap_digits((u64 *)secret, (u64 *)tmp, 4);
102 memcpy(secret, tmp, 32);
105 kpp_request_free(req);
107 kfree_sensitive(tmp);
111 /* set_ecdh_privkey() - set or generate ecc private key.
113 * Function generates an ecc private key in the crypto subsystem when receiving
114 * a NULL private key or sets the received key when not NULL.
116 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
117 * @private_key: user's ecc private key. When not NULL, the key is expected
118 * in little endian format.
120 * Return: zero on success; error code in case of error.
122 int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
124 u8 *buf, *tmp = NULL;
125 unsigned int buf_len;
130 tmp = kmalloc(32, GFP_KERNEL);
133 swap_digits((u64 *)private_key, (u64 *)tmp, 4);
138 buf_len = crypto_ecdh_key_len(&p);
139 buf = kmalloc(buf_len, GFP_KERNEL);
145 err = crypto_ecdh_encode_key(buf, buf_len, &p);
149 err = crypto_kpp_set_secret(tfm, buf, buf_len);
152 kfree_sensitive(buf);
154 kfree_sensitive(tmp);
158 /* generate_ecdh_public_key() - function assumes that the private key was
161 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
162 * @public_key: memory where the computed ecc public key will be saved.
164 * Return: zero on success; error code in case of error.
166 int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
168 struct kpp_request *req;
170 struct ecdh_completion result;
171 struct scatterlist dst;
174 tmp = kmalloc(64, GFP_KERNEL);
178 req = kpp_request_alloc(tfm, GFP_KERNEL);
184 init_completion(&result.completion);
185 sg_init_one(&dst, tmp, 64);
186 kpp_request_set_input(req, NULL, 0);
187 kpp_request_set_output(req, &dst, 64);
188 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
189 ecdh_complete, &result);
191 err = crypto_kpp_generate_public_key(req);
192 if (err == -EINPROGRESS) {
193 wait_for_completion(&result.completion);
199 /* The public key is handed back in little endian as expected by
200 * the Security Manager Protocol.
202 swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
203 swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
206 kpp_request_free(req);
212 /* generate_ecdh_keys() - generate ecc key pair.
214 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
215 * @public_key: memory where the computed ecc public key will be saved.
217 * Return: zero on success; error code in case of error.
219 int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
223 err = set_ecdh_privkey(tfm, NULL);
227 return generate_ecdh_public_key(tfm, public_key);