xref: /linux/include/crypto/internal/scompress.h (revision da6f9bf40ac267b5c720694a817beea84fa40f77)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Synchronous Compression operations
4  *
5  * Copyright 2015 LG Electronics Inc.
6  * Copyright (c) 2016, Intel Corporation
7  * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
8  */
9 #ifndef _CRYPTO_SCOMP_INT_H
10 #define _CRYPTO_SCOMP_INT_H
11 
12 #include <crypto/acompress.h>
13 #include <crypto/algapi.h>
14 
15 #define SCOMP_SCRATCH_SIZE	131072
16 
17 struct acomp_req;
18 
19 struct crypto_scomp {
20 	struct crypto_tfm base;
21 };
22 
23 /**
24  * struct scomp_alg - synchronous compression algorithm
25  *
26  * @alloc_ctx:	Function allocates algorithm specific context
27  * @free_ctx:	Function frees context allocated with alloc_ctx
28  * @compress:	Function performs a compress operation
29  * @decompress:	Function performs a de-compress operation
30  * @base:	Common crypto API algorithm data structure
31  * @stream:	Per-cpu memory for algorithm
32  * @calg:	Cmonn algorithm data structure shared with acomp
33  */
34 struct scomp_alg {
35 	void *(*alloc_ctx)(void);
36 	void (*free_ctx)(void *ctx);
37 	int (*compress)(struct crypto_scomp *tfm, const u8 *src,
38 			unsigned int slen, u8 *dst, unsigned int *dlen,
39 			void *ctx);
40 	int (*decompress)(struct crypto_scomp *tfm, const u8 *src,
41 			  unsigned int slen, u8 *dst, unsigned int *dlen,
42 			  void *ctx);
43 
44 	union {
45 		struct COMP_ALG_COMMON;
46 		struct comp_alg_common calg;
47 	};
48 };
49 
50 static inline struct scomp_alg *__crypto_scomp_alg(struct crypto_alg *alg)
51 {
52 	return container_of(alg, struct scomp_alg, base);
53 }
54 
55 static inline struct crypto_scomp *__crypto_scomp_tfm(struct crypto_tfm *tfm)
56 {
57 	return container_of(tfm, struct crypto_scomp, base);
58 }
59 
60 static inline struct crypto_tfm *crypto_scomp_tfm(struct crypto_scomp *tfm)
61 {
62 	return &tfm->base;
63 }
64 
65 static inline void crypto_free_scomp(struct crypto_scomp *tfm)
66 {
67 	crypto_destroy_tfm(tfm, crypto_scomp_tfm(tfm));
68 }
69 
70 static inline struct scomp_alg *crypto_scomp_alg(struct crypto_scomp *tfm)
71 {
72 	return __crypto_scomp_alg(crypto_scomp_tfm(tfm)->__crt_alg);
73 }
74 
75 static inline int crypto_scomp_compress(struct crypto_scomp *tfm,
76 					const u8 *src, unsigned int slen,
77 					u8 *dst, unsigned int *dlen, void *ctx)
78 {
79 	return crypto_scomp_alg(tfm)->compress(tfm, src, slen, dst, dlen, ctx);
80 }
81 
82 static inline int crypto_scomp_decompress(struct crypto_scomp *tfm,
83 					  const u8 *src, unsigned int slen,
84 					  u8 *dst, unsigned int *dlen,
85 					  void *ctx)
86 {
87 	return crypto_scomp_alg(tfm)->decompress(tfm, src, slen, dst, dlen,
88 						 ctx);
89 }
90 
91 /**
92  * crypto_register_scomp() -- Register synchronous compression algorithm
93  *
94  * Function registers an implementation of a synchronous
95  * compression algorithm
96  *
97  * @alg:	algorithm definition
98  *
99  * Return: zero on success; error code in case of error
100  */
101 int crypto_register_scomp(struct scomp_alg *alg);
102 
103 /**
104  * crypto_unregister_scomp() -- Unregister synchronous compression algorithm
105  *
106  * Function unregisters an implementation of a synchronous
107  * compression algorithm
108  *
109  * @alg:	algorithm definition
110  */
111 void crypto_unregister_scomp(struct scomp_alg *alg);
112 
113 int crypto_register_scomps(struct scomp_alg *algs, int count);
114 void crypto_unregister_scomps(struct scomp_alg *algs, int count);
115 
116 #endif
117