Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * AES ECB 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 ecb_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 = (struct nx_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_ECB;
0048     memcpy(csbcpb->cpb.aes_ecb.key, in_key, key_len);
0049 
0050     return 0;
0051 }
0052 
0053 static int ecb_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, NULL, req->dst, req->src,
0074                        &to_process, processed, NULL);
0075         if (rc)
0076             goto out;
0077 
0078         if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
0079             rc = -EINVAL;
0080             goto out;
0081         }
0082 
0083         rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
0084                    req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
0085         if (rc)
0086             goto out;
0087 
0088         atomic_inc(&(nx_ctx->stats->aes_ops));
0089         atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count),
0090                  &(nx_ctx->stats->aes_bytes));
0091 
0092         processed += to_process;
0093     } while (processed < req->cryptlen);
0094 
0095 out:
0096     spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
0097     return rc;
0098 }
0099 
0100 static int ecb_aes_nx_encrypt(struct skcipher_request *req)
0101 {
0102     return ecb_aes_nx_crypt(req, 1);
0103 }
0104 
0105 static int ecb_aes_nx_decrypt(struct skcipher_request *req)
0106 {
0107     return ecb_aes_nx_crypt(req, 0);
0108 }
0109 
0110 struct skcipher_alg nx_ecb_aes_alg = {
0111     .base.cra_name      = "ecb(aes)",
0112     .base.cra_driver_name   = "ecb-aes-nx",
0113     .base.cra_priority  = 300,
0114     .base.cra_blocksize = AES_BLOCK_SIZE,
0115     .base.cra_alignmask = 0xf,
0116     .base.cra_ctxsize   = sizeof(struct nx_crypto_ctx),
0117     .base.cra_module    = THIS_MODULE,
0118     .init           = nx_crypto_ctx_aes_ecb_init,
0119     .exit           = nx_crypto_ctx_skcipher_exit,
0120     .min_keysize        = AES_MIN_KEY_SIZE,
0121     .max_keysize        = AES_MAX_KEY_SIZE,
0122     .setkey         = ecb_aes_nx_set_key,
0123     .encrypt        = ecb_aes_nx_encrypt,
0124     .decrypt        = ecb_aes_nx_decrypt,
0125 };