Thomas Gleixner | 2b27bdc | 2019-05-29 16:57:50 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Cryptographic API. |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/module.h> |
| 8 | #include <linux/crypto.h> |
| 9 | #include <linux/vmalloc.h> |
Eric Dumazet | 42614b0 | 2014-05-27 10:28:55 -0700 | [diff] [blame] | 10 | #include <linux/mm.h> |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 11 | #include <linux/lzo.h> |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 12 | #include <crypto/internal/scompress.h> |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 13 | |
| 14 | struct lzo_ctx { |
| 15 | void *lzo_comp_mem; |
| 16 | }; |
| 17 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 18 | static void *lzo_alloc_ctx(struct crypto_scomp *tfm) |
| 19 | { |
| 20 | void *ctx; |
| 21 | |
Michal Hocko | 752ade6 | 2017-05-08 15:57:27 -0700 | [diff] [blame] | 22 | ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL); |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 23 | if (!ctx) |
| 24 | return ERR_PTR(-ENOMEM); |
| 25 | |
| 26 | return ctx; |
| 27 | } |
| 28 | |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 29 | static int lzo_init(struct crypto_tfm *tfm) |
| 30 | { |
| 31 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); |
| 32 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 33 | ctx->lzo_comp_mem = lzo_alloc_ctx(NULL); |
| 34 | if (IS_ERR(ctx->lzo_comp_mem)) |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 35 | return -ENOMEM; |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 40 | static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx) |
| 41 | { |
| 42 | kvfree(ctx); |
| 43 | } |
| 44 | |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 45 | static void lzo_exit(struct crypto_tfm *tfm) |
| 46 | { |
| 47 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); |
| 48 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 49 | lzo_free_ctx(NULL, ctx->lzo_comp_mem); |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 50 | } |
| 51 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 52 | static int __lzo_compress(const u8 *src, unsigned int slen, |
| 53 | u8 *dst, unsigned int *dlen, void *ctx) |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 54 | { |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 55 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
| 56 | int err; |
| 57 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 58 | err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx); |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 59 | |
| 60 | if (err != LZO_E_OK) |
| 61 | return -EINVAL; |
| 62 | |
| 63 | *dlen = tmp_len; |
| 64 | return 0; |
| 65 | } |
| 66 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 67 | static int lzo_compress(struct crypto_tfm *tfm, const u8 *src, |
| 68 | unsigned int slen, u8 *dst, unsigned int *dlen) |
| 69 | { |
| 70 | struct lzo_ctx *ctx = crypto_tfm_ctx(tfm); |
| 71 | |
| 72 | return __lzo_compress(src, slen, dst, dlen, ctx->lzo_comp_mem); |
| 73 | } |
| 74 | |
| 75 | static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src, |
| 76 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 77 | void *ctx) |
| 78 | { |
| 79 | return __lzo_compress(src, slen, dst, dlen, ctx); |
| 80 | } |
| 81 | |
| 82 | static int __lzo_decompress(const u8 *src, unsigned int slen, |
| 83 | u8 *dst, unsigned int *dlen) |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 84 | { |
| 85 | int err; |
| 86 | size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */ |
| 87 | |
| 88 | err = lzo1x_decompress_safe(src, slen, dst, &tmp_len); |
| 89 | |
| 90 | if (err != LZO_E_OK) |
| 91 | return -EINVAL; |
| 92 | |
| 93 | *dlen = tmp_len; |
| 94 | return 0; |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 95 | } |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 96 | |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 97 | static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src, |
| 98 | unsigned int slen, u8 *dst, unsigned int *dlen) |
| 99 | { |
| 100 | return __lzo_decompress(src, slen, dst, dlen); |
| 101 | } |
| 102 | |
| 103 | static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src, |
| 104 | unsigned int slen, u8 *dst, unsigned int *dlen, |
| 105 | void *ctx) |
| 106 | { |
| 107 | return __lzo_decompress(src, slen, dst, dlen); |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | static struct crypto_alg alg = { |
| 111 | .cra_name = "lzo", |
Eric Biggers | d6ebf52 | 2019-06-02 22:40:57 -0700 | [diff] [blame] | 112 | .cra_driver_name = "lzo-generic", |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 113 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, |
| 114 | .cra_ctxsize = sizeof(struct lzo_ctx), |
| 115 | .cra_module = THIS_MODULE, |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 116 | .cra_init = lzo_init, |
| 117 | .cra_exit = lzo_exit, |
| 118 | .cra_u = { .compress = { |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 119 | .coa_compress = lzo_compress, |
| 120 | .coa_decompress = lzo_decompress } } |
| 121 | }; |
| 122 | |
| 123 | static struct scomp_alg scomp = { |
| 124 | .alloc_ctx = lzo_alloc_ctx, |
| 125 | .free_ctx = lzo_free_ctx, |
| 126 | .compress = lzo_scompress, |
| 127 | .decompress = lzo_sdecompress, |
| 128 | .base = { |
| 129 | .cra_name = "lzo", |
| 130 | .cra_driver_name = "lzo-scomp", |
| 131 | .cra_module = THIS_MODULE, |
| 132 | } |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 133 | }; |
| 134 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 135 | static int __init lzo_mod_init(void) |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 136 | { |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 137 | int ret; |
| 138 | |
| 139 | ret = crypto_register_alg(&alg); |
| 140 | if (ret) |
| 141 | return ret; |
| 142 | |
| 143 | ret = crypto_register_scomp(&scomp); |
| 144 | if (ret) { |
| 145 | crypto_unregister_alg(&alg); |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | return ret; |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 150 | } |
| 151 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 152 | static void __exit lzo_mod_fini(void) |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 153 | { |
| 154 | crypto_unregister_alg(&alg); |
Giovanni Cabiddu | ac9d2c4 | 2016-10-21 13:19:49 +0100 | [diff] [blame] | 155 | crypto_unregister_scomp(&scomp); |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 156 | } |
| 157 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 158 | subsys_initcall(lzo_mod_init); |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 159 | module_exit(lzo_mod_fini); |
Zoltan Sogor | 0b77abb | 2007-12-07 16:53:23 +0800 | [diff] [blame] | 160 | |
| 161 | MODULE_LICENSE("GPL"); |
| 162 | MODULE_DESCRIPTION("LZO Compression Algorithm"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 163 | MODULE_ALIAS_CRYPTO("lzo"); |