540-crypto-xz-decompression-support.patch 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. --- a/crypto/Kconfig
  2. +++ b/crypto/Kconfig
  3. @@ -1545,6 +1545,13 @@ config CRYPTO_LZ4HC
  4. help
  5. This is the LZ4 high compression mode algorithm.
  6. +config CRYPTO_XZ
  7. + tristate "XZ compression algorithm"
  8. + select CRYPTO_ALGAPI
  9. + select XZ_DEC
  10. + help
  11. + This is the XZ algorithm. Only decompression is supported for now.
  12. +
  13. comment "Random Number Generation"
  14. config CRYPTO_ANSI_CPRNG
  15. --- a/crypto/Makefile
  16. +++ b/crypto/Makefile
  17. @@ -107,6 +107,7 @@ obj-$(CONFIG_CRYPTO_AUTHENC) += authenc.
  18. obj-$(CONFIG_CRYPTO_LZO) += lzo.o
  19. obj-$(CONFIG_CRYPTO_LZ4) += lz4.o
  20. obj-$(CONFIG_CRYPTO_LZ4HC) += lz4hc.o
  21. +obj-$(CONFIG_CRYPTO_XZ) += xz.o
  22. obj-$(CONFIG_CRYPTO_842) += 842.o
  23. obj-$(CONFIG_CRYPTO_RNG2) += rng.o
  24. obj-$(CONFIG_CRYPTO_ANSI_CPRNG) += ansi_cprng.o
  25. --- /dev/null
  26. +++ b/crypto/xz.c
  27. @@ -0,0 +1,117 @@
  28. +/*
  29. + * Cryptographic API.
  30. + *
  31. + * XZ decompression support.
  32. + *
  33. + * Copyright (c) 2012 Gabor Juhos <juhosg@openwrt.org>
  34. + *
  35. + * This program is free software; you can redistribute it and/or modify it
  36. + * under the terms of the GNU General Public License version 2 as published by
  37. + * the Free Software Foundation.
  38. + *
  39. + */
  40. +#include <linux/init.h>
  41. +#include <linux/module.h>
  42. +#include <linux/crypto.h>
  43. +#include <linux/xz.h>
  44. +#include <linux/interrupt.h>
  45. +#include <linux/mm.h>
  46. +#include <linux/net.h>
  47. +
  48. +struct xz_comp_ctx {
  49. + struct xz_dec *decomp_state;
  50. + struct xz_buf decomp_buf;
  51. +};
  52. +
  53. +static int crypto_xz_decomp_init(struct xz_comp_ctx *ctx)
  54. +{
  55. + ctx->decomp_state = xz_dec_init(XZ_SINGLE, 0);
  56. + if (!ctx->decomp_state)
  57. + return -ENOMEM;
  58. +
  59. + return 0;
  60. +}
  61. +
  62. +static void crypto_xz_decomp_exit(struct xz_comp_ctx *ctx)
  63. +{
  64. + xz_dec_end(ctx->decomp_state);
  65. +}
  66. +
  67. +static int crypto_xz_init(struct crypto_tfm *tfm)
  68. +{
  69. + struct xz_comp_ctx *ctx = crypto_tfm_ctx(tfm);
  70. +
  71. + return crypto_xz_decomp_init(ctx);
  72. +}
  73. +
  74. +static void crypto_xz_exit(struct crypto_tfm *tfm)
  75. +{
  76. + struct xz_comp_ctx *ctx = crypto_tfm_ctx(tfm);
  77. +
  78. + crypto_xz_decomp_exit(ctx);
  79. +}
  80. +
  81. +static int crypto_xz_compress(struct crypto_tfm *tfm, const u8 *src,
  82. + unsigned int slen, u8 *dst, unsigned int *dlen)
  83. +{
  84. + return -EOPNOTSUPP;
  85. +}
  86. +
  87. +static int crypto_xz_decompress(struct crypto_tfm *tfm, const u8 *src,
  88. + unsigned int slen, u8 *dst, unsigned int *dlen)
  89. +{
  90. + struct xz_comp_ctx *dctx = crypto_tfm_ctx(tfm);
  91. + struct xz_buf *xz_buf = &dctx->decomp_buf;
  92. + int ret;
  93. +
  94. + memset(xz_buf, '\0', sizeof(struct xz_buf));
  95. +
  96. + xz_buf->in = (u8 *) src;
  97. + xz_buf->in_pos = 0;
  98. + xz_buf->in_size = slen;
  99. + xz_buf->out = (u8 *) dst;
  100. + xz_buf->out_pos = 0;
  101. + xz_buf->out_size = *dlen;
  102. +
  103. + ret = xz_dec_run(dctx->decomp_state, xz_buf);
  104. + if (ret != XZ_STREAM_END) {
  105. + ret = -EINVAL;
  106. + goto out;
  107. + }
  108. +
  109. + *dlen = xz_buf->out_pos;
  110. + ret = 0;
  111. +
  112. +out:
  113. + return ret;
  114. +}
  115. +
  116. +static struct crypto_alg crypto_xz_alg = {
  117. + .cra_name = "xz",
  118. + .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
  119. + .cra_ctxsize = sizeof(struct xz_comp_ctx),
  120. + .cra_module = THIS_MODULE,
  121. + .cra_list = LIST_HEAD_INIT(crypto_xz_alg.cra_list),
  122. + .cra_init = crypto_xz_init,
  123. + .cra_exit = crypto_xz_exit,
  124. + .cra_u = { .compress = {
  125. + .coa_compress = crypto_xz_compress,
  126. + .coa_decompress = crypto_xz_decompress } }
  127. +};
  128. +
  129. +static int __init crypto_xz_mod_init(void)
  130. +{
  131. + return crypto_register_alg(&crypto_xz_alg);
  132. +}
  133. +
  134. +static void __exit crypto_xz_mod_exit(void)
  135. +{
  136. + crypto_unregister_alg(&crypto_xz_alg);
  137. +}
  138. +
  139. +module_init(crypto_xz_mod_init);
  140. +module_exit(crypto_xz_mod_exit);
  141. +
  142. +MODULE_LICENSE("GPL v2");
  143. +MODULE_DESCRIPTION("Crypto XZ decompression support");
  144. +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");