blob: bc26dc942821c518819dc47f24aba7b97ad989ae [file] [log] [blame]
Seth Jennings35a1fc12012-07-19 09:42:41 -05001/*
Dan Streetman2062c5b2015-05-07 13:49:15 -04002 * Cryptographic API for the 842 software compression algorithm.
Seth Jennings35a1fc12012-07-19 09:42:41 -05003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
Dan Streetman2062c5b2015-05-07 13:49:15 -040014 * Copyright (C) IBM Corporation, 2011-2015
Seth Jennings35a1fc12012-07-19 09:42:41 -050015 *
Dan Streetman2062c5b2015-05-07 13:49:15 -040016 * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
17 * Seth Jennings <sjenning@linux.vnet.ibm.com>
Seth Jennings35a1fc12012-07-19 09:42:41 -050018 *
Dan Streetman2062c5b2015-05-07 13:49:15 -040019 * Rewrite: Dan Streetman <ddstreet@ieee.org>
20 *
21 * This is the software implementation of compression and decompression using
22 * the 842 format. This uses the software 842 library at lib/842/ which is
23 * only a reference implementation, and is very, very slow as compared to other
24 * software compressors. You probably do not want to use this software
25 * compression. If you have access to the PowerPC 842 compression hardware, you
26 * want to use the 842 hardware compression interface, which is at:
27 * drivers/crypto/nx/nx-842-crypto.c
Seth Jennings35a1fc12012-07-19 09:42:41 -050028 */
29
30#include <linux/init.h>
31#include <linux/module.h>
32#include <linux/crypto.h>
Dan Streetman2062c5b2015-05-07 13:49:15 -040033#include <linux/sw842.h>
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010034#include <crypto/internal/scompress.h>
Seth Jennings35a1fc12012-07-19 09:42:41 -050035
Dan Streetman2062c5b2015-05-07 13:49:15 -040036struct crypto842_ctx {
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010037 void *wmem; /* working memory for compress */
Seth Jennings35a1fc12012-07-19 09:42:41 -050038};
39
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010040static 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
Dan Streetman2062c5b2015-05-07 13:49:15 -040074static int crypto842_compress(struct crypto_tfm *tfm,
75 const u8 *src, unsigned int slen,
76 u8 *dst, unsigned int *dlen)
Seth Jennings35a1fc12012-07-19 09:42:41 -050077{
Dan Streetman2062c5b2015-05-07 13:49:15 -040078 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
Seth Jennings35a1fc12012-07-19 09:42:41 -050079
Dan Streetman2062c5b2015-05-07 13:49:15 -040080 return sw842_compress(src, slen, dst, dlen, ctx->wmem);
Seth Jennings35a1fc12012-07-19 09:42:41 -050081}
82
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010083static 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
Dan Streetman2062c5b2015-05-07 13:49:15 -040090static int crypto842_decompress(struct crypto_tfm *tfm,
91 const u8 *src, unsigned int slen,
92 u8 *dst, unsigned int *dlen)
Seth Jennings35a1fc12012-07-19 09:42:41 -050093{
Dan Streetman2062c5b2015-05-07 13:49:15 -040094 return sw842_decompress(src, slen, dst, dlen);
Seth Jennings35a1fc12012-07-19 09:42:41 -050095}
96
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010097static 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
Seth Jennings35a1fc12012-07-19 09:42:41 -0500104static struct crypto_alg alg = {
105 .cra_name = "842",
Dan Streetman2062c5b2015-05-07 13:49:15 -0400106 .cra_driver_name = "842-generic",
107 .cra_priority = 100,
Seth Jennings35a1fc12012-07-19 09:42:41 -0500108 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
Dan Streetman2062c5b2015-05-07 13:49:15 -0400109 .cra_ctxsize = sizeof(struct crypto842_ctx),
Seth Jennings35a1fc12012-07-19 09:42:41 -0500110 .cra_module = THIS_MODULE,
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100111 .cra_init = crypto842_init,
112 .cra_exit = crypto842_exit,
Seth Jennings35a1fc12012-07-19 09:42:41 -0500113 .cra_u = { .compress = {
Dan Streetman2062c5b2015-05-07 13:49:15 -0400114 .coa_compress = crypto842_compress,
115 .coa_decompress = crypto842_decompress } }
Seth Jennings35a1fc12012-07-19 09:42:41 -0500116};
117
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100118static 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
Dan Streetman2062c5b2015-05-07 13:49:15 -0400131static int __init crypto842_mod_init(void)
Seth Jennings35a1fc12012-07-19 09:42:41 -0500132{
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100133 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;
Seth Jennings35a1fc12012-07-19 09:42:41 -0500146}
Dan Streetman2062c5b2015-05-07 13:49:15 -0400147module_init(crypto842_mod_init);
Seth Jennings35a1fc12012-07-19 09:42:41 -0500148
Dan Streetman2062c5b2015-05-07 13:49:15 -0400149static void __exit crypto842_mod_exit(void)
Seth Jennings35a1fc12012-07-19 09:42:41 -0500150{
151 crypto_unregister_alg(&alg);
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100152 crypto_unregister_scomp(&scomp);
Seth Jennings35a1fc12012-07-19 09:42:41 -0500153}
Dan Streetman2062c5b2015-05-07 13:49:15 -0400154module_exit(crypto842_mod_exit);
Seth Jennings35a1fc12012-07-19 09:42:41 -0500155
156MODULE_LICENSE("GPL");
Dan Streetman2062c5b2015-05-07 13:49:15 -0400157MODULE_DESCRIPTION("842 Software Compression Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800158MODULE_ALIAS_CRYPTO("842");
Dan Streetman2062c5b2015-05-07 13:49:15 -0400159MODULE_ALIAS_CRYPTO("842-generic");
160MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");