1ba3579e6STaehee Yoo /* SPDX-License-Identifier: GPL-2.0-or-later */
2ba3579e6STaehee Yoo /*
3ba3579e6STaehee Yoo * Glue Code for the AVX/AES-NI/GFNI assembler implementation of the ARIA Cipher
4ba3579e6STaehee Yoo *
5ba3579e6STaehee Yoo * Copyright (c) 2022 Taehee Yoo <ap420073@gmail.com>
6ba3579e6STaehee Yoo */
7ba3579e6STaehee Yoo
8ba3579e6STaehee Yoo #include <crypto/algapi.h>
9ba3579e6STaehee Yoo #include <crypto/internal/simd.h>
10ba3579e6STaehee Yoo #include <crypto/aria.h>
11ba3579e6STaehee Yoo #include <linux/crypto.h>
12ba3579e6STaehee Yoo #include <linux/err.h>
13ba3579e6STaehee Yoo #include <linux/module.h>
14ba3579e6STaehee Yoo #include <linux/types.h>
15ba3579e6STaehee Yoo
16ba3579e6STaehee Yoo #include "ecb_cbc_helpers.h"
17ba3579e6STaehee Yoo #include "aria-avx.h"
18ba3579e6STaehee Yoo
19ba3579e6STaehee Yoo asmlinkage void aria_aesni_avx_encrypt_16way(const void *ctx, u8 *dst,
20ba3579e6STaehee Yoo const u8 *src);
2137d8d3aeSTaehee Yoo EXPORT_SYMBOL_GPL(aria_aesni_avx_encrypt_16way);
22ba3579e6STaehee Yoo asmlinkage void aria_aesni_avx_decrypt_16way(const void *ctx, u8 *dst,
23ba3579e6STaehee Yoo const u8 *src);
2437d8d3aeSTaehee Yoo EXPORT_SYMBOL_GPL(aria_aesni_avx_decrypt_16way);
25ba3579e6STaehee Yoo asmlinkage void aria_aesni_avx_ctr_crypt_16way(const void *ctx, u8 *dst,
26ba3579e6STaehee Yoo const u8 *src,
27ba3579e6STaehee Yoo u8 *keystream, u8 *iv);
2837d8d3aeSTaehee Yoo EXPORT_SYMBOL_GPL(aria_aesni_avx_ctr_crypt_16way);
29*e3cf2f87STaehee Yoo #ifdef CONFIG_AS_GFNI
30ba3579e6STaehee Yoo asmlinkage void aria_aesni_avx_gfni_encrypt_16way(const void *ctx, u8 *dst,
31ba3579e6STaehee Yoo const u8 *src);
3237d8d3aeSTaehee Yoo EXPORT_SYMBOL_GPL(aria_aesni_avx_gfni_encrypt_16way);
33ba3579e6STaehee Yoo asmlinkage void aria_aesni_avx_gfni_decrypt_16way(const void *ctx, u8 *dst,
34ba3579e6STaehee Yoo const u8 *src);
3537d8d3aeSTaehee Yoo EXPORT_SYMBOL_GPL(aria_aesni_avx_gfni_decrypt_16way);
36ba3579e6STaehee Yoo asmlinkage void aria_aesni_avx_gfni_ctr_crypt_16way(const void *ctx, u8 *dst,
37ba3579e6STaehee Yoo const u8 *src,
38ba3579e6STaehee Yoo u8 *keystream, u8 *iv);
3937d8d3aeSTaehee Yoo EXPORT_SYMBOL_GPL(aria_aesni_avx_gfni_ctr_crypt_16way);
40*e3cf2f87STaehee Yoo #endif /* CONFIG_AS_GFNI */
41ba3579e6STaehee Yoo
42ba3579e6STaehee Yoo static struct aria_avx_ops aria_ops;
43ba3579e6STaehee Yoo
448e7d7ce2STaehee Yoo struct aria_avx_request_ctx {
458e7d7ce2STaehee Yoo u8 keystream[ARIA_AESNI_PARALLEL_BLOCK_SIZE];
468e7d7ce2STaehee Yoo };
478e7d7ce2STaehee Yoo
ecb_do_encrypt(struct skcipher_request * req,const u32 * rkey)48ba3579e6STaehee Yoo static int ecb_do_encrypt(struct skcipher_request *req, const u32 *rkey)
49ba3579e6STaehee Yoo {
50ba3579e6STaehee Yoo ECB_WALK_START(req, ARIA_BLOCK_SIZE, ARIA_AESNI_PARALLEL_BLOCKS);
51ba3579e6STaehee Yoo ECB_BLOCK(ARIA_AESNI_PARALLEL_BLOCKS, aria_ops.aria_encrypt_16way);
52ba3579e6STaehee Yoo ECB_BLOCK(1, aria_encrypt);
53ba3579e6STaehee Yoo ECB_WALK_END();
54ba3579e6STaehee Yoo }
55ba3579e6STaehee Yoo
ecb_do_decrypt(struct skcipher_request * req,const u32 * rkey)56ba3579e6STaehee Yoo static int ecb_do_decrypt(struct skcipher_request *req, const u32 *rkey)
57ba3579e6STaehee Yoo {
58ba3579e6STaehee Yoo ECB_WALK_START(req, ARIA_BLOCK_SIZE, ARIA_AESNI_PARALLEL_BLOCKS);
59ba3579e6STaehee Yoo ECB_BLOCK(ARIA_AESNI_PARALLEL_BLOCKS, aria_ops.aria_decrypt_16way);
60ba3579e6STaehee Yoo ECB_BLOCK(1, aria_decrypt);
61ba3579e6STaehee Yoo ECB_WALK_END();
62ba3579e6STaehee Yoo }
63ba3579e6STaehee Yoo
aria_avx_ecb_encrypt(struct skcipher_request * req)64ba3579e6STaehee Yoo static int aria_avx_ecb_encrypt(struct skcipher_request *req)
65ba3579e6STaehee Yoo {
66ba3579e6STaehee Yoo struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
67ba3579e6STaehee Yoo struct aria_ctx *ctx = crypto_skcipher_ctx(tfm);
68ba3579e6STaehee Yoo
69ba3579e6STaehee Yoo return ecb_do_encrypt(req, ctx->enc_key[0]);
70ba3579e6STaehee Yoo }
71ba3579e6STaehee Yoo
aria_avx_ecb_decrypt(struct skcipher_request * req)72ba3579e6STaehee Yoo static int aria_avx_ecb_decrypt(struct skcipher_request *req)
73ba3579e6STaehee Yoo {
74ba3579e6STaehee Yoo struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
75ba3579e6STaehee Yoo struct aria_ctx *ctx = crypto_skcipher_ctx(tfm);
76ba3579e6STaehee Yoo
77ba3579e6STaehee Yoo return ecb_do_decrypt(req, ctx->dec_key[0]);
78ba3579e6STaehee Yoo }
79ba3579e6STaehee Yoo
aria_avx_set_key(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)80ba3579e6STaehee Yoo static int aria_avx_set_key(struct crypto_skcipher *tfm, const u8 *key,
81ba3579e6STaehee Yoo unsigned int keylen)
82ba3579e6STaehee Yoo {
83ba3579e6STaehee Yoo return aria_set_key(&tfm->base, key, keylen);
84ba3579e6STaehee Yoo }
85ba3579e6STaehee Yoo
aria_avx_ctr_encrypt(struct skcipher_request * req)86ba3579e6STaehee Yoo static int aria_avx_ctr_encrypt(struct skcipher_request *req)
87ba3579e6STaehee Yoo {
888e7d7ce2STaehee Yoo struct aria_avx_request_ctx *req_ctx = skcipher_request_ctx(req);
89ba3579e6STaehee Yoo struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
90ba3579e6STaehee Yoo struct aria_ctx *ctx = crypto_skcipher_ctx(tfm);
91ba3579e6STaehee Yoo struct skcipher_walk walk;
92ba3579e6STaehee Yoo unsigned int nbytes;
93ba3579e6STaehee Yoo int err;
94ba3579e6STaehee Yoo
95ba3579e6STaehee Yoo err = skcipher_walk_virt(&walk, req, false);
96ba3579e6STaehee Yoo
97ba3579e6STaehee Yoo while ((nbytes = walk.nbytes) > 0) {
98ba3579e6STaehee Yoo const u8 *src = walk.src.virt.addr;
99ba3579e6STaehee Yoo u8 *dst = walk.dst.virt.addr;
100ba3579e6STaehee Yoo
101ba3579e6STaehee Yoo while (nbytes >= ARIA_AESNI_PARALLEL_BLOCK_SIZE) {
102ba3579e6STaehee Yoo kernel_fpu_begin();
1038e7d7ce2STaehee Yoo aria_ops.aria_ctr_crypt_16way(ctx, dst, src,
1048e7d7ce2STaehee Yoo &req_ctx->keystream[0],
105ba3579e6STaehee Yoo walk.iv);
106ba3579e6STaehee Yoo kernel_fpu_end();
107ba3579e6STaehee Yoo dst += ARIA_AESNI_PARALLEL_BLOCK_SIZE;
108ba3579e6STaehee Yoo src += ARIA_AESNI_PARALLEL_BLOCK_SIZE;
109ba3579e6STaehee Yoo nbytes -= ARIA_AESNI_PARALLEL_BLOCK_SIZE;
110ba3579e6STaehee Yoo }
111ba3579e6STaehee Yoo
112ba3579e6STaehee Yoo while (nbytes >= ARIA_BLOCK_SIZE) {
1138e7d7ce2STaehee Yoo memcpy(&req_ctx->keystream[0], walk.iv, ARIA_BLOCK_SIZE);
114ba3579e6STaehee Yoo crypto_inc(walk.iv, ARIA_BLOCK_SIZE);
115ba3579e6STaehee Yoo
1168e7d7ce2STaehee Yoo aria_encrypt(ctx, &req_ctx->keystream[0],
1178e7d7ce2STaehee Yoo &req_ctx->keystream[0]);
118ba3579e6STaehee Yoo
1198e7d7ce2STaehee Yoo crypto_xor_cpy(dst, src, &req_ctx->keystream[0],
1208e7d7ce2STaehee Yoo ARIA_BLOCK_SIZE);
121ba3579e6STaehee Yoo dst += ARIA_BLOCK_SIZE;
122ba3579e6STaehee Yoo src += ARIA_BLOCK_SIZE;
123ba3579e6STaehee Yoo nbytes -= ARIA_BLOCK_SIZE;
124ba3579e6STaehee Yoo }
125ba3579e6STaehee Yoo
126ba3579e6STaehee Yoo if (walk.nbytes == walk.total && nbytes > 0) {
1278e7d7ce2STaehee Yoo memcpy(&req_ctx->keystream[0], walk.iv,
1288e7d7ce2STaehee Yoo ARIA_BLOCK_SIZE);
129ba3579e6STaehee Yoo crypto_inc(walk.iv, ARIA_BLOCK_SIZE);
130ba3579e6STaehee Yoo
1318e7d7ce2STaehee Yoo aria_encrypt(ctx, &req_ctx->keystream[0],
1328e7d7ce2STaehee Yoo &req_ctx->keystream[0]);
133ba3579e6STaehee Yoo
1348e7d7ce2STaehee Yoo crypto_xor_cpy(dst, src, &req_ctx->keystream[0],
1358e7d7ce2STaehee Yoo nbytes);
136ba3579e6STaehee Yoo dst += nbytes;
137ba3579e6STaehee Yoo src += nbytes;
138ba3579e6STaehee Yoo nbytes = 0;
139ba3579e6STaehee Yoo }
140ba3579e6STaehee Yoo err = skcipher_walk_done(&walk, nbytes);
141ba3579e6STaehee Yoo }
142ba3579e6STaehee Yoo
143ba3579e6STaehee Yoo return err;
144ba3579e6STaehee Yoo }
145ba3579e6STaehee Yoo
aria_avx_init_tfm(struct crypto_skcipher * tfm)1468e7d7ce2STaehee Yoo static int aria_avx_init_tfm(struct crypto_skcipher *tfm)
1478e7d7ce2STaehee Yoo {
1488e7d7ce2STaehee Yoo crypto_skcipher_set_reqsize(tfm, sizeof(struct aria_avx_request_ctx));
1498e7d7ce2STaehee Yoo
1508e7d7ce2STaehee Yoo return 0;
1518e7d7ce2STaehee Yoo }
1528e7d7ce2STaehee Yoo
153ba3579e6STaehee Yoo static struct skcipher_alg aria_algs[] = {
154ba3579e6STaehee Yoo {
155ba3579e6STaehee Yoo .base.cra_name = "__ecb(aria)",
156ba3579e6STaehee Yoo .base.cra_driver_name = "__ecb-aria-avx",
157ba3579e6STaehee Yoo .base.cra_priority = 400,
158ba3579e6STaehee Yoo .base.cra_flags = CRYPTO_ALG_INTERNAL,
159ba3579e6STaehee Yoo .base.cra_blocksize = ARIA_BLOCK_SIZE,
160ba3579e6STaehee Yoo .base.cra_ctxsize = sizeof(struct aria_ctx),
161ba3579e6STaehee Yoo .base.cra_module = THIS_MODULE,
162ba3579e6STaehee Yoo .min_keysize = ARIA_MIN_KEY_SIZE,
163ba3579e6STaehee Yoo .max_keysize = ARIA_MAX_KEY_SIZE,
164ba3579e6STaehee Yoo .setkey = aria_avx_set_key,
165ba3579e6STaehee Yoo .encrypt = aria_avx_ecb_encrypt,
166ba3579e6STaehee Yoo .decrypt = aria_avx_ecb_decrypt,
167ba3579e6STaehee Yoo }, {
168ba3579e6STaehee Yoo .base.cra_name = "__ctr(aria)",
169ba3579e6STaehee Yoo .base.cra_driver_name = "__ctr-aria-avx",
170ba3579e6STaehee Yoo .base.cra_priority = 400,
171ba3579e6STaehee Yoo .base.cra_flags = CRYPTO_ALG_INTERNAL,
172ba3579e6STaehee Yoo .base.cra_blocksize = 1,
173ba3579e6STaehee Yoo .base.cra_ctxsize = sizeof(struct aria_ctx),
174ba3579e6STaehee Yoo .base.cra_module = THIS_MODULE,
175ba3579e6STaehee Yoo .min_keysize = ARIA_MIN_KEY_SIZE,
176ba3579e6STaehee Yoo .max_keysize = ARIA_MAX_KEY_SIZE,
177ba3579e6STaehee Yoo .ivsize = ARIA_BLOCK_SIZE,
178ba3579e6STaehee Yoo .chunksize = ARIA_BLOCK_SIZE,
179ba3579e6STaehee Yoo .walksize = 16 * ARIA_BLOCK_SIZE,
180ba3579e6STaehee Yoo .setkey = aria_avx_set_key,
181ba3579e6STaehee Yoo .encrypt = aria_avx_ctr_encrypt,
182ba3579e6STaehee Yoo .decrypt = aria_avx_ctr_encrypt,
1838e7d7ce2STaehee Yoo .init = aria_avx_init_tfm,
184ba3579e6STaehee Yoo }
185ba3579e6STaehee Yoo };
186ba3579e6STaehee Yoo
187ba3579e6STaehee Yoo static struct simd_skcipher_alg *aria_simd_algs[ARRAY_SIZE(aria_algs)];
188ba3579e6STaehee Yoo
aria_avx_init(void)189ba3579e6STaehee Yoo static int __init aria_avx_init(void)
190ba3579e6STaehee Yoo {
191ba3579e6STaehee Yoo const char *feature_name;
192ba3579e6STaehee Yoo
193ba3579e6STaehee Yoo if (!boot_cpu_has(X86_FEATURE_AVX) ||
194ba3579e6STaehee Yoo !boot_cpu_has(X86_FEATURE_AES) ||
195ba3579e6STaehee Yoo !boot_cpu_has(X86_FEATURE_OSXSAVE)) {
196ba3579e6STaehee Yoo pr_info("AVX or AES-NI instructions are not detected.\n");
197ba3579e6STaehee Yoo return -ENODEV;
198ba3579e6STaehee Yoo }
199ba3579e6STaehee Yoo
200ba3579e6STaehee Yoo if (!cpu_has_xfeatures(XFEATURE_MASK_SSE | XFEATURE_MASK_YMM,
201ba3579e6STaehee Yoo &feature_name)) {
202ba3579e6STaehee Yoo pr_info("CPU feature '%s' is not supported.\n", feature_name);
203ba3579e6STaehee Yoo return -ENODEV;
204ba3579e6STaehee Yoo }
205ba3579e6STaehee Yoo
206*e3cf2f87STaehee Yoo if (boot_cpu_has(X86_FEATURE_GFNI) && IS_ENABLED(CONFIG_AS_GFNI)) {
207ba3579e6STaehee Yoo aria_ops.aria_encrypt_16way = aria_aesni_avx_gfni_encrypt_16way;
208ba3579e6STaehee Yoo aria_ops.aria_decrypt_16way = aria_aesni_avx_gfni_decrypt_16way;
209ba3579e6STaehee Yoo aria_ops.aria_ctr_crypt_16way = aria_aesni_avx_gfni_ctr_crypt_16way;
210ba3579e6STaehee Yoo } else {
211ba3579e6STaehee Yoo aria_ops.aria_encrypt_16way = aria_aesni_avx_encrypt_16way;
212ba3579e6STaehee Yoo aria_ops.aria_decrypt_16way = aria_aesni_avx_decrypt_16way;
213ba3579e6STaehee Yoo aria_ops.aria_ctr_crypt_16way = aria_aesni_avx_ctr_crypt_16way;
214ba3579e6STaehee Yoo }
215ba3579e6STaehee Yoo
216ba3579e6STaehee Yoo return simd_register_skciphers_compat(aria_algs,
217ba3579e6STaehee Yoo ARRAY_SIZE(aria_algs),
218ba3579e6STaehee Yoo aria_simd_algs);
219ba3579e6STaehee Yoo }
220ba3579e6STaehee Yoo
aria_avx_exit(void)221ba3579e6STaehee Yoo static void __exit aria_avx_exit(void)
222ba3579e6STaehee Yoo {
223ba3579e6STaehee Yoo simd_unregister_skciphers(aria_algs, ARRAY_SIZE(aria_algs),
224ba3579e6STaehee Yoo aria_simd_algs);
225ba3579e6STaehee Yoo }
226ba3579e6STaehee Yoo
227ba3579e6STaehee Yoo module_init(aria_avx_init);
228ba3579e6STaehee Yoo module_exit(aria_avx_exit);
229ba3579e6STaehee Yoo
230ba3579e6STaehee Yoo MODULE_LICENSE("GPL");
231ba3579e6STaehee Yoo MODULE_AUTHOR("Taehee Yoo <ap420073@gmail.com>");
232ba3579e6STaehee Yoo MODULE_DESCRIPTION("ARIA Cipher Algorithm, AVX/AES-NI/GFNI optimized");
233ba3579e6STaehee Yoo MODULE_ALIAS_CRYPTO("aria");
234ba3579e6STaehee Yoo MODULE_ALIAS_CRYPTO("aria-aesni-avx");
235