Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * AES CBC routines supporting the Power 7+ Nest Accelerators driver
0004  *
0005  * Copyright (C) 2011-2012 International Business Machines Inc.
0006  *
0007  * Author: Kent Yoder <yoder1@us.ibm.com>
0008  */
0009 
0010 #include <crypto/aes.h>
0011 #include <crypto/algapi.h>
0012 #include <linux/module.h>
0013 #include <linux/types.h>
0014 #include <linux/crypto.h>
0015 #include <asm/vio.h>
0016 
0017 #include "nx_csbcpb.h"
0018 #include "nx.h"
0019 
0020 
0021 static int cbc_aes_nx_set_key(struct crypto_skcipher *tfm,
0022                   const u8               *in_key,
0023                   unsigned int            key_len)
0024 {
0025     struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
0026     struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
0027 
0028     nx_ctx_init(nx_ctx, HCOP_FC_AES);
0029 
0030     switch (key_len) {
0031     case AES_KEYSIZE_128:
0032         NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
0033         nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
0034         break;
0035     case AES_KEYSIZE_192:
0036         NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
0037         nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
0038         break;
0039     case AES_KEYSIZE_256:
0040         NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
0041         nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
0042         break;
0043     default:
0044         return -EINVAL;
0045     }
0046 
0047     csbcpb->cpb.hdr.mode = NX_MODE_AES_CBC;
0048     memcpy(csbcpb->cpb.aes_cbc.key, in_key, key_len);
0049 
0050     return 0;
0051 }
0052 
0053 static int cbc_aes_nx_crypt(struct skcipher_request *req,
0054                 int                      enc)
0055 {
0056     struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
0057     struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
0058     struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
0059     unsigned long irq_flags;
0060     unsigned int processed = 0, to_process;
0061     int rc;
0062 
0063     spin_lock_irqsave(&nx_ctx->lock, irq_flags);
0064 
0065     if (enc)
0066         NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
0067     else
0068         NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
0069 
0070     do {
0071         to_process = req->cryptlen - processed;
0072 
0073         rc = nx_build_sg_lists(nx_ctx, req->iv, req->dst, req->src,
0074                        &to_process, processed,
0075                        csbcpb->cpb.aes_cbc.iv);
0076         if (rc)
0077             goto out;
0078 
0079         if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
0080             rc = -EINVAL;
0081             goto out;
0082         }
0083 
0084         rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
0085                    req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
0086         if (rc)
0087             goto out;
0088 
0089         memcpy(req->iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
0090         atomic_inc(&(nx_ctx->stats->aes_ops));
0091         atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count),
0092                  &(nx_ctx->stats->aes_bytes));
0093 
0094         processed += to_process;
0095     } while (processed < req->cryptlen);
0096 out:
0097     spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
0098     return rc;
0099 }
0100 
0101 static int cbc_aes_nx_encrypt(struct skcipher_request *req)
0102 {
0103     return cbc_aes_nx_crypt(req, 1);
0104 }
0105 
0106 static int cbc_aes_nx_decrypt(struct skcipher_request *req)
0107 {
0108     return cbc_aes_nx_crypt(req, 0);
0109 }
0110 
0111 struct skcipher_alg nx_cbc_aes_alg = {
0112     .base.cra_name      = "cbc(aes)",
0113     .base.cra_driver_name   = "cbc-aes-nx",
0114     .base.cra_priority  = 300,
0115     .base.cra_blocksize = AES_BLOCK_SIZE,
0116     .base.cra_ctxsize   = sizeof(struct nx_crypto_ctx),
0117     .base.cra_alignmask = 0xf,
0118     .base.cra_module    = THIS_MODULE,
0119     .init           = nx_crypto_ctx_aes_cbc_init,
0120     .exit           = nx_crypto_ctx_skcipher_exit,
0121     .min_keysize        = AES_MIN_KEY_SIZE,
0122     .max_keysize        = AES_MAX_KEY_SIZE,
0123     .ivsize         = AES_BLOCK_SIZE,
0124     .setkey         = cbc_aes_nx_set_key,
0125     .encrypt        = cbc_aes_nx_encrypt,
0126     .decrypt        = cbc_aes_nx_decrypt,
0127 };