1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Glue Code for x86_64/AVX2/AES-NI assembler optimized version of Camellia
5 * Copyright © 2013 Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
8 #include <asm/crypto/camellia.h>
9 #include <asm/crypto/glue_helper.h>
10 #include <crypto/algapi.h>
11 #include <crypto/internal/simd.h>
12 #include <crypto/xts.h>
13 #include <linux/crypto.h>
14 #include <linux/err.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
18 #define CAMELLIA_AESNI_PARALLEL_BLOCKS 16
19 #define CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS 32
21 /* 32-way AVX2/AES-NI parallel cipher functions */
22 asmlinkage void camellia_ecb_enc_32way(const void *ctx, u8 *dst, const u8 *src);
23 asmlinkage void camellia_ecb_dec_32way(const void *ctx, u8 *dst, const u8 *src);
25 asmlinkage void camellia_cbc_dec_32way(const void *ctx, u8 *dst, const u8 *src);
26 asmlinkage void camellia_ctr_32way(const void *ctx, u8 *dst, const u8 *src,
29 asmlinkage void camellia_xts_enc_32way(const void *ctx, u8 *dst, const u8 *src,
31 asmlinkage void camellia_xts_dec_32way(const void *ctx, u8 *dst, const u8 *src,
34 static const struct common_glue_ctx camellia_enc = {
36 .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
39 .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
40 .fn_u = { .ecb = camellia_ecb_enc_32way }
42 .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
43 .fn_u = { .ecb = camellia_ecb_enc_16way }
46 .fn_u = { .ecb = camellia_enc_blk_2way }
49 .fn_u = { .ecb = camellia_enc_blk }
53 static const struct common_glue_ctx camellia_ctr = {
55 .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
58 .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
59 .fn_u = { .ctr = camellia_ctr_32way }
61 .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
62 .fn_u = { .ctr = camellia_ctr_16way }
65 .fn_u = { .ctr = camellia_crypt_ctr_2way }
68 .fn_u = { .ctr = camellia_crypt_ctr }
72 static const struct common_glue_ctx camellia_enc_xts = {
74 .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
77 .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
78 .fn_u = { .xts = camellia_xts_enc_32way }
80 .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
81 .fn_u = { .xts = camellia_xts_enc_16way }
84 .fn_u = { .xts = camellia_xts_enc }
88 static const struct common_glue_ctx camellia_dec = {
90 .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
93 .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
94 .fn_u = { .ecb = camellia_ecb_dec_32way }
96 .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
97 .fn_u = { .ecb = camellia_ecb_dec_16way }
100 .fn_u = { .ecb = camellia_dec_blk_2way }
103 .fn_u = { .ecb = camellia_dec_blk }
107 static const struct common_glue_ctx camellia_dec_cbc = {
109 .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
112 .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
113 .fn_u = { .cbc = camellia_cbc_dec_32way }
115 .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
116 .fn_u = { .cbc = camellia_cbc_dec_16way }
119 .fn_u = { .cbc = camellia_decrypt_cbc_2way }
122 .fn_u = { .cbc = camellia_dec_blk }
126 static const struct common_glue_ctx camellia_dec_xts = {
128 .fpu_blocks_limit = CAMELLIA_AESNI_PARALLEL_BLOCKS,
131 .num_blocks = CAMELLIA_AESNI_AVX2_PARALLEL_BLOCKS,
132 .fn_u = { .xts = camellia_xts_dec_32way }
134 .num_blocks = CAMELLIA_AESNI_PARALLEL_BLOCKS,
135 .fn_u = { .xts = camellia_xts_dec_16way }
138 .fn_u = { .xts = camellia_xts_dec }
142 static int camellia_setkey(struct crypto_skcipher *tfm, const u8 *key,
145 return __camellia_setkey(crypto_skcipher_ctx(tfm), key, keylen,
146 &tfm->base.crt_flags);
149 static int ecb_encrypt(struct skcipher_request *req)
151 return glue_ecb_req_128bit(&camellia_enc, req);
154 static int ecb_decrypt(struct skcipher_request *req)
156 return glue_ecb_req_128bit(&camellia_dec, req);
159 static int cbc_encrypt(struct skcipher_request *req)
161 return glue_cbc_encrypt_req_128bit(camellia_enc_blk, req);
164 static int cbc_decrypt(struct skcipher_request *req)
166 return glue_cbc_decrypt_req_128bit(&camellia_dec_cbc, req);
169 static int ctr_crypt(struct skcipher_request *req)
171 return glue_ctr_req_128bit(&camellia_ctr, req);
174 static int xts_encrypt(struct skcipher_request *req)
176 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
177 struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
179 return glue_xts_req_128bit(&camellia_enc_xts, req, camellia_enc_blk,
180 &ctx->tweak_ctx, &ctx->crypt_ctx, false);
183 static int xts_decrypt(struct skcipher_request *req)
185 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
186 struct camellia_xts_ctx *ctx = crypto_skcipher_ctx(tfm);
188 return glue_xts_req_128bit(&camellia_dec_xts, req, camellia_enc_blk,
189 &ctx->tweak_ctx, &ctx->crypt_ctx, true);
192 static struct skcipher_alg camellia_algs[] = {
194 .base.cra_name = "__ecb(camellia)",
195 .base.cra_driver_name = "__ecb-camellia-aesni-avx2",
196 .base.cra_priority = 500,
197 .base.cra_flags = CRYPTO_ALG_INTERNAL,
198 .base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
199 .base.cra_ctxsize = sizeof(struct camellia_ctx),
200 .base.cra_module = THIS_MODULE,
201 .min_keysize = CAMELLIA_MIN_KEY_SIZE,
202 .max_keysize = CAMELLIA_MAX_KEY_SIZE,
203 .setkey = camellia_setkey,
204 .encrypt = ecb_encrypt,
205 .decrypt = ecb_decrypt,
207 .base.cra_name = "__cbc(camellia)",
208 .base.cra_driver_name = "__cbc-camellia-aesni-avx2",
209 .base.cra_priority = 500,
210 .base.cra_flags = CRYPTO_ALG_INTERNAL,
211 .base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
212 .base.cra_ctxsize = sizeof(struct camellia_ctx),
213 .base.cra_module = THIS_MODULE,
214 .min_keysize = CAMELLIA_MIN_KEY_SIZE,
215 .max_keysize = CAMELLIA_MAX_KEY_SIZE,
216 .ivsize = CAMELLIA_BLOCK_SIZE,
217 .setkey = camellia_setkey,
218 .encrypt = cbc_encrypt,
219 .decrypt = cbc_decrypt,
221 .base.cra_name = "__ctr(camellia)",
222 .base.cra_driver_name = "__ctr-camellia-aesni-avx2",
223 .base.cra_priority = 500,
224 .base.cra_flags = CRYPTO_ALG_INTERNAL,
225 .base.cra_blocksize = 1,
226 .base.cra_ctxsize = sizeof(struct camellia_ctx),
227 .base.cra_module = THIS_MODULE,
228 .min_keysize = CAMELLIA_MIN_KEY_SIZE,
229 .max_keysize = CAMELLIA_MAX_KEY_SIZE,
230 .ivsize = CAMELLIA_BLOCK_SIZE,
231 .chunksize = CAMELLIA_BLOCK_SIZE,
232 .setkey = camellia_setkey,
233 .encrypt = ctr_crypt,
234 .decrypt = ctr_crypt,
236 .base.cra_name = "__xts(camellia)",
237 .base.cra_driver_name = "__xts-camellia-aesni-avx2",
238 .base.cra_priority = 500,
239 .base.cra_flags = CRYPTO_ALG_INTERNAL,
240 .base.cra_blocksize = CAMELLIA_BLOCK_SIZE,
241 .base.cra_ctxsize = sizeof(struct camellia_xts_ctx),
242 .base.cra_module = THIS_MODULE,
243 .min_keysize = 2 * CAMELLIA_MIN_KEY_SIZE,
244 .max_keysize = 2 * CAMELLIA_MAX_KEY_SIZE,
245 .ivsize = CAMELLIA_BLOCK_SIZE,
246 .setkey = xts_camellia_setkey,
247 .encrypt = xts_encrypt,
248 .decrypt = xts_decrypt,
252 static struct simd_skcipher_alg *camellia_simd_algs[ARRAY_SIZE(camellia_algs)];
254 static int __init camellia_aesni_init(void)
256 const char *feature_name;
258 if (!boot_cpu_has(X86_FEATURE_AVX) ||
259 !boot_cpu_has(X86_FEATURE_AVX2) ||
260 !boot_cpu_has(X86_FEATURE_AES) ||
261 !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
262 pr_info("AVX2 or AES-NI instructions are not detected.\n");
266 if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
268 pr_info("CPU feature '%s' is not supported.\n", feature_name);
272 return simd_register_skciphers_compat(camellia_algs,
273 ARRAY_SIZE(camellia_algs),
277 static void __exit camellia_aesni_fini(void)
279 simd_unregister_skciphers(camellia_algs, ARRAY_SIZE(camellia_algs),
283 module_init(camellia_aesni_init);
284 module_exit(camellia_aesni_fini);
286 MODULE_LICENSE("GPL");
287 MODULE_DESCRIPTION("Camellia Cipher Algorithm, AES-NI/AVX2 optimized");
288 MODULE_ALIAS_CRYPTO("camellia");
289 MODULE_ALIAS_CRYPTO("camellia-asm");