1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * AES CBC routines supporting the Power 7+ Nest Accelerators driver
4 *
5 * Copyright (C) 2011-2012 International Business Machines Inc.
6 *
7 * Author: Kent Yoder <yoder1@us.ibm.com>
8 */
9
10 #include <crypto/aes.h>
11 #include <crypto/internal/skcipher.h>
12 #include <linux/err.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/string.h>
17 #include <asm/vio.h>
18
19 #include "nx_csbcpb.h"
20 #include "nx.h"
21
22
cbc_aes_nx_set_key(struct crypto_skcipher * tfm,const u8 * in_key,unsigned int key_len)23 static int cbc_aes_nx_set_key(struct crypto_skcipher *tfm,
24 const u8 *in_key,
25 unsigned int key_len)
26 {
27 struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
28 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
29
30 nx_ctx_init(nx_ctx, HCOP_FC_AES);
31
32 switch (key_len) {
33 case AES_KEYSIZE_128:
34 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_128);
35 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_128];
36 break;
37 case AES_KEYSIZE_192:
38 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_192);
39 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_192];
40 break;
41 case AES_KEYSIZE_256:
42 NX_CPB_SET_KEY_SIZE(csbcpb, NX_KS_AES_256);
43 nx_ctx->ap = &nx_ctx->props[NX_PROPS_AES_256];
44 break;
45 default:
46 return -EINVAL;
47 }
48
49 csbcpb->cpb.hdr.mode = NX_MODE_AES_CBC;
50 memcpy(csbcpb->cpb.aes_cbc.key, in_key, key_len);
51
52 return 0;
53 }
54
cbc_aes_nx_crypt(struct skcipher_request * req,int enc)55 static int cbc_aes_nx_crypt(struct skcipher_request *req,
56 int enc)
57 {
58 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
59 struct nx_crypto_ctx *nx_ctx = crypto_skcipher_ctx(tfm);
60 struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
61 unsigned long irq_flags;
62 unsigned int processed = 0, to_process;
63 int rc;
64
65 spin_lock_irqsave(&nx_ctx->lock, irq_flags);
66
67 if (enc)
68 NX_CPB_FDM(csbcpb) |= NX_FDM_ENDE_ENCRYPT;
69 else
70 NX_CPB_FDM(csbcpb) &= ~NX_FDM_ENDE_ENCRYPT;
71
72 do {
73 to_process = req->cryptlen - processed;
74
75 rc = nx_build_sg_lists(nx_ctx, req->iv, req->dst, req->src,
76 &to_process, processed,
77 csbcpb->cpb.aes_cbc.iv);
78 if (rc)
79 goto out;
80
81 if (!nx_ctx->op.inlen || !nx_ctx->op.outlen) {
82 rc = -EINVAL;
83 goto out;
84 }
85
86 rc = nx_hcall_sync(nx_ctx, &nx_ctx->op,
87 req->base.flags & CRYPTO_TFM_REQ_MAY_SLEEP);
88 if (rc)
89 goto out;
90
91 memcpy(req->iv, csbcpb->cpb.aes_cbc.cv, AES_BLOCK_SIZE);
92 atomic_inc(&(nx_ctx->stats->aes_ops));
93 atomic64_add(be32_to_cpu(csbcpb->csb.processed_byte_count),
94 &(nx_ctx->stats->aes_bytes));
95
96 processed += to_process;
97 } while (processed < req->cryptlen);
98 out:
99 spin_unlock_irqrestore(&nx_ctx->lock, irq_flags);
100 return rc;
101 }
102
cbc_aes_nx_encrypt(struct skcipher_request * req)103 static int cbc_aes_nx_encrypt(struct skcipher_request *req)
104 {
105 return cbc_aes_nx_crypt(req, 1);
106 }
107
cbc_aes_nx_decrypt(struct skcipher_request * req)108 static int cbc_aes_nx_decrypt(struct skcipher_request *req)
109 {
110 return cbc_aes_nx_crypt(req, 0);
111 }
112
113 struct skcipher_alg nx_cbc_aes_alg = {
114 .base.cra_name = "cbc(aes)",
115 .base.cra_driver_name = "cbc-aes-nx",
116 .base.cra_priority = 300,
117 .base.cra_blocksize = AES_BLOCK_SIZE,
118 .base.cra_ctxsize = sizeof(struct nx_crypto_ctx),
119 .base.cra_alignmask = 0xf,
120 .base.cra_module = THIS_MODULE,
121 .init = nx_crypto_ctx_aes_cbc_init,
122 .exit = nx_crypto_ctx_skcipher_exit,
123 .min_keysize = AES_MIN_KEY_SIZE,
124 .max_keysize = AES_MAX_KEY_SIZE,
125 .ivsize = AES_BLOCK_SIZE,
126 .setkey = cbc_aes_nx_set_key,
127 .encrypt = cbc_aes_nx_encrypt,
128 .decrypt = cbc_aes_nx_decrypt,
129 };
130