2 * AES XCBC routines supporting the Power 7+ Nest Accelerators driver
4 * Copyright (C) 2011-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.
19 * Author: Kent Yoder <yoder1@us.ibm.com>
22 #include <crypto/internal/hash.h>
23 #include <crypto/aes.h>
24 #include <crypto/algapi.h>
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/crypto.h>
30 #include "nx_csbcpb.h"
35 u8 state
[AES_BLOCK_SIZE
];
37 u8 buffer
[AES_BLOCK_SIZE
];
40 static int nx_xcbc_set_key(struct crypto_shash
*desc
,
44 struct nx_crypto_ctx
*nx_ctx
= crypto_shash_ctx(desc
);
48 nx_ctx
->ap
= &nx_ctx
->props
[NX_PROPS_AES_128
];
54 memcpy(nx_ctx
->priv
.xcbc
.key
, in_key
, key_len
);
59 static int nx_xcbc_init(struct shash_desc
*desc
)
61 struct xcbc_state
*sctx
= shash_desc_ctx(desc
);
62 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
63 struct nx_csbcpb
*csbcpb
= nx_ctx
->csbcpb
;
66 nx_ctx_init(nx_ctx
, HCOP_FC_AES
);
68 memset(sctx
, 0, sizeof *sctx
);
70 NX_CPB_SET_KEY_SIZE(csbcpb
, NX_KS_AES_128
);
71 csbcpb
->cpb
.hdr
.mode
= NX_MODE_AES_XCBC_MAC
;
73 memcpy(csbcpb
->cpb
.aes_xcbc
.key
, nx_ctx
->priv
.xcbc
.key
, AES_BLOCK_SIZE
);
74 memset(nx_ctx
->priv
.xcbc
.key
, 0, sizeof *nx_ctx
->priv
.xcbc
.key
);
76 out_sg
= nx_build_sg_list(nx_ctx
->out_sg
, (u8
*)sctx
->state
,
77 AES_BLOCK_SIZE
, nx_ctx
->ap
->sglen
);
78 nx_ctx
->op
.outlen
= (nx_ctx
->out_sg
- out_sg
) * sizeof(struct nx_sg
);
83 static int nx_xcbc_update(struct shash_desc
*desc
,
87 struct xcbc_state
*sctx
= shash_desc_ctx(desc
);
88 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
89 struct nx_csbcpb
*csbcpb
= nx_ctx
->csbcpb
;
91 u32 to_process
, leftover
;
94 if (NX_CPB_FDM(csbcpb
) & NX_FDM_CONTINUATION
) {
95 /* we've hit the nx chip previously and we're updating again,
96 * so copy over the partial digest */
97 memcpy(csbcpb
->cpb
.aes_xcbc
.cv
,
98 csbcpb
->cpb
.aes_xcbc
.out_cv_mac
, AES_BLOCK_SIZE
);
101 /* 2 cases for total data len:
102 * 1: <= AES_BLOCK_SIZE: copy into state, return 0
103 * 2: > AES_BLOCK_SIZE: process X blocks, copy in leftover
105 if (len
+ sctx
->count
<= AES_BLOCK_SIZE
) {
106 memcpy(sctx
->buffer
+ sctx
->count
, data
, len
);
111 /* to_process: the AES_BLOCK_SIZE data chunk to process in this
113 to_process
= (sctx
->count
+ len
) & ~(AES_BLOCK_SIZE
- 1);
114 leftover
= (sctx
->count
+ len
) & (AES_BLOCK_SIZE
- 1);
116 /* the hardware will not accept a 0 byte operation for this algorithm
117 * and the operation MUST be finalized to be correct. So if we happen
118 * to get an update that falls on a block sized boundary, we must
119 * save off the last block to finalize with later. */
121 to_process
-= AES_BLOCK_SIZE
;
122 leftover
= AES_BLOCK_SIZE
;
126 in_sg
= nx_build_sg_list(nx_ctx
->in_sg
, sctx
->buffer
,
127 sctx
->count
, nx_ctx
->ap
->sglen
);
128 in_sg
= nx_build_sg_list(in_sg
, (u8
*)data
,
129 to_process
- sctx
->count
,
131 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) *
132 sizeof(struct nx_sg
);
134 in_sg
= nx_build_sg_list(nx_ctx
->in_sg
, (u8
*)data
, to_process
,
136 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) *
137 sizeof(struct nx_sg
);
140 NX_CPB_FDM(csbcpb
) |= NX_FDM_INTERMEDIATE
;
142 if (!nx_ctx
->op
.inlen
|| !nx_ctx
->op
.outlen
) {
147 rc
= nx_hcall_sync(nx_ctx
, &nx_ctx
->op
,
148 desc
->flags
& CRYPTO_TFM_REQ_MAY_SLEEP
);
152 atomic_inc(&(nx_ctx
->stats
->aes_ops
));
154 /* copy the leftover back into the state struct */
155 memcpy(sctx
->buffer
, data
+ len
- leftover
, leftover
);
156 sctx
->count
= leftover
;
158 /* everything after the first update is continuation */
159 NX_CPB_FDM(csbcpb
) |= NX_FDM_CONTINUATION
;
164 static int nx_xcbc_final(struct shash_desc
*desc
, u8
*out
)
166 struct xcbc_state
*sctx
= shash_desc_ctx(desc
);
167 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
168 struct nx_csbcpb
*csbcpb
= nx_ctx
->csbcpb
;
169 struct nx_sg
*in_sg
, *out_sg
;
172 if (NX_CPB_FDM(csbcpb
) & NX_FDM_CONTINUATION
) {
173 /* we've hit the nx chip previously, now we're finalizing,
174 * so copy over the partial digest */
175 memcpy(csbcpb
->cpb
.aes_xcbc
.cv
,
176 csbcpb
->cpb
.aes_xcbc
.out_cv_mac
, AES_BLOCK_SIZE
);
177 } else if (sctx
->count
== 0) {
178 /* we've never seen an update, so this is a 0 byte op. The
179 * hardware cannot handle a 0 byte op, so just copy out the
180 * known 0 byte result. This is cheaper than allocating a
181 * software context to do a 0 byte op */
182 u8 data
[] = { 0x75, 0xf0, 0x25, 0x1d, 0x52, 0x8a, 0xc0, 0x1c,
183 0x45, 0x73, 0xdf, 0xd5, 0x84, 0xd7, 0x9f, 0x29 };
184 memcpy(out
, data
, sizeof(data
));
188 /* final is represented by continuing the operation and indicating that
189 * this is not an intermediate operation */
190 NX_CPB_FDM(csbcpb
) &= ~NX_FDM_INTERMEDIATE
;
192 in_sg
= nx_build_sg_list(nx_ctx
->in_sg
, (u8
*)sctx
->buffer
,
193 sctx
->count
, nx_ctx
->ap
->sglen
);
194 out_sg
= nx_build_sg_list(nx_ctx
->out_sg
, out
, AES_BLOCK_SIZE
,
197 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) * sizeof(struct nx_sg
);
198 nx_ctx
->op
.outlen
= (nx_ctx
->out_sg
- out_sg
) * sizeof(struct nx_sg
);
200 if (!nx_ctx
->op
.outlen
) {
205 rc
= nx_hcall_sync(nx_ctx
, &nx_ctx
->op
,
206 desc
->flags
& CRYPTO_TFM_REQ_MAY_SLEEP
);
210 atomic_inc(&(nx_ctx
->stats
->aes_ops
));
212 memcpy(out
, csbcpb
->cpb
.aes_xcbc
.out_cv_mac
, AES_BLOCK_SIZE
);
217 struct shash_alg nx_shash_aes_xcbc_alg
= {
218 .digestsize
= AES_BLOCK_SIZE
,
219 .init
= nx_xcbc_init
,
220 .update
= nx_xcbc_update
,
221 .final
= nx_xcbc_final
,
222 .setkey
= nx_xcbc_set_key
,
223 .descsize
= sizeof(struct xcbc_state
),
224 .statesize
= sizeof(struct xcbc_state
),
226 .cra_name
= "xcbc(aes)",
227 .cra_driver_name
= "xcbc-aes-nx",
229 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
230 .cra_blocksize
= AES_BLOCK_SIZE
,
231 .cra_module
= THIS_MODULE
,
232 .cra_ctxsize
= sizeof(struct nx_crypto_ctx
),
233 .cra_init
= nx_crypto_ctx_aes_xcbc_init
,
234 .cra_exit
= nx_crypto_ctx_exit
,