Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Cryptographic API. |
| 3 | * |
| 4 | * Copyright (c) 2013 Chanho Min <chanho.min@lge.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License version 2 as published by |
| 8 | * the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 17 | * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | * |
| 19 | */ |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/crypto.h> |
| 23 | #include <linux/vmalloc.h> |
| 24 | #include <linux/lz4.h> |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 25 | #include <crypto/internal/scompress.h> |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 26 | |
| 27 | struct lz4hc_ctx { |
| 28 | void *lz4hc_comp_mem; |
| 29 | }; |
| 30 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 31 | static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm) |
| 32 | { |
| 33 | void *ctx; |
| 34 | |
| 35 | ctx = vmalloc(LZ4HC_MEM_COMPRESS); |
| 36 | if (!ctx) |
| 37 | return ERR_PTR(-ENOMEM); |
| 38 | |
| 39 | return ctx; |
| 40 | } |
| 41 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 42 | static int lz4hc_init(struct crypto_tfm *tfm) |
| 43 | { |
| 44 | struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 45 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 46 | ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL); |
| 47 | if (IS_ERR(ctx->lz4hc_comp_mem)) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 48 | return -ENOMEM; |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 53 | static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx) |
| 54 | { |
| 55 | vfree(ctx); |
| 56 | } |
| 57 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 58 | static void lz4hc_exit(struct crypto_tfm *tfm) |
| 59 | { |
| 60 | struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 61 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 62 | lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 65 | static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen, |
| 66 | u8 *dst, unsigned int *dlen, void *ctx) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 67 | { |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 68 | int out_len = LZ4_compress_HC(src, dst, slen, |
| 69 | *dlen, LZ4HC_DEFAULT_CLEVEL, ctx); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 70 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 71 | if (!out_len) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 72 | return -EINVAL; |
| 73 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 74 | *dlen = out_len; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 78 | static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src, |
| 79 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 80 | void *ctx) |
| 81 | { |
| 82 | return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx); |
| 83 | } |
| 84 | |
| 85 | static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src, |
| 86 | unsigned int slen, u8 *dst, |
| 87 | unsigned int *dlen) |
| 88 | { |
| 89 | struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); |
| 90 | |
| 91 | return __lz4hc_compress_crypto(src, slen, dst, dlen, |
| 92 | ctx->lz4hc_comp_mem); |
| 93 | } |
| 94 | |
| 95 | static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen, |
| 96 | u8 *dst, unsigned int *dlen, void *ctx) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 97 | { |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 98 | int out_len = LZ4_decompress_safe(src, dst, slen, *dlen); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 99 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 100 | if (out_len < 0) |
Myungho Jung | cd15f10 | 2017-04-09 17:34:22 -0700 | [diff] [blame] | 101 | return -EINVAL; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 102 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 103 | *dlen = out_len; |
| 104 | return 0; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 107 | static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src, |
| 108 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 109 | void *ctx) |
| 110 | { |
| 111 | return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL); |
| 112 | } |
| 113 | |
| 114 | static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, |
| 115 | unsigned int slen, u8 *dst, |
| 116 | unsigned int *dlen) |
| 117 | { |
| 118 | return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL); |
| 119 | } |
| 120 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 121 | static struct crypto_alg alg_lz4hc = { |
| 122 | .cra_name = "lz4hc", |
| 123 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, |
| 124 | .cra_ctxsize = sizeof(struct lz4hc_ctx), |
| 125 | .cra_module = THIS_MODULE, |
| 126 | .cra_list = LIST_HEAD_INIT(alg_lz4hc.cra_list), |
| 127 | .cra_init = lz4hc_init, |
| 128 | .cra_exit = lz4hc_exit, |
| 129 | .cra_u = { .compress = { |
| 130 | .coa_compress = lz4hc_compress_crypto, |
| 131 | .coa_decompress = lz4hc_decompress_crypto } } |
| 132 | }; |
| 133 | |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 134 | static struct scomp_alg scomp = { |
| 135 | .alloc_ctx = lz4hc_alloc_ctx, |
| 136 | .free_ctx = lz4hc_free_ctx, |
| 137 | .compress = lz4hc_scompress, |
| 138 | .decompress = lz4hc_sdecompress, |
| 139 | .base = { |
| 140 | .cra_name = "lz4hc", |
| 141 | .cra_driver_name = "lz4hc-scomp", |
| 142 | .cra_module = THIS_MODULE, |
| 143 | } |
| 144 | }; |
| 145 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 146 | static int __init lz4hc_mod_init(void) |
| 147 | { |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 148 | int ret; |
| 149 | |
| 150 | ret = crypto_register_alg(&alg_lz4hc); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | ret = crypto_register_scomp(&scomp); |
| 155 | if (ret) { |
| 156 | crypto_unregister_alg(&alg_lz4hc); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | return ret; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | static void __exit lz4hc_mod_fini(void) |
| 164 | { |
| 165 | crypto_unregister_alg(&alg_lz4hc); |
Giovanni Cabiddu | 91d53d9 | 2016-10-21 13:19:51 +0100 | [diff] [blame] | 166 | crypto_unregister_scomp(&scomp); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | module_init(lz4hc_mod_init); |
| 170 | module_exit(lz4hc_mod_fini); |
| 171 | |
| 172 | MODULE_LICENSE("GPL"); |
| 173 | MODULE_DESCRIPTION("LZ4HC Compression Algorithm"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 174 | MODULE_ALIAS_CRYPTO("lz4hc"); |