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
);
93 pr_err("alg: kpp: Failed to allocate %d bytes for buf\n",
97 crypto_ecdh_encode_key(buf
, buf_len
, &p
);
99 /* Set A private Key */
100 err
= crypto_kpp_set_secret(tfm
, (void *)buf
, buf_len
);
104 swap_digits((u64
*)public_key
, (u64
*)tmp
, 4); /* x */
105 swap_digits((u64
*)&public_key
[32], (u64
*)&tmp
[32], 4); /* y */
107 sg_init_one(&src
, tmp
, 64);
108 sg_init_one(&dst
, secret
, 32);
109 kpp_request_set_input(req
, &src
, 64);
110 kpp_request_set_output(req
, &dst
, 32);
111 kpp_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
112 ecdh_complete
, &result
);
113 err
= crypto_kpp_compute_shared_secret(req
);
114 if (err
== -EINPROGRESS
) {
115 wait_for_completion(&result
.completion
);
119 pr_err("alg: ecdh: compute shared secret failed. err %d\n",
124 swap_digits((u64
*)secret
, (u64
*)tmp
, 4);
125 memcpy(secret
, tmp
, 32);
130 kpp_request_free(req
);
132 crypto_free_kpp(tfm
);
138 bool generate_ecdh_keys(u8 public_key
[64], u8 private_key
[32])
140 struct crypto_kpp
*tfm
;
141 struct kpp_request
*req
;
143 struct ecdh_completion result
;
144 struct scatterlist dst
;
146 unsigned int buf_len
;
148 const unsigned short max_tries
= 16;
149 unsigned short tries
= 0;
151 tmp
= kmalloc(64, GFP_KERNEL
);
155 tfm
= crypto_alloc_kpp("ecdh", CRYPTO_ALG_INTERNAL
, 0);
157 pr_err("alg: kpp: Failed to load tfm for kpp: %ld\n",
162 req
= kpp_request_alloc(tfm
, GFP_KERNEL
);
166 init_completion(&result
.completion
);
169 p
.curve_id
= ECC_CURVE_NIST_P256
;
171 buf_len
= crypto_ecdh_key_len(&p
);
172 buf
= kmalloc(buf_len
, GFP_KERNEL
);
174 pr_err("alg: kpp: Failed to allocate %d bytes for buf\n",
180 if (tries
++ >= max_tries
)
183 /* Set private Key */
184 p
.key
= (char *)private_key
;
185 crypto_ecdh_encode_key(buf
, buf_len
, &p
);
186 err
= crypto_kpp_set_secret(tfm
, buf
, buf_len
);
190 sg_init_one(&dst
, tmp
, 64);
191 kpp_request_set_input(req
, NULL
, 0);
192 kpp_request_set_output(req
, &dst
, 64);
193 kpp_request_set_callback(req
, CRYPTO_TFM_REQ_MAY_BACKLOG
,
194 ecdh_complete
, &result
);
196 err
= crypto_kpp_generate_public_key(req
);
198 if (err
== -EINPROGRESS
) {
199 wait_for_completion(&result
.completion
);
203 /* Private key is not valid. Regenerate */
214 /* Keys are handed back in little endian as expected by Security
217 swap_digits((u64
*)tmp
, (u64
*)public_key
, 4); /* x */
218 swap_digits((u64
*)&tmp
[32], (u64
*)&public_key
[32], 4); /* y */
219 swap_digits((u64
*)private_key
, (u64
*)tmp
, 4);
220 memcpy(private_key
, tmp
, 32);
225 kpp_request_free(req
);
227 crypto_free_kpp(tfm
);