xref: /linux/include/crypto/internal/acompress.h (revision 2c321f3f)
12874c5fdSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
22ebda74fSGiovanni Cabiddu /*
32ebda74fSGiovanni Cabiddu  * Asynchronous Compression operations
42ebda74fSGiovanni Cabiddu  *
52ebda74fSGiovanni Cabiddu  * Copyright (c) 2016, Intel Corporation
62ebda74fSGiovanni Cabiddu  * Authors: Weigang Li <weigang.li@intel.com>
72ebda74fSGiovanni Cabiddu  *          Giovanni Cabiddu <giovanni.cabiddu@intel.com>
82ebda74fSGiovanni Cabiddu  */
92ebda74fSGiovanni Cabiddu #ifndef _CRYPTO_ACOMP_INT_H
102ebda74fSGiovanni Cabiddu #define _CRYPTO_ACOMP_INT_H
1114386d47SHerbert Xu 
122ebda74fSGiovanni Cabiddu #include <crypto/acompress.h>
1314386d47SHerbert Xu #include <crypto/algapi.h>
142ebda74fSGiovanni Cabiddu 
150a742389SHerbert Xu /**
160a742389SHerbert Xu  * struct acomp_alg - asynchronous compression algorithm
170a742389SHerbert Xu  *
180a742389SHerbert Xu  * @compress:	Function performs a compress operation
190a742389SHerbert Xu  * @decompress:	Function performs a de-compress operation
200a742389SHerbert Xu  * @dst_free:	Frees destination buffer if allocated inside the algorithm
210a742389SHerbert Xu  * @init:	Initialize the cryptographic transformation object.
220a742389SHerbert Xu  *		This function is used to initialize the cryptographic
230a742389SHerbert Xu  *		transformation object. This function is called only once at
240a742389SHerbert Xu  *		the instantiation time, right after the transformation context
250a742389SHerbert Xu  *		was allocated. In case the cryptographic hardware has some
260a742389SHerbert Xu  *		special requirements which need to be handled by software, this
270a742389SHerbert Xu  *		function shall check for the precise requirement of the
280a742389SHerbert Xu  *		transformation and put any software fallbacks in place.
290a742389SHerbert Xu  * @exit:	Deinitialize the cryptographic transformation object. This is a
300a742389SHerbert Xu  *		counterpart to @init, used to remove various changes set in
310a742389SHerbert Xu  *		@init.
320a742389SHerbert Xu  *
330a742389SHerbert Xu  * @reqsize:	Context size for (de)compression requests
340a742389SHerbert Xu  * @base:	Common crypto API algorithm data structure
350a742389SHerbert Xu  * @calg:	Cmonn algorithm data structure shared with scomp
360a742389SHerbert Xu  */
370a742389SHerbert Xu struct acomp_alg {
380a742389SHerbert Xu 	int (*compress)(struct acomp_req *req);
390a742389SHerbert Xu 	int (*decompress)(struct acomp_req *req);
400a742389SHerbert Xu 	void (*dst_free)(struct scatterlist *dst);
410a742389SHerbert Xu 	int (*init)(struct crypto_acomp *tfm);
420a742389SHerbert Xu 	void (*exit)(struct crypto_acomp *tfm);
430a742389SHerbert Xu 
440a742389SHerbert Xu 	unsigned int reqsize;
450a742389SHerbert Xu 
460a742389SHerbert Xu 	union {
470a742389SHerbert Xu 		struct COMP_ALG_COMMON;
480a742389SHerbert Xu 		struct comp_alg_common calg;
490a742389SHerbert Xu 	};
500a742389SHerbert Xu };
510a742389SHerbert Xu 
520a742389SHerbert Xu /*
532ebda74fSGiovanni Cabiddu  * Transform internal helpers.
542ebda74fSGiovanni Cabiddu  */
acomp_request_ctx(struct acomp_req * req)552ebda74fSGiovanni Cabiddu static inline void *acomp_request_ctx(struct acomp_req *req)
562ebda74fSGiovanni Cabiddu {
572ebda74fSGiovanni Cabiddu 	return req->__ctx;
582ebda74fSGiovanni Cabiddu }
592ebda74fSGiovanni Cabiddu 
acomp_tfm_ctx(struct crypto_acomp * tfm)602ebda74fSGiovanni Cabiddu static inline void *acomp_tfm_ctx(struct crypto_acomp *tfm)
612ebda74fSGiovanni Cabiddu {
622ebda74fSGiovanni Cabiddu 	return tfm->base.__crt_ctx;
632ebda74fSGiovanni Cabiddu }
642ebda74fSGiovanni Cabiddu 
acomp_request_complete(struct acomp_req * req,int err)652ebda74fSGiovanni Cabiddu static inline void acomp_request_complete(struct acomp_req *req,
662ebda74fSGiovanni Cabiddu 					  int err)
672ebda74fSGiovanni Cabiddu {
682ebda74fSGiovanni Cabiddu 	crypto_request_complete(&req->base, err);
694cc01c7fSHerbert Xu }
702ebda74fSGiovanni Cabiddu 
__acomp_request_alloc_noprof(struct crypto_acomp * tfm)712ebda74fSGiovanni Cabiddu static inline struct acomp_req *__acomp_request_alloc_noprof(struct crypto_acomp *tfm)
72*2c321f3fSSuren Baghdasaryan {
731ab53a77SGiovanni Cabiddu 	struct acomp_req *req;
741ab53a77SGiovanni Cabiddu 
751ab53a77SGiovanni Cabiddu 	req = kzalloc_noprof(sizeof(*req) + crypto_acomp_reqsize(tfm), GFP_KERNEL);
76*2c321f3fSSuren Baghdasaryan 	if (likely(req))
771ab53a77SGiovanni Cabiddu 		acomp_request_set_tfm(req, tfm);
781ab53a77SGiovanni Cabiddu 	return req;
791ab53a77SGiovanni Cabiddu }
801ab53a77SGiovanni Cabiddu #define __acomp_request_alloc(...)	alloc_hooks(__acomp_request_alloc_noprof(__VA_ARGS__))
81*2c321f3fSSuren Baghdasaryan 
__acomp_request_free(struct acomp_req * req)821ab53a77SGiovanni Cabiddu static inline void __acomp_request_free(struct acomp_req *req)
831ab53a77SGiovanni Cabiddu {
841ab53a77SGiovanni Cabiddu 	kfree_sensitive(req);
85453431a5SWaiman Long }
861ab53a77SGiovanni Cabiddu 
871ab53a77SGiovanni Cabiddu /**
882ebda74fSGiovanni Cabiddu  * crypto_register_acomp() -- Register asynchronous compression algorithm
892ebda74fSGiovanni Cabiddu  *
902ebda74fSGiovanni Cabiddu  * Function registers an implementation of an asynchronous
912ebda74fSGiovanni Cabiddu  * compression algorithm
922ebda74fSGiovanni Cabiddu  *
932ebda74fSGiovanni Cabiddu  * @alg:	algorithm definition
942ebda74fSGiovanni Cabiddu  *
952ebda74fSGiovanni Cabiddu  * Return:	zero on success; error code in case of error
962ebda74fSGiovanni Cabiddu  */
972ebda74fSGiovanni Cabiddu int crypto_register_acomp(struct acomp_alg *alg);
982ebda74fSGiovanni Cabiddu 
992ebda74fSGiovanni Cabiddu /**
1002ebda74fSGiovanni Cabiddu  * crypto_unregister_acomp() -- Unregister asynchronous compression algorithm
1012ebda74fSGiovanni Cabiddu  *
1022ebda74fSGiovanni Cabiddu  * Function unregisters an implementation of an asynchronous
1032ebda74fSGiovanni Cabiddu  * compression algorithm
1042ebda74fSGiovanni Cabiddu  *
1052ebda74fSGiovanni Cabiddu  * @alg:	algorithm definition
1062ebda74fSGiovanni Cabiddu  */
1072ebda74fSGiovanni Cabiddu void crypto_unregister_acomp(struct acomp_alg *alg);
108c6d633a9SEric Biggers 
1092ebda74fSGiovanni Cabiddu int crypto_register_acomps(struct acomp_alg *algs, int count);
1103ce5bc72SGiovanni Cabiddu void crypto_unregister_acomps(struct acomp_alg *algs, int count);
1113ce5bc72SGiovanni Cabiddu 
1123ce5bc72SGiovanni Cabiddu #endif
1132ebda74fSGiovanni Cabiddu