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 static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
32 for (i = 0; i < ndigits; i++)
33 out[i] = __swab64(in[ndigits - 1 - i]);
36 /* compute_ecdh_secret() - function assumes that the private key was
38 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
39 * @public_key: pair's ecc public key.
40 * secret: memory where the ecdh computed shared secret will be saved.
42 * Return: zero on success; error code in case of error.
44 int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
47 DECLARE_CRYPTO_WAIT(result);
48 struct kpp_request *req;
50 struct scatterlist src, dst;
53 tmp = kmalloc(64, GFP_KERNEL);
57 req = kpp_request_alloc(tfm, GFP_KERNEL);
63 swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
64 swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
66 sg_init_one(&src, tmp, 64);
67 sg_init_one(&dst, secret, 32);
68 kpp_request_set_input(req, &src, 64);
69 kpp_request_set_output(req, &dst, 32);
70 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
71 crypto_req_done, &result);
72 err = crypto_kpp_compute_shared_secret(req);
73 err = crypto_wait_req(err, &result);
75 pr_err("alg: ecdh: compute shared secret failed. err %d\n",
80 swap_digits((u64 *)secret, (u64 *)tmp, 4);
81 memcpy(secret, tmp, 32);
84 kpp_request_free(req);
90 /* set_ecdh_privkey() - set or generate ecc private key.
92 * Function generates an ecc private key in the crypto subsystem when receiving
93 * a NULL private key or sets the received key when not NULL.
95 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
96 * @private_key: user's ecc private key. When not NULL, the key is expected
97 * in little endian format.
99 * Return: zero on success; error code in case of error.
101 int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
103 u8 *buf, *tmp = NULL;
104 unsigned int buf_len;
109 tmp = kmalloc(32, GFP_KERNEL);
112 swap_digits((u64 *)private_key, (u64 *)tmp, 4);
117 buf_len = crypto_ecdh_key_len(&p);
118 buf = kmalloc(buf_len, GFP_KERNEL);
124 err = crypto_ecdh_encode_key(buf, buf_len, &p);
128 err = crypto_kpp_set_secret(tfm, buf, buf_len);
131 kfree_sensitive(buf);
133 kfree_sensitive(tmp);
137 /* generate_ecdh_public_key() - function assumes that the private key was
140 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
141 * @public_key: memory where the computed ecc public key will be saved.
143 * Return: zero on success; error code in case of error.
145 int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
147 DECLARE_CRYPTO_WAIT(result);
148 struct kpp_request *req;
150 struct scatterlist dst;
153 tmp = kmalloc(64, GFP_KERNEL);
157 req = kpp_request_alloc(tfm, GFP_KERNEL);
163 sg_init_one(&dst, tmp, 64);
164 kpp_request_set_input(req, NULL, 0);
165 kpp_request_set_output(req, &dst, 64);
166 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
167 crypto_req_done, &result);
169 err = crypto_kpp_generate_public_key(req);
170 err = crypto_wait_req(err, &result);
174 /* The public key is handed back in little endian as expected by
175 * the Security Manager Protocol.
177 swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
178 swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
181 kpp_request_free(req);
187 /* generate_ecdh_keys() - generate ecc key pair.
189 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
190 * @public_key: memory where the computed ecc public key will be saved.
192 * Return: zero on success; error code in case of error.
194 int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
198 err = set_ecdh_privkey(tfm, NULL);
202 return generate_ecdh_public_key(tfm, public_key);