Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * algif_skcipher: User-space interface for skcipher algorithms
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * This file provides the user-space API for symmetric key ciphers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * The following concept of the memory management is used:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * The kernel maintains two SGLs, the TX SGL and the RX SGL. The TX SGL is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * filled by user space with the data submitted via sendpage/sendmsg. Filling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * up the TX SGL does not cause a crypto operation -- the data will only be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * tracked by the kernel. Upon receipt of one recvmsg call, the caller must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * provide a buffer which is tracked with the RX SGL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * During the processing of the recvmsg operation, the cipher request is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * allocated and prepared. As part of the recvmsg operation, the processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * TX buffers are extracted from the TX SGL into a separate SGL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * After the completion of the crypto operation, the RX SGL and the cipher
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * request is released. The extracted TX SGL parts are released together with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * the RX SGL release.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include <crypto/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include <crypto/if_alg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <linux/net.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) static int skcipher_sendmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 			    size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct sock *psk = ask->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	struct alg_sock *pask = alg_sk(psk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	struct crypto_skcipher *tfm = pask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	unsigned ivsize = crypto_skcipher_ivsize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	return af_alg_sendmsg(sock, msg, size, ivsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static int _skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 			     size_t ignored, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	struct sock *psk = ask->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct alg_sock *pask = alg_sk(psk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	struct crypto_skcipher *tfm = pask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	unsigned int bs = crypto_skcipher_chunksize(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct af_alg_async_req *areq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	size_t len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	if (!ctx->init || (ctx->more && ctx->used < bs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		err = af_alg_wait_for_data(sk, flags, bs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	/* Allocate cipher request for current operation. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	areq = af_alg_alloc_areq(sk, sizeof(struct af_alg_async_req) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				     crypto_skcipher_reqsize(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	if (IS_ERR(areq))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return PTR_ERR(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	/* convert iovecs of output buffers into RX SGL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	err = af_alg_get_rsgl(sk, msg, flags, areq, ctx->used, &len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * If more buffers are to be expected to be processed, process only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 * full block size buffers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	if (ctx->more || len < ctx->used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 		len -= len % bs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	 * Create a per request TX SGL for this request which tracks the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	 * SG entries from the global TX SGL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	areq->tsgl_entries = af_alg_count_tsgl(sk, len, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (!areq->tsgl_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		areq->tsgl_entries = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	areq->tsgl = sock_kmalloc(sk, array_size(sizeof(*areq->tsgl),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 						 areq->tsgl_entries),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 				  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	if (!areq->tsgl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		goto free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	sg_init_table(areq->tsgl, areq->tsgl_entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	af_alg_pull_tsgl(sk, len, areq->tsgl, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	/* Initialize the crypto operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	skcipher_request_set_tfm(&areq->cra_u.skcipher_req, tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	skcipher_request_set_crypt(&areq->cra_u.skcipher_req, areq->tsgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 				   areq->first_rsgl.sgl.sg, len, ctx->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	if (msg->msg_iocb && !is_sync_kiocb(msg->msg_iocb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 		/* AIO operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		sock_hold(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		areq->iocb = msg->msg_iocb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 		/* Remember output size that will be generated. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		areq->outlen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		skcipher_request_set_callback(&areq->cra_u.skcipher_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 					      CRYPTO_TFM_REQ_MAY_SLEEP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 					      af_alg_async_cb, areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 		err = ctx->enc ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 			crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			crypto_skcipher_decrypt(&areq->cra_u.skcipher_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		/* AIO operation in progress */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		if (err == -EINPROGRESS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			return -EIOCBQUEUED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		sock_put(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		/* Synchronous operation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		skcipher_request_set_callback(&areq->cra_u.skcipher_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 					      CRYPTO_TFM_REQ_MAY_SLEEP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 					      CRYPTO_TFM_REQ_MAY_BACKLOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 					      crypto_req_done, &ctx->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		err = crypto_wait_req(ctx->enc ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			crypto_skcipher_encrypt(&areq->cra_u.skcipher_req) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 			crypto_skcipher_decrypt(&areq->cra_u.skcipher_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 						 &ctx->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	af_alg_free_resources(areq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	return err ? err : len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) static int skcipher_recvmsg(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 			    size_t ignored, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	while (msg_data_left(msg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		int err = _skcipher_recvmsg(sock, msg, ignored, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		 * This error covers -EIOCBQUEUED which implies that we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 		 * only handle one AIO request. If the caller wants to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 		 * multiple AIO requests in parallel, he must make multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 		 * separate AIO calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		 * Also return the error if no data has been processed so far.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 		if (err <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 			if (err == -EIOCBQUEUED || !ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 				ret = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 		ret += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	af_alg_wmem_wakeup(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) static struct proto_ops algif_skcipher_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	.family		=	PF_ALG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	.connect	=	sock_no_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	.socketpair	=	sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	.getname	=	sock_no_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	.ioctl		=	sock_no_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	.listen		=	sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	.shutdown	=	sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	.mmap		=	sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	.bind		=	sock_no_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	.accept		=	sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	.release	=	af_alg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	.sendmsg	=	skcipher_sendmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	.sendpage	=	af_alg_sendpage,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	.recvmsg	=	skcipher_recvmsg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	.poll		=	af_alg_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static int skcipher_check_key(struct socket *sock)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	struct sock *psk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	struct alg_sock *pask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	struct crypto_skcipher *tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct sock *sk = sock->sk;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	lock_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	if (!atomic_read(&ask->nokey_refcnt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		goto unlock_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	psk = ask->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	pask = alg_sk(ask->parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	tfm = pask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	err = -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	lock_sock_nested(psk, SINGLE_DEPTH_NESTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	atomic_dec(&pask->nokey_refcnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	atomic_set(&ask->nokey_refcnt, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	release_sock(psk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unlock_child:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	release_sock(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) static int skcipher_sendmsg_nokey(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 				  size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	err = skcipher_check_key(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	return skcipher_sendmsg(sock, msg, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static ssize_t skcipher_sendpage_nokey(struct socket *sock, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 				       int offset, size_t size, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	err = skcipher_check_key(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 	return af_alg_sendpage(sock, page, offset, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int skcipher_recvmsg_nokey(struct socket *sock, struct msghdr *msg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				  size_t ignored, int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	err = skcipher_check_key(sock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	return skcipher_recvmsg(sock, msg, ignored, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static struct proto_ops algif_skcipher_ops_nokey = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	.family		=	PF_ALG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	.connect	=	sock_no_connect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	.socketpair	=	sock_no_socketpair,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	.getname	=	sock_no_getname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 	.ioctl		=	sock_no_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	.listen		=	sock_no_listen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 	.shutdown	=	sock_no_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	.mmap		=	sock_no_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	.bind		=	sock_no_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	.accept		=	sock_no_accept,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	.release	=	af_alg_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	.sendmsg	=	skcipher_sendmsg_nokey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	.sendpage	=	skcipher_sendpage_nokey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	.recvmsg	=	skcipher_recvmsg_nokey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 	.poll		=	af_alg_poll,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) static void *skcipher_bind(const char *name, u32 type, u32 mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	return crypto_alloc_skcipher(name, type, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) static void skcipher_release(void *private)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	crypto_free_skcipher(private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) static int skcipher_setkey(void *private, const u8 *key, unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return crypto_skcipher_setkey(private, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static void skcipher_sock_destruct(struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct af_alg_ctx *ctx = ask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct sock *psk = ask->parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct alg_sock *pask = alg_sk(psk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	struct crypto_skcipher *tfm = pask->private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	af_alg_pull_tsgl(sk, ctx->used, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	sock_kzfree_s(sk, ctx->iv, crypto_skcipher_ivsize(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	sock_kfree_s(sk, ctx, ctx->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	af_alg_release_parent(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static int skcipher_accept_parent_nokey(void *private, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	struct af_alg_ctx *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	struct alg_sock *ask = alg_sk(sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	struct crypto_skcipher *tfm = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	unsigned int len = sizeof(*ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	ctx = sock_kmalloc(sk, len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	if (!ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	memset(ctx, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	ctx->iv = sock_kmalloc(sk, crypto_skcipher_ivsize(tfm),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 			       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	if (!ctx->iv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 		sock_kfree_s(sk, ctx, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	memset(ctx->iv, 0, crypto_skcipher_ivsize(tfm));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	INIT_LIST_HEAD(&ctx->tsgl_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	ctx->len = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	crypto_init_wait(&ctx->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	ask->private = ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	sk->sk_destruct = skcipher_sock_destruct;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) static int skcipher_accept_parent(void *private, struct sock *sk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	struct crypto_skcipher *tfm = private;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	if (crypto_skcipher_get_flags(tfm) & CRYPTO_TFM_NEED_KEY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 		return -ENOKEY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	return skcipher_accept_parent_nokey(private, sk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static const struct af_alg_type algif_type_skcipher = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	.bind		=	skcipher_bind,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	.release	=	skcipher_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	.setkey		=	skcipher_setkey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	.accept		=	skcipher_accept_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	.accept_nokey	=	skcipher_accept_parent_nokey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	.ops		=	&algif_skcipher_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	.ops_nokey	=	&algif_skcipher_ops_nokey,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 	.name		=	"skcipher",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	.owner		=	THIS_MODULE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static int __init algif_skcipher_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	return af_alg_register_type(&algif_type_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) static void __exit algif_skcipher_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	int err = af_alg_unregister_type(&algif_type_skcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	BUG_ON(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) module_init(algif_skcipher_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) module_exit(algif_skcipher_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) MODULE_LICENSE("GPL");