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/kpp.h>
27 #include <crypto/ecdh.h>
29 struct ecdh_completion {
30 struct completion completion;
34 static void ecdh_complete(struct crypto_async_request *req, int err)
36 struct ecdh_completion *res = req->data;
38 if (err == -EINPROGRESS)
42 complete(&res->completion);
45 static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
49 for (i = 0; i < ndigits; i++)
50 out[i] = __swab64(in[ndigits - 1 - i]);
53 bool compute_ecdh_secret(const u8 public_key[64], const u8 private_key[32],
56 struct crypto_kpp *tfm;
57 struct kpp_request *req;
59 struct ecdh_completion result;
60 struct scatterlist src, dst;
65 tmp = kmalloc(64, GFP_KERNEL);
69 tfm = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
71 pr_err("alg: kpp: Failed to load tfm for kpp: %ld\n",
76 req = kpp_request_alloc(tfm, GFP_KERNEL);
80 init_completion(&result.completion);
82 /* Security Manager Protocol holds digits in litte-endian order
83 * while ECC API expect big-endian data
85 swap_digits((u64 *)private_key, (u64 *)tmp, 4);
89 p.curve_id = ECC_CURVE_NIST_P256;
90 buf_len = crypto_ecdh_key_len(&p);
91 buf = kmalloc(buf_len, GFP_KERNEL);
95 crypto_ecdh_encode_key(buf, buf_len, &p);
97 /* Set A private Key */
98 err = crypto_kpp_set_secret(tfm, (void *)buf, buf_len);
102 swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
103 swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
105 sg_init_one(&src, tmp, 64);
106 sg_init_one(&dst, secret, 32);
107 kpp_request_set_input(req, &src, 64);
108 kpp_request_set_output(req, &dst, 32);
109 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
110 ecdh_complete, &result);
111 err = crypto_kpp_compute_shared_secret(req);
112 if (err == -EINPROGRESS) {
113 wait_for_completion(&result.completion);
117 pr_err("alg: ecdh: compute shared secret failed. err %d\n",
122 swap_digits((u64 *)secret, (u64 *)tmp, 4);
123 memcpy(secret, tmp, 32);
128 kpp_request_free(req);
130 crypto_free_kpp(tfm);
136 bool generate_ecdh_keys(u8 public_key[64], u8 private_key[32])
138 struct crypto_kpp *tfm;
139 struct kpp_request *req;
141 struct ecdh_completion result;
142 struct scatterlist dst;
144 unsigned int buf_len;
146 const unsigned short max_tries = 16;
147 unsigned short tries = 0;
149 tmp = kmalloc(64, GFP_KERNEL);
153 tfm = crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL, 0);
155 pr_err("alg: kpp: Failed to load tfm for kpp: %ld\n",
160 req = kpp_request_alloc(tfm, GFP_KERNEL);
164 init_completion(&result.completion);
167 p.curve_id = ECC_CURVE_NIST_P256;
169 buf_len = crypto_ecdh_key_len(&p);
170 buf = kmalloc(buf_len, GFP_KERNEL);
175 if (tries++ >= max_tries)
178 /* Set private Key */
179 p.key = (char *)private_key;
180 crypto_ecdh_encode_key(buf, buf_len, &p);
181 err = crypto_kpp_set_secret(tfm, buf, buf_len);
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);
193 if (err == -EINPROGRESS) {
194 wait_for_completion(&result.completion);
198 /* Private key is not valid. Regenerate */
209 /* Keys are handed back in little endian as expected by Security
212 swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
213 swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
214 swap_digits((u64 *)private_key, (u64 *)tmp, 4);
215 memcpy(private_key, tmp, 32);
220 kpp_request_free(req);
222 crypto_free_kpp(tfm);