1 // SPDX-License-Identifier: GPL-2.0 1 2 /* 3 * Crypto-API module for CRC-32 algorithms imp 4 * z/Architecture Vector Extension Facility. 5 * 6 * Copyright IBM Corp. 2015 7 * Author(s): Hendrik Brueckner <brueckner@lin 8 */ 9 #define KMSG_COMPONENT "crc32-vx" 10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fm 11 12 #include <linux/module.h> 13 #include <linux/cpufeature.h> 14 #include <linux/crc32.h> 15 #include <crypto/internal/hash.h> 16 #include <asm/fpu.h> 17 #include "crc32-vx.h" 18 19 #define CRC32_BLOCK_SIZE 1 20 #define CRC32_DIGEST_SIZE 4 21 22 #define VX_MIN_LEN 64 23 #define VX_ALIGNMENT 16L 24 #define VX_ALIGN_MASK (VX_ALIGNMENT 25 26 struct crc_ctx { 27 u32 key; 28 }; 29 30 struct crc_desc_ctx { 31 u32 crc; 32 }; 33 34 /* 35 * DEFINE_CRC32_VX() - Define a CRC-32 functio 36 * 37 * Creates a function to perform a particular 38 * on the message buffer, the hardware-acceler 39 * is used. Note that the message buffer is 40 * operations of VECTOR LOAD MULTIPLE instruct 41 * 42 */ 43 #define DEFINE_CRC32_VX(___fname, ___crc32_vx, 44 static u32 __pure ___fname(u32 crc, 45 unsigned char 46 { 47 unsigned long prealign, aligne 48 DECLARE_KERNEL_FPU_ONSTACK16(v 49 50 if (datalen < VX_MIN_LEN + VX_ 51 return ___crc32_sw(crc 52 53 if ((unsigned long)data & VX_A 54 prealign = VX_ALIGNMEN 55 ((unsigned l 56 datalen -= prealign; 57 crc = ___crc32_sw(crc, 58 data = (void *)((unsig 59 } 60 61 aligned = datalen & ~VX_ALIGN_ 62 remaining = datalen & VX_ALIGN 63 64 kernel_fpu_begin(&vxstate, KER 65 crc = ___crc32_vx(crc, data, a 66 kernel_fpu_end(&vxstate, KERNE 67 68 if (remaining) 69 crc = ___crc32_sw(crc, 70 71 return crc; 72 } 73 74 DEFINE_CRC32_VX(crc32_le_vx, crc32_le_vgfm_16, 75 DEFINE_CRC32_VX(crc32_be_vx, crc32_be_vgfm_16, 76 DEFINE_CRC32_VX(crc32c_le_vx, crc32c_le_vgfm_1 77 78 79 static int crc32_vx_cra_init_zero(struct crypt 80 { 81 struct crc_ctx *mctx = crypto_tfm_ctx( 82 83 mctx->key = 0; 84 return 0; 85 } 86 87 static int crc32_vx_cra_init_invert(struct cry 88 { 89 struct crc_ctx *mctx = crypto_tfm_ctx( 90 91 mctx->key = ~0; 92 return 0; 93 } 94 95 static int crc32_vx_init(struct shash_desc *de 96 { 97 struct crc_ctx *mctx = crypto_shash_ct 98 struct crc_desc_ctx *ctx = shash_desc_ 99 100 ctx->crc = mctx->key; 101 return 0; 102 } 103 104 static int crc32_vx_setkey(struct crypto_shash 105 unsigned int newkey 106 { 107 struct crc_ctx *mctx = crypto_shash_ct 108 109 if (newkeylen != sizeof(mctx->key)) 110 return -EINVAL; 111 mctx->key = le32_to_cpu(*(__le32 *)new 112 return 0; 113 } 114 115 static int crc32be_vx_setkey(struct crypto_sha 116 unsigned int newk 117 { 118 struct crc_ctx *mctx = crypto_shash_ct 119 120 if (newkeylen != sizeof(mctx->key)) 121 return -EINVAL; 122 mctx->key = be32_to_cpu(*(__be32 *)new 123 return 0; 124 } 125 126 static int crc32le_vx_final(struct shash_desc 127 { 128 struct crc_desc_ctx *ctx = shash_desc_ 129 130 *(__le32 *)out = cpu_to_le32p(&ctx->cr 131 return 0; 132 } 133 134 static int crc32be_vx_final(struct shash_desc 135 { 136 struct crc_desc_ctx *ctx = shash_desc_ 137 138 *(__be32 *)out = cpu_to_be32p(&ctx->cr 139 return 0; 140 } 141 142 static int crc32c_vx_final(struct shash_desc * 143 { 144 struct crc_desc_ctx *ctx = shash_desc_ 145 146 /* 147 * Perform a final XOR with 0xFFFFFFFF 148 * with the generic crc32c shash imple 149 */ 150 *(__le32 *)out = ~cpu_to_le32p(&ctx->c 151 return 0; 152 } 153 154 static int __crc32le_vx_finup(u32 *crc, const 155 u8 *out) 156 { 157 *(__le32 *)out = cpu_to_le32(crc32_le_ 158 return 0; 159 } 160 161 static int __crc32be_vx_finup(u32 *crc, const 162 u8 *out) 163 { 164 *(__be32 *)out = cpu_to_be32(crc32_be_ 165 return 0; 166 } 167 168 static int __crc32c_vx_finup(u32 *crc, const u 169 u8 *out) 170 { 171 /* 172 * Perform a final XOR with 0xFFFFFFFF 173 * with the generic crc32c shash imple 174 */ 175 *(__le32 *)out = ~cpu_to_le32(crc32c_l 176 return 0; 177 } 178 179 180 #define CRC32_VX_FINUP(alg, func) 181 static int alg ## _vx_finup(struct sha 182 unsigned in 183 { 184 return __ ## alg ## _vx_finup( 185 186 } 187 188 CRC32_VX_FINUP(crc32le, crc32_le_vx) 189 CRC32_VX_FINUP(crc32be, crc32_be_vx) 190 CRC32_VX_FINUP(crc32c, crc32c_le_vx) 191 192 #define CRC32_VX_DIGEST(alg, func) 193 static int alg ## _vx_digest(struct sh 194 unsigned 195 { 196 return __ ## alg ## _vx_finup( 197 198 } 199 200 CRC32_VX_DIGEST(crc32le, crc32_le_vx) 201 CRC32_VX_DIGEST(crc32be, crc32_be_vx) 202 CRC32_VX_DIGEST(crc32c, crc32c_le_vx) 203 204 #define CRC32_VX_UPDATE(alg, func) 205 static int alg ## _vx_update(struct sh 206 unsigned 207 { 208 struct crc_desc_ctx *ctx = sha 209 ctx->crc = func(ctx->crc, data 210 return 0; 211 } 212 213 CRC32_VX_UPDATE(crc32le, crc32_le_vx) 214 CRC32_VX_UPDATE(crc32be, crc32_be_vx) 215 CRC32_VX_UPDATE(crc32c, crc32c_le_vx) 216 217 218 static struct shash_alg crc32_vx_algs[] = { 219 /* CRC-32 LE */ 220 { 221 .init = crc32_ 222 .setkey = crc32_ 223 .update = crc32l 224 .final = crc32l 225 .finup = crc32l 226 .digest = crc32l 227 .descsize = sizeof 228 .digestsize = CRC32_ 229 .base = { 230 .cra_name = "cr 231 .cra_driver_name = "cr 232 .cra_priority = 200 233 .cra_flags = CRY 234 .cra_blocksize = CRC 235 .cra_ctxsize = siz 236 .cra_module = THI 237 .cra_init = crc 238 }, 239 }, 240 /* CRC-32 BE */ 241 { 242 .init = crc32_ 243 .setkey = crc32b 244 .update = crc32b 245 .final = crc32b 246 .finup = crc32b 247 .digest = crc32b 248 .descsize = sizeof 249 .digestsize = CRC32_ 250 .base = { 251 .cra_name = "cr 252 .cra_driver_name = "cr 253 .cra_priority = 200 254 .cra_flags = CRY 255 .cra_blocksize = CRC 256 .cra_ctxsize = siz 257 .cra_module = THI 258 .cra_init = crc 259 }, 260 }, 261 /* CRC-32C LE */ 262 { 263 .init = crc32_ 264 .setkey = crc32_ 265 .update = crc32c 266 .final = crc32c 267 .finup = crc32c 268 .digest = crc32c 269 .descsize = sizeof 270 .digestsize = CRC32_ 271 .base = { 272 .cra_name = "cr 273 .cra_driver_name = "cr 274 .cra_priority = 200 275 .cra_flags = CRY 276 .cra_blocksize = CRC 277 .cra_ctxsize = siz 278 .cra_module = THI 279 .cra_init = crc 280 }, 281 }, 282 }; 283 284 285 static int __init crc_vx_mod_init(void) 286 { 287 return crypto_register_shashes(crc32_v 288 ARRAY_S 289 } 290 291 static void __exit crc_vx_mod_exit(void) 292 { 293 crypto_unregister_shashes(crc32_vx_alg 294 } 295 296 module_cpu_feature_match(S390_CPU_FEATURE_VXRS 297 module_exit(crc_vx_mod_exit); 298 299 MODULE_AUTHOR("Hendrik Brueckner <brueckner@li 300 MODULE_DESCRIPTION("CRC-32 algorithms using z/ 301 MODULE_LICENSE("GPL"); 302 303 MODULE_ALIAS_CRYPTO("crc32"); 304 MODULE_ALIAS_CRYPTO("crc32-vx"); 305 MODULE_ALIAS_CRYPTO("crc32c"); 306 MODULE_ALIAS_CRYPTO("crc32c-vx"); 307
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.