162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Glue Code for AVX assembler versions of Serpent Cipher 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (C) 2012 Johannes Goetzfried 662306a36Sopenharmony_ci * <Johannes.Goetzfried@informatik.stud.uni-erlangen.de> 762306a36Sopenharmony_ci * 862306a36Sopenharmony_ci * Copyright © 2011-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/serpent.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci#include "serpent-avx.h" 2062306a36Sopenharmony_ci#include "ecb_cbc_helpers.h" 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci/* 8-way parallel cipher functions */ 2362306a36Sopenharmony_ciasmlinkage void serpent_ecb_enc_8way_avx(const void *ctx, u8 *dst, 2462306a36Sopenharmony_ci const u8 *src); 2562306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(serpent_ecb_enc_8way_avx); 2662306a36Sopenharmony_ci 2762306a36Sopenharmony_ciasmlinkage void serpent_ecb_dec_8way_avx(const void *ctx, u8 *dst, 2862306a36Sopenharmony_ci const u8 *src); 2962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(serpent_ecb_dec_8way_avx); 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ciasmlinkage void serpent_cbc_dec_8way_avx(const void *ctx, u8 *dst, 3262306a36Sopenharmony_ci const u8 *src); 3362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(serpent_cbc_dec_8way_avx); 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_cistatic int serpent_setkey_skcipher(struct crypto_skcipher *tfm, 3662306a36Sopenharmony_ci const u8 *key, unsigned int keylen) 3762306a36Sopenharmony_ci{ 3862306a36Sopenharmony_ci return __serpent_setkey(crypto_skcipher_ctx(tfm), key, keylen); 3962306a36Sopenharmony_ci} 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_cistatic int ecb_encrypt(struct skcipher_request *req) 4262306a36Sopenharmony_ci{ 4362306a36Sopenharmony_ci ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS); 4462306a36Sopenharmony_ci ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_enc_8way_avx); 4562306a36Sopenharmony_ci ECB_BLOCK(1, __serpent_encrypt); 4662306a36Sopenharmony_ci ECB_WALK_END(); 4762306a36Sopenharmony_ci} 4862306a36Sopenharmony_ci 4962306a36Sopenharmony_cistatic int ecb_decrypt(struct skcipher_request *req) 5062306a36Sopenharmony_ci{ 5162306a36Sopenharmony_ci ECB_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS); 5262306a36Sopenharmony_ci ECB_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_ecb_dec_8way_avx); 5362306a36Sopenharmony_ci ECB_BLOCK(1, __serpent_decrypt); 5462306a36Sopenharmony_ci ECB_WALK_END(); 5562306a36Sopenharmony_ci} 5662306a36Sopenharmony_ci 5762306a36Sopenharmony_cistatic int cbc_encrypt(struct skcipher_request *req) 5862306a36Sopenharmony_ci{ 5962306a36Sopenharmony_ci CBC_WALK_START(req, SERPENT_BLOCK_SIZE, -1); 6062306a36Sopenharmony_ci CBC_ENC_BLOCK(__serpent_encrypt); 6162306a36Sopenharmony_ci CBC_WALK_END(); 6262306a36Sopenharmony_ci} 6362306a36Sopenharmony_ci 6462306a36Sopenharmony_cistatic int cbc_decrypt(struct skcipher_request *req) 6562306a36Sopenharmony_ci{ 6662306a36Sopenharmony_ci CBC_WALK_START(req, SERPENT_BLOCK_SIZE, SERPENT_PARALLEL_BLOCKS); 6762306a36Sopenharmony_ci CBC_DEC_BLOCK(SERPENT_PARALLEL_BLOCKS, serpent_cbc_dec_8way_avx); 6862306a36Sopenharmony_ci CBC_DEC_BLOCK(1, __serpent_decrypt); 6962306a36Sopenharmony_ci CBC_WALK_END(); 7062306a36Sopenharmony_ci} 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_cistatic struct skcipher_alg serpent_algs[] = { 7362306a36Sopenharmony_ci { 7462306a36Sopenharmony_ci .base.cra_name = "__ecb(serpent)", 7562306a36Sopenharmony_ci .base.cra_driver_name = "__ecb-serpent-avx", 7662306a36Sopenharmony_ci .base.cra_priority = 500, 7762306a36Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 7862306a36Sopenharmony_ci .base.cra_blocksize = SERPENT_BLOCK_SIZE, 7962306a36Sopenharmony_ci .base.cra_ctxsize = sizeof(struct serpent_ctx), 8062306a36Sopenharmony_ci .base.cra_module = THIS_MODULE, 8162306a36Sopenharmony_ci .min_keysize = SERPENT_MIN_KEY_SIZE, 8262306a36Sopenharmony_ci .max_keysize = SERPENT_MAX_KEY_SIZE, 8362306a36Sopenharmony_ci .setkey = serpent_setkey_skcipher, 8462306a36Sopenharmony_ci .encrypt = ecb_encrypt, 8562306a36Sopenharmony_ci .decrypt = ecb_decrypt, 8662306a36Sopenharmony_ci }, { 8762306a36Sopenharmony_ci .base.cra_name = "__cbc(serpent)", 8862306a36Sopenharmony_ci .base.cra_driver_name = "__cbc-serpent-avx", 8962306a36Sopenharmony_ci .base.cra_priority = 500, 9062306a36Sopenharmony_ci .base.cra_flags = CRYPTO_ALG_INTERNAL, 9162306a36Sopenharmony_ci .base.cra_blocksize = SERPENT_BLOCK_SIZE, 9262306a36Sopenharmony_ci .base.cra_ctxsize = sizeof(struct serpent_ctx), 9362306a36Sopenharmony_ci .base.cra_module = THIS_MODULE, 9462306a36Sopenharmony_ci .min_keysize = SERPENT_MIN_KEY_SIZE, 9562306a36Sopenharmony_ci .max_keysize = SERPENT_MAX_KEY_SIZE, 9662306a36Sopenharmony_ci .ivsize = SERPENT_BLOCK_SIZE, 9762306a36Sopenharmony_ci .setkey = serpent_setkey_skcipher, 9862306a36Sopenharmony_ci .encrypt = cbc_encrypt, 9962306a36Sopenharmony_ci .decrypt = cbc_decrypt, 10062306a36Sopenharmony_ci }, 10162306a36Sopenharmony_ci}; 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_cistatic struct simd_skcipher_alg *serpent_simd_algs[ARRAY_SIZE(serpent_algs)]; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_cistatic int __init serpent_init(void) 10662306a36Sopenharmony_ci{ 10762306a36Sopenharmony_ci const char *feature_name; 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, 11062306a36Sopenharmony_ci &feature_name)) { 11162306a36Sopenharmony_ci pr_info("CPU feature '%s' is not supported.\n", feature_name); 11262306a36Sopenharmony_ci return -ENODEV; 11362306a36Sopenharmony_ci } 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci return simd_register_skciphers_compat(serpent_algs, 11662306a36Sopenharmony_ci ARRAY_SIZE(serpent_algs), 11762306a36Sopenharmony_ci serpent_simd_algs); 11862306a36Sopenharmony_ci} 11962306a36Sopenharmony_ci 12062306a36Sopenharmony_cistatic void __exit serpent_exit(void) 12162306a36Sopenharmony_ci{ 12262306a36Sopenharmony_ci simd_unregister_skciphers(serpent_algs, ARRAY_SIZE(serpent_algs), 12362306a36Sopenharmony_ci serpent_simd_algs); 12462306a36Sopenharmony_ci} 12562306a36Sopenharmony_ci 12662306a36Sopenharmony_cimodule_init(serpent_init); 12762306a36Sopenharmony_cimodule_exit(serpent_exit); 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ciMODULE_DESCRIPTION("Serpent Cipher Algorithm, AVX optimized"); 13062306a36Sopenharmony_ciMODULE_LICENSE("GPL"); 13162306a36Sopenharmony_ciMODULE_ALIAS_CRYPTO("serpent"); 132