18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Crypto-API module for CRC-32 algorithms implemented with the 48c2ecf20Sopenharmony_ci * z/Architecture Vector Extension Facility. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright IBM Corp. 2015 78c2ecf20Sopenharmony_ci * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci#define KMSG_COMPONENT "crc32-vx" 108c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/cpufeature.h> 148c2ecf20Sopenharmony_ci#include <linux/crc32.h> 158c2ecf20Sopenharmony_ci#include <crypto/internal/hash.h> 168c2ecf20Sopenharmony_ci#include <asm/fpu/api.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define CRC32_BLOCK_SIZE 1 208c2ecf20Sopenharmony_ci#define CRC32_DIGEST_SIZE 4 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define VX_MIN_LEN 64 238c2ecf20Sopenharmony_ci#define VX_ALIGNMENT 16L 248c2ecf20Sopenharmony_ci#define VX_ALIGN_MASK (VX_ALIGNMENT - 1) 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistruct crc_ctx { 278c2ecf20Sopenharmony_ci u32 key; 288c2ecf20Sopenharmony_ci}; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_cistruct crc_desc_ctx { 318c2ecf20Sopenharmony_ci u32 crc; 328c2ecf20Sopenharmony_ci}; 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* Prototypes for functions in assembly files */ 358c2ecf20Sopenharmony_ciu32 crc32_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size); 368c2ecf20Sopenharmony_ciu32 crc32_be_vgfm_16(u32 crc, unsigned char const *buf, size_t size); 378c2ecf20Sopenharmony_ciu32 crc32c_le_vgfm_16(u32 crc, unsigned char const *buf, size_t size); 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci/* 408c2ecf20Sopenharmony_ci * DEFINE_CRC32_VX() - Define a CRC-32 function using the vector extension 418c2ecf20Sopenharmony_ci * 428c2ecf20Sopenharmony_ci * Creates a function to perform a particular CRC-32 computation. Depending 438c2ecf20Sopenharmony_ci * on the message buffer, the hardware-accelerated or software implementation 448c2ecf20Sopenharmony_ci * is used. Note that the message buffer is aligned to improve fetch 458c2ecf20Sopenharmony_ci * operations of VECTOR LOAD MULTIPLE instructions. 468c2ecf20Sopenharmony_ci * 478c2ecf20Sopenharmony_ci */ 488c2ecf20Sopenharmony_ci#define DEFINE_CRC32_VX(___fname, ___crc32_vx, ___crc32_sw) \ 498c2ecf20Sopenharmony_ci static u32 __pure ___fname(u32 crc, \ 508c2ecf20Sopenharmony_ci unsigned char const *data, size_t datalen) \ 518c2ecf20Sopenharmony_ci { \ 528c2ecf20Sopenharmony_ci struct kernel_fpu vxstate; \ 538c2ecf20Sopenharmony_ci unsigned long prealign, aligned, remaining; \ 548c2ecf20Sopenharmony_ci \ 558c2ecf20Sopenharmony_ci if (datalen < VX_MIN_LEN + VX_ALIGN_MASK) \ 568c2ecf20Sopenharmony_ci return ___crc32_sw(crc, data, datalen); \ 578c2ecf20Sopenharmony_ci \ 588c2ecf20Sopenharmony_ci if ((unsigned long)data & VX_ALIGN_MASK) { \ 598c2ecf20Sopenharmony_ci prealign = VX_ALIGNMENT - \ 608c2ecf20Sopenharmony_ci ((unsigned long)data & VX_ALIGN_MASK); \ 618c2ecf20Sopenharmony_ci datalen -= prealign; \ 628c2ecf20Sopenharmony_ci crc = ___crc32_sw(crc, data, prealign); \ 638c2ecf20Sopenharmony_ci data = (void *)((unsigned long)data + prealign); \ 648c2ecf20Sopenharmony_ci } \ 658c2ecf20Sopenharmony_ci \ 668c2ecf20Sopenharmony_ci aligned = datalen & ~VX_ALIGN_MASK; \ 678c2ecf20Sopenharmony_ci remaining = datalen & VX_ALIGN_MASK; \ 688c2ecf20Sopenharmony_ci \ 698c2ecf20Sopenharmony_ci kernel_fpu_begin(&vxstate, KERNEL_VXR_LOW); \ 708c2ecf20Sopenharmony_ci crc = ___crc32_vx(crc, data, aligned); \ 718c2ecf20Sopenharmony_ci kernel_fpu_end(&vxstate, KERNEL_VXR_LOW); \ 728c2ecf20Sopenharmony_ci \ 738c2ecf20Sopenharmony_ci if (remaining) \ 748c2ecf20Sopenharmony_ci crc = ___crc32_sw(crc, data + aligned, remaining); \ 758c2ecf20Sopenharmony_ci \ 768c2ecf20Sopenharmony_ci return crc; \ 778c2ecf20Sopenharmony_ci } 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ciDEFINE_CRC32_VX(crc32_le_vx, crc32_le_vgfm_16, crc32_le) 808c2ecf20Sopenharmony_ciDEFINE_CRC32_VX(crc32_be_vx, crc32_be_vgfm_16, crc32_be) 818c2ecf20Sopenharmony_ciDEFINE_CRC32_VX(crc32c_le_vx, crc32c_le_vgfm_16, __crc32c_le) 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic int crc32_vx_cra_init_zero(struct crypto_tfm *tfm) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci struct crc_ctx *mctx = crypto_tfm_ctx(tfm); 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci mctx->key = 0; 898c2ecf20Sopenharmony_ci return 0; 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_cistatic int crc32_vx_cra_init_invert(struct crypto_tfm *tfm) 938c2ecf20Sopenharmony_ci{ 948c2ecf20Sopenharmony_ci struct crc_ctx *mctx = crypto_tfm_ctx(tfm); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci mctx->key = ~0; 978c2ecf20Sopenharmony_ci return 0; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_cistatic int crc32_vx_init(struct shash_desc *desc) 1018c2ecf20Sopenharmony_ci{ 1028c2ecf20Sopenharmony_ci struct crc_ctx *mctx = crypto_shash_ctx(desc->tfm); 1038c2ecf20Sopenharmony_ci struct crc_desc_ctx *ctx = shash_desc_ctx(desc); 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci ctx->crc = mctx->key; 1068c2ecf20Sopenharmony_ci return 0; 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic int crc32_vx_setkey(struct crypto_shash *tfm, const u8 *newkey, 1108c2ecf20Sopenharmony_ci unsigned int newkeylen) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci struct crc_ctx *mctx = crypto_shash_ctx(tfm); 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ci if (newkeylen != sizeof(mctx->key)) 1158c2ecf20Sopenharmony_ci return -EINVAL; 1168c2ecf20Sopenharmony_ci mctx->key = le32_to_cpu(*(__le32 *)newkey); 1178c2ecf20Sopenharmony_ci return 0; 1188c2ecf20Sopenharmony_ci} 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_cistatic int crc32be_vx_setkey(struct crypto_shash *tfm, const u8 *newkey, 1218c2ecf20Sopenharmony_ci unsigned int newkeylen) 1228c2ecf20Sopenharmony_ci{ 1238c2ecf20Sopenharmony_ci struct crc_ctx *mctx = crypto_shash_ctx(tfm); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci if (newkeylen != sizeof(mctx->key)) 1268c2ecf20Sopenharmony_ci return -EINVAL; 1278c2ecf20Sopenharmony_ci mctx->key = be32_to_cpu(*(__be32 *)newkey); 1288c2ecf20Sopenharmony_ci return 0; 1298c2ecf20Sopenharmony_ci} 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_cistatic int crc32le_vx_final(struct shash_desc *desc, u8 *out) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci struct crc_desc_ctx *ctx = shash_desc_ctx(desc); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci *(__le32 *)out = cpu_to_le32p(&ctx->crc); 1368c2ecf20Sopenharmony_ci return 0; 1378c2ecf20Sopenharmony_ci} 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_cistatic int crc32be_vx_final(struct shash_desc *desc, u8 *out) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct crc_desc_ctx *ctx = shash_desc_ctx(desc); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci *(__be32 *)out = cpu_to_be32p(&ctx->crc); 1448c2ecf20Sopenharmony_ci return 0; 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic int crc32c_vx_final(struct shash_desc *desc, u8 *out) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci struct crc_desc_ctx *ctx = shash_desc_ctx(desc); 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_ci /* 1528c2ecf20Sopenharmony_ci * Perform a final XOR with 0xFFFFFFFF to be in sync 1538c2ecf20Sopenharmony_ci * with the generic crc32c shash implementation. 1548c2ecf20Sopenharmony_ci */ 1558c2ecf20Sopenharmony_ci *(__le32 *)out = ~cpu_to_le32p(&ctx->crc); 1568c2ecf20Sopenharmony_ci return 0; 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic int __crc32le_vx_finup(u32 *crc, const u8 *data, unsigned int len, 1608c2ecf20Sopenharmony_ci u8 *out) 1618c2ecf20Sopenharmony_ci{ 1628c2ecf20Sopenharmony_ci *(__le32 *)out = cpu_to_le32(crc32_le_vx(*crc, data, len)); 1638c2ecf20Sopenharmony_ci return 0; 1648c2ecf20Sopenharmony_ci} 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_cistatic int __crc32be_vx_finup(u32 *crc, const u8 *data, unsigned int len, 1678c2ecf20Sopenharmony_ci u8 *out) 1688c2ecf20Sopenharmony_ci{ 1698c2ecf20Sopenharmony_ci *(__be32 *)out = cpu_to_be32(crc32_be_vx(*crc, data, len)); 1708c2ecf20Sopenharmony_ci return 0; 1718c2ecf20Sopenharmony_ci} 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_cistatic int __crc32c_vx_finup(u32 *crc, const u8 *data, unsigned int len, 1748c2ecf20Sopenharmony_ci u8 *out) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci /* 1778c2ecf20Sopenharmony_ci * Perform a final XOR with 0xFFFFFFFF to be in sync 1788c2ecf20Sopenharmony_ci * with the generic crc32c shash implementation. 1798c2ecf20Sopenharmony_ci */ 1808c2ecf20Sopenharmony_ci *(__le32 *)out = ~cpu_to_le32(crc32c_le_vx(*crc, data, len)); 1818c2ecf20Sopenharmony_ci return 0; 1828c2ecf20Sopenharmony_ci} 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci#define CRC32_VX_FINUP(alg, func) \ 1868c2ecf20Sopenharmony_ci static int alg ## _vx_finup(struct shash_desc *desc, const u8 *data, \ 1878c2ecf20Sopenharmony_ci unsigned int datalen, u8 *out) \ 1888c2ecf20Sopenharmony_ci { \ 1898c2ecf20Sopenharmony_ci return __ ## alg ## _vx_finup(shash_desc_ctx(desc), \ 1908c2ecf20Sopenharmony_ci data, datalen, out); \ 1918c2ecf20Sopenharmony_ci } 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ciCRC32_VX_FINUP(crc32le, crc32_le_vx) 1948c2ecf20Sopenharmony_ciCRC32_VX_FINUP(crc32be, crc32_be_vx) 1958c2ecf20Sopenharmony_ciCRC32_VX_FINUP(crc32c, crc32c_le_vx) 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci#define CRC32_VX_DIGEST(alg, func) \ 1988c2ecf20Sopenharmony_ci static int alg ## _vx_digest(struct shash_desc *desc, const u8 *data, \ 1998c2ecf20Sopenharmony_ci unsigned int len, u8 *out) \ 2008c2ecf20Sopenharmony_ci { \ 2018c2ecf20Sopenharmony_ci return __ ## alg ## _vx_finup(crypto_shash_ctx(desc->tfm), \ 2028c2ecf20Sopenharmony_ci data, len, out); \ 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ciCRC32_VX_DIGEST(crc32le, crc32_le_vx) 2068c2ecf20Sopenharmony_ciCRC32_VX_DIGEST(crc32be, crc32_be_vx) 2078c2ecf20Sopenharmony_ciCRC32_VX_DIGEST(crc32c, crc32c_le_vx) 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci#define CRC32_VX_UPDATE(alg, func) \ 2108c2ecf20Sopenharmony_ci static int alg ## _vx_update(struct shash_desc *desc, const u8 *data, \ 2118c2ecf20Sopenharmony_ci unsigned int datalen) \ 2128c2ecf20Sopenharmony_ci { \ 2138c2ecf20Sopenharmony_ci struct crc_desc_ctx *ctx = shash_desc_ctx(desc); \ 2148c2ecf20Sopenharmony_ci ctx->crc = func(ctx->crc, data, datalen); \ 2158c2ecf20Sopenharmony_ci return 0; \ 2168c2ecf20Sopenharmony_ci } 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ciCRC32_VX_UPDATE(crc32le, crc32_le_vx) 2198c2ecf20Sopenharmony_ciCRC32_VX_UPDATE(crc32be, crc32_be_vx) 2208c2ecf20Sopenharmony_ciCRC32_VX_UPDATE(crc32c, crc32c_le_vx) 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_cistatic struct shash_alg crc32_vx_algs[] = { 2248c2ecf20Sopenharmony_ci /* CRC-32 LE */ 2258c2ecf20Sopenharmony_ci { 2268c2ecf20Sopenharmony_ci .init = crc32_vx_init, 2278c2ecf20Sopenharmony_ci .setkey = crc32_vx_setkey, 2288c2ecf20Sopenharmony_ci .update = crc32le_vx_update, 2298c2ecf20Sopenharmony_ci .final = crc32le_vx_final, 2308c2ecf20Sopenharmony_ci .finup = crc32le_vx_finup, 2318c2ecf20Sopenharmony_ci .digest = crc32le_vx_digest, 2328c2ecf20Sopenharmony_ci .descsize = sizeof(struct crc_desc_ctx), 2338c2ecf20Sopenharmony_ci .digestsize = CRC32_DIGEST_SIZE, 2348c2ecf20Sopenharmony_ci .base = { 2358c2ecf20Sopenharmony_ci .cra_name = "crc32", 2368c2ecf20Sopenharmony_ci .cra_driver_name = "crc32-vx", 2378c2ecf20Sopenharmony_ci .cra_priority = 200, 2388c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 2398c2ecf20Sopenharmony_ci .cra_blocksize = CRC32_BLOCK_SIZE, 2408c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct crc_ctx), 2418c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 2428c2ecf20Sopenharmony_ci .cra_init = crc32_vx_cra_init_zero, 2438c2ecf20Sopenharmony_ci }, 2448c2ecf20Sopenharmony_ci }, 2458c2ecf20Sopenharmony_ci /* CRC-32 BE */ 2468c2ecf20Sopenharmony_ci { 2478c2ecf20Sopenharmony_ci .init = crc32_vx_init, 2488c2ecf20Sopenharmony_ci .setkey = crc32be_vx_setkey, 2498c2ecf20Sopenharmony_ci .update = crc32be_vx_update, 2508c2ecf20Sopenharmony_ci .final = crc32be_vx_final, 2518c2ecf20Sopenharmony_ci .finup = crc32be_vx_finup, 2528c2ecf20Sopenharmony_ci .digest = crc32be_vx_digest, 2538c2ecf20Sopenharmony_ci .descsize = sizeof(struct crc_desc_ctx), 2548c2ecf20Sopenharmony_ci .digestsize = CRC32_DIGEST_SIZE, 2558c2ecf20Sopenharmony_ci .base = { 2568c2ecf20Sopenharmony_ci .cra_name = "crc32be", 2578c2ecf20Sopenharmony_ci .cra_driver_name = "crc32be-vx", 2588c2ecf20Sopenharmony_ci .cra_priority = 200, 2598c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 2608c2ecf20Sopenharmony_ci .cra_blocksize = CRC32_BLOCK_SIZE, 2618c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct crc_ctx), 2628c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 2638c2ecf20Sopenharmony_ci .cra_init = crc32_vx_cra_init_zero, 2648c2ecf20Sopenharmony_ci }, 2658c2ecf20Sopenharmony_ci }, 2668c2ecf20Sopenharmony_ci /* CRC-32C LE */ 2678c2ecf20Sopenharmony_ci { 2688c2ecf20Sopenharmony_ci .init = crc32_vx_init, 2698c2ecf20Sopenharmony_ci .setkey = crc32_vx_setkey, 2708c2ecf20Sopenharmony_ci .update = crc32c_vx_update, 2718c2ecf20Sopenharmony_ci .final = crc32c_vx_final, 2728c2ecf20Sopenharmony_ci .finup = crc32c_vx_finup, 2738c2ecf20Sopenharmony_ci .digest = crc32c_vx_digest, 2748c2ecf20Sopenharmony_ci .descsize = sizeof(struct crc_desc_ctx), 2758c2ecf20Sopenharmony_ci .digestsize = CRC32_DIGEST_SIZE, 2768c2ecf20Sopenharmony_ci .base = { 2778c2ecf20Sopenharmony_ci .cra_name = "crc32c", 2788c2ecf20Sopenharmony_ci .cra_driver_name = "crc32c-vx", 2798c2ecf20Sopenharmony_ci .cra_priority = 200, 2808c2ecf20Sopenharmony_ci .cra_flags = CRYPTO_ALG_OPTIONAL_KEY, 2818c2ecf20Sopenharmony_ci .cra_blocksize = CRC32_BLOCK_SIZE, 2828c2ecf20Sopenharmony_ci .cra_ctxsize = sizeof(struct crc_ctx), 2838c2ecf20Sopenharmony_ci .cra_module = THIS_MODULE, 2848c2ecf20Sopenharmony_ci .cra_init = crc32_vx_cra_init_invert, 2858c2ecf20Sopenharmony_ci }, 2868c2ecf20Sopenharmony_ci }, 2878c2ecf20Sopenharmony_ci}; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_cistatic int __init crc_vx_mod_init(void) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci return crypto_register_shashes(crc32_vx_algs, 2938c2ecf20Sopenharmony_ci ARRAY_SIZE(crc32_vx_algs)); 2948c2ecf20Sopenharmony_ci} 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_cistatic void __exit crc_vx_mod_exit(void) 2978c2ecf20Sopenharmony_ci{ 2988c2ecf20Sopenharmony_ci crypto_unregister_shashes(crc32_vx_algs, ARRAY_SIZE(crc32_vx_algs)); 2998c2ecf20Sopenharmony_ci} 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_cimodule_cpu_feature_match(VXRS, crc_vx_mod_init); 3028c2ecf20Sopenharmony_cimodule_exit(crc_vx_mod_exit); 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ciMODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>"); 3058c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32"); 3088c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32-vx"); 3098c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32c"); 3108c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("crc32c-vx"); 311