1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * Cryptographic API. 2 * Cryptographic API. 4 * 3 * 5 * Copyright (c) 2013 Chanho Min <chanho.min@l 4 * Copyright (c) 2013 Chanho Min <chanho.min@lge.com> >> 5 * >> 6 * This program is free software; you can redistribute it and/or modify it >> 7 * under the terms of the GNU General Public License version 2 as published by >> 8 * the Free Software Foundation. >> 9 * >> 10 * This program is distributed in the hope that it will be useful, but WITHOUT >> 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or >> 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for >> 13 * more details. >> 14 * >> 15 * You should have received a copy of the GNU General Public License along with >> 16 * this program; if not, write to the Free Software Foundation, Inc., 51 >> 17 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA >> 18 * 6 */ 19 */ 7 #include <linux/init.h> 20 #include <linux/init.h> 8 #include <linux/module.h> 21 #include <linux/module.h> 9 #include <linux/crypto.h> 22 #include <linux/crypto.h> 10 #include <linux/vmalloc.h> 23 #include <linux/vmalloc.h> 11 #include <linux/lz4.h> 24 #include <linux/lz4.h> 12 #include <crypto/internal/scompress.h> 25 #include <crypto/internal/scompress.h> 13 26 14 struct lz4hc_ctx { 27 struct lz4hc_ctx { 15 void *lz4hc_comp_mem; 28 void *lz4hc_comp_mem; 16 }; 29 }; 17 30 18 static void *lz4hc_alloc_ctx(struct crypto_sco 31 static void *lz4hc_alloc_ctx(struct crypto_scomp *tfm) 19 { 32 { 20 void *ctx; 33 void *ctx; 21 34 22 ctx = vmalloc(LZ4HC_MEM_COMPRESS); 35 ctx = vmalloc(LZ4HC_MEM_COMPRESS); 23 if (!ctx) 36 if (!ctx) 24 return ERR_PTR(-ENOMEM); 37 return ERR_PTR(-ENOMEM); 25 38 26 return ctx; 39 return ctx; 27 } 40 } 28 41 29 static int lz4hc_init(struct crypto_tfm *tfm) 42 static int lz4hc_init(struct crypto_tfm *tfm) 30 { 43 { 31 struct lz4hc_ctx *ctx = crypto_tfm_ctx 44 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 32 45 33 ctx->lz4hc_comp_mem = lz4hc_alloc_ctx( 46 ctx->lz4hc_comp_mem = lz4hc_alloc_ctx(NULL); 34 if (IS_ERR(ctx->lz4hc_comp_mem)) 47 if (IS_ERR(ctx->lz4hc_comp_mem)) 35 return -ENOMEM; 48 return -ENOMEM; 36 49 37 return 0; 50 return 0; 38 } 51 } 39 52 40 static void lz4hc_free_ctx(struct crypto_scomp 53 static void lz4hc_free_ctx(struct crypto_scomp *tfm, void *ctx) 41 { 54 { 42 vfree(ctx); 55 vfree(ctx); 43 } 56 } 44 57 45 static void lz4hc_exit(struct crypto_tfm *tfm) 58 static void lz4hc_exit(struct crypto_tfm *tfm) 46 { 59 { 47 struct lz4hc_ctx *ctx = crypto_tfm_ctx 60 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 48 61 49 lz4hc_free_ctx(NULL, ctx->lz4hc_comp_m 62 lz4hc_free_ctx(NULL, ctx->lz4hc_comp_mem); 50 } 63 } 51 64 52 static int __lz4hc_compress_crypto(const u8 *s 65 static int __lz4hc_compress_crypto(const u8 *src, unsigned int slen, 53 u8 *dst, un 66 u8 *dst, unsigned int *dlen, void *ctx) 54 { 67 { 55 int out_len = LZ4_compress_HC(src, dst 68 int out_len = LZ4_compress_HC(src, dst, slen, 56 *dlen, LZ4HC_DEFAULT_CLEVEL, c 69 *dlen, LZ4HC_DEFAULT_CLEVEL, ctx); 57 70 58 if (!out_len) 71 if (!out_len) 59 return -EINVAL; 72 return -EINVAL; 60 73 61 *dlen = out_len; 74 *dlen = out_len; 62 return 0; 75 return 0; 63 } 76 } 64 77 65 static int lz4hc_scompress(struct crypto_scomp 78 static int lz4hc_scompress(struct crypto_scomp *tfm, const u8 *src, 66 unsigned int slen, 79 unsigned int slen, u8 *dst, unsigned int *dlen, 67 void *ctx) 80 void *ctx) 68 { 81 { 69 return __lz4hc_compress_crypto(src, sl 82 return __lz4hc_compress_crypto(src, slen, dst, dlen, ctx); 70 } 83 } 71 84 72 static int lz4hc_compress_crypto(struct crypto 85 static int lz4hc_compress_crypto(struct crypto_tfm *tfm, const u8 *src, 73 unsigned int 86 unsigned int slen, u8 *dst, 74 unsigned int 87 unsigned int *dlen) 75 { 88 { 76 struct lz4hc_ctx *ctx = crypto_tfm_ctx 89 struct lz4hc_ctx *ctx = crypto_tfm_ctx(tfm); 77 90 78 return __lz4hc_compress_crypto(src, sl 91 return __lz4hc_compress_crypto(src, slen, dst, dlen, 79 ctx->l 92 ctx->lz4hc_comp_mem); 80 } 93 } 81 94 82 static int __lz4hc_decompress_crypto(const u8 95 static int __lz4hc_decompress_crypto(const u8 *src, unsigned int slen, 83 u8 *dst, 96 u8 *dst, unsigned int *dlen, void *ctx) 84 { 97 { 85 int out_len = LZ4_decompress_safe(src, 98 int out_len = LZ4_decompress_safe(src, dst, slen, *dlen); 86 99 87 if (out_len < 0) 100 if (out_len < 0) 88 return -EINVAL; !! 101 return out_len; 89 102 90 *dlen = out_len; 103 *dlen = out_len; 91 return 0; 104 return 0; 92 } 105 } 93 106 94 static int lz4hc_sdecompress(struct crypto_sco 107 static int lz4hc_sdecompress(struct crypto_scomp *tfm, const u8 *src, 95 unsigned int slen 108 unsigned int slen, u8 *dst, unsigned int *dlen, 96 void *ctx) 109 void *ctx) 97 { 110 { 98 return __lz4hc_decompress_crypto(src, 111 return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL); 99 } 112 } 100 113 101 static int lz4hc_decompress_crypto(struct cryp 114 static int lz4hc_decompress_crypto(struct crypto_tfm *tfm, const u8 *src, 102 unsigned in 115 unsigned int slen, u8 *dst, 103 unsigned in 116 unsigned int *dlen) 104 { 117 { 105 return __lz4hc_decompress_crypto(src, 118 return __lz4hc_decompress_crypto(src, slen, dst, dlen, NULL); 106 } 119 } 107 120 108 static struct crypto_alg alg_lz4hc = { 121 static struct crypto_alg alg_lz4hc = { 109 .cra_name = "lz4hc", 122 .cra_name = "lz4hc", 110 .cra_driver_name = "lz4hc-gener << 111 .cra_flags = CRYPTO_ALG_T 123 .cra_flags = CRYPTO_ALG_TYPE_COMPRESS, 112 .cra_ctxsize = sizeof(struc 124 .cra_ctxsize = sizeof(struct lz4hc_ctx), 113 .cra_module = THIS_MODULE, 125 .cra_module = THIS_MODULE, >> 126 .cra_list = LIST_HEAD_INIT(alg_lz4hc.cra_list), 114 .cra_init = lz4hc_init, 127 .cra_init = lz4hc_init, 115 .cra_exit = lz4hc_exit, 128 .cra_exit = lz4hc_exit, 116 .cra_u = { .compress 129 .cra_u = { .compress = { 117 .coa_compress = lz4hc_compre 130 .coa_compress = lz4hc_compress_crypto, 118 .coa_decompress = lz4hc_decomp 131 .coa_decompress = lz4hc_decompress_crypto } } 119 }; 132 }; 120 133 121 static struct scomp_alg scomp = { 134 static struct scomp_alg scomp = { 122 .alloc_ctx = lz4hc_alloc_ 135 .alloc_ctx = lz4hc_alloc_ctx, 123 .free_ctx = lz4hc_free_c 136 .free_ctx = lz4hc_free_ctx, 124 .compress = lz4hc_scompr 137 .compress = lz4hc_scompress, 125 .decompress = lz4hc_sdecom 138 .decompress = lz4hc_sdecompress, 126 .base = { 139 .base = { 127 .cra_name = "lz4hc", 140 .cra_name = "lz4hc", 128 .cra_driver_name = "lz4hc-scom 141 .cra_driver_name = "lz4hc-scomp", 129 .cra_module = THIS_MODULE 142 .cra_module = THIS_MODULE, 130 } 143 } 131 }; 144 }; 132 145 133 static int __init lz4hc_mod_init(void) 146 static int __init lz4hc_mod_init(void) 134 { 147 { 135 int ret; 148 int ret; 136 149 137 ret = crypto_register_alg(&alg_lz4hc); 150 ret = crypto_register_alg(&alg_lz4hc); 138 if (ret) 151 if (ret) 139 return ret; 152 return ret; 140 153 141 ret = crypto_register_scomp(&scomp); 154 ret = crypto_register_scomp(&scomp); 142 if (ret) { 155 if (ret) { 143 crypto_unregister_alg(&alg_lz4 156 crypto_unregister_alg(&alg_lz4hc); 144 return ret; 157 return ret; 145 } 158 } 146 159 147 return ret; 160 return ret; 148 } 161 } 149 162 150 static void __exit lz4hc_mod_fini(void) 163 static void __exit lz4hc_mod_fini(void) 151 { 164 { 152 crypto_unregister_alg(&alg_lz4hc); 165 crypto_unregister_alg(&alg_lz4hc); 153 crypto_unregister_scomp(&scomp); 166 crypto_unregister_scomp(&scomp); 154 } 167 } 155 168 156 subsys_initcall(lz4hc_mod_init); !! 169 module_init(lz4hc_mod_init); 157 module_exit(lz4hc_mod_fini); 170 module_exit(lz4hc_mod_fini); 158 171 159 MODULE_LICENSE("GPL"); 172 MODULE_LICENSE("GPL"); 160 MODULE_DESCRIPTION("LZ4HC Compression Algorith 173 MODULE_DESCRIPTION("LZ4HC Compression Algorithm"); 161 MODULE_ALIAS_CRYPTO("lz4hc"); 174 MODULE_ALIAS_CRYPTO("lz4hc"); 162 175
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.