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 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/crypto.h> |
| 24 | #include <linux/vmalloc.h> |
| 25 | #include <linux/lz4.h> |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 26 | #include <crypto/internal/scompress.h> |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 27 | |
| 28 | struct lz4_ctx { |
| 29 | void *lz4_comp_mem; |
| 30 | }; |
| 31 | |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 32 | static void *lz4_alloc_ctx(struct crypto_scomp *tfm) |
| 33 | { |
| 34 | void *ctx; |
| 35 | |
| 36 | ctx = vmalloc(LZ4_MEM_COMPRESS); |
| 37 | if (!ctx) |
| 38 | return ERR_PTR(-ENOMEM); |
| 39 | |
| 40 | return ctx; |
| 41 | } |
| 42 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 43 | static int lz4_init(struct crypto_tfm *tfm) |
| 44 | { |
| 45 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); |
| 46 | |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 47 | ctx->lz4_comp_mem = lz4_alloc_ctx(NULL); |
| 48 | if (IS_ERR(ctx->lz4_comp_mem)) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 49 | return -ENOMEM; |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 54 | static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx) |
| 55 | { |
| 56 | vfree(ctx); |
| 57 | } |
| 58 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 59 | static void lz4_exit(struct crypto_tfm *tfm) |
| 60 | { |
| 61 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 62 | |
| 63 | lz4_free_ctx(NULL, ctx->lz4_comp_mem); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 66 | static int __lz4_compress_crypto(const u8 *src, unsigned int slen, |
| 67 | u8 *dst, unsigned int *dlen, void *ctx) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 68 | { |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 69 | int out_len = LZ4_compress_default(src, dst, |
| 70 | slen, *dlen, ctx); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 71 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 72 | if (!out_len) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 73 | return -EINVAL; |
| 74 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 75 | *dlen = out_len; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 79 | static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src, |
| 80 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 81 | void *ctx) |
| 82 | { |
| 83 | return __lz4_compress_crypto(src, slen, dst, dlen, ctx); |
| 84 | } |
| 85 | |
| 86 | static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src, |
| 87 | unsigned int slen, u8 *dst, unsigned int *dlen) |
| 88 | { |
| 89 | struct lz4_ctx *ctx = crypto_tfm_ctx(tfm); |
| 90 | |
| 91 | return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem); |
| 92 | } |
| 93 | |
| 94 | static int __lz4_decompress_crypto(const u8 *src, unsigned int slen, |
| 95 | u8 *dst, unsigned int *dlen, void *ctx) |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 96 | { |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 97 | int out_len = LZ4_decompress_safe(src, dst, slen, *dlen); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 98 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 99 | if (out_len < 0) |
Myungho Jung | cd15f10 | 2017-04-09 17:34:22 -0700 | [diff] [blame] | 100 | return -EINVAL; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 101 | |
Sven Schmidt | 73a15ac | 2017-02-24 15:01:19 -0800 | [diff] [blame] | 102 | *dlen = out_len; |
| 103 | return 0; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 106 | static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src, |
| 107 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 108 | void *ctx) |
| 109 | { |
| 110 | return __lz4_decompress_crypto(src, slen, dst, dlen, NULL); |
| 111 | } |
| 112 | |
| 113 | static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, |
| 114 | unsigned int slen, u8 *dst, |
| 115 | unsigned int *dlen) |
| 116 | { |
| 117 | return __lz4_decompress_crypto(src, slen, dst, dlen, NULL); |
| 118 | } |
| 119 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 120 | static struct crypto_alg alg_lz4 = { |
| 121 | .cra_name = "lz4", |
| 122 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, |
| 123 | .cra_ctxsize = sizeof(struct lz4_ctx), |
| 124 | .cra_module = THIS_MODULE, |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 125 | .cra_init = lz4_init, |
| 126 | .cra_exit = lz4_exit, |
| 127 | .cra_u = { .compress = { |
| 128 | .coa_compress = lz4_compress_crypto, |
| 129 | .coa_decompress = lz4_decompress_crypto } } |
| 130 | }; |
| 131 | |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 132 | static struct scomp_alg scomp = { |
| 133 | .alloc_ctx = lz4_alloc_ctx, |
| 134 | .free_ctx = lz4_free_ctx, |
| 135 | .compress = lz4_scompress, |
| 136 | .decompress = lz4_sdecompress, |
| 137 | .base = { |
| 138 | .cra_name = "lz4", |
| 139 | .cra_driver_name = "lz4-scomp", |
| 140 | .cra_module = THIS_MODULE, |
| 141 | } |
| 142 | }; |
| 143 | |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 144 | static int __init lz4_mod_init(void) |
| 145 | { |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 146 | int ret; |
| 147 | |
| 148 | ret = crypto_register_alg(&alg_lz4); |
| 149 | if (ret) |
| 150 | return ret; |
| 151 | |
| 152 | ret = crypto_register_scomp(&scomp); |
| 153 | if (ret) { |
| 154 | crypto_unregister_alg(&alg_lz4); |
| 155 | return ret; |
| 156 | } |
| 157 | |
| 158 | return ret; |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static void __exit lz4_mod_fini(void) |
| 162 | { |
| 163 | crypto_unregister_alg(&alg_lz4); |
Giovanni Cabiddu | 8cd9330 | 2016-10-21 13:19:50 +0100 | [diff] [blame] | 164 | crypto_unregister_scomp(&scomp); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 167 | subsys_initcall(lz4_mod_init); |
Chanho Min | 0ea8530 | 2013-07-08 16:01:51 -0700 | [diff] [blame] | 168 | module_exit(lz4_mod_fini); |
| 169 | |
| 170 | MODULE_LICENSE("GPL"); |
| 171 | MODULE_DESCRIPTION("LZ4 Compression Algorithm"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 172 | MODULE_ALIAS_CRYPTO("lz4"); |