1 // SPDX-License-Identifier: GPL-2.0-only
3 * AES CTR routines supporting the Power 7+ Nest Accelerators driver
5 * Copyright (C) 2011-2012 International Business Machines Inc.
7 * Author: Kent Yoder <yoder1@us.ibm.com>
10 #include <crypto/aes.h>
11 #include <crypto/ctr.h>
12 #include <crypto/algapi.h>
13 #include <linux/module.h>
14 #include <linux/types.h>
15 #include <linux/crypto.h>
18 #include "nx_csbcpb.h"
22 static int ctr_aes_nx_set_key(struct crypto_tfm *tfm,
26 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
27 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
29 nx_ctx_init(nx_ctx, HCOP_FC_AES);
33 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
34 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
37 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
38 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
41 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
42 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
48 csbcpb->cpb.hdr.mode = NX_MODE_AES_CTR;
49 memcpy(csbcpb->cpb.aes_ctr.key, in_key, key_len);
54 static int ctr3686_aes_nx_set_key(struct crypto_tfm *tfm,
58 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(tfm);
60 if (key_len < CTR_RFC3686_NONCE_SIZE)
63 memcpy(nx_ctx->priv.ctr.nonce,
64 in_key + key_len - CTR_RFC3686_NONCE_SIZE,
65 CTR_RFC3686_NONCE_SIZE);
67 key_len -= CTR_RFC3686_NONCE_SIZE;
69 return ctr_aes_nx_set_key(tfm, in_key, key_len);
72 static int ctr_aes_nx_crypt(struct blkcipher_desc *desc,
73 struct scatterlist *dst,
74 struct scatterlist *src,
77 struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm);
78 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
79 unsigned long irq_flags;
80 unsigned int processed = 0, to_process;
83 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
86 to_process = nbytes - processed;
88 rc = nx_build_sg_lists(nx_ctx, desc, dst, src, &to_process,
89 processed, csbcpb->cpb.aes_ctr.iv);
93 if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
98 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
99 desc->flags & CRYPTO_TFM_REQ_MAY_SLEEP);
103 memcpy(desc->info, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
105 atomic_inc(&(nx_ctx->stats->aes_ops));
106 atomic64_add(csbcpb->csb.processed_byte_count,
107 &(nx_ctx->stats->aes_bytes));
109 processed += to_process;
110 } while (processed < nbytes);
112 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
116 static int ctr3686_aes_nx_crypt(struct blkcipher_desc *desc,
117 struct scatterlist *dst,
118 struct scatterlist *src,
121 struct nx_crypto_ctx *nx_ctx = crypto_blkcipher_ctx(desc->tfm);
124 memcpy(iv, nx_ctx->priv.ctr.nonce, CTR_RFC3686_IV_SIZE);
125 memcpy(iv + CTR_RFC3686_NONCE_SIZE,
126 desc->info, CTR_RFC3686_IV_SIZE);
127 iv[12] = iv[13] = iv[14] = 0;
132 return ctr_aes_nx_crypt(desc, dst, src, nbytes);
135 struct crypto_alg nx_ctr3686_aes_alg = {
136 .cra_name = "rfc3686(ctr(aes))",
137 .cra_driver_name = "rfc3686-ctr-aes-nx",
139 .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
141 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
142 .cra_type = &crypto_blkcipher_type,
143 .cra_module = THIS_MODULE,
144 .cra_init = nx_crypto_ctx_aes_ctr_init,
145 .cra_exit = nx_crypto_ctx_exit,
147 .min_keysize = AES_MIN_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
148 .max_keysize = AES_MAX_KEY_SIZE + CTR_RFC3686_NONCE_SIZE,
149 .ivsize = CTR_RFC3686_IV_SIZE,
150 .setkey = ctr3686_aes_nx_set_key,
151 .encrypt = ctr3686_aes_nx_crypt,
152 .decrypt = ctr3686_aes_nx_crypt,