xref: /OK3568_Linux_fs/kernel/drivers/crypto/cavium/zip/zip_crypto.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /***********************license start************************************
2*4882a593Smuzhiyun  * Copyright (c) 2003-2017 Cavium, Inc.
3*4882a593Smuzhiyun  * All rights reserved.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * License: one of 'Cavium License' or 'GNU General Public License Version 2'
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * This file is provided under the terms of the Cavium License (see below)
8*4882a593Smuzhiyun  * or under the terms of GNU General Public License, Version 2, as
9*4882a593Smuzhiyun  * published by the Free Software Foundation. When using or redistributing
10*4882a593Smuzhiyun  * this file, you may do so under either license.
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * Cavium License:  Redistribution and use in source and binary forms, with
13*4882a593Smuzhiyun  * or without modification, are permitted provided that the following
14*4882a593Smuzhiyun  * conditions are met:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *  * Redistributions of source code must retain the above copyright
17*4882a593Smuzhiyun  *    notice, this list of conditions and the following disclaimer.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *  * Redistributions in binary form must reproduce the above
20*4882a593Smuzhiyun  *    copyright notice, this list of conditions and the following
21*4882a593Smuzhiyun  *    disclaimer in the documentation and/or other materials provided
22*4882a593Smuzhiyun  *    with the distribution.
23*4882a593Smuzhiyun  *
24*4882a593Smuzhiyun  *  * Neither the name of Cavium Inc. nor the names of its contributors may be
25*4882a593Smuzhiyun  *    used to endorse or promote products derived from this software without
26*4882a593Smuzhiyun  *    specific prior written permission.
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * This Software, including technical data, may be subject to U.S. export
29*4882a593Smuzhiyun  * control laws, including the U.S. Export Administration Act and its
30*4882a593Smuzhiyun  * associated regulations, and may be subject to export or import
31*4882a593Smuzhiyun  * regulations in other countries.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34*4882a593Smuzhiyun  * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35*4882a593Smuzhiyun  * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36*4882a593Smuzhiyun  * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37*4882a593Smuzhiyun  * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38*4882a593Smuzhiyun  * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39*4882a593Smuzhiyun  * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40*4882a593Smuzhiyun  * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41*4882a593Smuzhiyun  * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42*4882a593Smuzhiyun  * ENTIRE  RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43*4882a593Smuzhiyun  * WITH YOU.
44*4882a593Smuzhiyun  ***********************license end**************************************/
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #include "zip_crypto.h"
47*4882a593Smuzhiyun 
zip_static_init_zip_ops(struct zip_operation * zip_ops,int lzs_flag)48*4882a593Smuzhiyun static void zip_static_init_zip_ops(struct zip_operation *zip_ops,
49*4882a593Smuzhiyun 				    int lzs_flag)
50*4882a593Smuzhiyun {
51*4882a593Smuzhiyun 	zip_ops->flush        = ZIP_FLUSH_FINISH;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	/* equivalent to level 6 of opensource zlib */
54*4882a593Smuzhiyun 	zip_ops->speed          = 1;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	if (!lzs_flag) {
57*4882a593Smuzhiyun 		zip_ops->ccode		= 0; /* Auto Huffman */
58*4882a593Smuzhiyun 		zip_ops->lzs_flag	= 0;
59*4882a593Smuzhiyun 		zip_ops->format		= ZLIB_FORMAT;
60*4882a593Smuzhiyun 	} else {
61*4882a593Smuzhiyun 		zip_ops->ccode		= 3; /* LZS Encoding */
62*4882a593Smuzhiyun 		zip_ops->lzs_flag	= 1;
63*4882a593Smuzhiyun 		zip_ops->format		= LZS_FORMAT;
64*4882a593Smuzhiyun 	}
65*4882a593Smuzhiyun 	zip_ops->begin_file   = 1;
66*4882a593Smuzhiyun 	zip_ops->history_len  = 0;
67*4882a593Smuzhiyun 	zip_ops->end_file     = 1;
68*4882a593Smuzhiyun 	zip_ops->compcode     = 0;
69*4882a593Smuzhiyun 	zip_ops->csum	      = 1; /* Adler checksum desired */
70*4882a593Smuzhiyun }
71*4882a593Smuzhiyun 
zip_ctx_init(struct zip_kernel_ctx * zip_ctx,int lzs_flag)72*4882a593Smuzhiyun static int zip_ctx_init(struct zip_kernel_ctx *zip_ctx, int lzs_flag)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	struct zip_operation  *comp_ctx   = &zip_ctx->zip_comp;
75*4882a593Smuzhiyun 	struct zip_operation  *decomp_ctx = &zip_ctx->zip_decomp;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	zip_static_init_zip_ops(comp_ctx, lzs_flag);
78*4882a593Smuzhiyun 	zip_static_init_zip_ops(decomp_ctx, lzs_flag);
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	comp_ctx->input  = zip_data_buf_alloc(MAX_INPUT_BUFFER_SIZE);
81*4882a593Smuzhiyun 	if (!comp_ctx->input)
82*4882a593Smuzhiyun 		return -ENOMEM;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	comp_ctx->output = zip_data_buf_alloc(MAX_OUTPUT_BUFFER_SIZE);
85*4882a593Smuzhiyun 	if (!comp_ctx->output)
86*4882a593Smuzhiyun 		goto err_comp_input;
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	decomp_ctx->input  = zip_data_buf_alloc(MAX_INPUT_BUFFER_SIZE);
89*4882a593Smuzhiyun 	if (!decomp_ctx->input)
90*4882a593Smuzhiyun 		goto err_comp_output;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	decomp_ctx->output = zip_data_buf_alloc(MAX_OUTPUT_BUFFER_SIZE);
93*4882a593Smuzhiyun 	if (!decomp_ctx->output)
94*4882a593Smuzhiyun 		goto err_decomp_input;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	return 0;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun err_decomp_input:
99*4882a593Smuzhiyun 	zip_data_buf_free(decomp_ctx->input, MAX_INPUT_BUFFER_SIZE);
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun err_comp_output:
102*4882a593Smuzhiyun 	zip_data_buf_free(comp_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun err_comp_input:
105*4882a593Smuzhiyun 	zip_data_buf_free(comp_ctx->input, MAX_INPUT_BUFFER_SIZE);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	return -ENOMEM;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
zip_ctx_exit(struct zip_kernel_ctx * zip_ctx)110*4882a593Smuzhiyun static void zip_ctx_exit(struct zip_kernel_ctx *zip_ctx)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun 	struct zip_operation  *comp_ctx   = &zip_ctx->zip_comp;
113*4882a593Smuzhiyun 	struct zip_operation  *dec_ctx = &zip_ctx->zip_decomp;
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun 	zip_data_buf_free(comp_ctx->input, MAX_INPUT_BUFFER_SIZE);
116*4882a593Smuzhiyun 	zip_data_buf_free(comp_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	zip_data_buf_free(dec_ctx->input, MAX_INPUT_BUFFER_SIZE);
119*4882a593Smuzhiyun 	zip_data_buf_free(dec_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun 
zip_compress(const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,struct zip_kernel_ctx * zip_ctx)122*4882a593Smuzhiyun static int zip_compress(const u8 *src, unsigned int slen,
123*4882a593Smuzhiyun 		 u8 *dst, unsigned int *dlen,
124*4882a593Smuzhiyun 		 struct zip_kernel_ctx *zip_ctx)
125*4882a593Smuzhiyun {
126*4882a593Smuzhiyun 	struct zip_operation  *zip_ops   = NULL;
127*4882a593Smuzhiyun 	struct zip_state      *zip_state;
128*4882a593Smuzhiyun 	struct zip_device     *zip = NULL;
129*4882a593Smuzhiyun 	int ret;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	if (!zip_ctx || !src || !dst || !dlen)
132*4882a593Smuzhiyun 		return -ENOMEM;
133*4882a593Smuzhiyun 
134*4882a593Smuzhiyun 	zip = zip_get_device(zip_get_node_id());
135*4882a593Smuzhiyun 	if (!zip)
136*4882a593Smuzhiyun 		return -ENODEV;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	zip_state = kzalloc(sizeof(*zip_state), GFP_ATOMIC);
139*4882a593Smuzhiyun 	if (!zip_state)
140*4882a593Smuzhiyun 		return -ENOMEM;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	zip_ops = &zip_ctx->zip_comp;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	zip_ops->input_len  = slen;
145*4882a593Smuzhiyun 	zip_ops->output_len = *dlen;
146*4882a593Smuzhiyun 	memcpy(zip_ops->input, src, slen);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun 	ret = zip_deflate(zip_ops, zip_state, zip);
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	if (!ret) {
151*4882a593Smuzhiyun 		*dlen = zip_ops->output_len;
152*4882a593Smuzhiyun 		memcpy(dst, zip_ops->output, *dlen);
153*4882a593Smuzhiyun 	}
154*4882a593Smuzhiyun 	kfree(zip_state);
155*4882a593Smuzhiyun 	return ret;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
zip_decompress(const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,struct zip_kernel_ctx * zip_ctx)158*4882a593Smuzhiyun static int zip_decompress(const u8 *src, unsigned int slen,
159*4882a593Smuzhiyun 		   u8 *dst, unsigned int *dlen,
160*4882a593Smuzhiyun 		   struct zip_kernel_ctx *zip_ctx)
161*4882a593Smuzhiyun {
162*4882a593Smuzhiyun 	struct zip_operation  *zip_ops   = NULL;
163*4882a593Smuzhiyun 	struct zip_state      *zip_state;
164*4882a593Smuzhiyun 	struct zip_device     *zip = NULL;
165*4882a593Smuzhiyun 	int ret;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	if (!zip_ctx || !src || !dst || !dlen)
168*4882a593Smuzhiyun 		return -ENOMEM;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	zip = zip_get_device(zip_get_node_id());
171*4882a593Smuzhiyun 	if (!zip)
172*4882a593Smuzhiyun 		return -ENODEV;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 	zip_state = kzalloc(sizeof(*zip_state), GFP_ATOMIC);
175*4882a593Smuzhiyun 	if (!zip_state)
176*4882a593Smuzhiyun 		return -ENOMEM;
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	zip_ops = &zip_ctx->zip_decomp;
179*4882a593Smuzhiyun 	memcpy(zip_ops->input, src, slen);
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	/* Work around for a bug in zlib which needs an extra bytes sometimes */
182*4882a593Smuzhiyun 	if (zip_ops->ccode != 3) /* Not LZS Encoding */
183*4882a593Smuzhiyun 		zip_ops->input[slen++] = 0;
184*4882a593Smuzhiyun 
185*4882a593Smuzhiyun 	zip_ops->input_len  = slen;
186*4882a593Smuzhiyun 	zip_ops->output_len = *dlen;
187*4882a593Smuzhiyun 
188*4882a593Smuzhiyun 	ret = zip_inflate(zip_ops, zip_state, zip);
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (!ret) {
191*4882a593Smuzhiyun 		*dlen = zip_ops->output_len;
192*4882a593Smuzhiyun 		memcpy(dst, zip_ops->output, *dlen);
193*4882a593Smuzhiyun 	}
194*4882a593Smuzhiyun 	kfree(zip_state);
195*4882a593Smuzhiyun 	return ret;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun /* Legacy Compress framework start */
zip_alloc_comp_ctx_deflate(struct crypto_tfm * tfm)199*4882a593Smuzhiyun int zip_alloc_comp_ctx_deflate(struct crypto_tfm *tfm)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun 	int ret;
202*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 	ret = zip_ctx_init(zip_ctx, 0);
205*4882a593Smuzhiyun 
206*4882a593Smuzhiyun 	return ret;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
zip_alloc_comp_ctx_lzs(struct crypto_tfm * tfm)209*4882a593Smuzhiyun int zip_alloc_comp_ctx_lzs(struct crypto_tfm *tfm)
210*4882a593Smuzhiyun {
211*4882a593Smuzhiyun 	int ret;
212*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	ret = zip_ctx_init(zip_ctx, 1);
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	return ret;
217*4882a593Smuzhiyun }
218*4882a593Smuzhiyun 
zip_free_comp_ctx(struct crypto_tfm * tfm)219*4882a593Smuzhiyun void zip_free_comp_ctx(struct crypto_tfm *tfm)
220*4882a593Smuzhiyun {
221*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	zip_ctx_exit(zip_ctx);
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun 
zip_comp_compress(struct crypto_tfm * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)226*4882a593Smuzhiyun int  zip_comp_compress(struct crypto_tfm *tfm,
227*4882a593Smuzhiyun 		       const u8 *src, unsigned int slen,
228*4882a593Smuzhiyun 		       u8 *dst, unsigned int *dlen)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun 	int ret;
231*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 	ret = zip_compress(src, slen, dst, dlen, zip_ctx);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	return ret;
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun 
zip_comp_decompress(struct crypto_tfm * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen)238*4882a593Smuzhiyun int  zip_comp_decompress(struct crypto_tfm *tfm,
239*4882a593Smuzhiyun 			 const u8 *src, unsigned int slen,
240*4882a593Smuzhiyun 			 u8 *dst, unsigned int *dlen)
241*4882a593Smuzhiyun {
242*4882a593Smuzhiyun 	int ret;
243*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
244*4882a593Smuzhiyun 
245*4882a593Smuzhiyun 	ret = zip_decompress(src, slen, dst, dlen, zip_ctx);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	return ret;
248*4882a593Smuzhiyun } /* Legacy compress framework end */
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun /* SCOMP framework start */
zip_alloc_scomp_ctx_deflate(struct crypto_scomp * tfm)251*4882a593Smuzhiyun void *zip_alloc_scomp_ctx_deflate(struct crypto_scomp *tfm)
252*4882a593Smuzhiyun {
253*4882a593Smuzhiyun 	int ret;
254*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	zip_ctx = kzalloc(sizeof(*zip_ctx), GFP_KERNEL);
257*4882a593Smuzhiyun 	if (!zip_ctx)
258*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	ret = zip_ctx_init(zip_ctx, 0);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	if (ret) {
263*4882a593Smuzhiyun 		kfree_sensitive(zip_ctx);
264*4882a593Smuzhiyun 		return ERR_PTR(ret);
265*4882a593Smuzhiyun 	}
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	return zip_ctx;
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun 
zip_alloc_scomp_ctx_lzs(struct crypto_scomp * tfm)270*4882a593Smuzhiyun void *zip_alloc_scomp_ctx_lzs(struct crypto_scomp *tfm)
271*4882a593Smuzhiyun {
272*4882a593Smuzhiyun 	int ret;
273*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	zip_ctx = kzalloc(sizeof(*zip_ctx), GFP_KERNEL);
276*4882a593Smuzhiyun 	if (!zip_ctx)
277*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	ret = zip_ctx_init(zip_ctx, 1);
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	if (ret) {
282*4882a593Smuzhiyun 		kfree_sensitive(zip_ctx);
283*4882a593Smuzhiyun 		return ERR_PTR(ret);
284*4882a593Smuzhiyun 	}
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	return zip_ctx;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun 
zip_free_scomp_ctx(struct crypto_scomp * tfm,void * ctx)289*4882a593Smuzhiyun void zip_free_scomp_ctx(struct crypto_scomp *tfm, void *ctx)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx = ctx;
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	zip_ctx_exit(zip_ctx);
294*4882a593Smuzhiyun 	kfree_sensitive(zip_ctx);
295*4882a593Smuzhiyun }
296*4882a593Smuzhiyun 
zip_scomp_compress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)297*4882a593Smuzhiyun int zip_scomp_compress(struct crypto_scomp *tfm,
298*4882a593Smuzhiyun 		       const u8 *src, unsigned int slen,
299*4882a593Smuzhiyun 		       u8 *dst, unsigned int *dlen, void *ctx)
300*4882a593Smuzhiyun {
301*4882a593Smuzhiyun 	int ret;
302*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx  = ctx;
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	ret = zip_compress(src, slen, dst, dlen, zip_ctx);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	return ret;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun 
zip_scomp_decompress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)309*4882a593Smuzhiyun int zip_scomp_decompress(struct crypto_scomp *tfm,
310*4882a593Smuzhiyun 			 const u8 *src, unsigned int slen,
311*4882a593Smuzhiyun 			 u8 *dst, unsigned int *dlen, void *ctx)
312*4882a593Smuzhiyun {
313*4882a593Smuzhiyun 	int ret;
314*4882a593Smuzhiyun 	struct zip_kernel_ctx *zip_ctx = ctx;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	ret = zip_decompress(src, slen, dst, dlen, zip_ctx);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	return ret;
319*4882a593Smuzhiyun } /* SCOMP framework end */
320