Blame view
net/bluetooth/ecdh_helper.c
5.93 KB
58771c1cb Bluetooth: conver... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
/* * ECDH helper functions - KPP wrappings * * Copyright (C) 2017 Intel Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation; * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS * SOFTWARE IS DISCLAIMED. */ #include "ecdh_helper.h" |
58771c1cb Bluetooth: conver... |
24 |
#include <linux/scatterlist.h> |
58771c1cb Bluetooth: conver... |
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#include <crypto/ecdh.h> struct ecdh_completion { struct completion completion; int err; }; static void ecdh_complete(struct crypto_async_request *req, int err) { struct ecdh_completion *res = req->data; if (err == -EINPROGRESS) return; res->err = err; complete(&res->completion); } static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits) { int i; for (i = 0; i < ndigits; i++) out[i] = __swab64(in[ndigits - 1 - i]); } |
c0153b0b9 Bluetooth: let th... |
50 51 52 53 54 55 56 57 |
/* compute_ecdh_secret() - function assumes that the private key was * already set. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp(). * @public_key: pair's ecc public key. * secret: memory where the ecdh computed shared secret will be saved. * * Return: zero on success; error code in case of error. */ |
a29764161 Bluetooth: ecdh_h... |
58 |
int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64], |
c0153b0b9 Bluetooth: let th... |
59 |
u8 secret[32]) |
58771c1cb Bluetooth: conver... |
60 |
{ |
58771c1cb Bluetooth: conver... |
61 |
struct kpp_request *req; |
c0153b0b9 Bluetooth: let th... |
62 |
u8 *tmp; |
58771c1cb Bluetooth: conver... |
63 64 |
struct ecdh_completion result; struct scatterlist src, dst; |
a29764161 Bluetooth: ecdh_h... |
65 |
int err; |
58771c1cb Bluetooth: conver... |
66 |
|
763d9a302 Bluetooth: alloca... |
67 68 |
tmp = kmalloc(64, GFP_KERNEL); if (!tmp) |
a29764161 Bluetooth: ecdh_h... |
69 |
return -ENOMEM; |
763d9a302 Bluetooth: alloca... |
70 |
|
58771c1cb Bluetooth: conver... |
71 |
req = kpp_request_alloc(tfm, GFP_KERNEL); |
a29764161 Bluetooth: ecdh_h... |
72 73 |
if (!req) { err = -ENOMEM; |
47eb2ac80 Bluetooth: move e... |
74 |
goto free_tmp; |
a29764161 Bluetooth: ecdh_h... |
75 |
} |
58771c1cb Bluetooth: conver... |
76 77 |
init_completion(&result.completion); |
58771c1cb Bluetooth: conver... |
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */ swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */ sg_init_one(&src, tmp, 64); sg_init_one(&dst, secret, 32); kpp_request_set_input(req, &src, 64); kpp_request_set_output(req, &dst, 32); kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, ecdh_complete, &result); err = crypto_kpp_compute_shared_secret(req); if (err == -EINPROGRESS) { wait_for_completion(&result.completion); err = result.err; } if (err < 0) { pr_err("alg: ecdh: compute shared secret failed. err %d ", err); goto free_all; } swap_digits((u64 *)secret, (u64 *)tmp, 4); memcpy(secret, tmp, 32); free_all: |
58771c1cb Bluetooth: conver... |
103 |
kpp_request_free(req); |
763d9a302 Bluetooth: alloca... |
104 |
free_tmp: |
453431a54 mm, treewide: ren... |
105 |
kfree_sensitive(tmp); |
a29764161 Bluetooth: ecdh_h... |
106 |
return err; |
58771c1cb Bluetooth: conver... |
107 |
} |
c0153b0b9 Bluetooth: let th... |
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
/* set_ecdh_privkey() - set or generate ecc private key. * * Function generates an ecc private key in the crypto subsystem when receiving * a NULL private key or sets the received key when not NULL. * * @tfm: KPP tfm handle allocated with crypto_alloc_kpp(). * @private_key: user's ecc private key. When not NULL, the key is expected * in little endian format. * * Return: zero on success; error code in case of error. */ int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32]) { u8 *buf, *tmp = NULL; unsigned int buf_len; int err; struct ecdh p = {0}; p.curve_id = ECC_CURVE_NIST_P256; if (private_key) { tmp = kmalloc(32, GFP_KERNEL); if (!tmp) return -ENOMEM; swap_digits((u64 *)private_key, (u64 *)tmp, 4); p.key = tmp; p.key_size = 32; } buf_len = crypto_ecdh_key_len(&p); buf = kmalloc(buf_len, GFP_KERNEL); if (!buf) { err = -ENOMEM; goto free_tmp; } err = crypto_ecdh_encode_key(buf, buf_len, &p); if (err) goto free_all; err = crypto_kpp_set_secret(tfm, buf, buf_len); /* fall through */ free_all: |
453431a54 mm, treewide: ren... |
151 |
kfree_sensitive(buf); |
c0153b0b9 Bluetooth: let th... |
152 |
free_tmp: |
453431a54 mm, treewide: ren... |
153 |
kfree_sensitive(tmp); |
c0153b0b9 Bluetooth: let th... |
154 155 156 157 158 159 160 161 162 163 164 165 |
return err; } /* generate_ecdh_public_key() - function assumes that the private key was * already set. * * @tfm: KPP tfm handle allocated with crypto_alloc_kpp(). * @public_key: memory where the computed ecc public key will be saved. * * Return: zero on success; error code in case of error. */ int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64]) |
58771c1cb Bluetooth: conver... |
166 |
{ |
58771c1cb Bluetooth: conver... |
167 |
struct kpp_request *req; |
c0153b0b9 Bluetooth: let th... |
168 |
u8 *tmp; |
58771c1cb Bluetooth: conver... |
169 170 |
struct ecdh_completion result; struct scatterlist dst; |
a29764161 Bluetooth: ecdh_h... |
171 |
int err; |
58771c1cb Bluetooth: conver... |
172 |
|
763d9a302 Bluetooth: alloca... |
173 174 |
tmp = kmalloc(64, GFP_KERNEL); if (!tmp) |
a29764161 Bluetooth: ecdh_h... |
175 |
return -ENOMEM; |
763d9a302 Bluetooth: alloca... |
176 |
|
58771c1cb Bluetooth: conver... |
177 |
req = kpp_request_alloc(tfm, GFP_KERNEL); |
a29764161 Bluetooth: ecdh_h... |
178 179 |
if (!req) { err = -ENOMEM; |
47eb2ac80 Bluetooth: move e... |
180 |
goto free_tmp; |
a29764161 Bluetooth: ecdh_h... |
181 |
} |
58771c1cb Bluetooth: conver... |
182 183 |
init_completion(&result.completion); |
c0153b0b9 Bluetooth: let th... |
184 185 186 187 188 |
sg_init_one(&dst, tmp, 64); kpp_request_set_input(req, NULL, 0); kpp_request_set_output(req, &dst, 64); kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, ecdh_complete, &result); |
58771c1cb Bluetooth: conver... |
189 |
|
c0153b0b9 Bluetooth: let th... |
190 191 192 193 194 195 196 197 198 199 |
err = crypto_kpp_generate_public_key(req); if (err == -EINPROGRESS) { wait_for_completion(&result.completion); err = result.err; } if (err < 0) goto free_all; /* The public key is handed back in little endian as expected by * the Security Manager Protocol. |
58771c1cb Bluetooth: conver... |
200 201 202 |
*/ swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */ swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */ |
58771c1cb Bluetooth: conver... |
203 204 |
free_all: |
58771c1cb Bluetooth: conver... |
205 |
kpp_request_free(req); |
763d9a302 Bluetooth: alloca... |
206 207 |
free_tmp: kfree(tmp); |
a29764161 Bluetooth: ecdh_h... |
208 |
return err; |
58771c1cb Bluetooth: conver... |
209 |
} |
c0153b0b9 Bluetooth: let th... |
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
/* generate_ecdh_keys() - generate ecc key pair. * * @tfm: KPP tfm handle allocated with crypto_alloc_kpp(). * @public_key: memory where the computed ecc public key will be saved. * * Return: zero on success; error code in case of error. */ int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64]) { int err; err = set_ecdh_privkey(tfm, NULL); if (err) return err; return generate_ecdh_public_key(tfm, public_key); } |