1 /*
2  * Synchronous Compression operations
3  *
4  * Copyright 2015 LG Electronics Inc.
5  * Copyright (c) 2016, Intel Corporation
6  * Author: Giovanni Cabiddu <giovanni.cabiddu@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the Free
10  * Software Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  */
14 #ifndef _CRYPTO_SCOMP_INT_H
15 #define _CRYPTO_SCOMP_INT_H
16 #include <linux/crypto.h>
17 
18 #define SCOMP_SCRATCH_SIZE	131072
19 
20 struct crypto_scomp {
21 	struct crypto_tfm base;
22 };
23 
24 /**
25  * struct scomp_alg - synchronous compression algorithm
26  *
27  * @alloc_ctx:	Function allocates algorithm specific context
28  * @free_ctx:	Function frees context allocated with alloc_ctx
29  * @compress:	Function performs a compress operation
30  * @decompress:	Function performs a de-compress operation
31  * @init:	Initialize the cryptographic transformation object.
32  *		This function is used to initialize the cryptographic
33  *		transformation object. This function is called only once at
34  *		the instantiation time, right after the transformation context
35  *		was allocated. In case the cryptographic hardware has some
36  *		special requirements which need to be handled by software, this
37  *		function shall check for the precise requirement of the
38  *		transformation and put any software fallbacks in place.
39  * @exit:	Deinitialize the cryptographic transformation object. This is a
40  *		counterpart to @init, used to remove various changes set in
41  *		@init.
42  * @base:	Common crypto API algorithm data structure
43  */
44 struct scomp_alg {
45 	void *(*alloc_ctx)(struct crypto_scomp *tfm);
46 	void (*free_ctx)(struct crypto_scomp *tfm, void *ctx);
47 	int (*compress)(struct crypto_scomp *tfm, const u8 *src,
48 			unsigned int slen, u8 *dst, unsigned int *dlen,
49 			void *ctx);
50 	int (*decompress)(struct crypto_scomp *tfm, const u8 *src,
51 			  unsigned int slen, u8 *dst, unsigned int *dlen,
52 			  void *ctx);
53 	struct crypto_alg base;
54 };
55 
56 static inline struct scomp_alg *__crypto_scomp_alg(struct crypto_alg *alg)
57 {
58 	return container_of(alg, struct scomp_alg, base);
59 }
60 
61 static inline struct crypto_scomp *__crypto_scomp_tfm(struct crypto_tfm *tfm)
62 {
63 	return container_of(tfm, struct crypto_scomp, base);
64 }
65 
66 static inline struct crypto_tfm *crypto_scomp_tfm(struct crypto_scomp *tfm)
67 {
68 	return &tfm->base;
69 }
70 
71 static inline void crypto_free_scomp(struct crypto_scomp *tfm)
72 {
73 	crypto_destroy_tfm(tfm, crypto_scomp_tfm(tfm));
74 }
75 
76 static inline struct scomp_alg *crypto_scomp_alg(struct crypto_scomp *tfm)
77 {
78 	return __crypto_scomp_alg(crypto_scomp_tfm(tfm)->__crt_alg);
79 }
80 
81 static inline void *crypto_scomp_alloc_ctx(struct crypto_scomp *tfm)
82 {
83 	return crypto_scomp_alg(tfm)->alloc_ctx(tfm);
84 }
85 
86 static inline void crypto_scomp_free_ctx(struct crypto_scomp *tfm,
87 					 void *ctx)
88 {
89 	return crypto_scomp_alg(tfm)->free_ctx(tfm, ctx);
90 }
91 
92 static inline int crypto_scomp_compress(struct crypto_scomp *tfm,
93 					const u8 *src, unsigned int slen,
94 					u8 *dst, unsigned int *dlen, void *ctx)
95 {
96 	return crypto_scomp_alg(tfm)->compress(tfm, src, slen, dst, dlen, ctx);
97 }
98 
99 static inline int crypto_scomp_decompress(struct crypto_scomp *tfm,
100 					  const u8 *src, unsigned int slen,
101 					  u8 *dst, unsigned int *dlen,
102 					  void *ctx)
103 {
104 	return crypto_scomp_alg(tfm)->decompress(tfm, src, slen, dst, dlen,
105 						 ctx);
106 }
107 
108 int crypto_init_scomp_ops_async(struct crypto_tfm *tfm);
109 struct acomp_req *crypto_acomp_scomp_alloc_ctx(struct acomp_req *req);
110 void crypto_acomp_scomp_free_ctx(struct acomp_req *req);
111 
112 /**
113  * crypto_register_scomp() -- Register synchronous compression algorithm
114  *
115  * Function registers an implementation of a synchronous
116  * compression algorithm
117  *
118  * @alg:	algorithm definition
119  *
120  * Return: zero on success; error code in case of error
121  */
122 int crypto_register_scomp(struct scomp_alg *alg);
123 
124 /**
125  * crypto_unregister_scomp() -- Unregister synchronous compression algorithm
126  *
127  * Function unregisters an implementation of a synchronous
128  * compression algorithm
129  *
130  * @alg:	algorithm definition
131  *
132  * Return: zero on success; error code in case of error
133  */
134 int crypto_unregister_scomp(struct scomp_alg *alg);
135 
136 int crypto_register_scomps(struct scomp_alg *algs, int count);
137 void crypto_unregister_scomps(struct scomp_alg *algs, int count);
138 
139 #endif
140