Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 2 | /* |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 3 | * Cryptographic API for the 842 software compression algorithm. |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 4 | * |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 5 | * Copyright (C) IBM Corporation, 2011-2015 |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 6 | * |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 7 | * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com> |
| 8 | * Seth Jennings <sjenning@linux.vnet.ibm.com> |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 9 | * |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 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 |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/init.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/crypto.h> |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 24 | #include <linux/sw842.h> |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 25 | #include <crypto/internal/scompress.h> |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 26 | |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 27 | struct crypto842_ctx { |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 28 | void *wmem; /* working memory for compress */ |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 29 | }; |
| 30 | |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 31 | static 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 | |
| 42 | static 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 | |
| 53 | static void crypto842_free_ctx(struct crypto_scomp *tfm, void *ctx) |
| 54 | { |
| 55 | kfree(ctx); |
| 56 | } |
| 57 | |
| 58 | static 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 Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 65 | static int crypto842_compress(struct crypto_tfm *tfm, |
| 66 | const u8 *src, unsigned int slen, |
| 67 | u8 *dst, unsigned int *dlen) |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 68 | { |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 69 | struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm); |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 70 | |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 71 | return sw842_compress(src, slen, dst, dlen, ctx->wmem); |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 74 | static 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 Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 81 | static int crypto842_decompress(struct crypto_tfm *tfm, |
| 82 | const u8 *src, unsigned int slen, |
| 83 | u8 *dst, unsigned int *dlen) |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 84 | { |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 85 | return sw842_decompress(src, slen, dst, dlen); |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 88 | static 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 Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 95 | static struct crypto_alg alg = { |
| 96 | .cra_name = "842", |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 97 | .cra_driver_name = "842-generic", |
| 98 | .cra_priority = 100, |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 99 | .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 100 | .cra_ctxsize = sizeof(struct crypto842_ctx), |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 101 | .cra_module = THIS_MODULE, |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 102 | .cra_init = crypto842_init, |
| 103 | .cra_exit = crypto842_exit, |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 104 | .cra_u = { .compress = { |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 105 | .coa_compress = crypto842_compress, |
| 106 | .coa_decompress = crypto842_decompress } } |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 107 | }; |
| 108 | |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 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, |
| 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 Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 122 | static int __init crypto842_mod_init(void) |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 123 | { |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 124 | 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 Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 137 | } |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 138 | subsys_initcall(crypto842_mod_init); |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 139 | |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 140 | static void __exit crypto842_mod_exit(void) |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 141 | { |
| 142 | crypto_unregister_alg(&alg); |
Giovanni Cabiddu | 6a8de3a | 2016-10-21 13:19:52 +0100 | [diff] [blame] | 143 | crypto_unregister_scomp(&scomp); |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 144 | } |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 145 | module_exit(crypto842_mod_exit); |
Seth Jennings | 35a1fc1 | 2012-07-19 09:42:41 -0500 | [diff] [blame] | 146 | |
| 147 | MODULE_LICENSE("GPL"); |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 148 | MODULE_DESCRIPTION("842 Software Compression Algorithm"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 149 | MODULE_ALIAS_CRYPTO("842"); |
Dan Streetman | 2062c5b | 2015-05-07 13:49:15 -0400 | [diff] [blame] | 150 | MODULE_ALIAS_CRYPTO("842-generic"); |
| 151 | MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); |