18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Glue Code for the AVX assembler implementation of the Cast6 Cipher 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2012 Johannes Goetzfried 68c2ecf20Sopenharmony_ci * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi> 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/module.h> 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <linux/crypto.h> 148c2ecf20Sopenharmony_ci#include <linux/err.h> 158c2ecf20Sopenharmony_ci#include <crypto/algapi.h> 168c2ecf20Sopenharmony_ci#include <crypto/cast6.h> 178c2ecf20Sopenharmony_ci#include <crypto/internal/simd.h> 188c2ecf20Sopenharmony_ci#include <crypto/xts.h> 198c2ecf20Sopenharmony_ci#include <asm/crypto/glue_helper.h> 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#define CAST6_PARALLEL_BLOCKS 8 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciasmlinkage void cast6_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src); 248c2ecf20Sopenharmony_ciasmlinkage void cast6_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src); 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciasmlinkage void cast6_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src); 278c2ecf20Sopenharmony_ciasmlinkage void cast6_ctr_8way(const void *ctx, u8 *dst, const u8 *src, 288c2ecf20Sopenharmony_ci le128 *iv); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ciasmlinkage void cast6_xts_enc_8way(const void *ctx, u8 *dst, const u8 *src, 318c2ecf20Sopenharmony_ci le128 *iv); 328c2ecf20Sopenharmony_ciasmlinkage void cast6_xts_dec_8way(const void *ctx, u8 *dst, const u8 *src, 338c2ecf20Sopenharmony_ci le128 *iv); 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic int cast6_setkey_skcipher(struct crypto_skcipher *tfm, 368c2ecf20Sopenharmony_ci const u8 *key, unsigned int keylen) 378c2ecf20Sopenharmony_ci{ 388c2ecf20Sopenharmony_ci return cast6_setkey(&tfm->base, key, keylen); 398c2ecf20Sopenharmony_ci} 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic void cast6_xts_enc(const void *ctx, u8 *dst, const u8 *src, le128 *iv) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci glue_xts_crypt_128bit_one(ctx, dst, src, iv, __cast6_encrypt); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic void cast6_xts_dec(const void *ctx, u8 *dst, const u8 *src, le128 *iv) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci glue_xts_crypt_128bit_one(ctx, dst, src, iv, __cast6_decrypt); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic void cast6_crypt_ctr(const void *ctx, u8 *d, const u8 *s, le128 *iv) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci be128 ctrblk; 548c2ecf20Sopenharmony_ci u128 *dst = (u128 *)d; 558c2ecf20Sopenharmony_ci const u128 *src = (const u128 *)s; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci le128_to_be128(&ctrblk, iv); 588c2ecf20Sopenharmony_ci le128_inc(iv); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci __cast6_encrypt(ctx, (u8 *)&ctrblk, (u8 *)&ctrblk); 618c2ecf20Sopenharmony_ci u128_xor(dst, src, (u128 *)&ctrblk); 628c2ecf20Sopenharmony_ci} 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cistatic const struct common_glue_ctx cast6_enc = { 658c2ecf20Sopenharmony_ci .num_funcs = 2, 668c2ecf20Sopenharmony_ci .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS, 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci .funcs = { { 698c2ecf20Sopenharmony_ci .num_blocks = CAST6_PARALLEL_BLOCKS, 708c2ecf20Sopenharmony_ci .fn_u = { .ecb = cast6_ecb_enc_8way } 718c2ecf20Sopenharmony_ci }, { 728c2ecf20Sopenharmony_ci .num_blocks = 1, 738c2ecf20Sopenharmony_ci .fn_u = { .ecb = __cast6_encrypt } 748c2ecf20Sopenharmony_ci } } 758c2ecf20Sopenharmony_ci}; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_cistatic const struct common_glue_ctx cast6_ctr = { 788c2ecf20Sopenharmony_ci .num_funcs = 2, 798c2ecf20Sopenharmony_ci .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS, 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci .funcs = { { 828c2ecf20Sopenharmony_ci .num_blocks = CAST6_PARALLEL_BLOCKS, 838c2ecf20Sopenharmony_ci .fn_u = { .ctr = cast6_ctr_8way } 848c2ecf20Sopenharmony_ci }, { 858c2ecf20Sopenharmony_ci .num_blocks = 1, 868c2ecf20Sopenharmony_ci .fn_u = { .ctr = cast6_crypt_ctr } 878c2ecf20Sopenharmony_ci } } 888c2ecf20Sopenharmony_ci}; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_cistatic const struct common_glue_ctx cast6_enc_xts = { 918c2ecf20Sopenharmony_ci .num_funcs = 2, 928c2ecf20Sopenharmony_ci .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS, 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci .funcs = { { 958c2ecf20Sopenharmony_ci .num_blocks = CAST6_PARALLEL_BLOCKS, 968c2ecf20Sopenharmony_ci .fn_u = { .xts = cast6_xts_enc_8way } 978c2ecf20Sopenharmony_ci }, { 988c2ecf20Sopenharmony_ci .num_blocks = 1, 998c2ecf20Sopenharmony_ci .fn_u = { .xts = cast6_xts_enc } 1008c2ecf20Sopenharmony_ci } } 1018c2ecf20Sopenharmony_ci}; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_cistatic const struct common_glue_ctx cast6_dec = { 1048c2ecf20Sopenharmony_ci .num_funcs = 2, 1058c2ecf20Sopenharmony_ci .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS, 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci .funcs = { { 1088c2ecf20Sopenharmony_ci .num_blocks = CAST6_PARALLEL_BLOCKS, 1098c2ecf20Sopenharmony_ci .fn_u = { .ecb = cast6_ecb_dec_8way } 1108c2ecf20Sopenharmony_ci }, { 1118c2ecf20Sopenharmony_ci .num_blocks = 1, 1128c2ecf20Sopenharmony_ci .fn_u = { .ecb = __cast6_decrypt } 1138c2ecf20Sopenharmony_ci } } 1148c2ecf20Sopenharmony_ci}; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_cistatic const struct common_glue_ctx cast6_dec_cbc = { 1178c2ecf20Sopenharmony_ci .num_funcs = 2, 1188c2ecf20Sopenharmony_ci .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS, 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci .funcs = { { 1218c2ecf20Sopenharmony_ci .num_blocks = CAST6_PARALLEL_BLOCKS, 1228c2ecf20Sopenharmony_ci .fn_u = { .cbc = cast6_cbc_dec_8way } 1238c2ecf20Sopenharmony_ci }, { 1248c2ecf20Sopenharmony_ci .num_blocks = 1, 1258c2ecf20Sopenharmony_ci .fn_u = { .cbc = __cast6_decrypt } 1268c2ecf20Sopenharmony_ci } } 1278c2ecf20Sopenharmony_ci}; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_cistatic const struct common_glue_ctx cast6_dec_xts = { 1308c2ecf20Sopenharmony_ci .num_funcs = 2, 1318c2ecf20Sopenharmony_ci .fpu_blocks_limit = CAST6_PARALLEL_BLOCKS, 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci .funcs = { { 1348c2ecf20Sopenharmony_ci .num_blocks = CAST6_PARALLEL_BLOCKS, 1358c2ecf20Sopenharmony_ci .fn_u = { .xts = cast6_xts_dec_8way } 1368c2ecf20Sopenharmony_ci }, { 1378c2ecf20Sopenharmony_ci .num_blocks = 1, 1388c2ecf20Sopenharmony_ci .fn_u = { .xts = cast6_xts_dec } 1398c2ecf20Sopenharmony_ci } } 1408c2ecf20Sopenharmony_ci}; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req) 1438c2ecf20Sopenharmony_ci{ 1448c2ecf20Sopenharmony_ci return glue_ecb_req_128bit(&cast6_enc, req); 1458c2ecf20Sopenharmony_ci} 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req) 1488c2ecf20Sopenharmony_ci{ 1498c2ecf20Sopenharmony_ci return glue_ecb_req_128bit(&cast6_dec, req); 1508c2ecf20Sopenharmony_ci} 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci return glue_cbc_encrypt_req_128bit(__cast6_encrypt, req); 1558c2ecf20Sopenharmony_ci} 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req) 1588c2ecf20Sopenharmony_ci{ 1598c2ecf20Sopenharmony_ci return glue_cbc_decrypt_req_128bit(&cast6_dec_cbc, req); 1608c2ecf20Sopenharmony_ci} 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_cistatic int ctr_crypt(struct skcipher_request *req) 1638c2ecf20Sopenharmony_ci{ 1648c2ecf20Sopenharmony_ci return glue_ctr_req_128bit(&cast6_ctr, req); 1658c2ecf20Sopenharmony_ci} 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistruct cast6_xts_ctx { 1688c2ecf20Sopenharmony_ci struct cast6_ctx tweak_ctx; 1698c2ecf20Sopenharmony_ci struct cast6_ctx crypt_ctx; 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_cistatic int xts_cast6_setkey(struct crypto_skcipher *tfm, const u8 *key, 1738c2ecf20Sopenharmony_ci unsigned int keylen) 1748c2ecf20Sopenharmony_ci{ 1758c2ecf20Sopenharmony_ci struct cast6_xts_ctx *ctx = crypto_skcipher_ctx(tfm); 1768c2ecf20Sopenharmony_ci int err; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci err = xts_verify_key(tfm, key, keylen); 1798c2ecf20Sopenharmony_ci if (err) 1808c2ecf20Sopenharmony_ci return err; 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci /* first half of xts-key is for crypt */ 1838c2ecf20Sopenharmony_ci err = __cast6_setkey(&ctx->crypt_ctx, key, keylen / 2); 1848c2ecf20Sopenharmony_ci if (err) 1858c2ecf20Sopenharmony_ci return err; 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci /* second half of xts-key is for tweak */ 1888c2ecf20Sopenharmony_ci return __cast6_setkey(&ctx->tweak_ctx, key + keylen / 2, keylen / 2); 1898c2ecf20Sopenharmony_ci} 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_cistatic int xts_encrypt(struct skcipher_request *req) 1928c2ecf20Sopenharmony_ci{ 1938c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 1948c2ecf20Sopenharmony_ci struct cast6_xts_ctx *ctx = crypto_skcipher_ctx(tfm); 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci return glue_xts_req_128bit(&cast6_enc_xts, req, __cast6_encrypt, 1978c2ecf20Sopenharmony_ci &ctx->tweak_ctx, &ctx->crypt_ctx, false); 1988c2ecf20Sopenharmony_ci} 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cistatic int xts_decrypt(struct skcipher_request *req) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); 2038c2ecf20Sopenharmony_ci struct cast6_xts_ctx *ctx = crypto_skcipher_ctx(tfm); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci return glue_xts_req_128bit(&cast6_dec_xts, req, __cast6_encrypt, 2068c2ecf20Sopenharmony_ci &ctx->tweak_ctx, &ctx->crypt_ctx, true); 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic struct skcipher_alg cast6_algs[] = { 2108c2ecf20Sopenharmony_ci { 2118c2ecf20Sopenharmony_ci .base.cra_name = "__ecb(cast6)", 2128c2ecf20Sopenharmony_ci .base.cra_driver_name = "__ecb-cast6-avx", 2138c2ecf20Sopenharmony_ci .base.cra_priority = 200, 2148c2ecf20Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 2158c2ecf20Sopenharmony_ci .base.cra_blocksize = CAST6_BLOCK_SIZE, 2168c2ecf20Sopenharmony_ci .base.cra_ctxsize = sizeof(struct cast6_ctx), 2178c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 2188c2ecf20Sopenharmony_ci .min_keysize = CAST6_MIN_KEY_SIZE, 2198c2ecf20Sopenharmony_ci .max_keysize = CAST6_MAX_KEY_SIZE, 2208c2ecf20Sopenharmony_ci .setkey = cast6_setkey_skcipher, 2218c2ecf20Sopenharmony_ci .encrypt = ecb_encrypt, 2228c2ecf20Sopenharmony_ci .decrypt = ecb_decrypt, 2238c2ecf20Sopenharmony_ci }, { 2248c2ecf20Sopenharmony_ci .base.cra_name = "__cbc(cast6)", 2258c2ecf20Sopenharmony_ci .base.cra_driver_name = "__cbc-cast6-avx", 2268c2ecf20Sopenharmony_ci .base.cra_priority = 200, 2278c2ecf20Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 2288c2ecf20Sopenharmony_ci .base.cra_blocksize = CAST6_BLOCK_SIZE, 2298c2ecf20Sopenharmony_ci .base.cra_ctxsize = sizeof(struct cast6_ctx), 2308c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 2318c2ecf20Sopenharmony_ci .min_keysize = CAST6_MIN_KEY_SIZE, 2328c2ecf20Sopenharmony_ci .max_keysize = CAST6_MAX_KEY_SIZE, 2338c2ecf20Sopenharmony_ci .ivsize = CAST6_BLOCK_SIZE, 2348c2ecf20Sopenharmony_ci .setkey = cast6_setkey_skcipher, 2358c2ecf20Sopenharmony_ci .encrypt = cbc_encrypt, 2368c2ecf20Sopenharmony_ci .decrypt = cbc_decrypt, 2378c2ecf20Sopenharmony_ci }, { 2388c2ecf20Sopenharmony_ci .base.cra_name = "__ctr(cast6)", 2398c2ecf20Sopenharmony_ci .base.cra_driver_name = "__ctr-cast6-avx", 2408c2ecf20Sopenharmony_ci .base.cra_priority = 200, 2418c2ecf20Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 2428c2ecf20Sopenharmony_ci .base.cra_blocksize = 1, 2438c2ecf20Sopenharmony_ci .base.cra_ctxsize = sizeof(struct cast6_ctx), 2448c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 2458c2ecf20Sopenharmony_ci .min_keysize = CAST6_MIN_KEY_SIZE, 2468c2ecf20Sopenharmony_ci .max_keysize = CAST6_MAX_KEY_SIZE, 2478c2ecf20Sopenharmony_ci .ivsize = CAST6_BLOCK_SIZE, 2488c2ecf20Sopenharmony_ci .chunksize = CAST6_BLOCK_SIZE, 2498c2ecf20Sopenharmony_ci .setkey = cast6_setkey_skcipher, 2508c2ecf20Sopenharmony_ci .encrypt = ctr_crypt, 2518c2ecf20Sopenharmony_ci .decrypt = ctr_crypt, 2528c2ecf20Sopenharmony_ci }, { 2538c2ecf20Sopenharmony_ci .base.cra_name = "__xts(cast6)", 2548c2ecf20Sopenharmony_ci .base.cra_driver_name = "__xts-cast6-avx", 2558c2ecf20Sopenharmony_ci .base.cra_priority = 200, 2568c2ecf20Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 2578c2ecf20Sopenharmony_ci .base.cra_blocksize = CAST6_BLOCK_SIZE, 2588c2ecf20Sopenharmony_ci .base.cra_ctxsize = sizeof(struct cast6_xts_ctx), 2598c2ecf20Sopenharmony_ci .base.cra_module = THIS_MODULE, 2608c2ecf20Sopenharmony_ci .min_keysize = 2 * CAST6_MIN_KEY_SIZE, 2618c2ecf20Sopenharmony_ci .max_keysize = 2 * CAST6_MAX_KEY_SIZE, 2628c2ecf20Sopenharmony_ci .ivsize = CAST6_BLOCK_SIZE, 2638c2ecf20Sopenharmony_ci .setkey = xts_cast6_setkey, 2648c2ecf20Sopenharmony_ci .encrypt = xts_encrypt, 2658c2ecf20Sopenharmony_ci .decrypt = xts_decrypt, 2668c2ecf20Sopenharmony_ci }, 2678c2ecf20Sopenharmony_ci}; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic struct simd_skcipher_alg *cast6_simd_algs[ARRAY_SIZE(cast6_algs)]; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_cistatic int __init cast6_init(void) 2728c2ecf20Sopenharmony_ci{ 2738c2ecf20Sopenharmony_ci const char *feature_name; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, 2768c2ecf20Sopenharmony_ci &feature_name)) { 2778c2ecf20Sopenharmony_ci pr_info("CPU feature '%s' is not supported.\n", feature_name); 2788c2ecf20Sopenharmony_ci return -ENODEV; 2798c2ecf20Sopenharmony_ci } 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci return simd_register_skciphers_compat(cast6_algs, 2828c2ecf20Sopenharmony_ci ARRAY_SIZE(cast6_algs), 2838c2ecf20Sopenharmony_ci cast6_simd_algs); 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_cistatic void __exit cast6_exit(void) 2878c2ecf20Sopenharmony_ci{ 2888c2ecf20Sopenharmony_ci simd_unregister_skciphers(cast6_algs, ARRAY_SIZE(cast6_algs), 2898c2ecf20Sopenharmony_ci cast6_simd_algs); 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cimodule_init(cast6_init); 2938c2ecf20Sopenharmony_cimodule_exit(cast6_exit); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Cast6 Cipher Algorithm, AVX optimized"); 2968c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 2978c2ecf20Sopenharmony_ciMODULE_ALIAS_CRYPTO("cast6"); 298