Loading...
1/*
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
25#include <linux/scatterlist.h>
26#include <crypto/ecdh.h>
27
28static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
29{
30 int i;
31
32 for (i = 0; i < ndigits; i++)
33 out[i] = __swab64(in[ndigits - 1 - i]);
34}
35
36/* compute_ecdh_secret() - function assumes that the private key was
37 * already set.
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.
41 *
42 * Return: zero on success; error code in case of error.
43 */
44int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
45 u8 secret[32])
46{
47 DECLARE_CRYPTO_WAIT(result);
48 struct kpp_request *req;
49 u8 *tmp;
50 struct scatterlist src, dst;
51 int err;
52
53 tmp = kmalloc(64, GFP_KERNEL);
54 if (!tmp)
55 return -ENOMEM;
56
57 req = kpp_request_alloc(tfm, GFP_KERNEL);
58 if (!req) {
59 err = -ENOMEM;
60 goto free_tmp;
61 }
62
63 swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
64 swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
65
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);
74 if (err < 0) {
75 pr_err("alg: ecdh: compute shared secret failed. err %d\n",
76 err);
77 goto free_all;
78 }
79
80 swap_digits((u64 *)secret, (u64 *)tmp, 4);
81 memcpy(secret, tmp, 32);
82
83free_all:
84 kpp_request_free(req);
85free_tmp:
86 kfree_sensitive(tmp);
87 return err;
88}
89
90/* set_ecdh_privkey() - set or generate ecc private key.
91 *
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.
94 *
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.
98 *
99 * Return: zero on success; error code in case of error.
100 */
101int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
102{
103 u8 *buf, *tmp = NULL;
104 unsigned int buf_len;
105 int err;
106 struct ecdh p = {0};
107
108 if (private_key) {
109 tmp = kmalloc(32, GFP_KERNEL);
110 if (!tmp)
111 return -ENOMEM;
112 swap_digits((u64 *)private_key, (u64 *)tmp, 4);
113 p.key = tmp;
114 p.key_size = 32;
115 }
116
117 buf_len = crypto_ecdh_key_len(&p);
118 buf = kmalloc(buf_len, GFP_KERNEL);
119 if (!buf) {
120 err = -ENOMEM;
121 goto free_tmp;
122 }
123
124 err = crypto_ecdh_encode_key(buf, buf_len, &p);
125 if (err)
126 goto free_all;
127
128 err = crypto_kpp_set_secret(tfm, buf, buf_len);
129 /* fall through */
130free_all:
131 kfree_sensitive(buf);
132free_tmp:
133 kfree_sensitive(tmp);
134 return err;
135}
136
137/* generate_ecdh_public_key() - function assumes that the private key was
138 * already set.
139 *
140 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
141 * @public_key: memory where the computed ecc public key will be saved.
142 *
143 * Return: zero on success; error code in case of error.
144 */
145int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
146{
147 DECLARE_CRYPTO_WAIT(result);
148 struct kpp_request *req;
149 u8 *tmp;
150 struct scatterlist dst;
151 int err;
152
153 tmp = kmalloc(64, GFP_KERNEL);
154 if (!tmp)
155 return -ENOMEM;
156
157 req = kpp_request_alloc(tfm, GFP_KERNEL);
158 if (!req) {
159 err = -ENOMEM;
160 goto free_tmp;
161 }
162
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);
168
169 err = crypto_kpp_generate_public_key(req);
170 err = crypto_wait_req(err, &result);
171 if (err < 0)
172 goto free_all;
173
174 /* The public key is handed back in little endian as expected by
175 * the Security Manager Protocol.
176 */
177 swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
178 swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
179
180free_all:
181 kpp_request_free(req);
182free_tmp:
183 kfree(tmp);
184 return err;
185}
186
187/* generate_ecdh_keys() - generate ecc key pair.
188 *
189 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
190 * @public_key: memory where the computed ecc public key will be saved.
191 *
192 * Return: zero on success; error code in case of error.
193 */
194int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
195{
196 int err;
197
198 err = set_ecdh_privkey(tfm, NULL);
199 if (err)
200 return err;
201
202 return generate_ecdh_public_key(tfm, public_key);
203}
1/*
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
25#include <linux/scatterlist.h>
26#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
52/* 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 */
60int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
61 u8 secret[32])
62{
63 struct kpp_request *req;
64 u8 *tmp;
65 struct ecdh_completion result;
66 struct scatterlist src, dst;
67 int err;
68
69 tmp = kmalloc(64, GFP_KERNEL);
70 if (!tmp)
71 return -ENOMEM;
72
73 req = kpp_request_alloc(tfm, GFP_KERNEL);
74 if (!req) {
75 err = -ENOMEM;
76 goto free_tmp;
77 }
78
79 init_completion(&result.completion);
80
81 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:
105 kpp_request_free(req);
106free_tmp:
107 kfree_sensitive(tmp);
108 return err;
109}
110
111/* 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])
123{
124 u8 *buf, *tmp = NULL;
125 unsigned int buf_len;
126 int err;
127 struct ecdh p = {0};
128
129 if (private_key) {
130 tmp = kmalloc(32, GFP_KERNEL);
131 if (!tmp)
132 return -ENOMEM;
133 swap_digits((u64 *)private_key, (u64 *)tmp, 4);
134 p.key = tmp;
135 p.key_size = 32;
136 }
137
138 buf_len = crypto_ecdh_key_len(&p);
139 buf = kmalloc(buf_len, GFP_KERNEL);
140 if (!buf) {
141 err = -ENOMEM;
142 goto free_tmp;
143 }
144
145 err = crypto_ecdh_encode_key(buf, buf_len, &p);
146 if (err)
147 goto free_all;
148
149 err = crypto_kpp_set_secret(tfm, buf, buf_len);
150 /* fall through */
151free_all:
152 kfree_sensitive(buf);
153free_tmp:
154 kfree_sensitive(tmp);
155 return err;
156}
157
158/* generate_ecdh_public_key() - function assumes that the private key was
159 * already set.
160 *
161 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
162 * @public_key: memory where the computed ecc public key will be saved.
163 *
164 * Return: zero on success; error code in case of error.
165 */
166int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
167{
168 struct kpp_request *req;
169 u8 *tmp;
170 struct ecdh_completion result;
171 struct scatterlist dst;
172 int err;
173
174 tmp = kmalloc(64, GFP_KERNEL);
175 if (!tmp)
176 return -ENOMEM;
177
178 req = kpp_request_alloc(tfm, GFP_KERNEL);
179 if (!req) {
180 err = -ENOMEM;
181 goto free_tmp;
182 }
183
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);
190
191 err = crypto_kpp_generate_public_key(req);
192 if (err == -EINPROGRESS) {
193 wait_for_completion(&result.completion);
194 err = result.err;
195 }
196 if (err < 0)
197 goto free_all;
198
199 /* The public key is handed back in little endian as expected by
200 * the Security Manager Protocol.
201 */
202 swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
203 swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
204
205free_all:
206 kpp_request_free(req);
207free_tmp:
208 kfree(tmp);
209 return err;
210}
211
212/* generate_ecdh_keys() - generate ecc key pair.
213 *
214 * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
215 * @public_key: memory where the computed ecc public key will be saved.
216 *
217 * Return: zero on success; error code in case of error.
218 */
219int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
220{
221 int err;
222
223 err = set_ecdh_privkey(tfm, NULL);
224 if (err)
225 return err;
226
227 return generate_ecdh_public_key(tfm, public_key);
228}