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/algapi.h>
13 
14 #define SCOMP_SCRATCH_SIZE	131072
15 
16 struct crypto_scomp {
17 	struct crypto_tfm base;
18 };
19 
20 /**
21  * struct scomp_alg - synchronous compression algorithm
22  *
23  * @alloc_ctx:	Function allocates algorithm specific context
24  * @free_ctx:	Function frees context allocated with alloc_ctx
25  * @compress:	Function performs a compress operation
26  * @decompress:	Function performs a de-compress operation
27  * @base:	Common crypto API algorithm data structure
28  */
29 struct scomp_alg {
30 	void *(*alloc_ctx)(struct crypto_scomp *tfm);
31 	void (*free_ctx)(struct crypto_scomp *tfm, void *ctx);
32 	int (*compress)(struct crypto_scomp *tfm, const u8 *src,
33 			unsigned int slen, u8 *dst, unsigned int *dlen,
34 			void *ctx);
35 	int (*decompress)(struct crypto_scomp *tfm, const u8 *src,
36 			  unsigned int slen, u8 *dst, unsigned int *dlen,
37 			  void *ctx);
38 	struct crypto_alg base;
39 };
40 
__crypto_scomp_alg(struct crypto_alg * alg)41 static inline struct scomp_alg *__crypto_scomp_alg(struct crypto_alg *alg)
42 {
43 	return container_of(alg, struct scomp_alg, base);
44 }
45 
__crypto_scomp_tfm(struct crypto_tfm * tfm)46 static inline struct crypto_scomp *__crypto_scomp_tfm(struct crypto_tfm *tfm)
47 {
48 	return container_of(tfm, struct crypto_scomp, base);
49 }
50 
crypto_scomp_tfm(struct crypto_scomp * tfm)51 static inline struct crypto_tfm *crypto_scomp_tfm(struct crypto_scomp *tfm)
52 {
53 	return &tfm->base;
54 }
55 
crypto_free_scomp(struct crypto_scomp * tfm)56 static inline void crypto_free_scomp(struct crypto_scomp *tfm)
57 {
58 	crypto_destroy_tfm(tfm, crypto_scomp_tfm(tfm));
59 }
60 
crypto_scomp_alg(struct crypto_scomp * tfm)61 static inline struct scomp_alg *crypto_scomp_alg(struct crypto_scomp *tfm)
62 {
63 	return __crypto_scomp_alg(crypto_scomp_tfm(tfm)->__crt_alg);
64 }
65 
crypto_scomp_alloc_ctx(struct crypto_scomp * tfm)66 static inline void *crypto_scomp_alloc_ctx(struct crypto_scomp *tfm)
67 {
68 	return crypto_scomp_alg(tfm)->alloc_ctx(tfm);
69 }
70 
crypto_scomp_free_ctx(struct crypto_scomp * tfm,void * ctx)71 static inline void crypto_scomp_free_ctx(struct crypto_scomp *tfm,
72 					 void *ctx)
73 {
74 	return crypto_scomp_alg(tfm)->free_ctx(tfm, ctx);
75 }
76 
crypto_scomp_compress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)77 static inline int crypto_scomp_compress(struct crypto_scomp *tfm,
78 					const u8 *src, unsigned int slen,
79 					u8 *dst, unsigned int *dlen, void *ctx)
80 {
81 	return crypto_scomp_alg(tfm)->compress(tfm, src, slen, dst, dlen, ctx);
82 }
83 
crypto_scomp_decompress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)84 static inline int crypto_scomp_decompress(struct crypto_scomp *tfm,
85 					  const u8 *src, unsigned int slen,
86 					  u8 *dst, unsigned int *dlen,
87 					  void *ctx)
88 {
89 	return crypto_scomp_alg(tfm)->decompress(tfm, src, slen, dst, dlen,
90 						 ctx);
91 }
92 
93 int crypto_init_scomp_ops_async(struct crypto_tfm *tfm);
94 struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req);
95 void crypto_acomp_scomp_free_ctx(struct acomp_req *req);
96 
97 /**
98  * crypto_register_scomp() -- Register synchronous compression algorithm
99  *
100  * Function registers an implementation of a synchronous
101  * compression algorithm
102  *
103  * @alg:	algorithm definition
104  *
105  * Return: zero on success; error code in case of error
106  */
107 int crypto_register_scomp(struct scomp_alg *alg);
108 
109 /**
110  * crypto_unregister_scomp() -- Unregister synchronous compression algorithm
111  *
112  * Function unregisters an implementation of a synchronous
113  * compression algorithm
114  *
115  * @alg:	algorithm definition
116  */
117 void crypto_unregister_scomp(struct scomp_alg *alg);
118 
119 int crypto_register_scomps(struct scomp_alg *algs, int count);
120 void crypto_unregister_scomps(struct scomp_alg *algs, int count);
121 
122 #endif
123