164d85cc9SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2*dbb153c0SAditya Srivastava /*
34f7f60d3SMarcelo H. Cerri * AES CTR routines supporting VMX instructions on the Power 8
44f7f60d3SMarcelo H. Cerri *
54f7f60d3SMarcelo H. Cerri * Copyright (C) 2015 International Business Machines Inc.
64f7f60d3SMarcelo H. Cerri *
74f7f60d3SMarcelo H. Cerri * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
84f7f60d3SMarcelo H. Cerri */
94f7f60d3SMarcelo H. Cerri
10626ddb2fSEric Biggers #include <asm/simd.h>
114f7f60d3SMarcelo H. Cerri #include <asm/switch_to.h>
124f7f60d3SMarcelo H. Cerri #include <crypto/aes.h>
13626ddb2fSEric Biggers #include <crypto/internal/simd.h>
142621a869SEric Biggers #include <crypto/internal/skcipher.h>
15e666d4e9SPaulo Flabiano Smorigo
164f7f60d3SMarcelo H. Cerri #include "aesp8-ppc.h"
174f7f60d3SMarcelo H. Cerri
184f7f60d3SMarcelo H. Cerri struct p8_aes_ctr_ctx {
192621a869SEric Biggers struct crypto_skcipher *fallback;
204f7f60d3SMarcelo H. Cerri struct aes_key enc_key;
214f7f60d3SMarcelo H. Cerri };
224f7f60d3SMarcelo H. Cerri
p8_aes_ctr_init(struct crypto_skcipher * tfm)232621a869SEric Biggers static int p8_aes_ctr_init(struct crypto_skcipher *tfm)
244f7f60d3SMarcelo H. Cerri {
252621a869SEric Biggers struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
262621a869SEric Biggers struct crypto_skcipher *fallback;
274f7f60d3SMarcelo H. Cerri
282621a869SEric Biggers fallback = crypto_alloc_skcipher("ctr(aes)", 0,
292621a869SEric Biggers CRYPTO_ALG_NEED_FALLBACK |
302621a869SEric Biggers CRYPTO_ALG_ASYNC);
314f7f60d3SMarcelo H. Cerri if (IS_ERR(fallback)) {
322621a869SEric Biggers pr_err("Failed to allocate ctr(aes) fallback: %ld\n",
332621a869SEric Biggers PTR_ERR(fallback));
344f7f60d3SMarcelo H. Cerri return PTR_ERR(fallback);
354f7f60d3SMarcelo H. Cerri }
364f7f60d3SMarcelo H. Cerri
372621a869SEric Biggers crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
382621a869SEric Biggers crypto_skcipher_reqsize(fallback));
394f7f60d3SMarcelo H. Cerri ctx->fallback = fallback;
404f7f60d3SMarcelo H. Cerri return 0;
414f7f60d3SMarcelo H. Cerri }
424f7f60d3SMarcelo H. Cerri
p8_aes_ctr_exit(struct crypto_skcipher * tfm)432621a869SEric Biggers static void p8_aes_ctr_exit(struct crypto_skcipher *tfm)
444f7f60d3SMarcelo H. Cerri {
452621a869SEric Biggers struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
464f7f60d3SMarcelo H. Cerri
472621a869SEric Biggers crypto_free_skcipher(ctx->fallback);
484f7f60d3SMarcelo H. Cerri }
494f7f60d3SMarcelo H. Cerri
p8_aes_ctr_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)502621a869SEric Biggers static int p8_aes_ctr_setkey(struct crypto_skcipher *tfm, const u8 *key,
514f7f60d3SMarcelo H. Cerri unsigned int keylen)
524f7f60d3SMarcelo H. Cerri {
532621a869SEric Biggers struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
544f7f60d3SMarcelo H. Cerri int ret;
554f7f60d3SMarcelo H. Cerri
567dede913SLi Zhong preempt_disable();
574f7f60d3SMarcelo H. Cerri pagefault_disable();
582d6f0600SLeonidas Da Silva Barbosa enable_kernel_vsx();
594f7f60d3SMarcelo H. Cerri ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
60dc4fbba1SAnton Blanchard disable_kernel_vsx();
614f7f60d3SMarcelo H. Cerri pagefault_enable();
627dede913SLi Zhong preempt_enable();
634f7f60d3SMarcelo H. Cerri
642621a869SEric Biggers ret |= crypto_skcipher_setkey(ctx->fallback, key, keylen);
65694e0db6SEric Biggers
66694e0db6SEric Biggers return ret ? -EINVAL : 0;
674f7f60d3SMarcelo H. Cerri }
684f7f60d3SMarcelo H. Cerri
p8_aes_ctr_final(const struct p8_aes_ctr_ctx * ctx,struct skcipher_walk * walk)692621a869SEric Biggers static void p8_aes_ctr_final(const struct p8_aes_ctr_ctx *ctx,
702621a869SEric Biggers struct skcipher_walk *walk)
714f7f60d3SMarcelo H. Cerri {
724f7f60d3SMarcelo H. Cerri u8 *ctrblk = walk->iv;
734f7f60d3SMarcelo H. Cerri u8 keystream[AES_BLOCK_SIZE];
744f7f60d3SMarcelo H. Cerri u8 *src = walk->src.virt.addr;
754f7f60d3SMarcelo H. Cerri u8 *dst = walk->dst.virt.addr;
764f7f60d3SMarcelo H. Cerri unsigned int nbytes = walk->nbytes;
774f7f60d3SMarcelo H. Cerri
787dede913SLi Zhong preempt_disable();
794f7f60d3SMarcelo H. Cerri pagefault_disable();
802d6f0600SLeonidas Da Silva Barbosa enable_kernel_vsx();
814f7f60d3SMarcelo H. Cerri aes_p8_encrypt(ctrblk, keystream, &ctx->enc_key);
82dc4fbba1SAnton Blanchard disable_kernel_vsx();
834f7f60d3SMarcelo H. Cerri pagefault_enable();
847dede913SLi Zhong preempt_enable();
854f7f60d3SMarcelo H. Cerri
8645fe93dfSArd Biesheuvel crypto_xor_cpy(dst, keystream, src, nbytes);
874f7f60d3SMarcelo H. Cerri crypto_inc(ctrblk, AES_BLOCK_SIZE);
884f7f60d3SMarcelo H. Cerri }
894f7f60d3SMarcelo H. Cerri
p8_aes_ctr_crypt(struct skcipher_request * req)902621a869SEric Biggers static int p8_aes_ctr_crypt(struct skcipher_request *req)
914f7f60d3SMarcelo H. Cerri {
922621a869SEric Biggers struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
932621a869SEric Biggers const struct p8_aes_ctr_ctx *ctx = crypto_skcipher_ctx(tfm);
942621a869SEric Biggers struct skcipher_walk walk;
952621a869SEric Biggers unsigned int nbytes;
964f7f60d3SMarcelo H. Cerri int ret;
974f7f60d3SMarcelo H. Cerri
98626ddb2fSEric Biggers if (!crypto_simd_usable()) {
992621a869SEric Biggers struct skcipher_request *subreq = skcipher_request_ctx(req);
1002621a869SEric Biggers
1012621a869SEric Biggers *subreq = *req;
1022621a869SEric Biggers skcipher_request_set_tfm(subreq, ctx->fallback);
1032621a869SEric Biggers return crypto_skcipher_encrypt(subreq);
1042621a869SEric Biggers }
1052621a869SEric Biggers
1062621a869SEric Biggers ret = skcipher_walk_virt(&walk, req, false);
1074f7f60d3SMarcelo H. Cerri while ((nbytes = walk.nbytes) >= AES_BLOCK_SIZE) {
1087dede913SLi Zhong preempt_disable();
1094f7f60d3SMarcelo H. Cerri pagefault_disable();
1102d6f0600SLeonidas Da Silva Barbosa enable_kernel_vsx();
1114beb1060SHerbert Xu aes_p8_ctr32_encrypt_blocks(walk.src.virt.addr,
1124beb1060SHerbert Xu walk.dst.virt.addr,
1132621a869SEric Biggers nbytes / AES_BLOCK_SIZE,
1142621a869SEric Biggers &ctx->enc_key, walk.iv);
115dc4fbba1SAnton Blanchard disable_kernel_vsx();
1164f7f60d3SMarcelo H. Cerri pagefault_enable();
1177dede913SLi Zhong preempt_enable();
1184f7f60d3SMarcelo H. Cerri
1192621a869SEric Biggers do {
1204f7f60d3SMarcelo H. Cerri crypto_inc(walk.iv, AES_BLOCK_SIZE);
1212621a869SEric Biggers } while ((nbytes -= AES_BLOCK_SIZE) >= AES_BLOCK_SIZE);
1221d4aa0b4SLeonidas Da Silva Barbosa
1232621a869SEric Biggers ret = skcipher_walk_done(&walk, nbytes);
1244f7f60d3SMarcelo H. Cerri }
1252621a869SEric Biggers if (nbytes) {
1264f7f60d3SMarcelo H. Cerri p8_aes_ctr_final(ctx, &walk);
1272621a869SEric Biggers ret = skcipher_walk_done(&walk, 0);
1284f7f60d3SMarcelo H. Cerri }
1294f7f60d3SMarcelo H. Cerri return ret;
1304f7f60d3SMarcelo H. Cerri }
1314f7f60d3SMarcelo H. Cerri
1322621a869SEric Biggers struct skcipher_alg p8_aes_ctr_alg = {
1332621a869SEric Biggers .base.cra_name = "ctr(aes)",
1342621a869SEric Biggers .base.cra_driver_name = "p8_aes_ctr",
1352621a869SEric Biggers .base.cra_module = THIS_MODULE,
1362621a869SEric Biggers .base.cra_priority = 2000,
1372621a869SEric Biggers .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
1382621a869SEric Biggers .base.cra_blocksize = 1,
1392621a869SEric Biggers .base.cra_ctxsize = sizeof(struct p8_aes_ctr_ctx),
1404f7f60d3SMarcelo H. Cerri .setkey = p8_aes_ctr_setkey,
1414f7f60d3SMarcelo H. Cerri .encrypt = p8_aes_ctr_crypt,
1424f7f60d3SMarcelo H. Cerri .decrypt = p8_aes_ctr_crypt,
1432621a869SEric Biggers .init = p8_aes_ctr_init,
1442621a869SEric Biggers .exit = p8_aes_ctr_exit,
1452621a869SEric Biggers .min_keysize = AES_MIN_KEY_SIZE,
1462621a869SEric Biggers .max_keysize = AES_MAX_KEY_SIZE,
1472621a869SEric Biggers .ivsize = AES_BLOCK_SIZE,
1482621a869SEric Biggers .chunksize = AES_BLOCK_SIZE,
1494f7f60d3SMarcelo H. Cerri };
150