18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * if_alg: User-space algorithm interface
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2010 Herbert Xu <herbert@gondor.apana.org.au>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#ifndef _CRYPTO_IF_ALG_H
98c2ecf20Sopenharmony_ci#define _CRYPTO_IF_ALG_H
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/compiler.h>
128c2ecf20Sopenharmony_ci#include <linux/completion.h>
138c2ecf20Sopenharmony_ci#include <linux/if_alg.h>
148c2ecf20Sopenharmony_ci#include <linux/scatterlist.h>
158c2ecf20Sopenharmony_ci#include <linux/types.h>
168c2ecf20Sopenharmony_ci#include <linux/atomic.h>
178c2ecf20Sopenharmony_ci#include <net/sock.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <crypto/aead.h>
208c2ecf20Sopenharmony_ci#include <crypto/skcipher.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define ALG_MAX_PAGES			16
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistruct crypto_async_request;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct alg_sock {
278c2ecf20Sopenharmony_ci	/* struct sock must be the first member of struct alg_sock */
288c2ecf20Sopenharmony_ci	struct sock sk;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci	struct sock *parent;
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	atomic_t refcnt;
338c2ecf20Sopenharmony_ci	atomic_t nokey_refcnt;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	const struct af_alg_type *type;
368c2ecf20Sopenharmony_ci	void *private;
378c2ecf20Sopenharmony_ci};
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct af_alg_control {
408c2ecf20Sopenharmony_ci	struct af_alg_iv *iv;
418c2ecf20Sopenharmony_ci	int op;
428c2ecf20Sopenharmony_ci	unsigned int aead_assoclen;
438c2ecf20Sopenharmony_ci};
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_cistruct af_alg_type {
468c2ecf20Sopenharmony_ci	void *(*bind)(const char *name, u32 type, u32 mask);
478c2ecf20Sopenharmony_ci	void (*release)(void *private);
488c2ecf20Sopenharmony_ci	int (*setkey)(void *private, const u8 *key, unsigned int keylen);
498c2ecf20Sopenharmony_ci	int (*setentropy)(void *private, sockptr_t entropy, unsigned int len);
508c2ecf20Sopenharmony_ci	int (*accept)(void *private, struct sock *sk);
518c2ecf20Sopenharmony_ci	int (*accept_nokey)(void *private, struct sock *sk);
528c2ecf20Sopenharmony_ci	int (*setauthsize)(void *private, unsigned int authsize);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci	struct proto_ops *ops;
558c2ecf20Sopenharmony_ci	struct proto_ops *ops_nokey;
568c2ecf20Sopenharmony_ci	struct module *owner;
578c2ecf20Sopenharmony_ci	char name[14];
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistruct af_alg_sgl {
618c2ecf20Sopenharmony_ci	struct scatterlist sg[ALG_MAX_PAGES + 1];
628c2ecf20Sopenharmony_ci	struct page *pages[ALG_MAX_PAGES];
638c2ecf20Sopenharmony_ci	unsigned int npages;
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci/* TX SGL entry */
678c2ecf20Sopenharmony_cistruct af_alg_tsgl {
688c2ecf20Sopenharmony_ci	struct list_head list;
698c2ecf20Sopenharmony_ci	unsigned int cur;		/* Last processed SG entry */
708c2ecf20Sopenharmony_ci	struct scatterlist sg[];	/* Array of SGs forming the SGL */
718c2ecf20Sopenharmony_ci};
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci#define MAX_SGL_ENTS ((4096 - sizeof(struct af_alg_tsgl)) / \
748c2ecf20Sopenharmony_ci		      sizeof(struct scatterlist) - 1)
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci/* RX SGL entry */
778c2ecf20Sopenharmony_cistruct af_alg_rsgl {
788c2ecf20Sopenharmony_ci	struct af_alg_sgl sgl;
798c2ecf20Sopenharmony_ci	struct list_head list;
808c2ecf20Sopenharmony_ci	size_t sg_num_bytes;		/* Bytes of data in that SGL */
818c2ecf20Sopenharmony_ci};
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/**
848c2ecf20Sopenharmony_ci * struct af_alg_async_req - definition of crypto request
858c2ecf20Sopenharmony_ci * @iocb:		IOCB for AIO operations
868c2ecf20Sopenharmony_ci * @sk:			Socket the request is associated with
878c2ecf20Sopenharmony_ci * @first_rsgl:		First RX SG
888c2ecf20Sopenharmony_ci * @last_rsgl:		Pointer to last RX SG
898c2ecf20Sopenharmony_ci * @rsgl_list:		Track RX SGs
908c2ecf20Sopenharmony_ci * @tsgl:		Private, per request TX SGL of buffers to process
918c2ecf20Sopenharmony_ci * @tsgl_entries:	Number of entries in priv. TX SGL
928c2ecf20Sopenharmony_ci * @outlen:		Number of output bytes generated by crypto op
938c2ecf20Sopenharmony_ci * @areqlen:		Length of this data structure
948c2ecf20Sopenharmony_ci * @cra_u:		Cipher request
958c2ecf20Sopenharmony_ci */
968c2ecf20Sopenharmony_cistruct af_alg_async_req {
978c2ecf20Sopenharmony_ci	struct kiocb *iocb;
988c2ecf20Sopenharmony_ci	struct sock *sk;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	struct af_alg_rsgl first_rsgl;
1018c2ecf20Sopenharmony_ci	struct af_alg_rsgl *last_rsgl;
1028c2ecf20Sopenharmony_ci	struct list_head rsgl_list;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	struct scatterlist *tsgl;
1058c2ecf20Sopenharmony_ci	unsigned int tsgl_entries;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	unsigned int outlen;
1088c2ecf20Sopenharmony_ci	unsigned int areqlen;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	union {
1118c2ecf20Sopenharmony_ci		struct aead_request aead_req;
1128c2ecf20Sopenharmony_ci		struct skcipher_request skcipher_req;
1138c2ecf20Sopenharmony_ci	} cra_u;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* req ctx trails this struct */
1168c2ecf20Sopenharmony_ci};
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/**
1198c2ecf20Sopenharmony_ci * struct af_alg_ctx - definition of the crypto context
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci * The crypto context tracks the input data during the lifetime of an AF_ALG
1228c2ecf20Sopenharmony_ci * socket.
1238c2ecf20Sopenharmony_ci *
1248c2ecf20Sopenharmony_ci * @tsgl_list:		Link to TX SGL
1258c2ecf20Sopenharmony_ci * @iv:			IV for cipher operation
1268c2ecf20Sopenharmony_ci * @aead_assoclen:	Length of AAD for AEAD cipher operations
1278c2ecf20Sopenharmony_ci * @completion:		Work queue for synchronous operation
1288c2ecf20Sopenharmony_ci * @used:		TX bytes sent to kernel. This variable is used to
1298c2ecf20Sopenharmony_ci *			ensure that user space cannot cause the kernel
1308c2ecf20Sopenharmony_ci *			to allocate too much memory in sendmsg operation.
1318c2ecf20Sopenharmony_ci * @rcvused:		Total RX bytes to be filled by kernel. This variable
1328c2ecf20Sopenharmony_ci *			is used to ensure user space cannot cause the kernel
1338c2ecf20Sopenharmony_ci *			to allocate too much memory in a recvmsg operation.
1348c2ecf20Sopenharmony_ci * @more:		More data to be expected from user space?
1358c2ecf20Sopenharmony_ci * @merge:		Shall new data from user space be merged into existing
1368c2ecf20Sopenharmony_ci *			SG?
1378c2ecf20Sopenharmony_ci * @enc:		Cryptographic operation to be performed when
1388c2ecf20Sopenharmony_ci *			recvmsg is invoked.
1398c2ecf20Sopenharmony_ci * @init:		True if metadata has been sent.
1408c2ecf20Sopenharmony_ci * @len:		Length of memory allocated for this data structure.
1418c2ecf20Sopenharmony_ci * @inflight:		Non-zero when AIO requests are in flight.
1428c2ecf20Sopenharmony_ci */
1438c2ecf20Sopenharmony_cistruct af_alg_ctx {
1448c2ecf20Sopenharmony_ci	struct list_head tsgl_list;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci	void *iv;
1478c2ecf20Sopenharmony_ci	size_t aead_assoclen;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	struct crypto_wait wait;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	size_t used;
1528c2ecf20Sopenharmony_ci	atomic_t rcvused;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	bool more;
1558c2ecf20Sopenharmony_ci	bool merge;
1568c2ecf20Sopenharmony_ci	bool enc;
1578c2ecf20Sopenharmony_ci	bool init;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	unsigned int len;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	unsigned int inflight;
1628c2ecf20Sopenharmony_ci};
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ciint af_alg_register_type(const struct af_alg_type *type);
1658c2ecf20Sopenharmony_ciint af_alg_unregister_type(const struct af_alg_type *type);
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ciint af_alg_release(struct socket *sock);
1688c2ecf20Sopenharmony_civoid af_alg_release_parent(struct sock *sk);
1698c2ecf20Sopenharmony_ciint af_alg_accept(struct sock *sk, struct socket *newsock, bool kern);
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ciint af_alg_make_sg(struct af_alg_sgl *sgl, struct iov_iter *iter, int len);
1728c2ecf20Sopenharmony_civoid af_alg_free_sg(struct af_alg_sgl *sgl);
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_cistatic inline struct alg_sock *alg_sk(struct sock *sk)
1758c2ecf20Sopenharmony_ci{
1768c2ecf20Sopenharmony_ci	return (struct alg_sock *)sk;
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/**
1808c2ecf20Sopenharmony_ci * Size of available buffer for sending data from user space to kernel.
1818c2ecf20Sopenharmony_ci *
1828c2ecf20Sopenharmony_ci * @sk socket of connection to user space
1838c2ecf20Sopenharmony_ci * @return number of bytes still available
1848c2ecf20Sopenharmony_ci */
1858c2ecf20Sopenharmony_cistatic inline int af_alg_sndbuf(struct sock *sk)
1868c2ecf20Sopenharmony_ci{
1878c2ecf20Sopenharmony_ci	struct alg_sock *ask = alg_sk(sk);
1888c2ecf20Sopenharmony_ci	struct af_alg_ctx *ctx = ask->private;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return max_t(int, max_t(int, sk->sk_sndbuf & PAGE_MASK, PAGE_SIZE) -
1918c2ecf20Sopenharmony_ci			  ctx->used, 0);
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci/**
1958c2ecf20Sopenharmony_ci * Can the send buffer still be written to?
1968c2ecf20Sopenharmony_ci *
1978c2ecf20Sopenharmony_ci * @sk socket of connection to user space
1988c2ecf20Sopenharmony_ci * @return true => writable, false => not writable
1998c2ecf20Sopenharmony_ci */
2008c2ecf20Sopenharmony_cistatic inline bool af_alg_writable(struct sock *sk)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	return PAGE_SIZE <= af_alg_sndbuf(sk);
2038c2ecf20Sopenharmony_ci}
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci/**
2068c2ecf20Sopenharmony_ci * Size of available buffer used by kernel for the RX user space operation.
2078c2ecf20Sopenharmony_ci *
2088c2ecf20Sopenharmony_ci * @sk socket of connection to user space
2098c2ecf20Sopenharmony_ci * @return number of bytes still available
2108c2ecf20Sopenharmony_ci */
2118c2ecf20Sopenharmony_cistatic inline int af_alg_rcvbuf(struct sock *sk)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	struct alg_sock *ask = alg_sk(sk);
2148c2ecf20Sopenharmony_ci	struct af_alg_ctx *ctx = ask->private;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	return max_t(int, max_t(int, sk->sk_rcvbuf & PAGE_MASK, PAGE_SIZE) -
2178c2ecf20Sopenharmony_ci		     atomic_read(&ctx->rcvused), 0);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci/**
2218c2ecf20Sopenharmony_ci * Can the RX buffer still be written to?
2228c2ecf20Sopenharmony_ci *
2238c2ecf20Sopenharmony_ci * @sk socket of connection to user space
2248c2ecf20Sopenharmony_ci * @return true => writable, false => not writable
2258c2ecf20Sopenharmony_ci */
2268c2ecf20Sopenharmony_cistatic inline bool af_alg_readable(struct sock *sk)
2278c2ecf20Sopenharmony_ci{
2288c2ecf20Sopenharmony_ci	return PAGE_SIZE <= af_alg_rcvbuf(sk);
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ciunsigned int af_alg_count_tsgl(struct sock *sk, size_t bytes, size_t offset);
2328c2ecf20Sopenharmony_civoid af_alg_pull_tsgl(struct sock *sk, size_t used, struct scatterlist *dst,
2338c2ecf20Sopenharmony_ci		      size_t dst_offset);
2348c2ecf20Sopenharmony_civoid af_alg_wmem_wakeup(struct sock *sk);
2358c2ecf20Sopenharmony_ciint af_alg_wait_for_data(struct sock *sk, unsigned flags, unsigned min);
2368c2ecf20Sopenharmony_ciint af_alg_sendmsg(struct socket *sock, struct msghdr *msg, size_t size,
2378c2ecf20Sopenharmony_ci		   unsigned int ivsize);
2388c2ecf20Sopenharmony_cissize_t af_alg_sendpage(struct socket *sock, struct page *page,
2398c2ecf20Sopenharmony_ci			int offset, size_t size, int flags);
2408c2ecf20Sopenharmony_civoid af_alg_free_resources(struct af_alg_async_req *areq);
2418c2ecf20Sopenharmony_civoid af_alg_async_cb(struct crypto_async_request *_req, int err);
2428c2ecf20Sopenharmony_ci__poll_t af_alg_poll(struct file *file, struct socket *sock,
2438c2ecf20Sopenharmony_ci			 poll_table *wait);
2448c2ecf20Sopenharmony_cistruct af_alg_async_req *af_alg_alloc_areq(struct sock *sk,
2458c2ecf20Sopenharmony_ci					   unsigned int areqlen);
2468c2ecf20Sopenharmony_ciint af_alg_get_rsgl(struct sock *sk, struct msghdr *msg, int flags,
2478c2ecf20Sopenharmony_ci		    struct af_alg_async_req *areq, size_t maxsize,
2488c2ecf20Sopenharmony_ci		    size_t *outlen);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci#endif	/* _CRYPTO_IF_ALG_H */
251