This source file includes following definitions.
- ecdh_complete
- swap_digits
- compute_ecdh_secret
- set_ecdh_privkey
- generate_ecdh_public_key
- generate_ecdh_keys
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include "ecdh_helper.h"
24
25 #include <linux/scatterlist.h>
26 #include <crypto/ecdh.h>
27
28 struct ecdh_completion {
29 struct completion completion;
30 int err;
31 };
32
33 static 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
44 static 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
53
54
55
56
57
58
59
60 int 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);
82 swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4);
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
104 free_all:
105 kpp_request_free(req);
106 free_tmp:
107 kzfree(tmp);
108 return err;
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122 int 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 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
153 free_all:
154 kzfree(buf);
155 free_tmp:
156 kzfree(tmp);
157 return err;
158 }
159
160
161
162
163
164
165
166
167
168 int 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;
175
176 tmp = kmalloc(64, GFP_KERNEL);
177 if (!tmp)
178 return -ENOMEM;
179
180 req = kpp_request_alloc(tfm, GFP_KERNEL);
181 if (!req) {
182 err = -ENOMEM;
183 goto free_tmp;
184 }
185
186 init_completion(&result.completion);
187 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);
192
193 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;
200
201
202
203
204 swap_digits((u64 *)tmp, (u64 *)public_key, 4);
205 swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4);
206
207 free_all:
208 kpp_request_free(req);
209 free_tmp:
210 kfree(tmp);
211 return err;
212 }
213
214
215
216
217
218
219
220
221 int 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 }