2 * AES GCM routines supporting the Power 7+ Nest Accelerators driver
4 * Copyright (C) 2012 International Business Machines Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 only.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <crypto/internal/aead.h>
23 #include <crypto/aes.h>
24 #include <crypto/algapi.h>
25 #include <crypto/scatterwalk.h>
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/crypto.h>
31 #include "nx_csbcpb.h"
35 static int gcm_aes_nx_set_key(struct crypto_aead *tfm,
39 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
40 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
41 struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
43 nx_ctx_init(nx_ctx, HCOP_FC_AES);
47 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
48 NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_128);
49 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
52 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
53 NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_192);
54 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
57 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
58 NX_CPB_SET_KEY_SIZE(csbcpb_aead, NX_KS_AES_256);
59 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
65 csbcpb->cpb.hdr.mode = NX_MODE_AES_GCM;
66 memcpy(csbcpb->cpb.aes_gcm.key, in_key, key_len);
68 csbcpb_aead->cpb.hdr.mode = NX_MODE_AES_GCA;
69 memcpy(csbcpb_aead->cpb.aes_gca.key, in_key, key_len);
74 static int gcm4106_aes_nx_set_key(struct crypto_aead *tfm,
78 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
79 char *nonce = nx_ctx->priv.gcm.nonce;
87 rc = gcm_aes_nx_set_key(tfm, in_key, key_len);
91 memcpy(nonce, in_key + key_len, 4);
96 static int gcm_aes_nx_setauthsize(struct crypto_aead *tfm,
97 unsigned int authsize)
99 if (authsize > crypto_aead_alg(tfm)->maxauthsize)
102 crypto_aead_crt(tfm)->authsize = authsize;
107 static int gcm4106_aes_nx_setauthsize(struct crypto_aead *tfm,
108 unsigned int authsize)
119 crypto_aead_crt(tfm)->authsize = authsize;
124 static int nx_gca(struct nx_crypto_ctx *nx_ctx,
125 struct aead_request *req,
128 struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
130 struct scatter_walk walk;
131 struct nx_sg *nx_sg = nx_ctx->in_sg;
133 if (req->assoclen > nx_ctx->ap->databytelen)
136 if (req->assoclen <= AES_BLOCK_SIZE) {
137 scatterwalk_start(&walk, req->assoc);
138 scatterwalk_copychunks(out, &walk, req->assoclen,
139 SCATTERWALK_FROM_SG);
140 scatterwalk_done(&walk, SCATTERWALK_FROM_SG, 0);
146 nx_sg = nx_walk_and_build(nx_sg, nx_ctx->ap->sglen, req->assoc, 0,
148 nx_ctx->op_aead.inlen = (nx_ctx->in_sg - nx_sg) * sizeof(struct nx_sg);
150 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op_aead,
151 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
155 atomic_inc(&(nx_ctx->stats->aes_ops));
156 atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
158 memcpy(out, csbcpb_aead->cpb.aes_gca.out_pat, AES_BLOCK_SIZE);
163 static int gcm_aes_nx_crypt(struct aead_request *req, int enc)
165 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
166 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
167 struct blkcipher_desc desc;
168 unsigned int nbytes = req->cryptlen;
171 if (nbytes > nx_ctx->ap->databytelen)
174 desc.info = nx_ctx->priv.gcm.iv;
175 /* initialize the counter */
176 *(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1;
178 /* For scenarios where the input message is zero length, AES CTR mode
179 * may be used. Set the source data to be a single block (16B) of all
180 * zeros, and set the input IV value to be the same as the GMAC IV
181 * value. - nx_wb 4.8.1.3 */
183 char src[AES_BLOCK_SIZE] = {};
184 struct scatterlist sg;
186 desc.tfm = crypto_alloc_blkcipher("ctr(aes)", 0, 0);
187 if (IS_ERR(desc.tfm)) {
192 crypto_blkcipher_setkey(desc.tfm, csbcpb->cpb.aes_gcm.key,
193 NX_CPB_KEY_SIZE(csbcpb) == NX_KS_AES_128 ? 16 :
194 NX_CPB_KEY_SIZE(csbcpb) == NX_KS_AES_192 ? 24 : 32);
196 sg_init_one(&sg, src, AES_BLOCK_SIZE);
198 crypto_blkcipher_encrypt_iv(&desc, req->dst, &sg,
201 crypto_blkcipher_decrypt_iv(&desc, req->dst, &sg,
203 crypto_free_blkcipher(desc.tfm);
209 desc.tfm = (struct crypto_blkcipher *)req->base.tfm;
211 csbcpb->cpb.aes_gcm.bit_length_aad = req->assoclen * 8;
214 rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad);
220 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
222 nbytes -= crypto_aead_authsize(crypto_aead_reqtfm(req));
224 csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8;
226 rc = nx_build_sg_lists(nx_ctx, &desc, req->dst, req->src, nbytes,
227 csbcpb->cpb.aes_gcm.iv_or_cnt);
231 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
232 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
236 atomic_inc(&(nx_ctx->stats->aes_ops));
237 atomic64_add(csbcpb->csb.processed_byte_count,
238 &(nx_ctx->stats->aes_bytes));
241 /* copy out the auth tag */
242 scatterwalk_map_and_copy(csbcpb->cpb.aes_gcm.out_pat_or_mac,
244 crypto_aead_authsize(crypto_aead_reqtfm(req)),
246 } else if (req->assoclen) {
247 u8 *itag = nx_ctx->priv.gcm.iauth_tag;
248 u8 *otag = csbcpb->cpb.aes_gcm.out_pat_or_mac;
250 scatterwalk_map_and_copy(itag, req->dst, nbytes,
251 crypto_aead_authsize(crypto_aead_reqtfm(req)),
252 SCATTERWALK_FROM_SG);
253 rc = memcmp(itag, otag,
254 crypto_aead_authsize(crypto_aead_reqtfm(req))) ?
261 static int gcm_aes_nx_encrypt(struct aead_request *req)
263 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
264 char *iv = nx_ctx->priv.gcm.iv;
266 memcpy(iv, req->iv, 12);
268 return gcm_aes_nx_crypt(req, 1);
271 static int gcm_aes_nx_decrypt(struct aead_request *req)
273 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
274 char *iv = nx_ctx->priv.gcm.iv;
276 memcpy(iv, req->iv, 12);
278 return gcm_aes_nx_crypt(req, 0);
281 static int gcm4106_aes_nx_encrypt(struct aead_request *req)
283 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
284 char *iv = nx_ctx->priv.gcm.iv;
285 char *nonce = nx_ctx->priv.gcm.nonce;
287 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
288 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
290 return gcm_aes_nx_crypt(req, 1);
293 static int gcm4106_aes_nx_decrypt(struct aead_request *req)
295 struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
296 char *iv = nx_ctx->priv.gcm.iv;
297 char *nonce = nx_ctx->priv.gcm.nonce;
299 memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
300 memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
302 return gcm_aes_nx_crypt(req, 0);
305 /* tell the block cipher walk routines that this is a stream cipher by
306 * setting cra_blocksize to 1. Even using blkcipher_walk_virt_block
307 * during encrypt/decrypt doesn't solve this problem, because it calls
308 * blkcipher_walk_done under the covers, which doesn't use walk->blocksize,
309 * but instead uses this tfm->blocksize. */
310 struct crypto_alg nx_gcm_aes_alg = {
311 .cra_name = "gcm(aes)",
312 .cra_driver_name = "gcm-aes-nx",
314 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
316 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
317 .cra_type = &crypto_aead_type,
318 .cra_module = THIS_MODULE,
319 .cra_init = nx_crypto_ctx_aes_gcm_init,
320 .cra_exit = nx_crypto_ctx_exit,
322 .ivsize = AES_BLOCK_SIZE,
323 .maxauthsize = AES_BLOCK_SIZE,
324 .setkey = gcm_aes_nx_set_key,
325 .setauthsize = gcm_aes_nx_setauthsize,
326 .encrypt = gcm_aes_nx_encrypt,
327 .decrypt = gcm_aes_nx_decrypt,
331 struct crypto_alg nx_gcm4106_aes_alg = {
332 .cra_name = "rfc4106(gcm(aes))",
333 .cra_driver_name = "rfc4106-gcm-aes-nx",
335 .cra_flags = CRYPTO_ALG_TYPE_AEAD,
337 .cra_ctxsize = sizeof(struct nx_crypto_ctx),
338 .cra_type = &crypto_nivaead_type,
339 .cra_module = THIS_MODULE,
340 .cra_init = nx_crypto_ctx_aes_gcm_init,
341 .cra_exit = nx_crypto_ctx_exit,
344 .maxauthsize = AES_BLOCK_SIZE,
346 .setkey = gcm4106_aes_nx_set_key,
347 .setauthsize = gcm4106_aes_nx_setauthsize,
348 .encrypt = gcm4106_aes_nx_encrypt,
349 .decrypt = gcm4106_aes_nx_decrypt,