1 // SPDX-License-Identifier: GPL-2.0-or-later << 2 /* 1 /* 3 * Cryptographic API for the 842 software comp 2 * Cryptographic API for the 842 software compression algorithm. 4 * 3 * >> 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 * 5 * Copyright (C) IBM Corporation, 2011-2015 14 * Copyright (C) IBM Corporation, 2011-2015 6 * 15 * 7 * Original Authors: Robert Jennings <rcj@linu 16 * Original Authors: Robert Jennings <rcj@linux.vnet.ibm.com> 8 * Seth Jennings <sjenning@l 17 * Seth Jennings <sjenning@linux.vnet.ibm.com> 9 * 18 * 10 * Rewrite: Dan Streetman <ddstreet@ieee.org> 19 * Rewrite: Dan Streetman <ddstreet@ieee.org> 11 * 20 * 12 * This is the software implementation of comp 21 * This is the software implementation of compression and decompression using 13 * the 842 format. This uses the software 842 22 * the 842 format. This uses the software 842 library at lib/842/ which is 14 * only a reference implementation, and is ver 23 * only a reference implementation, and is very, very slow as compared to other 15 * software compressors. You probably do not 24 * software compressors. You probably do not want to use this software 16 * compression. If you have access to the Pow 25 * compression. If you have access to the PowerPC 842 compression hardware, you 17 * want to use the 842 hardware compression in 26 * want to use the 842 hardware compression interface, which is at: 18 * drivers/crypto/nx/nx-842-crypto.c 27 * drivers/crypto/nx/nx-842-crypto.c 19 */ 28 */ 20 29 21 #include <linux/init.h> 30 #include <linux/init.h> 22 #include <linux/module.h> 31 #include <linux/module.h> 23 #include <linux/crypto.h> 32 #include <linux/crypto.h> 24 #include <linux/sw842.h> 33 #include <linux/sw842.h> 25 #include <crypto/internal/scompress.h> 34 #include <crypto/internal/scompress.h> 26 35 27 struct crypto842_ctx { 36 struct crypto842_ctx { 28 void *wmem; /* working memory for 37 void *wmem; /* working memory for compress */ 29 }; 38 }; 30 39 31 static void *crypto842_alloc_ctx(struct crypto 40 static void *crypto842_alloc_ctx(struct crypto_scomp *tfm) 32 { 41 { 33 void *ctx; 42 void *ctx; 34 43 35 ctx = kmalloc(SW842_MEM_COMPRESS, GFP_ 44 ctx = kmalloc(SW842_MEM_COMPRESS, GFP_KERNEL); 36 if (!ctx) 45 if (!ctx) 37 return ERR_PTR(-ENOMEM); 46 return ERR_PTR(-ENOMEM); 38 47 39 return ctx; 48 return ctx; 40 } 49 } 41 50 42 static int crypto842_init(struct crypto_tfm *t 51 static int crypto842_init(struct crypto_tfm *tfm) 43 { 52 { 44 struct crypto842_ctx *ctx = crypto_tfm 53 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm); 45 54 46 ctx->wmem = crypto842_alloc_ctx(NULL); 55 ctx->wmem = crypto842_alloc_ctx(NULL); 47 if (IS_ERR(ctx->wmem)) 56 if (IS_ERR(ctx->wmem)) 48 return -ENOMEM; 57 return -ENOMEM; 49 58 50 return 0; 59 return 0; 51 } 60 } 52 61 53 static void crypto842_free_ctx(struct crypto_s 62 static void crypto842_free_ctx(struct crypto_scomp *tfm, void *ctx) 54 { 63 { 55 kfree(ctx); 64 kfree(ctx); 56 } 65 } 57 66 58 static void crypto842_exit(struct crypto_tfm * 67 static void crypto842_exit(struct crypto_tfm *tfm) 59 { 68 { 60 struct crypto842_ctx *ctx = crypto_tfm 69 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm); 61 70 62 crypto842_free_ctx(NULL, ctx->wmem); 71 crypto842_free_ctx(NULL, ctx->wmem); 63 } 72 } 64 73 65 static int crypto842_compress(struct crypto_tf 74 static int crypto842_compress(struct crypto_tfm *tfm, 66 const u8 *src, u 75 const u8 *src, unsigned int slen, 67 u8 *dst, unsigne 76 u8 *dst, unsigned int *dlen) 68 { 77 { 69 struct crypto842_ctx *ctx = crypto_tfm 78 struct crypto842_ctx *ctx = crypto_tfm_ctx(tfm); 70 79 71 return sw842_compress(src, slen, dst, 80 return sw842_compress(src, slen, dst, dlen, ctx->wmem); 72 } 81 } 73 82 74 static int crypto842_scompress(struct crypto_s 83 static int crypto842_scompress(struct crypto_scomp *tfm, 75 const u8 *src, 84 const u8 *src, unsigned int slen, 76 u8 *dst, unsign 85 u8 *dst, unsigned int *dlen, void *ctx) 77 { 86 { 78 return sw842_compress(src, slen, dst, 87 return sw842_compress(src, slen, dst, dlen, ctx); 79 } 88 } 80 89 81 static int crypto842_decompress(struct crypto_ 90 static int crypto842_decompress(struct crypto_tfm *tfm, 82 const u8 *src, 91 const u8 *src, unsigned int slen, 83 u8 *dst, unsig 92 u8 *dst, unsigned int *dlen) 84 { 93 { 85 return sw842_decompress(src, slen, dst 94 return sw842_decompress(src, slen, dst, dlen); 86 } 95 } 87 96 88 static int crypto842_sdecompress(struct crypto 97 static int crypto842_sdecompress(struct crypto_scomp *tfm, 89 const u8 *src 98 const u8 *src, unsigned int slen, 90 u8 *dst, unsi 99 u8 *dst, unsigned int *dlen, void *ctx) 91 { 100 { 92 return sw842_decompress(src, slen, dst 101 return sw842_decompress(src, slen, dst, dlen); 93 } 102 } 94 103 95 static struct crypto_alg alg = { 104 static struct crypto_alg alg = { 96 .cra_name = "842", 105 .cra_name = "842", 97 .cra_driver_name = "842-generic 106 .cra_driver_name = "842-generic", 98 .cra_priority = 100, 107 .cra_priority = 100, 99 .cra_flags = CRYPTO_ALG_T 108 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, 100 .cra_ctxsize = sizeof(struc 109 .cra_ctxsize = sizeof(struct crypto842_ctx), 101 .cra_module = THIS_MODULE, 110 .cra_module = THIS_MODULE, 102 .cra_init = crypto842_in 111 .cra_init = crypto842_init, 103 .cra_exit = crypto842_ex 112 .cra_exit = crypto842_exit, 104 .cra_u = { .compress 113 .cra_u = { .compress = { 105 .coa_compress = crypto842_co 114 .coa_compress = crypto842_compress, 106 .coa_decompress = crypto842_de 115 .coa_decompress = crypto842_decompress } } 107 }; 116 }; 108 117 109 static struct scomp_alg scomp = { 118 static struct scomp_alg scomp = { 110 .alloc_ctx = crypto842_al 119 .alloc_ctx = crypto842_alloc_ctx, 111 .free_ctx = crypto842_fr 120 .free_ctx = crypto842_free_ctx, 112 .compress = crypto842_sc 121 .compress = crypto842_scompress, 113 .decompress = crypto842_sd 122 .decompress = crypto842_sdecompress, 114 .base = { 123 .base = { 115 .cra_name = "842", 124 .cra_name = "842", 116 .cra_driver_name = "842-scomp" 125 .cra_driver_name = "842-scomp", 117 .cra_priority = 100, 126 .cra_priority = 100, 118 .cra_module = THIS_MODULE 127 .cra_module = THIS_MODULE, 119 } 128 } 120 }; 129 }; 121 130 122 static int __init crypto842_mod_init(void) 131 static int __init crypto842_mod_init(void) 123 { 132 { 124 int ret; 133 int ret; 125 134 126 ret = crypto_register_alg(&alg); 135 ret = crypto_register_alg(&alg); 127 if (ret) 136 if (ret) 128 return ret; 137 return ret; 129 138 130 ret = crypto_register_scomp(&scomp); 139 ret = crypto_register_scomp(&scomp); 131 if (ret) { 140 if (ret) { 132 crypto_unregister_alg(&alg); 141 crypto_unregister_alg(&alg); 133 return ret; 142 return ret; 134 } 143 } 135 144 136 return ret; 145 return ret; 137 } 146 } 138 subsys_initcall(crypto842_mod_init); !! 147 module_init(crypto842_mod_init); 139 148 140 static void __exit crypto842_mod_exit(void) 149 static void __exit crypto842_mod_exit(void) 141 { 150 { 142 crypto_unregister_alg(&alg); 151 crypto_unregister_alg(&alg); 143 crypto_unregister_scomp(&scomp); 152 crypto_unregister_scomp(&scomp); 144 } 153 } 145 module_exit(crypto842_mod_exit); 154 module_exit(crypto842_mod_exit); 146 155 147 MODULE_LICENSE("GPL"); 156 MODULE_LICENSE("GPL"); 148 MODULE_DESCRIPTION("842 Software Compression A 157 MODULE_DESCRIPTION("842 Software Compression Algorithm"); 149 MODULE_ALIAS_CRYPTO("842"); 158 MODULE_ALIAS_CRYPTO("842"); 150 MODULE_ALIAS_CRYPTO("842-generic"); 159 MODULE_ALIAS_CRYPTO("842-generic"); 151 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.or 160 MODULE_AUTHOR("Dan Streetman <ddstreet@ieee.org>"); 152 161
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.