1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Cryptographic API for the 842 software compression algorithm.
5 * Copyright (C) IBM Corporation, 2011-2015
7 * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com>
8 * Seth Jennings <sjenning@linux.vnet.ibm.com>
10 * Rewrite: Dan Streetman <ddstreet@ieee.org>
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
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/crypto.h>
24 #include <linux/sw842.h>
25 #include <crypto/internal/scompress.h>
27 struct crypto842_ctx
{
28 void *wmem
; /* working memory for compress */
31 static void *crypto842_alloc_ctx(struct crypto_scomp
*tfm
)
35 ctx
= kmalloc(SW842_MEM_COMPRESS
, GFP_KERNEL
);
37 return ERR_PTR(-ENOMEM
);
42 static int crypto842_init(struct crypto_tfm
*tfm
)
44 struct crypto842_ctx
*ctx
= crypto_tfm_ctx(tfm
);
46 ctx
->wmem
= crypto842_alloc_ctx(NULL
);
47 if (IS_ERR(ctx
->wmem
))
53 static void crypto842_free_ctx(struct crypto_scomp
*tfm
, void *ctx
)
58 static void crypto842_exit(struct crypto_tfm
*tfm
)
60 struct crypto842_ctx
*ctx
= crypto_tfm_ctx(tfm
);
62 crypto842_free_ctx(NULL
, ctx
->wmem
);
65 static int crypto842_compress(struct crypto_tfm
*tfm
,
66 const u8
*src
, unsigned int slen
,
67 u8
*dst
, unsigned int *dlen
)
69 struct crypto842_ctx
*ctx
= crypto_tfm_ctx(tfm
);
71 return sw842_compress(src
, slen
, dst
, dlen
, ctx
->wmem
);
74 static int crypto842_scompress(struct crypto_scomp
*tfm
,
75 const u8
*src
, unsigned int slen
,
76 u8
*dst
, unsigned int *dlen
, void *ctx
)
78 return sw842_compress(src
, slen
, dst
, dlen
, ctx
);
81 static int crypto842_decompress(struct crypto_tfm
*tfm
,
82 const u8
*src
, unsigned int slen
,
83 u8
*dst
, unsigned int *dlen
)
85 return sw842_decompress(src
, slen
, dst
, dlen
);
88 static int crypto842_sdecompress(struct crypto_scomp
*tfm
,
89 const u8
*src
, unsigned int slen
,
90 u8
*dst
, unsigned int *dlen
, void *ctx
)
92 return sw842_decompress(src
, slen
, dst
, dlen
);
95 static struct crypto_alg alg
= {
97 .cra_driver_name
= "842-generic",
99 .cra_flags
= CRYPTO_ALG_TYPE_COMPRESS
,
100 .cra_ctxsize
= sizeof(struct crypto842_ctx
),
101 .cra_module
= THIS_MODULE
,
102 .cra_init
= crypto842_init
,
103 .cra_exit
= crypto842_exit
,
104 .cra_u
= { .compress
= {
105 .coa_compress
= crypto842_compress
,
106 .coa_decompress
= crypto842_decompress
} }
109 static struct scomp_alg scomp
= {
110 .alloc_ctx
= crypto842_alloc_ctx
,
111 .free_ctx
= crypto842_free_ctx
,
112 .compress
= crypto842_scompress
,
113 .decompress
= crypto842_sdecompress
,
116 .cra_driver_name
= "842-scomp",
118 .cra_module
= THIS_MODULE
,
122 static int __init
crypto842_mod_init(void)
126 ret
= crypto_register_alg(&alg
);
130 ret
= crypto_register_scomp(&scomp
);
132 crypto_unregister_alg(&alg
);
138 subsys_initcall(crypto842_mod_init
);
140 static void __exit
crypto842_mod_exit(void)
142 crypto_unregister_alg(&alg
);
143 crypto_unregister_scomp(&scomp
);
145 module_exit(crypto842_mod_exit
);
147 MODULE_LICENSE("GPL");
148 MODULE_DESCRIPTION("842 Software Compression Algorithm");
149 MODULE_ALIAS_CRYPTO("842");
150 MODULE_ALIAS_CRYPTO("842-generic");
151 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>");