Blame view

crypto/lzo.c 3.31 KB
2b27bdcc2   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
2
3
  /*
   * Cryptographic API.
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
4
5
6
7
8
9
   */
  
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/crypto.h>
  #include <linux/vmalloc.h>
42614b058   Eric Dumazet   crypto: lzo - try...
10
  #include <linux/mm.h>
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
11
  #include <linux/lzo.h>
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
12
  #include <crypto/internal/scompress.h>
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
13
14
15
16
  
  struct lzo_ctx {
  	void *lzo_comp_mem;
  };
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
17
18
19
  static void *lzo_alloc_ctx(struct crypto_scomp *tfm)
  {
  	void *ctx;
752ade68c   Michal Hocko   treewide: use kv[...
20
  	ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
21
22
23
24
25
  	if (!ctx)
  		return ERR_PTR(-ENOMEM);
  
  	return ctx;
  }
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
26
27
28
  static int lzo_init(struct crypto_tfm *tfm)
  {
  	struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
29
30
  	ctx->lzo_comp_mem = lzo_alloc_ctx(NULL);
  	if (IS_ERR(ctx->lzo_comp_mem))
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
31
32
33
34
  		return -ENOMEM;
  
  	return 0;
  }
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
35
36
37
38
  static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx)
  {
  	kvfree(ctx);
  }
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
39
40
41
  static void lzo_exit(struct crypto_tfm *tfm)
  {
  	struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
42
  	lzo_free_ctx(NULL, ctx->lzo_comp_mem);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
43
  }
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
44
45
  static int __lzo_compress(const u8 *src, unsigned int slen,
  			  u8 *dst, unsigned int *dlen, void *ctx)
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
46
  {
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
47
48
  	size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  	int err;
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
49
  	err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
50
51
52
53
54
55
56
  
  	if (err != LZO_E_OK)
  		return -EINVAL;
  
  	*dlen = tmp_len;
  	return 0;
  }
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  static int lzo_compress(struct crypto_tfm *tfm, const u8 *src,
  			unsigned int slen, u8 *dst, unsigned int *dlen)
  {
  	struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
  
  	return __lzo_compress(src, slen, dst, dlen, ctx->lzo_comp_mem);
  }
  
  static int lzo_scompress(struct crypto_scomp *tfm, const u8 *src,
  			 unsigned int slen, u8 *dst, unsigned int *dlen,
  			 void *ctx)
  {
  	return __lzo_compress(src, slen, dst, dlen, ctx);
  }
  
  static int __lzo_decompress(const u8 *src, unsigned int slen,
  			    u8 *dst, unsigned int *dlen)
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
74
75
76
77
78
79
80
81
82
83
84
  {
  	int err;
  	size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  
  	err = lzo1x_decompress_safe(src, slen, dst, &tmp_len);
  
  	if (err != LZO_E_OK)
  		return -EINVAL;
  
  	*dlen = tmp_len;
  	return 0;
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
85
  }
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
86

ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
87
88
89
90
91
92
93
94
95
96
97
  static int lzo_decompress(struct crypto_tfm *tfm, const u8 *src,
  			  unsigned int slen, u8 *dst, unsigned int *dlen)
  {
  	return __lzo_decompress(src, slen, dst, dlen);
  }
  
  static int lzo_sdecompress(struct crypto_scomp *tfm, const u8 *src,
  			   unsigned int slen, u8 *dst, unsigned int *dlen,
  			   void *ctx)
  {
  	return __lzo_decompress(src, slen, dst, dlen);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
98
99
100
101
  }
  
  static struct crypto_alg alg = {
  	.cra_name		= "lzo",
d6ebf5286   Eric Biggers   crypto: make all ...
102
  	.cra_driver_name	= "lzo-generic",
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
103
104
105
  	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
  	.cra_ctxsize		= sizeof(struct lzo_ctx),
  	.cra_module		= THIS_MODULE,
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
106
107
108
  	.cra_init		= lzo_init,
  	.cra_exit		= lzo_exit,
  	.cra_u			= { .compress = {
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  	.coa_compress		= lzo_compress,
  	.coa_decompress		= lzo_decompress } }
  };
  
  static struct scomp_alg scomp = {
  	.alloc_ctx		= lzo_alloc_ctx,
  	.free_ctx		= lzo_free_ctx,
  	.compress		= lzo_scompress,
  	.decompress		= lzo_sdecompress,
  	.base			= {
  		.cra_name	= "lzo",
  		.cra_driver_name = "lzo-scomp",
  		.cra_module	 = THIS_MODULE,
  	}
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
123
  };
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
124
  static int __init lzo_mod_init(void)
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
125
  {
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
126
127
128
129
130
131
132
133
134
135
136
137
138
  	int ret;
  
  	ret = crypto_register_alg(&alg);
  	if (ret)
  		return ret;
  
  	ret = crypto_register_scomp(&scomp);
  	if (ret) {
  		crypto_unregister_alg(&alg);
  		return ret;
  	}
  
  	return ret;
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
139
  }
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
140
  static void __exit lzo_mod_fini(void)
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
141
142
  {
  	crypto_unregister_alg(&alg);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
143
  	crypto_unregister_scomp(&scomp);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
144
  }
c4741b230   Eric Biggers   crypto: run initc...
145
  subsys_initcall(lzo_mod_init);
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
146
  module_exit(lzo_mod_fini);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
147
148
149
  
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("LZO Compression Algorithm");
5d26a105b   Kees Cook   crypto: prefix mo...
150
  MODULE_ALIAS_CRYPTO("lzo");