Blame view

crypto/lzo.c 3.87 KB
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  /*
   * Cryptographic API.
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 as published by
   * the Free Software Foundation.
   *
   * This program is distributed in the hope that it will be useful, but WITHOUT
   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   * more details.
   *
   * You should have received a copy of the GNU General Public License along with
   * this program; if not, write to the Free Software Foundation, Inc., 51
   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
   *
   */
  
  #include <linux/init.h>
  #include <linux/module.h>
  #include <linux/crypto.h>
  #include <linux/vmalloc.h>
42614b058   Eric Dumazet   crypto: lzo - try...
23
  #include <linux/mm.h>
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
24
  #include <linux/lzo.h>
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
25
  #include <crypto/internal/scompress.h>
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
26
27
28
29
  
  struct lzo_ctx {
  	void *lzo_comp_mem;
  };
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
30
31
32
  static void *lzo_alloc_ctx(struct crypto_scomp *tfm)
  {
  	void *ctx;
752ade68c   Michal Hocko   treewide: use kv[...
33
  	ctx = kvmalloc(LZO1X_MEM_COMPRESS, GFP_KERNEL);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
34
35
36
37
38
  	if (!ctx)
  		return ERR_PTR(-ENOMEM);
  
  	return ctx;
  }
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
39
40
41
  static int lzo_init(struct crypto_tfm *tfm)
  {
  	struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
42
43
  	ctx->lzo_comp_mem = lzo_alloc_ctx(NULL);
  	if (IS_ERR(ctx->lzo_comp_mem))
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
44
45
46
47
  		return -ENOMEM;
  
  	return 0;
  }
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
48
49
50
51
  static void lzo_free_ctx(struct crypto_scomp *tfm, void *ctx)
  {
  	kvfree(ctx);
  }
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
52
53
54
  static void lzo_exit(struct crypto_tfm *tfm)
  {
  	struct lzo_ctx *ctx = crypto_tfm_ctx(tfm);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
55
  	lzo_free_ctx(NULL, ctx->lzo_comp_mem);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
56
  }
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
57
58
  static int __lzo_compress(const u8 *src, unsigned int slen,
  			  u8 *dst, unsigned int *dlen, void *ctx)
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
59
  {
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
60
61
  	size_t tmp_len = *dlen; /* size_t(ulong) <-> uint on 64 bit */
  	int err;
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
62
  	err = lzo1x_1_compress(src, slen, dst, &tmp_len, ctx);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
63
64
65
66
67
68
69
  
  	if (err != LZO_E_OK)
  		return -EINVAL;
  
  	*dlen = tmp_len;
  	return 0;
  }
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  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...
87
88
89
90
91
92
93
94
95
96
97
  {
  	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...
98
  }
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
99

ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
100
101
102
103
104
105
106
107
108
109
110
  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...
111
112
113
114
115
116
117
  }
  
  static struct crypto_alg alg = {
  	.cra_name		= "lzo",
  	.cra_flags		= CRYPTO_ALG_TYPE_COMPRESS,
  	.cra_ctxsize		= sizeof(struct lzo_ctx),
  	.cra_module		= THIS_MODULE,
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
118
119
120
  	.cra_init		= lzo_init,
  	.cra_exit		= lzo_exit,
  	.cra_u			= { .compress = {
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  	.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...
135
  };
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
136
  static int __init lzo_mod_init(void)
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
137
  {
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
138
139
140
141
142
143
144
145
146
147
148
149
150
  	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...
151
  }
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
152
  static void __exit lzo_mod_fini(void)
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
153
154
  {
  	crypto_unregister_alg(&alg);
ac9d2c4b3   Giovanni Cabiddu   crypto: acomp - a...
155
  	crypto_unregister_scomp(&scomp);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
156
  }
3af5b90bd   Kamalesh Babulal   [CRYPTO] all: Cle...
157
158
  module_init(lzo_mod_init);
  module_exit(lzo_mod_fini);
0b77abb3b   Zoltan Sogor   [CRYPTO] lzo: Add...
159
160
161
  
  MODULE_LICENSE("GPL");
  MODULE_DESCRIPTION("LZO Compression Algorithm");
5d26a105b   Kees Cook   crypto: prefix mo...
162
  MODULE_ALIAS_CRYPTO("lzo");