blob: 0606f8862e7872ad7ea876c23db09b94439dc984 [file] [log] [blame]
Thomas Gleixner2b27bdc2019-05-29 16:57:50 -07001// SPDX-License-Identifier: GPL-2.0-only
Chanho Min0ea85302013-07-08 16:01:51 -07002/*
3 * Cryptographic API.
4 *
5 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com>
Chanho Min0ea85302013-07-08 16:01:51 -07006 */
7
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/crypto.h>
11#include <linux/vmalloc.h>
12#include <linux/lz4.h>
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010013#include <crypto/internal/scompress.h>
Chanho Min0ea85302013-07-08 16:01:51 -070014
15struct lz4_ctx {
16 void *lz4_comp_mem;
17};
18
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010019static void *lz4_alloc_ctx(struct crypto_scomp *tfm)
20{
21 void *ctx;
22
23 ctx = vmalloc(LZ4_MEM_COMPRESS);
24 if (!ctx)
25 return ERR_PTR(-ENOMEM);
26
27 return ctx;
28}
29
Chanho Min0ea85302013-07-08 16:01:51 -070030static int lz4_init(struct crypto_tfm *tfm)
31{
32 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
33
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010034 ctx->lz4_comp_mem = lz4_alloc_ctx(NULL);
35 if (IS_ERR(ctx->lz4_comp_mem))
Chanho Min0ea85302013-07-08 16:01:51 -070036 return -ENOMEM;
37
38 return 0;
39}
40
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010041static void lz4_free_ctx(struct crypto_scomp *tfm, void *ctx)
42{
43 vfree(ctx);
44}
45
Chanho Min0ea85302013-07-08 16:01:51 -070046static void lz4_exit(struct crypto_tfm *tfm)
47{
48 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010049
50 lz4_free_ctx(NULL, ctx->lz4_comp_mem);
Chanho Min0ea85302013-07-08 16:01:51 -070051}
52
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010053static int __lz4_compress_crypto(const u8 *src, unsigned int slen,
54 u8 *dst, unsigned int *dlen, void *ctx)
Chanho Min0ea85302013-07-08 16:01:51 -070055{
Sven Schmidt73a15ac2017-02-24 15:01:19 -080056 int out_len = LZ4_compress_default(src, dst,
57 slen, *dlen, ctx);
Chanho Min0ea85302013-07-08 16:01:51 -070058
Sven Schmidt73a15ac2017-02-24 15:01:19 -080059 if (!out_len)
Chanho Min0ea85302013-07-08 16:01:51 -070060 return -EINVAL;
61
Sven Schmidt73a15ac2017-02-24 15:01:19 -080062 *dlen = out_len;
Chanho Min0ea85302013-07-08 16:01:51 -070063 return 0;
64}
65
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010066static int lz4_scompress(struct crypto_scomp *tfm, const u8 *src,
67 unsigned int slen, u8 *dst, unsigned int *dlen,
68 void *ctx)
69{
70 return __lz4_compress_crypto(src, slen, dst, dlen, ctx);
71}
72
73static int lz4_compress_crypto(struct crypto_tfm *tfm, const u8 *src,
74 unsigned int slen, u8 *dst, unsigned int *dlen)
75{
76 struct lz4_ctx *ctx = crypto_tfm_ctx(tfm);
77
78 return __lz4_compress_crypto(src, slen, dst, dlen, ctx->lz4_comp_mem);
79}
80
81static int __lz4_decompress_crypto(const u8 *src, unsigned int slen,
82 u8 *dst, unsigned int *dlen, void *ctx)
Chanho Min0ea85302013-07-08 16:01:51 -070083{
Sven Schmidt73a15ac2017-02-24 15:01:19 -080084 int out_len = LZ4_decompress_safe(src, dst, slen, *dlen);
Chanho Min0ea85302013-07-08 16:01:51 -070085
Sven Schmidt73a15ac2017-02-24 15:01:19 -080086 if (out_len < 0)
Myungho Jungcd15f102017-04-09 17:34:22 -070087 return -EINVAL;
Chanho Min0ea85302013-07-08 16:01:51 -070088
Sven Schmidt73a15ac2017-02-24 15:01:19 -080089 *dlen = out_len;
90 return 0;
Chanho Min0ea85302013-07-08 16:01:51 -070091}
92
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +010093static int lz4_sdecompress(struct crypto_scomp *tfm, const u8 *src,
94 unsigned int slen, u8 *dst, unsigned int *dlen,
95 void *ctx)
96{
97 return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
98}
99
100static int lz4_decompress_crypto(struct crypto_tfm *tfm, const u8 *src,
101 unsigned int slen, u8 *dst,
102 unsigned int *dlen)
103{
104 return __lz4_decompress_crypto(src, slen, dst, dlen, NULL);
105}
106
Chanho Min0ea85302013-07-08 16:01:51 -0700107static struct crypto_alg alg_lz4 = {
108 .cra_name = "lz4",
Eric Biggersd6ebf522019-06-02 22:40:57 -0700109 .cra_driver_name = "lz4-generic",
Chanho Min0ea85302013-07-08 16:01:51 -0700110 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
111 .cra_ctxsize = sizeof(struct lz4_ctx),
112 .cra_module = THIS_MODULE,
Chanho Min0ea85302013-07-08 16:01:51 -0700113 .cra_init = lz4_init,
114 .cra_exit = lz4_exit,
115 .cra_u = { .compress = {
116 .coa_compress = lz4_compress_crypto,
117 .coa_decompress = lz4_decompress_crypto } }
118};
119
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +0100120static struct scomp_alg scomp = {
121 .alloc_ctx = lz4_alloc_ctx,
122 .free_ctx = lz4_free_ctx,
123 .compress = lz4_scompress,
124 .decompress = lz4_sdecompress,
125 .base = {
126 .cra_name = "lz4",
127 .cra_driver_name = "lz4-scomp",
128 .cra_module = THIS_MODULE,
129 }
130};
131
Chanho Min0ea85302013-07-08 16:01:51 -0700132static int __init lz4_mod_init(void)
133{
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +0100134 int ret;
135
136 ret = crypto_register_alg(&alg_lz4);
137 if (ret)
138 return ret;
139
140 ret = crypto_register_scomp(&scomp);
141 if (ret) {
142 crypto_unregister_alg(&alg_lz4);
143 return ret;
144 }
145
146 return ret;
Chanho Min0ea85302013-07-08 16:01:51 -0700147}
148
149static void __exit lz4_mod_fini(void)
150{
151 crypto_unregister_alg(&alg_lz4);
Giovanni Cabiddu8cd93302016-10-21 13:19:50 +0100152 crypto_unregister_scomp(&scomp);
Chanho Min0ea85302013-07-08 16:01:51 -0700153}
154
Eric Biggersc4741b22019-04-11 21:57:42 -0700155subsys_initcall(lz4_mod_init);
Chanho Min0ea85302013-07-08 16:01:51 -0700156module_exit(lz4_mod_fini);
157
158MODULE_LICENSE("GPL");
159MODULE_DESCRIPTION("LZ4 Compression Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800160MODULE_ALIAS_CRYPTO("lz4");