camellia_aesni_avx_glue.c (4137B)
1// SPDX-License-Identifier: GPL-2.0-or-later 2/* 3 * Glue Code for x86_64/AVX/AES-NI assembler optimized version of Camellia 4 * 5 * Copyright © 2012-2013 Jussi Kivilinna <jussi.kivilinna@iki.fi> 6 */ 7 8#include <crypto/algapi.h> 9#include <crypto/internal/simd.h> 10#include <linux/crypto.h> 11#include <linux/err.h> 12#include <linux/module.h> 13#include <linux/types.h> 14 15#include "camellia.h" 16#include "ecb_cbc_helpers.h" 17 18#define CAMELLIA_AESNI_PARALLEL_BLOCKS 16 19 20/* 16-way parallel cipher functions (avx/aes-ni) */ 21asmlinkage void camellia_ecb_enc_16way(const void *ctx, u8 *dst, const u8 *src); 22EXPORT_SYMBOL_GPL(camellia_ecb_enc_16way); 23 24asmlinkage void camellia_ecb_dec_16way(const void *ctx, u8 *dst, const u8 *src); 25EXPORT_SYMBOL_GPL(camellia_ecb_dec_16way); 26 27asmlinkage void camellia_cbc_dec_16way(const void *ctx, u8 *dst, const u8 *src); 28EXPORT_SYMBOL_GPL(camellia_cbc_dec_16way); 29 30static int camellia_setkey(struct crypto_skcipher *tfm, const u8 *key, 31 unsigned int keylen) 32{ 33 return __camellia_setkey(crypto_skcipher_ctx(tfm), key, keylen); 34} 35 36static int ecb_encrypt(struct skcipher_request *req) 37{ 38 ECB_WALK_START(req, CAMELLIA_BLOCK_SIZE, CAMELLIA_AESNI_PARALLEL_BLOCKS); 39 ECB_BLOCK(CAMELLIA_AESNI_PARALLEL_BLOCKS, camellia_ecb_enc_16way); 40 ECB_BLOCK(2, camellia_enc_blk_2way); 41 ECB_BLOCK(1, camellia_enc_blk); 42 ECB_WALK_END(); 43} 44 45static int ecb_decrypt(struct skcipher_request *req) 46{ 47 ECB_WALK_START(req, CAMELLIA_BLOCK_SIZE, CAMELLIA_AESNI_PARALLEL_BLOCKS); 48 ECB_BLOCK(CAMELLIA_AESNI_PARALLEL_BLOCKS, camellia_ecb_dec_16way); 49 ECB_BLOCK(2, camellia_dec_blk_2way); 50 ECB_BLOCK(1, camellia_dec_blk); 51 ECB_WALK_END(); 52} 53 54static int cbc_encrypt(struct skcipher_request *req) 55{ 56 CBC_WALK_START(req, CAMELLIA_BLOCK_SIZE, -1); 57 CBC_ENC_BLOCK(camellia_enc_blk); 58 CBC_WALK_END(); 59} 60 61static int cbc_decrypt(struct skcipher_request *req) 62{ 63 CBC_WALK_START(req, CAMELLIA_BLOCK_SIZE, CAMELLIA_AESNI_PARALLEL_BLOCKS); 64 CBC_DEC_BLOCK(CAMELLIA_AESNI_PARALLEL_BLOCKS, camellia_cbc_dec_16way); 65 CBC_DEC_BLOCK(2, camellia_decrypt_cbc_2way); 66 CBC_DEC_BLOCK(1, camellia_dec_blk); 67 CBC_WALK_END(); 68} 69 70static struct skcipher_alg camellia_algs[] = { 71 { 72 .base.cra_name = "__ecb(camellia)", 73 .base.cra_driver_name = "__ecb-camellia-aesni", 74 .base.cra_priority = 400, 75 .base.cra_flags = CRYPTO_ALG_INTERNAL, 76 .base.cra_blocksize = CAMELLIA_BLOCK_SIZE, 77 .base.cra_ctxsize = sizeof(struct camellia_ctx), 78 .base.cra_module = THIS_MODULE, 79 .min_keysize = CAMELLIA_MIN_KEY_SIZE, 80 .max_keysize = CAMELLIA_MAX_KEY_SIZE, 81 .setkey = camellia_setkey, 82 .encrypt = ecb_encrypt, 83 .decrypt = ecb_decrypt, 84 }, { 85 .base.cra_name = "__cbc(camellia)", 86 .base.cra_driver_name = "__cbc-camellia-aesni", 87 .base.cra_priority = 400, 88 .base.cra_flags = CRYPTO_ALG_INTERNAL, 89 .base.cra_blocksize = CAMELLIA_BLOCK_SIZE, 90 .base.cra_ctxsize = sizeof(struct camellia_ctx), 91 .base.cra_module = THIS_MODULE, 92 .min_keysize = CAMELLIA_MIN_KEY_SIZE, 93 .max_keysize = CAMELLIA_MAX_KEY_SIZE, 94 .ivsize = CAMELLIA_BLOCK_SIZE, 95 .setkey = camellia_setkey, 96 .encrypt = cbc_encrypt, 97 .decrypt = cbc_decrypt, 98 } 99}; 100 101static struct simd_skcipher_alg *camellia_simd_algs[ARRAY_SIZE(camellia_algs)]; 102 103static int __init camellia_aesni_init(void) 104{ 105 const char *feature_name; 106 107 if (!boot_cpu_has(X86_FEATURE_AVX) || 108 !boot_cpu_has(X86_FEATURE_AES) || 109 !boot_cpu_has(X86_FEATURE_OSXSAVE)) { 110 pr_info("AVX or AES-NI instructions are not detected.\n"); 111 return -ENODEV; 112 } 113 114 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM, 115 &feature_name)) { 116 pr_info("CPU feature '%s' is not supported.\n", feature_name); 117 return -ENODEV; 118 } 119 120 return simd_register_skciphers_compat(camellia_algs, 121 ARRAY_SIZE(camellia_algs), 122 camellia_simd_algs); 123} 124 125static void __exit camellia_aesni_fini(void) 126{ 127 simd_unregister_skciphers(camellia_algs, ARRAY_SIZE(camellia_algs), 128 camellia_simd_algs); 129} 130 131module_init(camellia_aesni_init); 132module_exit(camellia_aesni_fini); 133 134MODULE_LICENSE("GPL"); 135MODULE_DESCRIPTION("Camellia Cipher Algorithm, AES-NI/AVX optimized"); 136MODULE_ALIAS_CRYPTO("camellia"); 137MODULE_ALIAS_CRYPTO("camellia-asm");