| 1 |
From 70cd1d16d6f72ecc7f7d1578a050913db8df503d Mon Sep 17 00:00:00 2001 |
| 2 |
From: Stephan Mueller <[email protected]> |
| 3 |
Date: Sun, 7 Jan 2018 18:17:23 +0100 |
| 4 |
Subject: [PATCH v3 8/8] crypto: AF_ALG - add KPP support |
| 5 |
|
| 6 |
The patch externalizes the KPP kernel crypto API to user space. This |
| 7 |
allows user space to make use of Diffie-Hellman and EC Diffie-Hellman |
| 8 |
operations. |
| 9 |
|
| 10 |
The following operations are supported: |
| 11 |
|
| 12 |
* DH parameters formatted in PKCS#3 with ALG_SET_DH_PARAMETERS |
| 13 |
setsockopt. The call returns the buffer length user space must |
| 14 |
allocate for the shared secret / public key operation. |
| 15 |
|
| 16 |
* ECDH curve selection via ALG_SET_ECDH_CURVE setsockopt. The call |
| 17 |
returns the buffer length user space must allocate for the shared |
| 18 |
secret / public key operation. |
| 19 |
|
| 20 |
* The private key can be set with the ALG_SET_KEY setsockopt. It is |
| 21 |
permissible to provide a NULL key. In this case, the kernel uses the |
| 22 |
tries to generate a private key of appropriate size and sets the key |
| 23 |
with the TFM. The idea is that the caller can obtain the public key |
| 24 |
from the private key, exchange it with the peer, inject the peer's |
| 25 |
public key into the kernel and derive the shared secret. This way, |
| 26 |
the private key does not leave the kernel realm. The call returns the |
| 27 |
buffer length user space must allocate for the shared secret / public |
| 28 |
key operation. |
| 29 |
|
| 30 |
* The public key is obtained from the private key via the recvmsg |
| 31 |
operation. For this operation, no data sent to the kernel via sendmsg |
| 32 |
or sendpage is required. |
| 33 |
|
| 34 |
* The shared secret is obtained by providing the peer's public key via |
| 35 |
sendmsg / sendpage and reading the shared secret via recvmsg. |
| 36 |
|
| 37 |
Signed-off-by: Stephan Mueller <[email protected]> |
| 38 |
--- |
| 39 |
crypto/Kconfig | 10 + |
| 40 |
crypto/Makefile | 1 + |
| 41 |
crypto/af_alg.c | 2 + |
| 42 |
crypto/algif_kpp.c | 612 ++++++++++++++++++++++++++++++++++++++++++++++++ |
| 43 |
include/crypto/if_alg.h | 2 + |
| 44 |
5 files changed, 627 insertions(+) |
| 45 |
create mode 100644 crypto/algif_kpp.c |
| 46 |
|
| 47 |
diff --git a/crypto/Kconfig b/crypto/Kconfig |
| 48 |
index 1785679b27f8..601b7e7b30b0 100644 |
| 49 |
--- a/crypto/Kconfig |
| 50 |
+++ b/crypto/Kconfig |
| 51 |
@@ -1781,6 +1781,16 @@ config CRYPTO_USER_API_AKCIPHER |
| 52 |
This option enables the user-space interface for asymmetric |
| 53 |
key cipher algorithms. |
| 54 |
|
| 55 |
+config CRYPTO_USER_API_KPP |
| 56 |
+ tristate "User-space interface for key protocol primitives algorithms" |
| 57 |
+ depends on NET |
| 58 |
+ select CRYPTO_KPP2 |
| 59 |
+ select CRYPTO_USER_API |
| 60 |
+ help |
| 61 |
+ This option enables the user-spaces interface for key protocol |
| 62 |
+ primitives algorithms. This covers Diffie-Hellman and EC |
| 63 |
+ Diffie-Hellman. |
| 64 |
+ |
| 65 |
config CRYPTO_HASH_INFO |
| 66 |
bool |
| 67 |
|
| 68 |
diff --git a/crypto/Makefile b/crypto/Makefile |
| 69 |
index c58f1fe2176a..5540e9d3ef0a 100644 |
| 70 |
--- a/crypto/Makefile |
| 71 |
+++ b/crypto/Makefile |
| 72 |
@@ -141,6 +141,7 @@ obj-$(CONFIG_CRYPTO_USER_API_SKCIPHER) += algif_skcipher.o |
| 73 |
obj-$(CONFIG_CRYPTO_USER_API_RNG) += algif_rng.o |
| 74 |
obj-$(CONFIG_CRYPTO_USER_API_AEAD) += algif_aead.o |
| 75 |
obj-$(CONFIG_CRYPTO_USER_API_AKCIPHER) += algif_akcipher.o |
| 76 |
+obj-$(CONFIG_CRYPTO_USER_API_KPP) += algif_kpp.o |
| 77 |
|
| 78 |
ecdh_generic-y := ecc.o |
| 79 |
ecdh_generic-y += ecdh.o |
| 80 |
diff --git a/crypto/af_alg.c b/crypto/af_alg.c |
| 81 |
index 681e77e1ca09..2699f3798acd 100644 |
| 82 |
--- a/crypto/af_alg.c |
| 83 |
+++ b/crypto/af_alg.c |
| 84 |
@@ -855,6 +855,8 @@ int af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size, |
| 85 |
case ALG_OP_SIGN: |
| 86 |
case ALG_OP_ENCRYPT: |
| 87 |
case ALG_OP_DECRYPT: |
| 88 |
+ case ALG_OP_KEYGEN: |
| 89 |
+ case ALG_OP_SSGEN: |
| 90 |
op = con.op; |
| 91 |
break; |
| 92 |
default: |
| 93 |
diff --git a/crypto/algif_kpp.c b/crypto/algif_kpp.c |
| 94 |
new file mode 100644 |
| 95 |
index 000000000000..3f561fe2b8ab |
| 96 |
--- /dev/null |
| 97 |
+++ b/crypto/algif_kpp.c |
| 98 |
@@ -0,0 +1,612 @@ |
| 99 |
+/* |
| 100 |
+ * algif_kpp: User-space interface for key protocol primitives algorithms |
| 101 |
+ * |
| 102 |
+ * Copyright (C) 2018 - 2022, Stephan Mueller <[email protected]> |
| 103 |
+ * |
| 104 |
+ * This file provides the user-space API for key protocol primitives. |
| 105 |
+ * |
| 106 |
+ * This program is free software; you can redistribute it and/or modify it |
| 107 |
+ * under the terms of the GNU General Public License as published by the Free |
| 108 |
+ * Software Foundation; either version 2 of the License, or (at your option) |
| 109 |
+ * any later version. |
| 110 |
+ * |
| 111 |
+ * The following concept of the memory management is used: |
| 112 |
+ * |
| 113 |
+ * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is |
| 114 |
+ * filled by user space with the data submitted via sendpage/sendmsg. Filling |
| 115 |
+ * up the TX SGL does not cause a crypto operation -- the data will only be |
| 116 |
+ * tracked by the kernel. Upon receipt of one recvmsg call, the caller must |
| 117 |
+ * provide a buffer which is tracked with the RX SGL. |
| 118 |
+ * |
| 119 |
+ * During the processing of the recvmsg operation, the cipher request is |
| 120 |
+ * allocated and prepared. As part of the recvmsg operation, the processed |
| 121 |
+ * TX buffers are extracted from the TX SGL into a separate SGL. |
| 122 |
+ * |
| 123 |
+ * After the completion of the crypto operation, the RX SGL and the cipher |
| 124 |
+ * request is released. The extracted TX SGL parts are released together with |
| 125 |
+ * the RX SGL release. |
| 126 |
+ */ |
| 127 |
+ |
| 128 |
+#include <crypto/dh.h> |
| 129 |
+#include <crypto/ecdh.h> |
| 130 |
+#include <crypto/kpp.h> |
| 131 |
+#include <crypto/rng.h> |
| 132 |
+#include <crypto/if_alg.h> |
| 133 |
+#include <crypto/scatterwalk.h> |
| 134 |
+#include <linux/init.h> |
| 135 |
+#include <linux/kernel.h> |
| 136 |
+#include <linux/list.h> |
| 137 |
+#include <linux/mm.h> |
| 138 |
+#include <linux/module.h> |
| 139 |
+#include <linux/net.h> |
| 140 |
+#include <net/sock.h> |
| 141 |
+ |
| 142 |
+struct kpp_tfm { |
| 143 |
+ struct crypto_kpp *kpp; |
| 144 |
+ bool has_key; |
| 145 |
+ |
| 146 |
+#define KPP_NO_PARAMS 0 |
| 147 |
+#define KPP_DH_PARAMS 1 |
| 148 |
+#define KPP_ECDH_PARAMS 2 |
| 149 |
+ int has_params; /* Type of KPP mechanism */ |
| 150 |
+}; |
| 151 |
+ |
| 152 |
+static int kpp_sendmsg(struct socket *sock, struct msghdr *msg, size_t size) |
| 153 |
+{ |
| 154 |
+ return af_alg_sendmsg(sock, msg, size, 0); |
| 155 |
+} |
| 156 |
+ |
| 157 |
+static inline int kpp_cipher_op(struct af_alg_ctx *ctx, |
| 158 |
+ struct af_alg_async_req *areq) |
| 159 |
+{ |
| 160 |
+ switch (ctx->op) { |
| 161 |
+ case ALG_OP_KEYGEN: |
| 162 |
+ return crypto_kpp_generate_public_key(&areq->cra_u.kpp_req); |
| 163 |
+ case ALG_OP_SSGEN: |
| 164 |
+ return crypto_kpp_compute_shared_secret(&areq->cra_u.kpp_req); |
| 165 |
+ default: |
| 166 |
+ return -EOPNOTSUPP; |
| 167 |
+ } |
| 168 |
+} |
| 169 |
+ |
| 170 |
+static int _kpp_recvmsg(struct socket *sock, struct msghdr *msg, |
| 171 |
+ size_t ignored, int flags) |
| 172 |
+{ |
| 173 |
+ struct sock *sk = sock->sk; |
| 174 |
+ struct alg_sock *ask = alg_sk(sk); |
| 175 |
+ struct sock *psk = ask->parent; |
| 176 |
+ struct alg_sock *pask = alg_sk(psk); |
| 177 |
+ struct af_alg_ctx *ctx = ask->private; |
| 178 |
+ struct kpp_tfm *kpp = pask->private; |
| 179 |
+ struct crypto_kpp *tfm = kpp->kpp; |
| 180 |
+ struct af_alg_async_req *areq; |
| 181 |
+ size_t len; |
| 182 |
+ size_t used = 0; |
| 183 |
+ int err; |
| 184 |
+ int maxsize; |
| 185 |
+ |
| 186 |
+ if (!ctx->used) { |
| 187 |
+ err = af_alg_wait_for_data(sk, flags); |
| 188 |
+ if (err) |
| 189 |
+ return err; |
| 190 |
+ } |
| 191 |
+ |
| 192 |
+ maxsize = crypto_kpp_maxsize(tfm); |
| 193 |
+ if (maxsize < 0) |
| 194 |
+ return maxsize; |
| 195 |
+ |
| 196 |
+ /* Allocate cipher request for current operation. */ |
| 197 |
+ areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) + |
| 198 |
+ crypto_kpp_reqsize(tfm)); |
| 199 |
+ if (IS_ERR(areq)) |
| 200 |
+ return PTR_ERR(areq); |
| 201 |
+ |
| 202 |
+ /* convert iovecs of output buffers into RX SGL */ |
| 203 |
+ err = af_alg_get_rsgl(sk, msg, flags, areq, maxsize, &len); |
| 204 |
+ if (err) |
| 205 |
+ goto free; |
| 206 |
+ |
| 207 |
+ /* ensure output buffer is sufficiently large */ |
| 208 |
+ if (len < maxsize) { |
| 209 |
+ err = -EMSGSIZE; |
| 210 |
+ goto free; |
| 211 |
+ } |
| 212 |
+ |
| 213 |
+ /* |
| 214 |
+ * Create a per request TX SGL for this request which tracks the |
| 215 |
+ * SG entries from the global TX SGL. |
| 216 |
+ */ |
| 217 |
+ if (ctx->op == ALG_OP_SSGEN) { |
| 218 |
+ used = ctx->used; |
| 219 |
+ |
| 220 |
+ areq->tsgl_entries = af_alg_count_tsgl(sk, used, 0); |
| 221 |
+ if (!areq->tsgl_entries) |
| 222 |
+ areq->tsgl_entries = 1; |
| 223 |
+ areq->tsgl = sock_kmalloc( |
| 224 |
+ sk, sizeof(*areq->tsgl) * areq->tsgl_entries, |
| 225 |
+ GFP_KERNEL); |
| 226 |
+ if (!areq->tsgl) { |
| 227 |
+ err = -ENOMEM; |
| 228 |
+ goto free; |
| 229 |
+ } |
| 230 |
+ sg_init_table(areq->tsgl, areq->tsgl_entries); |
| 231 |
+ af_alg_pull_tsgl(sk, used, areq->tsgl, 0); |
| 232 |
+ } |
| 233 |
+ |
| 234 |
+ /* Initialize the crypto operation */ |
| 235 |
+ kpp_request_set_input(&areq->cra_u.kpp_req, areq->tsgl, used); |
| 236 |
+ kpp_request_set_output(&areq->cra_u.kpp_req, areq->first_rsgl.sgl.sg, |
| 237 |
+ len); |
| 238 |
+ kpp_request_set_tfm(&areq->cra_u.kpp_req, tfm); |
| 239 |
+ |
| 240 |
+ if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) { |
| 241 |
+ /* AIO operation */ |
| 242 |
+ sock_hold(sk); |
| 243 |
+ areq->iocb = msg->msg_iocb; |
| 244 |
+ |
| 245 |
+ /* Remember output size that will be generated. */ |
| 246 |
+ areq->outlen = len; |
| 247 |
+ |
| 248 |
+ kpp_request_set_callback(&areq->cra_u.kpp_req, |
| 249 |
+ CRYPTO_TFM_REQ_MAY_SLEEP, |
| 250 |
+ af_alg_async_cb, areq); |
| 251 |
+ err = kpp_cipher_op(ctx, areq); |
| 252 |
+ |
| 253 |
+ /* AIO operation in progress */ |
| 254 |
+ if (err == -EINPROGRESS || err == -EBUSY) |
| 255 |
+ return -EIOCBQUEUED; |
| 256 |
+ |
| 257 |
+ sock_put(sk); |
| 258 |
+ } else { |
| 259 |
+ /* Synchronous operation */ |
| 260 |
+ kpp_request_set_callback(&areq->cra_u.kpp_req, |
| 261 |
+ CRYPTO_TFM_REQ_MAY_SLEEP | |
| 262 |
+ CRYPTO_TFM_REQ_MAY_BACKLOG, |
| 263 |
+ crypto_req_done, |
| 264 |
+ &ctx->wait); |
| 265 |
+ err = crypto_wait_req(kpp_cipher_op(ctx, areq), &ctx->wait); |
| 266 |
+ } |
| 267 |
+ |
| 268 |
+free: |
| 269 |
+ af_alg_free_resources(areq); |
| 270 |
+ |
| 271 |
+ return err ? err : len; |
| 272 |
+} |
| 273 |
+ |
| 274 |
+static int kpp_recvmsg(struct socket *sock, struct msghdr *msg, |
| 275 |
+ size_t ignored, int flags) |
| 276 |
+{ |
| 277 |
+ struct sock *sk = sock->sk; |
| 278 |
+ struct alg_sock *ask = alg_sk(sk); |
| 279 |
+ struct sock *psk = ask->parent; |
| 280 |
+ struct alg_sock *pask = alg_sk(psk); |
| 281 |
+ struct kpp_tfm *kpp = pask->private; |
| 282 |
+ struct crypto_kpp *tfm = kpp->kpp; |
| 283 |
+ int ret = 0; |
| 284 |
+ int err; |
| 285 |
+ |
| 286 |
+ lock_sock(sk); |
| 287 |
+ |
| 288 |
+ while (msg_data_left(msg)) { |
| 289 |
+ err = _kpp_recvmsg(sock, msg, ignored, flags); |
| 290 |
+ |
| 291 |
+ /* |
| 292 |
+ * This error covers -EIOCBQUEUED which implies that we can |
| 293 |
+ * only handle one AIO request. If the caller wants to have |
| 294 |
+ * multiple AIO requests in parallel, he must make multiple |
| 295 |
+ * separate AIO calls. |
| 296 |
+ */ |
| 297 |
+ if (err <= 0) { |
| 298 |
+ if (err == -EIOCBQUEUED || err == -EBADMSG || !ret) |
| 299 |
+ ret = err; |
| 300 |
+ goto out; |
| 301 |
+ } |
| 302 |
+ |
| 303 |
+ ret += err; |
| 304 |
+ |
| 305 |
+ /* |
| 306 |
+ * The caller must provide crypto_kpp_maxsize per request. |
| 307 |
+ * If he provides more, we conclude that multiple kpp |
| 308 |
+ * operations are requested. |
| 309 |
+ */ |
| 310 |
+ iov_iter_advance(&msg->msg_iter, |
| 311 |
+ crypto_kpp_maxsize(tfm) - err); |
| 312 |
+ } |
| 313 |
+ |
| 314 |
+out: |
| 315 |
+ |
| 316 |
+ af_alg_wmem_wakeup(sk); |
| 317 |
+ release_sock(sk); |
| 318 |
+ return ret; |
| 319 |
+} |
| 320 |
+ |
| 321 |
+static struct proto_ops algif_kpp_ops = { |
| 322 |
+ .family = PF_ALG, |
| 323 |
+ |
| 324 |
+ .connect = sock_no_connect, |
| 325 |
+ .socketpair = sock_no_socketpair, |
| 326 |
+ .getname = sock_no_getname, |
| 327 |
+ .ioctl = sock_no_ioctl, |
| 328 |
+ .listen = sock_no_listen, |
| 329 |
+ .shutdown = sock_no_shutdown, |
| 330 |
+ .getsockopt = sock_no_getsockopt, |
| 331 |
+ .mmap = sock_no_mmap, |
| 332 |
+ .bind = sock_no_bind, |
| 333 |
+ .accept = sock_no_accept, |
| 334 |
+ .setsockopt = sock_no_setsockopt, |
| 335 |
+ |
| 336 |
+ .release = af_alg_release, |
| 337 |
+ .sendmsg = kpp_sendmsg, |
| 338 |
+ .sendpage = af_alg_sendpage, |
| 339 |
+ .recvmsg = kpp_recvmsg, |
| 340 |
+ .poll = af_alg_poll, |
| 341 |
+}; |
| 342 |
+ |
| 343 |
+static int kpp_check_key(struct socket *sock) |
| 344 |
+{ |
| 345 |
+ struct sock *psk; |
| 346 |
+ struct alg_sock *pask; |
| 347 |
+ struct kpp_tfm *tfm; |
| 348 |
+ struct sock *sk = sock->sk; |
| 349 |
+ struct alg_sock *ask = alg_sk(sk); |
| 350 |
+ int err = 0; |
| 351 |
+ |
| 352 |
+ lock_sock(sk); |
| 353 |
+ if (ask->refcnt) |
| 354 |
+ goto unlock_child; |
| 355 |
+ |
| 356 |
+ psk = ask->parent; |
| 357 |
+ pask = alg_sk(ask->parent); |
| 358 |
+ tfm = pask->private; |
| 359 |
+ |
| 360 |
+ lock_sock_nested(psk, SINGLE_DEPTH_NESTING); |
| 361 |
+ if (!tfm->has_key || (tfm->has_params == KPP_NO_PARAMS)) { |
| 362 |
+ err = -ENOKEY; |
| 363 |
+ goto unlock; |
| 364 |
+ } |
| 365 |
+ |
| 366 |
+ if (!pask->refcnt++) |
| 367 |
+ sock_hold(psk); |
| 368 |
+ |
| 369 |
+ ask->refcnt = 1; |
| 370 |
+ sock_put(psk); |
| 371 |
+ |
| 372 |
+ err = 0; |
| 373 |
+ |
| 374 |
+unlock: |
| 375 |
+ release_sock(psk); |
| 376 |
+unlock_child: |
| 377 |
+ release_sock(sk); |
| 378 |
+ |
| 379 |
+ return err; |
| 380 |
+} |
| 381 |
+ |
| 382 |
+static int kpp_sendmsg_nokey(struct socket *sock, struct msghdr *msg, |
| 383 |
+ size_t size) |
| 384 |
+{ |
| 385 |
+ int err; |
| 386 |
+ |
| 387 |
+ err = kpp_check_key(sock); |
| 388 |
+ if (err) |
| 389 |
+ return err; |
| 390 |
+ |
| 391 |
+ return kpp_sendmsg(sock, msg, size); |
| 392 |
+} |
| 393 |
+ |
| 394 |
+static ssize_t kpp_sendpage_nokey(struct socket *sock, struct page *page, |
| 395 |
+ int offset, size_t size, int flags) |
| 396 |
+{ |
| 397 |
+ int err; |
| 398 |
+ |
| 399 |
+ err = kpp_check_key(sock); |
| 400 |
+ if (err) |
| 401 |
+ return err; |
| 402 |
+ |
| 403 |
+ return af_alg_sendpage(sock, page, offset, size, flags); |
| 404 |
+} |
| 405 |
+ |
| 406 |
+static int kpp_recvmsg_nokey(struct socket *sock, struct msghdr *msg, |
| 407 |
+ size_t ignored, int flags) |
| 408 |
+{ |
| 409 |
+ int err; |
| 410 |
+ |
| 411 |
+ err = kpp_check_key(sock); |
| 412 |
+ if (err) |
| 413 |
+ return err; |
| 414 |
+ |
| 415 |
+ return kpp_recvmsg(sock, msg, ignored, flags); |
| 416 |
+} |
| 417 |
+ |
| 418 |
+static struct proto_ops algif_kpp_ops_nokey = { |
| 419 |
+ .family = PF_ALG, |
| 420 |
+ |
| 421 |
+ .connect = sock_no_connect, |
| 422 |
+ .socketpair = sock_no_socketpair, |
| 423 |
+ .getname = sock_no_getname, |
| 424 |
+ .ioctl = sock_no_ioctl, |
| 425 |
+ .listen = sock_no_listen, |
| 426 |
+ .shutdown = sock_no_shutdown, |
| 427 |
+ .getsockopt = sock_no_getsockopt, |
| 428 |
+ .mmap = sock_no_mmap, |
| 429 |
+ .bind = sock_no_bind, |
| 430 |
+ .accept = sock_no_accept, |
| 431 |
+ .setsockopt = sock_no_setsockopt, |
| 432 |
+ |
| 433 |
+ .release = af_alg_release, |
| 434 |
+ .sendmsg = kpp_sendmsg_nokey, |
| 435 |
+ .sendpage = kpp_sendpage_nokey, |
| 436 |
+ .recvmsg = kpp_recvmsg_nokey, |
| 437 |
+ .poll = af_alg_poll, |
| 438 |
+}; |
| 439 |
+ |
| 440 |
+static void *kpp_bind(const char *name, u32 type, u32 mask) |
| 441 |
+{ |
| 442 |
+ struct kpp_tfm *tfm; |
| 443 |
+ struct crypto_kpp *kpp; |
| 444 |
+ |
| 445 |
+ tfm = kmalloc(sizeof(*tfm), GFP_KERNEL); |
| 446 |
+ if (!tfm) |
| 447 |
+ return ERR_PTR(-ENOMEM); |
| 448 |
+ |
| 449 |
+ kpp = crypto_alloc_kpp(name, type, mask); |
| 450 |
+ if (IS_ERR(kpp)) { |
| 451 |
+ kfree(tfm); |
| 452 |
+ return ERR_CAST(kpp); |
| 453 |
+ } |
| 454 |
+ |
| 455 |
+ tfm->kpp = kpp; |
| 456 |
+ tfm->has_key = false; |
| 457 |
+ tfm->has_params = KPP_NO_PARAMS; |
| 458 |
+ |
| 459 |
+ return tfm; |
| 460 |
+} |
| 461 |
+ |
| 462 |
+static void kpp_release(void *private) |
| 463 |
+{ |
| 464 |
+ struct kpp_tfm *tfm = private; |
| 465 |
+ struct crypto_kpp *kpp = tfm->kpp; |
| 466 |
+ |
| 467 |
+ crypto_free_kpp(kpp); |
| 468 |
+ kfree(tfm); |
| 469 |
+} |
| 470 |
+ |
| 471 |
+static int kpp_dh_set_secret(struct crypto_kpp *tfm, struct dh *params) |
| 472 |
+{ |
| 473 |
+ char *packed_key = NULL; |
| 474 |
+ unsigned int packed_key_len; |
| 475 |
+ int ret; |
| 476 |
+ |
| 477 |
+ packed_key_len = crypto_dh_key_len(params); |
| 478 |
+ packed_key = kmalloc(packed_key_len, GFP_KERNEL); |
| 479 |
+ if (!packed_key) |
| 480 |
+ return -ENOMEM; |
| 481 |
+ |
| 482 |
+ ret = crypto_dh_encode_key(packed_key, packed_key_len, params); |
| 483 |
+ if (ret) |
| 484 |
+ goto out; |
| 485 |
+ |
| 486 |
+ ret = crypto_kpp_set_secret(tfm, packed_key, packed_key_len); |
| 487 |
+ |
| 488 |
+out: |
| 489 |
+ kfree(packed_key); |
| 490 |
+ return ret; |
| 491 |
+} |
| 492 |
+ |
| 493 |
+static int kpp_dh_set_privkey(struct crypto_kpp *tfm, const u8 *key, |
| 494 |
+ unsigned int keylen) |
| 495 |
+{ |
| 496 |
+ struct dh params = { |
| 497 |
+ .key = key, |
| 498 |
+ .key_size = keylen, |
| 499 |
+ .p = NULL, |
| 500 |
+ .p_size = 0, |
| 501 |
+ .g = NULL, |
| 502 |
+ .g_size = 0, |
| 503 |
+ }; |
| 504 |
+ |
| 505 |
+ return kpp_dh_set_secret(tfm, ¶ms); |
| 506 |
+} |
| 507 |
+ |
| 508 |
+static int kpp_ecdh_set_secret(struct crypto_kpp *tfm, struct ecdh *params) |
| 509 |
+{ |
| 510 |
+ char *packed_key = NULL; |
| 511 |
+ unsigned int packed_key_len; |
| 512 |
+ int ret; |
| 513 |
+ |
| 514 |
+ packed_key_len = crypto_ecdh_key_len(params); |
| 515 |
+ packed_key = kmalloc(packed_key_len, GFP_KERNEL); |
| 516 |
+ if (!packed_key) |
| 517 |
+ return -ENOMEM; |
| 518 |
+ |
| 519 |
+ ret = crypto_ecdh_encode_key(packed_key, packed_key_len, params); |
| 520 |
+ if (ret) |
| 521 |
+ goto out; |
| 522 |
+ |
| 523 |
+ ret = crypto_kpp_set_secret(tfm, packed_key, packed_key_len); |
| 524 |
+ |
| 525 |
+out: |
| 526 |
+ kfree(packed_key); |
| 527 |
+ return ret; |
| 528 |
+} |
| 529 |
+ |
| 530 |
+static int kpp_ecdh_set_privkey(struct crypto_kpp *tfm, const u8 *key, |
| 531 |
+ unsigned int keylen) |
| 532 |
+{ |
| 533 |
+ struct ecdh params = { |
| 534 |
+ .curve_id = 0, |
| 535 |
+ .key = key, |
| 536 |
+ .key_size = keylen, |
| 537 |
+ }; |
| 538 |
+ |
| 539 |
+ return kpp_ecdh_set_secret(tfm, ¶ms); |
| 540 |
+} |
| 541 |
+ |
| 542 |
+static int kpp_setprivkey(void *private, const u8 *key, unsigned int keylen) |
| 543 |
+{ |
| 544 |
+ struct kpp_tfm *kpp = private; |
| 545 |
+ struct crypto_kpp *tfm = kpp->kpp; |
| 546 |
+ int err; |
| 547 |
+ |
| 548 |
+ if (kpp->has_params == KPP_NO_PARAMS) |
| 549 |
+ return -ENOKEY; |
| 550 |
+ |
| 551 |
+ /* The DH code cannot generate private keys. ECDH can do that */ |
| 552 |
+ if ((!key || !keylen) && (kpp->has_params == KPP_DH_PARAMS)) { |
| 553 |
+ kpp->has_key = false; |
| 554 |
+ return -EOPNOTSUPP; |
| 555 |
+ } |
| 556 |
+ |
| 557 |
+ switch (kpp->has_params) { |
| 558 |
+ case KPP_DH_PARAMS: |
| 559 |
+ err = kpp_dh_set_privkey(tfm, key, keylen); |
| 560 |
+ break; |
| 561 |
+ case KPP_ECDH_PARAMS: |
| 562 |
+ err = kpp_ecdh_set_privkey(tfm, key, keylen); |
| 563 |
+ break; |
| 564 |
+ default: |
| 565 |
+ err = -EFAULT; |
| 566 |
+ } |
| 567 |
+ |
| 568 |
+ kpp->has_key = !err; |
| 569 |
+ |
| 570 |
+ /* Return the maximum size of the kpp operation. */ |
| 571 |
+ if (!err) |
| 572 |
+ err = crypto_kpp_maxsize(tfm); |
| 573 |
+ |
| 574 |
+ return err; |
| 575 |
+} |
| 576 |
+ |
| 577 |
+static int kpp_dh_setparams_pkcs3(void *private, const u8 *params, |
| 578 |
+ unsigned int paramslen) |
| 579 |
+{ |
| 580 |
+ struct kpp_tfm *kpp = private; |
| 581 |
+ struct crypto_kpp *tfm = kpp->kpp; |
| 582 |
+ int err; |
| 583 |
+ |
| 584 |
+ /* If parameters were already set, disallow setting them again. */ |
| 585 |
+ if (kpp->has_params != KPP_NO_PARAMS) |
| 586 |
+ return -EINVAL; |
| 587 |
+ |
| 588 |
+ err = crypto_kpp_set_params(tfm, params, paramslen); |
| 589 |
+ if (!err) { |
| 590 |
+ kpp->has_params = KPP_DH_PARAMS; |
| 591 |
+ /* Return the maximum size of the kpp operation. */ |
| 592 |
+ err = crypto_kpp_maxsize(tfm); |
| 593 |
+ } else |
| 594 |
+ kpp->has_params = KPP_NO_PARAMS; |
| 595 |
+ |
| 596 |
+ return err; |
| 597 |
+} |
| 598 |
+ |
| 599 |
+static int kpp_ecdh_setcurve(void *private, const u8 *curveid, |
| 600 |
+ unsigned int curveidlen) |
| 601 |
+{ |
| 602 |
+ struct kpp_tfm *kpp = private; |
| 603 |
+ struct crypto_kpp *tfm = kpp->kpp; |
| 604 |
+ int err; |
| 605 |
+ struct ecdh params = { |
| 606 |
+ .key = NULL, |
| 607 |
+ .key_size = 0, |
| 608 |
+ }; |
| 609 |
+ |
| 610 |
+ /* If parameters were already set, disallow setting them again. */ |
| 611 |
+ if (kpp->has_params != KPP_NO_PARAMS) |
| 612 |
+ return -EINVAL; |
| 613 |
+ |
| 614 |
+ if (curveidlen != sizeof(unsigned long)) |
| 615 |
+ return -EINVAL; |
| 616 |
+ |
| 617 |
+ err = kstrtou16(curveid, 10, ¶ms.curve_id); |
| 618 |
+ if (err) |
| 619 |
+ return err; |
| 620 |
+ |
| 621 |
+ err = kpp_ecdh_set_secret(tfm, ¶ms); |
| 622 |
+ if (!err) { |
| 623 |
+ kpp->has_params = KPP_ECDH_PARAMS; |
| 624 |
+ /* Return the maximum size of the kpp operation. */ |
| 625 |
+ err = crypto_kpp_maxsize(tfm); |
| 626 |
+ } else |
| 627 |
+ kpp->has_params = KPP_NO_PARAMS; |
| 628 |
+ |
| 629 |
+ return err; |
| 630 |
+} |
| 631 |
+ |
| 632 |
+static void kpp_sock_destruct(struct sock *sk) |
| 633 |
+{ |
| 634 |
+ struct alg_sock *ask = alg_sk(sk); |
| 635 |
+ struct af_alg_ctx *ctx = ask->private; |
| 636 |
+ |
| 637 |
+ af_alg_pull_tsgl(sk, ctx->used, NULL, 0); |
| 638 |
+ sock_kfree_s(sk, ctx, ctx->len); |
| 639 |
+ af_alg_release_parent(sk); |
| 640 |
+} |
| 641 |
+ |
| 642 |
+static int kpp_accept_parent_nokey(void *private, struct sock *sk) |
| 643 |
+{ |
| 644 |
+ struct af_alg_ctx *ctx; |
| 645 |
+ struct alg_sock *ask = alg_sk(sk); |
| 646 |
+ unsigned int len = sizeof(*ctx); |
| 647 |
+ |
| 648 |
+ ctx = sock_kmalloc(sk, len, GFP_KERNEL); |
| 649 |
+ if (!ctx) |
| 650 |
+ return -ENOMEM; |
| 651 |
+ |
| 652 |
+ INIT_LIST_HEAD(&ctx->tsgl_list); |
| 653 |
+ ctx->len = len; |
| 654 |
+ ctx->used = 0; |
| 655 |
+ atomic_set(&ctx->rcvused, 0); |
| 656 |
+ ctx->more = 0; |
| 657 |
+ ctx->merge = 0; |
| 658 |
+ ctx->op = 0; |
| 659 |
+ crypto_init_wait(&ctx->wait); |
| 660 |
+ |
| 661 |
+ ask->private = ctx; |
| 662 |
+ |
| 663 |
+ sk->sk_destruct = kpp_sock_destruct; |
| 664 |
+ |
| 665 |
+ return 0; |
| 666 |
+} |
| 667 |
+ |
| 668 |
+static int kpp_accept_parent(void *private, struct sock *sk) |
| 669 |
+{ |
| 670 |
+ struct kpp_tfm *tfm = private; |
| 671 |
+ |
| 672 |
+ if (!tfm->has_key || (tfm->has_params == KPP_NO_PARAMS)) |
| 673 |
+ return -ENOKEY; |
| 674 |
+ |
| 675 |
+ return kpp_accept_parent_nokey(private, sk); |
| 676 |
+} |
| 677 |
+ |
| 678 |
+static const struct af_alg_type algif_type_kpp = { |
| 679 |
+ .bind = kpp_bind, |
| 680 |
+ .release = kpp_release, |
| 681 |
+ .setkey = kpp_setprivkey, |
| 682 |
+ .setpubkey = NULL, |
| 683 |
+ .dhparams = kpp_dh_setparams_pkcs3, |
| 684 |
+ .ecdhcurve = kpp_ecdh_setcurve, |
| 685 |
+ .setauthsize = NULL, |
| 686 |
+ .accept = kpp_accept_parent, |
| 687 |
+ .accept_nokey = kpp_accept_parent_nokey, |
| 688 |
+ .ops = &algif_kpp_ops, |
| 689 |
+ .ops_nokey = &algif_kpp_ops_nokey, |
| 690 |
+ .name = "kpp", |
| 691 |
+ .owner = THIS_MODULE |
| 692 |
+}; |
| 693 |
+ |
| 694 |
+static int __init algif_kpp_init(void) |
| 695 |
+{ |
| 696 |
+ return af_alg_register_type(&algif_type_kpp); |
| 697 |
+} |
| 698 |
+ |
| 699 |
+static void __exit algif_kpp_exit(void) |
| 700 |
+{ |
| 701 |
+ int err = af_alg_unregister_type(&algif_type_kpp); |
| 702 |
+ |
| 703 |
+ BUG_ON(err); |
| 704 |
+} |
| 705 |
+ |
| 706 |
+module_init(algif_kpp_init); |
| 707 |
+module_exit(algif_kpp_exit); |
| 708 |
+MODULE_LICENSE("GPL"); |
| 709 |
+MODULE_AUTHOR("Stephan Mueller <[email protected]>"); |
| 710 |
+MODULE_DESCRIPTION("Key protocol primitives kernel crypto API user space interface"); |
| 711 |
diff --git a/include/crypto/if_alg.h b/include/crypto/if_alg.h |
| 712 |
index 641315e3051a..426135ff0617 100644 |
| 713 |
--- a/include/crypto/if_alg.h |
| 714 |
+++ b/include/crypto/if_alg.h |
| 715 |
@@ -24,6 +24,7 @@ |
| 716 |
#include <crypto/aead.h> |
| 717 |
#include <crypto/skcipher.h> |
| 718 |
#include <crypto/akcipher.h> |
| 719 |
+#include <crypto/kpp.h> |
| 720 |
|
| 721 |
#define ALG_MAX_PAGES 16 |
| 722 |
|
| 723 |
@@ -119,6 +120,7 @@ struct af_alg_async_req { |
| 724 |
struct aead_request aead_req; |
| 725 |
struct skcipher_request skcipher_req; |
| 726 |
struct akcipher_request akcipher_req; |
| 727 |
+ struct kpp_request kpp_req; |
| 728 |
} cra_u; |
| 729 |
|
| 730 |
/* req ctx trails this struct */ |
| 731 |
-- |
| 732 |
2.14.3 |
| 733 |
|