1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/crypto.h>
33#include <linux/sw842.h>
34#include <crypto/internal/scompress.h>
35
36struct crypto842_ctx {
37 void *wmem;
38};
39
40static void *crypto842_alloc_ctx(struct crypto_scomp *tfm)
41{
42 void *ctx;
43
44 ctx = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
45 if (!ctx)
46 return ERR_PTR(-ENOMEM);
47
48 return ctx;
49}
50
51static int crypto842_init(struct crypto_tfm *tfm)
52{
53 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
54
55 ctx->wmem = crypto842_alloc_ctx(NULL);
56 if (IS_ERR(ctx->wmem))
57 return -ENOMEM;
58
59 return 0;
60}
61
62static void crypto842_free_ctx(struct crypto_scomp *tfm, void *ctx)
63{
64 kfree(ctx);
65}
66
67static void crypto842_exit(struct crypto_tfm *tfm)
68{
69 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
70
71 crypto842_free_ctx(NULL, ctx->wmem);
72}
73
74static int crypto842_compress(struct crypto_tfm *tfm,
75 const u8 *src, unsigned int slen,
76 u8 *dst, unsigned int *dlen)
77{
78 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
79
80 return sw842_compress(src, slen, dst, dlen, ctx->wmem);
81}
82
83static int crypto842_scompress(struct crypto_scomp *tfm,
84 const u8 *src, unsigned int slen,
85 u8 *dst, unsigned int *dlen, void *ctx)
86{
87 return sw842_compress(src, slen, dst, dlen, ctx);
88}
89
90static int crypto842_decompress(struct crypto_tfm *tfm,
91 const u8 *src, unsigned int slen,
92 u8 *dst, unsigned int *dlen)
93{
94 return sw842_decompress(src, slen, dst, dlen);
95}
96
97static int crypto842_sdecompress(struct crypto_scomp *tfm,
98 const u8 *src, unsigned int slen,
99 u8 *dst, unsigned int *dlen, void *ctx)
100{
101 return sw842_decompress(src, slen, dst, dlen);
102}
103
104static struct crypto_alg alg = {
105 .cra_name = "842",
106 .cra_driver_name = "842-generic",
107 .cra_priority = 100,
108 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
109 .cra_ctxsize = sizeof(struct crypto842_ctx),
110 .cra_module = THIS_MODULE,
111 .cra_init = crypto842_init,
112 .cra_exit = crypto842_exit,
113 .cra_u = { .compress = {
114 .coa_compress = crypto842_compress,
115 .coa_decompress = crypto842_decompress } }
116};
117
118static struct scomp_alg scomp = {
119 .alloc_ctx = crypto842_alloc_ctx,
120 .free_ctx = crypto842_free_ctx,
121 .compress = crypto842_scompress,
122 .decompress = crypto842_sdecompress,
123 .base = {
124 .cra_name = "842",
125 .cra_driver_name = "842-scomp",
126 .cra_priority = 100,
127 .cra_module = THIS_MODULE,
128 }
129};
130
131static int __init crypto842_mod_init(void)
132{
133 int ret;
134
135 ret = crypto_register_alg(&alg);
136 if (ret)
137 return ret;
138
139 ret = crypto_register_scomp(&scomp);
140 if (ret) {
141 crypto_unregister_alg(&alg);
142 return ret;
143 }
144
145 return ret;
146}
147module_init(crypto842_mod_init);
148
149static void __exit crypto842_mod_exit(void)
150{
151 crypto_unregister_alg(&alg);
152 crypto_unregister_scomp(&scomp);
153}
154module_exit(crypto842_mod_exit);
155
156MODULE_LICENSE("GPL");
157MODULE_DESCRIPTION("842 Software Compression Algorithm");
158MODULE_ALIAS_CRYPTO("842");
159MODULE_ALIAS_CRYPTO("842-generic");
160MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
161