Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / crypto / cavium / zip / zip_crypto.c
blob8df4d26cf9d46894e68c86f2032fb3f9b4ca899d
1 /***********************license start************************************
2 * Copyright (c) 2003-2017 Cavium, Inc.
3 * All rights reserved.
5 * License: one of 'Cavium License' or 'GNU General Public License Version 2'
7 * This file is provided under the terms of the Cavium License (see below)
8 * or under the terms of GNU General Public License, Version 2, as
9 * published by the Free Software Foundation. When using or redistributing
10 * this file, you may do so under either license.
12 * Cavium License: Redistribution and use in source and binary forms, with
13 * or without modification, are permitted provided that the following
14 * conditions are met:
16 * * Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
19 * * Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
24 * * Neither the name of Cavium Inc. nor the names of its contributors may be
25 * used to endorse or promote products derived from this software without
26 * specific prior written permission.
28 * This Software, including technical data, may be subject to U.S. export
29 * control laws, including the U.S. Export Administration Act and its
30 * associated regulations, and may be subject to export or import
31 * regulations in other countries.
33 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, THE SOFTWARE IS PROVIDED "AS IS"
34 * AND WITH ALL FAULTS AND CAVIUM INC. MAKES NO PROMISES, REPRESENTATIONS
35 * OR WARRANTIES, EITHER EXPRESS, IMPLIED, STATUTORY, OR OTHERWISE, WITH
36 * RESPECT TO THE SOFTWARE, INCLUDING ITS CONDITION, ITS CONFORMITY TO ANY
37 * REPRESENTATION OR DESCRIPTION, OR THE EXISTENCE OF ANY LATENT OR PATENT
38 * DEFECTS, AND CAVIUM SPECIFICALLY DISCLAIMS ALL IMPLIED (IF ANY)
39 * WARRANTIES OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, FITNESS FOR A
40 * PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR COMPLETENESS, QUIET
41 * ENJOYMENT, QUIET POSSESSION OR CORRESPONDENCE TO DESCRIPTION. THE
42 * ENTIRE RISK ARISING OUT OF USE OR PERFORMANCE OF THE SOFTWARE LIES
43 * WITH YOU.
44 ***********************license end**************************************/
46 #include "zip_crypto.h"
48 static void zip_static_init_zip_ops(struct zip_operation *zip_ops,
49 int lzs_flag)
51 zip_ops->flush = ZIP_FLUSH_FINISH;
53 /* equivalent to level 6 of opensource zlib */
54 zip_ops->speed = 1;
56 if (!lzs_flag) {
57 zip_ops->ccode = 0; /* Auto Huffman */
58 zip_ops->lzs_flag = 0;
59 zip_ops->format = ZLIB_FORMAT;
60 } else {
61 zip_ops->ccode = 3; /* LZS Encoding */
62 zip_ops->lzs_flag = 1;
63 zip_ops->format = LZS_FORMAT;
65 zip_ops->begin_file = 1;
66 zip_ops->history_len = 0;
67 zip_ops->end_file = 1;
68 zip_ops->compcode = 0;
69 zip_ops->csum = 1; /* Adler checksum desired */
72 int zip_ctx_init(struct zip_kernel_ctx *zip_ctx, int lzs_flag)
74 struct zip_operation *comp_ctx = &zip_ctx->zip_comp;
75 struct zip_operation *decomp_ctx = &zip_ctx->zip_decomp;
77 zip_static_init_zip_ops(comp_ctx, lzs_flag);
78 zip_static_init_zip_ops(decomp_ctx, lzs_flag);
80 comp_ctx->input = zip_data_buf_alloc(MAX_INPUT_BUFFER_SIZE);
81 if (!comp_ctx->input)
82 return -ENOMEM;
84 comp_ctx->output = zip_data_buf_alloc(MAX_OUTPUT_BUFFER_SIZE);
85 if (!comp_ctx->output)
86 goto err_comp_input;
88 decomp_ctx->input = zip_data_buf_alloc(MAX_INPUT_BUFFER_SIZE);
89 if (!decomp_ctx->input)
90 goto err_comp_output;
92 decomp_ctx->output = zip_data_buf_alloc(MAX_OUTPUT_BUFFER_SIZE);
93 if (!decomp_ctx->output)
94 goto err_decomp_input;
96 return 0;
98 err_decomp_input:
99 zip_data_buf_free(decomp_ctx->input, MAX_INPUT_BUFFER_SIZE);
101 err_comp_output:
102 zip_data_buf_free(comp_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
104 err_comp_input:
105 zip_data_buf_free(comp_ctx->input, MAX_INPUT_BUFFER_SIZE);
107 return -ENOMEM;
110 void zip_ctx_exit(struct zip_kernel_ctx *zip_ctx)
112 struct zip_operation *comp_ctx = &zip_ctx->zip_comp;
113 struct zip_operation *dec_ctx = &zip_ctx->zip_decomp;
115 zip_data_buf_free(comp_ctx->input, MAX_INPUT_BUFFER_SIZE);
116 zip_data_buf_free(comp_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
118 zip_data_buf_free(dec_ctx->input, MAX_INPUT_BUFFER_SIZE);
119 zip_data_buf_free(dec_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
122 int zip_compress(const u8 *src, unsigned int slen,
123 u8 *dst, unsigned int *dlen,
124 struct zip_kernel_ctx *zip_ctx)
126 struct zip_operation *zip_ops = NULL;
127 struct zip_state zip_state;
128 struct zip_device *zip = NULL;
129 int ret;
131 if (!zip_ctx || !src || !dst || !dlen)
132 return -ENOMEM;
134 zip = zip_get_device(zip_get_node_id());
135 if (!zip)
136 return -ENODEV;
138 memset(&zip_state, 0, sizeof(struct zip_state));
139 zip_ops = &zip_ctx->zip_comp;
141 zip_ops->input_len = slen;
142 zip_ops->output_len = *dlen;
143 memcpy(zip_ops->input, src, slen);
145 ret = zip_deflate(zip_ops, &zip_state, zip);
147 if (!ret) {
148 *dlen = zip_ops->output_len;
149 memcpy(dst, zip_ops->output, *dlen);
152 return ret;
155 int zip_decompress(const u8 *src, unsigned int slen,
156 u8 *dst, unsigned int *dlen,
157 struct zip_kernel_ctx *zip_ctx)
159 struct zip_operation *zip_ops = NULL;
160 struct zip_state zip_state;
161 struct zip_device *zip = NULL;
162 int ret;
164 if (!zip_ctx || !src || !dst || !dlen)
165 return -ENOMEM;
167 zip = zip_get_device(zip_get_node_id());
168 if (!zip)
169 return -ENODEV;
171 memset(&zip_state, 0, sizeof(struct zip_state));
172 zip_ops = &zip_ctx->zip_decomp;
173 memcpy(zip_ops->input, src, slen);
175 /* Work around for a bug in zlib which needs an extra bytes sometimes */
176 if (zip_ops->ccode != 3) /* Not LZS Encoding */
177 zip_ops->input[slen++] = 0;
179 zip_ops->input_len = slen;
180 zip_ops->output_len = *dlen;
182 ret = zip_inflate(zip_ops, &zip_state, zip);
184 if (!ret) {
185 *dlen = zip_ops->output_len;
186 memcpy(dst, zip_ops->output, *dlen);
189 return ret;
192 /* Legacy Compress framework start */
193 int zip_alloc_comp_ctx_deflate(struct crypto_tfm *tfm)
195 int ret;
196 struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
198 ret = zip_ctx_init(zip_ctx, 0);
200 return ret;
203 int zip_alloc_comp_ctx_lzs(struct crypto_tfm *tfm)
205 int ret;
206 struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
208 ret = zip_ctx_init(zip_ctx, 1);
210 return ret;
213 void zip_free_comp_ctx(struct crypto_tfm *tfm)
215 struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
217 zip_ctx_exit(zip_ctx);
220 int zip_comp_compress(struct crypto_tfm *tfm,
221 const u8 *src, unsigned int slen,
222 u8 *dst, unsigned int *dlen)
224 int ret;
225 struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
227 ret = zip_compress(src, slen, dst, dlen, zip_ctx);
229 return ret;
232 int zip_comp_decompress(struct crypto_tfm *tfm,
233 const u8 *src, unsigned int slen,
234 u8 *dst, unsigned int *dlen)
236 int ret;
237 struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
239 ret = zip_decompress(src, slen, dst, dlen, zip_ctx);
241 return ret;
242 } /* Legacy compress framework end */
244 /* SCOMP framework start */
245 void *zip_alloc_scomp_ctx_deflate(struct crypto_scomp *tfm)
247 int ret;
248 struct zip_kernel_ctx *zip_ctx;
250 zip_ctx = kzalloc(sizeof(*zip_ctx), GFP_KERNEL);
251 if (!zip_ctx)
252 return ERR_PTR(-ENOMEM);
254 ret = zip_ctx_init(zip_ctx, 0);
256 if (ret) {
257 kzfree(zip_ctx);
258 return ERR_PTR(ret);
261 return zip_ctx;
264 void *zip_alloc_scomp_ctx_lzs(struct crypto_scomp *tfm)
266 int ret;
267 struct zip_kernel_ctx *zip_ctx;
269 zip_ctx = kzalloc(sizeof(*zip_ctx), GFP_KERNEL);
270 if (!zip_ctx)
271 return ERR_PTR(-ENOMEM);
273 ret = zip_ctx_init(zip_ctx, 1);
275 if (ret) {
276 kzfree(zip_ctx);
277 return ERR_PTR(ret);
280 return zip_ctx;
283 void zip_free_scomp_ctx(struct crypto_scomp *tfm, void *ctx)
285 struct zip_kernel_ctx *zip_ctx = ctx;
287 zip_ctx_exit(zip_ctx);
288 kzfree(zip_ctx);
291 int zip_scomp_compress(struct crypto_scomp *tfm,
292 const u8 *src, unsigned int slen,
293 u8 *dst, unsigned int *dlen, void *ctx)
295 int ret;
296 struct zip_kernel_ctx *zip_ctx = ctx;
298 ret = zip_compress(src, slen, dst, dlen, zip_ctx);
300 return ret;
303 int zip_scomp_decompress(struct crypto_scomp *tfm,
304 const u8 *src, unsigned int slen,
305 u8 *dst, unsigned int *dlen, void *ctx)
307 int ret;
308 struct zip_kernel_ctx *zip_ctx = ctx;
310 ret = zip_decompress(src, slen, dst, dlen, zip_ctx);
312 return ret;
313 } /* SCOMP framework end */