blob: 2155ce8028774fefcd2703a7eb16e1c3e1a6e11c [file] [log] [blame]
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +01001/*
2 * ECDH helper functions - KPP wrappings
3 *
4 * Copyright (C) 2017 Intel Corporation
5 *
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;
9 *
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.
18 *
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.
22 */
23#include "ecdh_helper.h"
24
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010025#include <linux/scatterlist.h>
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010026#include <crypto/ecdh.h>
27
28struct ecdh_completion {
29 struct completion completion;
30 int err;
31};
32
33static void ecdh_complete(struct crypto_async_request *req, int err)
34{
35 struct ecdh_completion *res = req->data;
36
37 if (err == -EINPROGRESS)
38 return;
39
40 res->err = err;
41 complete(&res->completion);
42}
43
44static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
45{
46 int i;
47
48 for (i = 0; i < ndigits; i++)
49 out[i] = __swab64(in[ndigits - 1 - i]);
50}
51
Tudor Ambarusc0153b02017-09-28 17:14:55 +030052/* compute_ecdh_secret() - function assumes that the private key was
53 * already set.
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.
57 *
58 * Return: zero on success; error code in case of error.
59 */
Tudor Ambarusa2976412017-09-28 17:14:52 +030060int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
Tudor Ambarusc0153b02017-09-28 17:14:55 +030061 u8 secret[32])
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010062{
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010063 struct kpp_request *req;
Tudor Ambarusc0153b02017-09-28 17:14:55 +030064 u8 *tmp;
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010065 struct ecdh_completion result;
66 struct scatterlist src, dst;
Tudor Ambarusa2976412017-09-28 17:14:52 +030067 int err;
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010068
Salvatore Benedetto763d9a32017-04-25 16:59:47 +010069 tmp = kmalloc(64, GFP_KERNEL);
70 if (!tmp)
Tudor Ambarusa2976412017-09-28 17:14:52 +030071 return -ENOMEM;
Salvatore Benedetto763d9a32017-04-25 16:59:47 +010072
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010073 req = kpp_request_alloc(tfm, GFP_KERNEL);
Tudor Ambarusa2976412017-09-28 17:14:52 +030074 if (!req) {
75 err = -ENOMEM;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +030076 goto free_tmp;
Tudor Ambarusa2976412017-09-28 17:14:52 +030077 }
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010078
79 init_completion(&result.completion);
80
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +010081 swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
82 swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
83
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);
93 err = result.err;
94 }
95 if (err < 0) {
96 pr_err("alg: ecdh: compute shared secret failed. err %d\n",
97 err);
98 goto free_all;
99 }
100
101 swap_digits((u64 *)secret, (u64 *)tmp, 4);
102 memcpy(secret, tmp, 32);
103
104free_all:
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100105 kpp_request_free(req);
Salvatore Benedetto763d9a32017-04-25 16:59:47 +0100106free_tmp:
Tudor Ambarus168ed652017-09-28 17:14:54 +0300107 kzfree(tmp);
Tudor Ambarusa2976412017-09-28 17:14:52 +0300108 return err;
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100109}
110
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300111/* set_ecdh_privkey() - set or generate ecc private key.
112 *
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.
115 *
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.
119 *
120 * Return: zero on success; error code in case of error.
121 */
122int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100123{
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300124 u8 *buf, *tmp = NULL;
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100125 unsigned int buf_len;
Tudor Ambarusa2976412017-09-28 17:14:52 +0300126 int err;
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300127 struct ecdh p = {0};
128
129 p.curve_id = ECC_CURVE_NIST_P256;
130
131 if (private_key) {
132 tmp = kmalloc(32, GFP_KERNEL);
133 if (!tmp)
134 return -ENOMEM;
135 swap_digits((u64 *)private_key, (u64 *)tmp, 4);
136 p.key = tmp;
137 p.key_size = 32;
138 }
139
140 buf_len = crypto_ecdh_key_len(&p);
141 buf = kmalloc(buf_len, GFP_KERNEL);
142 if (!buf) {
143 err = -ENOMEM;
144 goto free_tmp;
145 }
146
147 err = crypto_ecdh_encode_key(buf, buf_len, &p);
148 if (err)
149 goto free_all;
150
151 err = crypto_kpp_set_secret(tfm, buf, buf_len);
152 /* fall through */
153free_all:
154 kzfree(buf);
155free_tmp:
156 kzfree(tmp);
157 return err;
158}
159
160/* generate_ecdh_public_key() - function assumes that the private key was
161 * already set.
162 *
163 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
164 * @public_key: memory where the computed ecc public key will be saved.
165 *
166 * Return: zero on success; error code in case of error.
167 */
168int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
169{
170 struct kpp_request *req;
171 u8 *tmp;
172 struct ecdh_completion result;
173 struct scatterlist dst;
174 int err;
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100175
Salvatore Benedetto763d9a32017-04-25 16:59:47 +0100176 tmp = kmalloc(64, GFP_KERNEL);
177 if (!tmp)
Tudor Ambarusa2976412017-09-28 17:14:52 +0300178 return -ENOMEM;
Salvatore Benedetto763d9a32017-04-25 16:59:47 +0100179
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100180 req = kpp_request_alloc(tfm, GFP_KERNEL);
Tudor Ambarusa2976412017-09-28 17:14:52 +0300181 if (!req) {
182 err = -ENOMEM;
Tudor Ambarus47eb2ac2017-09-28 17:14:51 +0300183 goto free_tmp;
Tudor Ambarusa2976412017-09-28 17:14:52 +0300184 }
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100185
186 init_completion(&result.completion);
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300187 sg_init_one(&dst, tmp, 64);
188 kpp_request_set_input(req, NULL, 0);
189 kpp_request_set_output(req, &dst, 64);
190 kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
191 ecdh_complete, &result);
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100192
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300193 err = crypto_kpp_generate_public_key(req);
194 if (err == -EINPROGRESS) {
195 wait_for_completion(&result.completion);
196 err = result.err;
197 }
198 if (err < 0)
199 goto free_all;
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100200
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300201 /* The public key is handed back in little endian as expected by
202 * the Security Manager Protocol.
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100203 */
204 swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
205 swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100206
207free_all:
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100208 kpp_request_free(req);
Salvatore Benedetto763d9a32017-04-25 16:59:47 +0100209free_tmp:
210 kfree(tmp);
Tudor Ambarusa2976412017-09-28 17:14:52 +0300211 return err;
Salvatore Benedetto58771c1c2017-04-24 13:13:20 +0100212}
Tudor Ambarusc0153b02017-09-28 17:14:55 +0300213
214/* generate_ecdh_keys() - generate ecc key pair.
215 *
216 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
217 * @public_key: memory where the computed ecc public key will be saved.
218 *
219 * Return: zero on success; error code in case of error.
220 */
221int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
222{
223 int err;
224
225 err = set_ecdh_privkey(tfm, NULL);
226 if (err)
227 return err;
228
229 return generate_ecdh_public_key(tfm, public_key);
230}