blob: e59e54d769609ba6a881fcdfe6ada502d7627d27 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Seth Jennings35a1fc12012-07-19 09:42:41 -05002/*
Dan Streetman2062c5b2015-05-07 13:49:15 -04003 * Cryptographic API for the 842 software compression algorithm.
Seth Jennings35a1fc12012-07-19 09:42:41 -05004 *
Dan Streetman2062c5b2015-05-07 13:49:15 -04005 * Copyright (C) IBM Corporation, 2011-2015
Seth Jennings35a1fc12012-07-19 09:42:41 -05006 *
Dan Streetman2062c5b2015-05-07 13:49:15 -04007 * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
8 * Seth Jennings <sjenning@linux.vnet.ibm.com>
Seth Jennings35a1fc12012-07-19 09:42:41 -05009 *
Dan Streetman2062c5b2015-05-07 13:49:15 -040010 * Rewrite: Dan Streetman <ddstreet@ieee.org>
11 *
12 * This is the software implementation of compression and decompression using
13 * the 842 format. This uses the software 842 library at lib/842/ which is
14 * only a reference implementation, and is very, very slow as compared to other
15 * software compressors. You probably do not want to use this software
16 * compression. If you have access to the PowerPC 842 compression hardware, you
17 * want to use the 842 hardware compression interface, which is at:
18 * drivers/crypto/nx/nx-842-crypto.c
Seth Jennings35a1fc12012-07-19 09:42:41 -050019 */
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/crypto.h>
Dan Streetman2062c5b2015-05-07 13:49:15 -040024#include <linux/sw842.h>
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010025#include <crypto/internal/scompress.h>
Seth Jennings35a1fc12012-07-19 09:42:41 -050026
Dan Streetman2062c5b2015-05-07 13:49:15 -040027struct crypto842_ctx {
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010028 void *wmem; /* working memory for compress */
Seth Jennings35a1fc12012-07-19 09:42:41 -050029};
30
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010031static void *crypto842_alloc_ctx(struct crypto_scomp *tfm)
32{
33 void *ctx;
34
35 ctx = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
36 if (!ctx)
37 return ERR_PTR(-ENOMEM);
38
39 return ctx;
40}
41
42static int crypto842_init(struct crypto_tfm *tfm)
43{
44 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
45
46 ctx->wmem = crypto842_alloc_ctx(NULL);
47 if (IS_ERR(ctx->wmem))
48 return -ENOMEM;
49
50 return 0;
51}
52
53static void crypto842_free_ctx(struct crypto_scomp *tfm, void *ctx)
54{
55 kfree(ctx);
56}
57
58static void crypto842_exit(struct crypto_tfm *tfm)
59{
60 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
61
62 crypto842_free_ctx(NULL, ctx->wmem);
63}
64
Dan Streetman2062c5b2015-05-07 13:49:15 -040065static int crypto842_compress(struct crypto_tfm *tfm,
66 const u8 *src, unsigned int slen,
67 u8 *dst, unsigned int *dlen)
Seth Jennings35a1fc12012-07-19 09:42:41 -050068{
Dan Streetman2062c5b2015-05-07 13:49:15 -040069 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm);
Seth Jennings35a1fc12012-07-19 09:42:41 -050070
Dan Streetman2062c5b2015-05-07 13:49:15 -040071 return sw842_compress(src, slen, dst, dlen, ctx->wmem);
Seth Jennings35a1fc12012-07-19 09:42:41 -050072}
73
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010074static int crypto842_scompress(struct crypto_scomp *tfm,
75 const u8 *src, unsigned int slen,
76 u8 *dst, unsigned int *dlen, void *ctx)
77{
78 return sw842_compress(src, slen, dst, dlen, ctx);
79}
80
Dan Streetman2062c5b2015-05-07 13:49:15 -040081static int crypto842_decompress(struct crypto_tfm *tfm,
82 const u8 *src, unsigned int slen,
83 u8 *dst, unsigned int *dlen)
Seth Jennings35a1fc12012-07-19 09:42:41 -050084{
Dan Streetman2062c5b2015-05-07 13:49:15 -040085 return sw842_decompress(src, slen, dst, dlen);
Seth Jennings35a1fc12012-07-19 09:42:41 -050086}
87
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +010088static int crypto842_sdecompress(struct crypto_scomp *tfm,
89 const u8 *src, unsigned int slen,
90 u8 *dst, unsigned int *dlen, void *ctx)
91{
92 return sw842_decompress(src, slen, dst, dlen);
93}
94
Seth Jennings35a1fc12012-07-19 09:42:41 -050095static struct crypto_alg alg = {
96 .cra_name = "842",
Dan Streetman2062c5b2015-05-07 13:49:15 -040097 .cra_driver_name = "842-generic",
98 .cra_priority = 100,
Seth Jennings35a1fc12012-07-19 09:42:41 -050099 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS,
Dan Streetman2062c5b2015-05-07 13:49:15 -0400100 .cra_ctxsize = sizeof(struct crypto842_ctx),
Seth Jennings35a1fc12012-07-19 09:42:41 -0500101 .cra_module = THIS_MODULE,
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100102 .cra_init = crypto842_init,
103 .cra_exit = crypto842_exit,
Seth Jennings35a1fc12012-07-19 09:42:41 -0500104 .cra_u = { .compress = {
Dan Streetman2062c5b2015-05-07 13:49:15 -0400105 .coa_compress = crypto842_compress,
106 .coa_decompress = crypto842_decompress } }
Seth Jennings35a1fc12012-07-19 09:42:41 -0500107};
108
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100109static struct scomp_alg scomp = {
110 .alloc_ctx = crypto842_alloc_ctx,
111 .free_ctx = crypto842_free_ctx,
112 .compress = crypto842_scompress,
113 .decompress = crypto842_sdecompress,
114 .base = {
115 .cra_name = "842",
116 .cra_driver_name = "842-scomp",
117 .cra_priority = 100,
118 .cra_module = THIS_MODULE,
119 }
120};
121
Dan Streetman2062c5b2015-05-07 13:49:15 -0400122static int __init crypto842_mod_init(void)
Seth Jennings35a1fc12012-07-19 09:42:41 -0500123{
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100124 int ret;
125
126 ret = crypto_register_alg(&alg);
127 if (ret)
128 return ret;
129
130 ret = crypto_register_scomp(&scomp);
131 if (ret) {
132 crypto_unregister_alg(&alg);
133 return ret;
134 }
135
136 return ret;
Seth Jennings35a1fc12012-07-19 09:42:41 -0500137}
Eric Biggersc4741b22019-04-11 21:57:42 -0700138subsys_initcall(crypto842_mod_init);
Seth Jennings35a1fc12012-07-19 09:42:41 -0500139
Dan Streetman2062c5b2015-05-07 13:49:15 -0400140static void __exit crypto842_mod_exit(void)
Seth Jennings35a1fc12012-07-19 09:42:41 -0500141{
142 crypto_unregister_alg(&alg);
Giovanni Cabiddu6a8de3a2016-10-21 13:19:52 +0100143 crypto_unregister_scomp(&scomp);
Seth Jennings35a1fc12012-07-19 09:42:41 -0500144}
Dan Streetman2062c5b2015-05-07 13:49:15 -0400145module_exit(crypto842_mod_exit);
Seth Jennings35a1fc12012-07-19 09:42:41 -0500146
147MODULE_LICENSE("GPL");
Dan Streetman2062c5b2015-05-07 13:49:15 -0400148MODULE_DESCRIPTION("842 Software Compression Algorithm");
Kees Cook5d26a102014-11-20 17:05:53 -0800149MODULE_ALIAS_CRYPTO("842");
Dan Streetman2062c5b2015-05-07 13:49:15 -0400150MODULE_ALIAS_CRYPTO("842-generic");
151MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");