~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/arch/powerpc/crypto/aes_cbc.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 // SPDX-License-Identifier: GPL-2.0-only
  2 /*
  3  * AES CBC routines supporting VMX instructions on the Power 8
  4  *
  5  * Copyright (C) 2015 International Business Machines Inc.
  6  *
  7  * Author: Marcelo Henrique Cerri <mhcerri@br.ibm.com>
  8  */
  9 
 10 #include <asm/simd.h>
 11 #include <asm/switch_to.h>
 12 #include <crypto/aes.h>
 13 #include <crypto/internal/simd.h>
 14 #include <crypto/internal/skcipher.h>
 15 
 16 #include "aesp8-ppc.h"
 17 
 18 struct p8_aes_cbc_ctx {
 19         struct crypto_skcipher *fallback;
 20         struct aes_key enc_key;
 21         struct aes_key dec_key;
 22 };
 23 
 24 static int p8_aes_cbc_init(struct crypto_skcipher *tfm)
 25 {
 26         struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
 27         struct crypto_skcipher *fallback;
 28 
 29         fallback = crypto_alloc_skcipher("cbc(aes)", 0,
 30                                          CRYPTO_ALG_NEED_FALLBACK |
 31                                          CRYPTO_ALG_ASYNC);
 32         if (IS_ERR(fallback)) {
 33                 pr_err("Failed to allocate cbc(aes) fallback: %ld\n",
 34                        PTR_ERR(fallback));
 35                 return PTR_ERR(fallback);
 36         }
 37 
 38         crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
 39                                     crypto_skcipher_reqsize(fallback));
 40         ctx->fallback = fallback;
 41         return 0;
 42 }
 43 
 44 static void p8_aes_cbc_exit(struct crypto_skcipher *tfm)
 45 {
 46         struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
 47 
 48         crypto_free_skcipher(ctx->fallback);
 49 }
 50 
 51 static int p8_aes_cbc_setkey(struct crypto_skcipher *tfm, const u8 *key,
 52                              unsigned int keylen)
 53 {
 54         struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
 55         int ret;
 56 
 57         preempt_disable();
 58         pagefault_disable();
 59         enable_kernel_vsx();
 60         ret = aes_p8_set_encrypt_key(key, keylen * 8, &ctx->enc_key);
 61         ret |= aes_p8_set_decrypt_key(key, keylen * 8, &ctx->dec_key);
 62         disable_kernel_vsx();
 63         pagefault_enable();
 64         preempt_enable();
 65 
 66         ret |= crypto_skcipher_setkey(ctx->fallback, key, keylen);
 67 
 68         return ret ? -EINVAL : 0;
 69 }
 70 
 71 static int p8_aes_cbc_crypt(struct skcipher_request *req, int enc)
 72 {
 73         struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
 74         const struct p8_aes_cbc_ctx *ctx = crypto_skcipher_ctx(tfm);
 75         struct skcipher_walk walk;
 76         unsigned int nbytes;
 77         int ret;
 78 
 79         if (!crypto_simd_usable()) {
 80                 struct skcipher_request *subreq = skcipher_request_ctx(req);
 81 
 82                 *subreq = *req;
 83                 skcipher_request_set_tfm(subreq, ctx->fallback);
 84                 return enc ? crypto_skcipher_encrypt(subreq) :
 85                              crypto_skcipher_decrypt(subreq);
 86         }
 87 
 88         ret = skcipher_walk_virt(&walk, req, false);
 89         while ((nbytes = walk.nbytes) != 0) {
 90                 preempt_disable();
 91                 pagefault_disable();
 92                 enable_kernel_vsx();
 93                 aes_p8_cbc_encrypt(walk.src.virt.addr,
 94                                    walk.dst.virt.addr,
 95                                    round_down(nbytes, AES_BLOCK_SIZE),
 96                                    enc ? &ctx->enc_key : &ctx->dec_key,
 97                                    walk.iv, enc);
 98                 disable_kernel_vsx();
 99                 pagefault_enable();
100                 preempt_enable();
101 
102                 ret = skcipher_walk_done(&walk, nbytes % AES_BLOCK_SIZE);
103         }
104         return ret;
105 }
106 
107 static int p8_aes_cbc_encrypt(struct skcipher_request *req)
108 {
109         return p8_aes_cbc_crypt(req, 1);
110 }
111 
112 static int p8_aes_cbc_decrypt(struct skcipher_request *req)
113 {
114         return p8_aes_cbc_crypt(req, 0);
115 }
116 
117 struct skcipher_alg p8_aes_cbc_alg = {
118         .base.cra_name = "cbc(aes)",
119         .base.cra_driver_name = "p8_aes_cbc",
120         .base.cra_module = THIS_MODULE,
121         .base.cra_priority = 2000,
122         .base.cra_flags = CRYPTO_ALG_NEED_FALLBACK,
123         .base.cra_blocksize = AES_BLOCK_SIZE,
124         .base.cra_ctxsize = sizeof(struct p8_aes_cbc_ctx),
125         .setkey = p8_aes_cbc_setkey,
126         .encrypt = p8_aes_cbc_encrypt,
127         .decrypt = p8_aes_cbc_decrypt,
128         .init = p8_aes_cbc_init,
129         .exit = p8_aes_cbc_exit,
130         .min_keysize = AES_MIN_KEY_SIZE,
131         .max_keysize = AES_MAX_KEY_SIZE,
132         .ivsize = AES_BLOCK_SIZE,
133 };
134 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php