162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Glue Code for AVX assembler version of Twofish Cipher 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2012 Johannes Goetzfried 662306a36Sopenharmony_ci * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@iki.fi> 962306a36Sopenharmony_ci */ 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci#include <linux/module.h> 1262306a36Sopenharmony_ci#include <linux/types.h> 1362306a36Sopenharmony_ci#include <linux/crypto.h> 1462306a36Sopenharmony_ci#include <linux/err.h> 1562306a36Sopenharmony_ci#include <crypto/algapi.h> 1662306a36Sopenharmony_ci#include <crypto/internal/simd.h> 1762306a36Sopenharmony_ci#include <crypto/twofish.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include "twofish.h" 2062306a36Sopenharmony_ci#include "ecb_cbc_helpers.h" 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#define TWOFISH_PARALLEL_BLOCKS 8 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci/* 8-way parallel cipher functions */ 2562306a36Sopenharmony_ciasmlinkage void twofish_ecb_enc_8way(const void *ctx, u8 *dst, const u8 *src); 2662306a36Sopenharmony_ciasmlinkage void twofish_ecb_dec_8way(const void *ctx, u8 *dst, const u8 *src); 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ciasmlinkage void twofish_cbc_dec_8way(const void *ctx, u8 *dst, const u8 *src); 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_cistatic int twofish_setkey_skcipher(struct crypto_skcipher *tfm, 3162306a36Sopenharmony_ci const u8 *key, unsigned int keylen) 3262306a36Sopenharmony_ci{ 3362306a36Sopenharmony_ci return twofish_setkey(&tfm->base, key, keylen); 3462306a36Sopenharmony_ci} 3562306a36Sopenharmony_ci 3662306a36Sopenharmony_cistatic inline void twofish_enc_blk_3way(const void *ctx, u8 *dst, const u8 *src) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci __twofish_enc_blk_3way(ctx, dst, src, false); 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS); 4462306a36Sopenharmony_ci ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_enc_8way); 4562306a36Sopenharmony_ci ECB_BLOCK(3, twofish_enc_blk_3way); 4662306a36Sopenharmony_ci ECB_BLOCK(1, twofish_enc_blk); 4762306a36Sopenharmony_ci ECB_WALK_END(); 4862306a36Sopenharmony_ci} 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req) 5162306a36Sopenharmony_ci{ 5262306a36Sopenharmony_ci ECB_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS); 5362306a36Sopenharmony_ci ECB_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_ecb_dec_8way); 5462306a36Sopenharmony_ci ECB_BLOCK(3, twofish_dec_blk_3way); 5562306a36Sopenharmony_ci ECB_BLOCK(1, twofish_dec_blk); 5662306a36Sopenharmony_ci ECB_WALK_END(); 5762306a36Sopenharmony_ci} 5862306a36Sopenharmony_ci 5962306a36Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req) 6062306a36Sopenharmony_ci{ 6162306a36Sopenharmony_ci CBC_WALK_START(req, TF_BLOCK_SIZE, -1); 6262306a36Sopenharmony_ci CBC_ENC_BLOCK(twofish_enc_blk); 6362306a36Sopenharmony_ci CBC_WALK_END(); 6462306a36Sopenharmony_ci} 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req) 6762306a36Sopenharmony_ci{ 6862306a36Sopenharmony_ci CBC_WALK_START(req, TF_BLOCK_SIZE, TWOFISH_PARALLEL_BLOCKS); 6962306a36Sopenharmony_ci CBC_DEC_BLOCK(TWOFISH_PARALLEL_BLOCKS, twofish_cbc_dec_8way); 7062306a36Sopenharmony_ci CBC_DEC_BLOCK(3, twofish_dec_blk_cbc_3way); 7162306a36Sopenharmony_ci CBC_DEC_BLOCK(1, twofish_dec_blk); 7262306a36Sopenharmony_ci CBC_WALK_END(); 7362306a36Sopenharmony_ci} 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_cistatic struct skcipher_alg twofish_algs[] = { 7662306a36Sopenharmony_ci { 7762306a36Sopenharmony_ci .base.cra_name = "__ecb(twofish)", 7862306a36Sopenharmony_ci .base.cra_driver_name = "__ecb-twofish-avx", 7962306a36Sopenharmony_ci .base.cra_priority = 400, 8062306a36Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 8162306a36Sopenharmony_ci .base.cra_blocksize = TF_BLOCK_SIZE, 8262306a36Sopenharmony_ci .base.cra_ctxsize = sizeof(struct twofish_ctx), 8362306a36Sopenharmony_ci .base.cra_module = THIS_MODULE, 8462306a36Sopenharmony_ci .min_keysize = TF_MIN_KEY_SIZE, 8562306a36Sopenharmony_ci .max_keysize = TF_MAX_KEY_SIZE, 8662306a36Sopenharmony_ci .setkey = twofish_setkey_skcipher, 8762306a36Sopenharmony_ci .encrypt = ecb_encrypt, 8862306a36Sopenharmony_ci .decrypt = ecb_decrypt, 8962306a36Sopenharmony_ci }, { 9062306a36Sopenharmony_ci .base.cra_name = "__cbc(twofish)", 9162306a36Sopenharmony_ci .base.cra_driver_name = "__cbc-twofish-avx", 9262306a36Sopenharmony_ci .base.cra_priority = 400, 9362306a36Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 9462306a36Sopenharmony_ci .base.cra_blocksize = TF_BLOCK_SIZE, 9562306a36Sopenharmony_ci .base.cra_ctxsize = sizeof(struct twofish_ctx), 9662306a36Sopenharmony_ci .base.cra_module = THIS_MODULE, 9762306a36Sopenharmony_ci .min_keysize = TF_MIN_KEY_SIZE, 9862306a36Sopenharmony_ci .max_keysize = TF_MAX_KEY_SIZE, 9962306a36Sopenharmony_ci .ivsize = TF_BLOCK_SIZE, 10062306a36Sopenharmony_ci .setkey = twofish_setkey_skcipher, 10162306a36Sopenharmony_ci .encrypt = cbc_encrypt, 10262306a36Sopenharmony_ci .decrypt = cbc_decrypt, 10362306a36Sopenharmony_ci }, 10462306a36Sopenharmony_ci}; 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_cistatic struct simd_skcipher_alg *twofish_simd_algs[ARRAY_SIZE(twofish_algs)]; 10762306a36Sopenharmony_ci 10862306a36Sopenharmony_cistatic int __init twofish_init(void) 10962306a36Sopenharmony_ci{ 11062306a36Sopenharmony_ci const char *feature_name; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, &feature_name)) { 11362306a36Sopenharmony_ci pr_info("CPU feature '%s' is not supported.\n", feature_name); 11462306a36Sopenharmony_ci return -ENODEV; 11562306a36Sopenharmony_ci } 11662306a36Sopenharmony_ci 11762306a36Sopenharmony_ci return simd_register_skciphers_compat(twofish_algs, 11862306a36Sopenharmony_ci ARRAY_SIZE(twofish_algs), 11962306a36Sopenharmony_ci twofish_simd_algs); 12062306a36Sopenharmony_ci} 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_cistatic void __exit twofish_exit(void) 12362306a36Sopenharmony_ci{ 12462306a36Sopenharmony_ci simd_unregister_skciphers(twofish_algs, ARRAY_SIZE(twofish_algs), 12562306a36Sopenharmony_ci twofish_simd_algs); 12662306a36Sopenharmony_ci} 12762306a36Sopenharmony_ci 12862306a36Sopenharmony_cimodule_init(twofish_init); 12962306a36Sopenharmony_cimodule_exit(twofish_exit); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ciMODULE_DESCRIPTION("Twofish Cipher Algorithm, AVX optimized"); 13262306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 13362306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("twofish"); 134