zip_crypto.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /***********************license start************************************
  2. * Copyright (c) 2003-2017 Cavium, Inc.
  3. * All rights reserved.
  4. *
  5. * License: one of 'Cavium License' or 'GNU General Public License Version 2'
  6. *
  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.
  11. *
  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:
  15. *
  16. * * Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  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.
  23. *
  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.
  27. *
  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.
  32. *
  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**************************************/
  45. #include "zip_crypto.h"
  46. static void zip_static_init_zip_ops(struct zip_operation *zip_ops,
  47. int lzs_flag)
  48. {
  49. zip_ops->flush = ZIP_FLUSH_FINISH;
  50. /* equivalent to level 6 of opensource zlib */
  51. zip_ops->speed = 1;
  52. if (!lzs_flag) {
  53. zip_ops->ccode = 0; /* Auto Huffman */
  54. zip_ops->lzs_flag = 0;
  55. zip_ops->format = ZLIB_FORMAT;
  56. } else {
  57. zip_ops->ccode = 3; /* LZS Encoding */
  58. zip_ops->lzs_flag = 1;
  59. zip_ops->format = LZS_FORMAT;
  60. }
  61. zip_ops->begin_file = 1;
  62. zip_ops->history_len = 0;
  63. zip_ops->end_file = 1;
  64. zip_ops->compcode = 0;
  65. zip_ops->csum = 1; /* Adler checksum desired */
  66. }
  67. static int zip_ctx_init(struct zip_kernel_ctx *zip_ctx, int lzs_flag)
  68. {
  69. struct zip_operation *comp_ctx = &zip_ctx->zip_comp;
  70. struct zip_operation *decomp_ctx = &zip_ctx->zip_decomp;
  71. zip_static_init_zip_ops(comp_ctx, lzs_flag);
  72. zip_static_init_zip_ops(decomp_ctx, lzs_flag);
  73. comp_ctx->input = zip_data_buf_alloc(MAX_INPUT_BUFFER_SIZE);
  74. if (!comp_ctx->input)
  75. return -ENOMEM;
  76. comp_ctx->output = zip_data_buf_alloc(MAX_OUTPUT_BUFFER_SIZE);
  77. if (!comp_ctx->output)
  78. goto err_comp_input;
  79. decomp_ctx->input = zip_data_buf_alloc(MAX_INPUT_BUFFER_SIZE);
  80. if (!decomp_ctx->input)
  81. goto err_comp_output;
  82. decomp_ctx->output = zip_data_buf_alloc(MAX_OUTPUT_BUFFER_SIZE);
  83. if (!decomp_ctx->output)
  84. goto err_decomp_input;
  85. return 0;
  86. err_decomp_input:
  87. zip_data_buf_free(decomp_ctx->input, MAX_INPUT_BUFFER_SIZE);
  88. err_comp_output:
  89. zip_data_buf_free(comp_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
  90. err_comp_input:
  91. zip_data_buf_free(comp_ctx->input, MAX_INPUT_BUFFER_SIZE);
  92. return -ENOMEM;
  93. }
  94. static void zip_ctx_exit(struct zip_kernel_ctx *zip_ctx)
  95. {
  96. struct zip_operation *comp_ctx = &zip_ctx->zip_comp;
  97. struct zip_operation *dec_ctx = &zip_ctx->zip_decomp;
  98. zip_data_buf_free(comp_ctx->input, MAX_INPUT_BUFFER_SIZE);
  99. zip_data_buf_free(comp_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
  100. zip_data_buf_free(dec_ctx->input, MAX_INPUT_BUFFER_SIZE);
  101. zip_data_buf_free(dec_ctx->output, MAX_OUTPUT_BUFFER_SIZE);
  102. }
  103. static int zip_compress(const u8 *src, unsigned int slen,
  104. u8 *dst, unsigned int *dlen,
  105. struct zip_kernel_ctx *zip_ctx)
  106. {
  107. struct zip_operation *zip_ops = NULL;
  108. struct zip_state *zip_state;
  109. struct zip_device *zip = NULL;
  110. int ret;
  111. if (!zip_ctx || !src || !dst || !dlen)
  112. return -ENOMEM;
  113. zip = zip_get_device(zip_get_node_id());
  114. if (!zip)
  115. return -ENODEV;
  116. zip_state = kzalloc(sizeof(*zip_state), GFP_ATOMIC);
  117. if (!zip_state)
  118. return -ENOMEM;
  119. zip_ops = &zip_ctx->zip_comp;
  120. zip_ops->input_len = slen;
  121. zip_ops->output_len = *dlen;
  122. memcpy(zip_ops->input, src, slen);
  123. ret = zip_deflate(zip_ops, zip_state, zip);
  124. if (!ret) {
  125. *dlen = zip_ops->output_len;
  126. memcpy(dst, zip_ops->output, *dlen);
  127. }
  128. kfree(zip_state);
  129. return ret;
  130. }
  131. static int zip_decompress(const u8 *src, unsigned int slen,
  132. u8 *dst, unsigned int *dlen,
  133. struct zip_kernel_ctx *zip_ctx)
  134. {
  135. struct zip_operation *zip_ops = NULL;
  136. struct zip_state *zip_state;
  137. struct zip_device *zip = NULL;
  138. int ret;
  139. if (!zip_ctx || !src || !dst || !dlen)
  140. return -ENOMEM;
  141. zip = zip_get_device(zip_get_node_id());
  142. if (!zip)
  143. return -ENODEV;
  144. zip_state = kzalloc(sizeof(*zip_state), GFP_ATOMIC);
  145. if (!zip_state)
  146. return -ENOMEM;
  147. zip_ops = &zip_ctx->zip_decomp;
  148. memcpy(zip_ops->input, src, slen);
  149. /* Work around for a bug in zlib which needs an extra bytes sometimes */
  150. if (zip_ops->ccode != 3) /* Not LZS Encoding */
  151. zip_ops->input[slen++] = 0;
  152. zip_ops->input_len = slen;
  153. zip_ops->output_len = *dlen;
  154. ret = zip_inflate(zip_ops, zip_state, zip);
  155. if (!ret) {
  156. *dlen = zip_ops->output_len;
  157. memcpy(dst, zip_ops->output, *dlen);
  158. }
  159. kfree(zip_state);
  160. return ret;
  161. }
  162. /* Legacy Compress framework start */
  163. int zip_alloc_comp_ctx_deflate(struct crypto_tfm *tfm)
  164. {
  165. struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
  166. return zip_ctx_init(zip_ctx, 0);
  167. }
  168. int zip_alloc_comp_ctx_lzs(struct crypto_tfm *tfm)
  169. {
  170. struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
  171. return zip_ctx_init(zip_ctx, 1);
  172. }
  173. void zip_free_comp_ctx(struct crypto_tfm *tfm)
  174. {
  175. struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
  176. zip_ctx_exit(zip_ctx);
  177. }
  178. int zip_comp_compress(struct crypto_tfm *tfm,
  179. const u8 *src, unsigned int slen,
  180. u8 *dst, unsigned int *dlen)
  181. {
  182. struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
  183. return zip_compress(src, slen, dst, dlen, zip_ctx);
  184. }
  185. int zip_comp_decompress(struct crypto_tfm *tfm,
  186. const u8 *src, unsigned int slen,
  187. u8 *dst, unsigned int *dlen)
  188. {
  189. struct zip_kernel_ctx *zip_ctx = crypto_tfm_ctx(tfm);
  190. return zip_decompress(src, slen, dst, dlen, zip_ctx);
  191. } /* Legacy compress framework end */
  192. /* SCOMP framework start */
  193. void *zip_alloc_scomp_ctx_deflate(struct crypto_scomp *tfm)
  194. {
  195. int ret;
  196. struct zip_kernel_ctx *zip_ctx;
  197. zip_ctx = kzalloc(sizeof(*zip_ctx), GFP_KERNEL);
  198. if (!zip_ctx)
  199. return ERR_PTR(-ENOMEM);
  200. ret = zip_ctx_init(zip_ctx, 0);
  201. if (ret) {
  202. kfree_sensitive(zip_ctx);
  203. return ERR_PTR(ret);
  204. }
  205. return zip_ctx;
  206. }
  207. void *zip_alloc_scomp_ctx_lzs(struct crypto_scomp *tfm)
  208. {
  209. int ret;
  210. struct zip_kernel_ctx *zip_ctx;
  211. zip_ctx = kzalloc(sizeof(*zip_ctx), GFP_KERNEL);
  212. if (!zip_ctx)
  213. return ERR_PTR(-ENOMEM);
  214. ret = zip_ctx_init(zip_ctx, 1);
  215. if (ret) {
  216. kfree_sensitive(zip_ctx);
  217. return ERR_PTR(ret);
  218. }
  219. return zip_ctx;
  220. }
  221. void zip_free_scomp_ctx(struct crypto_scomp *tfm, void *ctx)
  222. {
  223. struct zip_kernel_ctx *zip_ctx = ctx;
  224. zip_ctx_exit(zip_ctx);
  225. kfree_sensitive(zip_ctx);
  226. }
  227. int zip_scomp_compress(struct crypto_scomp *tfm,
  228. const u8 *src, unsigned int slen,
  229. u8 *dst, unsigned int *dlen, void *ctx)
  230. {
  231. struct zip_kernel_ctx *zip_ctx = ctx;
  232. return zip_compress(src, slen, dst, dlen, zip_ctx);
  233. }
  234. int zip_scomp_decompress(struct crypto_scomp *tfm,
  235. const u8 *src, unsigned int slen,
  236. u8 *dst, unsigned int *dlen, void *ctx)
  237. {
  238. struct zip_kernel_ctx *zip_ctx = ctx;
  239. return zip_decompress(src, slen, dst, dlen, zip_ctx);
  240. } /* SCOMP framework end */