Dave Rodgman | 45ec975 | 2019-03-07 16:30:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU General Public License version 2 as published by |
| 6 | * the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 11 | * more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along with |
| 14 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 15 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/crypto.h> |
| 22 | #include <linux/vmalloc.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/lzo.h> |
| 25 | #include <crypto/internal/scompress.h> |
| 26 | |
| 27 | struct lzorle_ctx { |
| 28 | void *lzorle_comp_mem; |
| 29 | }; |
| 30 | |
| 31 | static void *lzorle_alloc_ctx(struct crypto_scomp *tfm) |
| 32 | { |
| 33 | void *ctx; |
| 34 | |
| 35 | ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
| 36 | if (!ctx) |
| 37 | return ERR_PTR(-ENOMEM); |
| 38 | |
| 39 | return ctx; |
| 40 | } |
| 41 | |
| 42 | static int lzorle_init(struct crypto_tfm *tfm) |
| 43 | { |
| 44 | struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm); |
| 45 | |
| 46 | ctx->lzorle_comp_mem = lzorle_alloc_ctx(NULL); |
| 47 | if (IS_ERR(ctx->lzorle_comp_mem)) |
| 48 | return -ENOMEM; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | static void lzorle_free_ctx(struct crypto_scomp *tfm, void *ctx) |
| 54 | { |
| 55 | kvfree(ctx); |
| 56 | } |
| 57 | |
| 58 | static void lzorle_exit(struct crypto_tfm *tfm) |
| 59 | { |
| 60 | struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm); |
| 61 | |
| 62 | lzorle_free_ctx(NULL, ctx->lzorle_comp_mem); |
| 63 | } |
| 64 | |
| 65 | static int __lzorle_compress(const u8 *src, unsigned int slen, |
| 66 | u8 *dst, unsigned int *dlen, void *ctx) |
| 67 | { |
| 68 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
| 69 | int err; |
| 70 | |
| 71 | err = lzorle1x_1_compress(src, slen, dst, &tmp_len, ctx); |
| 72 | |
| 73 | if (err != LZO_E_OK) |
| 74 | return -EINVAL; |
| 75 | |
| 76 | *dlen = tmp_len; |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static int lzorle_compress(struct crypto_tfm *tfm, const u8 *src, |
| 81 | unsigned int slen, u8 *dst, unsigned int *dlen) |
| 82 | { |
| 83 | struct lzorle_ctx *ctx = crypto_tfm_ctx(tfm); |
| 84 | |
| 85 | return __lzorle_compress(src, slen, dst, dlen, ctx->lzorle_comp_mem); |
| 86 | } |
| 87 | |
| 88 | static int lzorle_scompress(struct crypto_scomp *tfm, const u8 *src, |
| 89 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 90 | void *ctx) |
| 91 | { |
| 92 | return __lzorle_compress(src, slen, dst, dlen, ctx); |
| 93 | } |
| 94 | |
| 95 | static int __lzorle_decompress(const u8 *src, unsigned int slen, |
| 96 | u8 *dst, unsigned int *dlen) |
| 97 | { |
| 98 | int err; |
| 99 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
| 100 | |
| 101 | err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); |
| 102 | |
| 103 | if (err != LZO_E_OK) |
| 104 | return -EINVAL; |
| 105 | |
| 106 | *dlen = tmp_len; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int lzorle_decompress(struct crypto_tfm *tfm, const u8 *src, |
| 111 | unsigned int slen, u8 *dst, unsigned int *dlen) |
| 112 | { |
| 113 | return __lzorle_decompress(src, slen, dst, dlen); |
| 114 | } |
| 115 | |
| 116 | static int lzorle_sdecompress(struct crypto_scomp *tfm, const u8 *src, |
| 117 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 118 | void *ctx) |
| 119 | { |
| 120 | return __lzorle_decompress(src, slen, dst, dlen); |
| 121 | } |
| 122 | |
| 123 | static struct crypto_alg alg = { |
| 124 | .cra_name = "lzo-rle", |
Eric Biggers | d6ebf52 | 2019-06-02 22:40:57 -0700 | [diff] [blame^] | 125 | .cra_driver_name = "lzo-rle-generic", |
Dave Rodgman | 45ec975 | 2019-03-07 16:30:44 -0800 | [diff] [blame] | 126 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, |
| 127 | .cra_ctxsize = sizeof(struct lzorle_ctx), |
| 128 | .cra_module = THIS_MODULE, |
| 129 | .cra_init = lzorle_init, |
| 130 | .cra_exit = lzorle_exit, |
| 131 | .cra_u = { .compress = { |
| 132 | .coa_compress = lzorle_compress, |
| 133 | .coa_decompress = lzorle_decompress } } |
| 134 | }; |
| 135 | |
| 136 | static struct scomp_alg scomp = { |
| 137 | .alloc_ctx = lzorle_alloc_ctx, |
| 138 | .free_ctx = lzorle_free_ctx, |
| 139 | .compress = lzorle_scompress, |
| 140 | .decompress = lzorle_sdecompress, |
| 141 | .base = { |
| 142 | .cra_name = "lzo-rle", |
| 143 | .cra_driver_name = "lzo-rle-scomp", |
| 144 | .cra_module = THIS_MODULE, |
| 145 | } |
| 146 | }; |
| 147 | |
| 148 | static int __init lzorle_mod_init(void) |
| 149 | { |
| 150 | int ret; |
| 151 | |
| 152 | ret = crypto_register_alg(&alg); |
| 153 | if (ret) |
| 154 | return ret; |
| 155 | |
| 156 | ret = crypto_register_scomp(&scomp); |
| 157 | if (ret) { |
| 158 | crypto_unregister_alg(&alg); |
| 159 | return ret; |
| 160 | } |
| 161 | |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static void __exit lzorle_mod_fini(void) |
| 166 | { |
| 167 | crypto_unregister_alg(&alg); |
| 168 | crypto_unregister_scomp(&scomp); |
| 169 | } |
| 170 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 171 | subsys_initcall(lzorle_mod_init); |
Dave Rodgman | 45ec975 | 2019-03-07 16:30:44 -0800 | [diff] [blame] | 172 | module_exit(lzorle_mod_fini); |
| 173 | |
| 174 | MODULE_LICENSE("GPL"); |
| 175 | MODULE_DESCRIPTION("LZO-RLE Compression Algorithm"); |
| 176 | MODULE_ALIAS_CRYPTO("lzo-rle"); |