1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Cryptographic API for the 842 software compression algorithm.
4  *
5  * Copyright (C) IBM Corporation, 2011-2015
6  *
7  * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
8  *                   Seth Jennings <sjenning@linux.vnet.ibm.com>
9  *
10  * 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
19  */
20 
21 #include <crypto/internal/scompress.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/sw842.h>
25 
26 struct crypto842_ctx {
27 	void *wmem;	/* working memory for compress */
28 };
29 
crypto842_alloc_ctx(void)30 static void *crypto842_alloc_ctx(void)
31 {
32 	void *ctx;
33 
34 	ctx = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL);
35 	if (!ctx)
36 		return ERR_PTR(-ENOMEM);
37 
38 	return ctx;
39 }
40 
crypto842_free_ctx(void * ctx)41 static void crypto842_free_ctx(void *ctx)
42 {
43 	kfree(ctx);
44 }
45 
crypto842_scompress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)46 static int crypto842_scompress(struct crypto_scomp *tfm,
47 			       const u8 *src, unsigned int slen,
48 			       u8 *dst, unsigned int *dlen, void *ctx)
49 {
50 	return sw842_compress(src, slen, dst, dlen, ctx);
51 }
52 
crypto842_sdecompress(struct crypto_scomp * tfm,const u8 * src,unsigned int slen,u8 * dst,unsigned int * dlen,void * ctx)53 static int crypto842_sdecompress(struct crypto_scomp *tfm,
54 				 const u8 *src, unsigned int slen,
55 				 u8 *dst, unsigned int *dlen, void *ctx)
56 {
57 	return sw842_decompress(src, slen, dst, dlen);
58 }
59 
60 static struct scomp_alg scomp = {
61 	.alloc_ctx		= crypto842_alloc_ctx,
62 	.free_ctx		= crypto842_free_ctx,
63 	.compress		= crypto842_scompress,
64 	.decompress		= crypto842_sdecompress,
65 	.base			= {
66 		.cra_name	= "842",
67 		.cra_driver_name = "842-scomp",
68 		.cra_priority	 = 100,
69 		.cra_module	 = THIS_MODULE,
70 	}
71 };
72 
crypto842_mod_init(void)73 static int __init crypto842_mod_init(void)
74 {
75 	return crypto_register_scomp(&scomp);
76 }
77 subsys_initcall(crypto842_mod_init);
78 
crypto842_mod_exit(void)79 static void __exit crypto842_mod_exit(void)
80 {
81 	crypto_unregister_scomp(&scomp);
82 }
83 module_exit(crypto842_mod_exit);
84 
85 MODULE_LICENSE("GPL");
86 MODULE_DESCRIPTION("842 Software Compression Algorithm");
87 MODULE_ALIAS_CRYPTO("842");
88 MODULE_ALIAS_CRYPTO("842-generic");
89 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");
90