2 * SHA-512 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/sha.h>
24 #include <linux/module.h>
27 #include "nx_csbcpb.h"
31 static int nx_sha512_init(struct shash_desc
*desc
)
33 struct sha512_state
*sctx
= shash_desc_ctx(desc
);
34 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
37 nx_ctx_init(nx_ctx
, HCOP_FC_SHA
);
39 memset(sctx
, 0, sizeof *sctx
);
41 nx_ctx
->ap
= &nx_ctx
->props
[NX_PROPS_SHA512
];
43 NX_CPB_SET_DIGEST_SIZE(nx_ctx
->csbcpb
, NX_DS_SHA512
);
44 out_sg
= nx_build_sg_list(nx_ctx
->out_sg
, (u8
*)sctx
->state
,
45 SHA512_DIGEST_SIZE
, nx_ctx
->ap
->sglen
);
46 nx_ctx
->op
.outlen
= (nx_ctx
->out_sg
- out_sg
) * sizeof(struct nx_sg
);
51 static int nx_sha512_update(struct shash_desc
*desc
, const u8
*data
,
54 struct sha512_state
*sctx
= shash_desc_ctx(desc
);
55 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
56 struct nx_csbcpb
*csbcpb
= (struct nx_csbcpb
*)nx_ctx
->csbcpb
;
58 u64 to_process
, leftover
, spbc_bits
;
61 if (NX_CPB_FDM(csbcpb
) & NX_FDM_CONTINUATION
) {
62 /* we've hit the nx chip previously and we're updating again,
63 * so copy over the partial digest */
64 memcpy(csbcpb
->cpb
.sha512
.input_partial_digest
,
65 csbcpb
->cpb
.sha512
.message_digest
, SHA512_DIGEST_SIZE
);
68 /* 2 cases for total data len:
69 * 1: <= SHA512_BLOCK_SIZE: copy into state, return 0
70 * 2: > SHA512_BLOCK_SIZE: process X blocks, copy in leftover
72 if ((u64
)len
+ sctx
->count
[0] <= SHA512_BLOCK_SIZE
) {
73 memcpy(sctx
->buf
+ sctx
->count
[0], data
, len
);
74 sctx
->count
[0] += len
;
78 /* to_process: the SHA512_BLOCK_SIZE data chunk to process in this
80 to_process
= (sctx
->count
[0] + len
) & ~(SHA512_BLOCK_SIZE
- 1);
81 leftover
= (sctx
->count
[0] + len
) & (SHA512_BLOCK_SIZE
- 1);
84 in_sg
= nx_build_sg_list(nx_ctx
->in_sg
, (u8
*)sctx
->buf
,
85 sctx
->count
[0], nx_ctx
->ap
->sglen
);
86 in_sg
= nx_build_sg_list(in_sg
, (u8
*)data
,
87 to_process
- sctx
->count
[0],
89 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) *
92 in_sg
= nx_build_sg_list(nx_ctx
->in_sg
, (u8
*)data
,
93 to_process
, nx_ctx
->ap
->sglen
);
94 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) *
98 NX_CPB_FDM(csbcpb
) |= NX_FDM_INTERMEDIATE
;
100 if (!nx_ctx
->op
.inlen
|| !nx_ctx
->op
.outlen
) {
105 rc
= nx_hcall_sync(nx_ctx
, &nx_ctx
->op
,
106 desc
->flags
& CRYPTO_TFM_REQ_MAY_SLEEP
);
110 atomic_inc(&(nx_ctx
->stats
->sha512_ops
));
112 /* copy the leftover back into the state struct */
113 memcpy(sctx
->buf
, data
+ len
- leftover
, leftover
);
114 sctx
->count
[0] = leftover
;
116 spbc_bits
= csbcpb
->cpb
.sha512
.spbc
* 8;
117 csbcpb
->cpb
.sha512
.message_bit_length_lo
+= spbc_bits
;
118 if (csbcpb
->cpb
.sha512
.message_bit_length_lo
< spbc_bits
)
119 csbcpb
->cpb
.sha512
.message_bit_length_hi
++;
121 /* everything after the first update is continuation */
122 NX_CPB_FDM(csbcpb
) |= NX_FDM_CONTINUATION
;
127 static int nx_sha512_final(struct shash_desc
*desc
, u8
*out
)
129 struct sha512_state
*sctx
= shash_desc_ctx(desc
);
130 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
131 struct nx_csbcpb
*csbcpb
= (struct nx_csbcpb
*)nx_ctx
->csbcpb
;
132 struct nx_sg
*in_sg
, *out_sg
;
136 if (NX_CPB_FDM(csbcpb
) & NX_FDM_CONTINUATION
) {
137 /* we've hit the nx chip previously, now we're finalizing,
138 * so copy over the partial digest */
139 memcpy(csbcpb
->cpb
.sha512
.input_partial_digest
,
140 csbcpb
->cpb
.sha512
.message_digest
, SHA512_DIGEST_SIZE
);
143 /* final is represented by continuing the operation and indicating that
144 * this is not an intermediate operation */
145 NX_CPB_FDM(csbcpb
) &= ~NX_FDM_INTERMEDIATE
;
147 count0
= sctx
->count
[0] * 8;
149 csbcpb
->cpb
.sha512
.message_bit_length_lo
+= count0
;
150 if (csbcpb
->cpb
.sha512
.message_bit_length_lo
< count0
)
151 csbcpb
->cpb
.sha512
.message_bit_length_hi
++;
153 in_sg
= nx_build_sg_list(nx_ctx
->in_sg
, sctx
->buf
, sctx
->count
[0],
155 out_sg
= nx_build_sg_list(nx_ctx
->out_sg
, out
, SHA512_DIGEST_SIZE
,
157 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) * sizeof(struct nx_sg
);
158 nx_ctx
->op
.outlen
= (nx_ctx
->out_sg
- out_sg
) * sizeof(struct nx_sg
);
160 if (!nx_ctx
->op
.outlen
) {
165 rc
= nx_hcall_sync(nx_ctx
, &nx_ctx
->op
,
166 desc
->flags
& CRYPTO_TFM_REQ_MAY_SLEEP
);
170 atomic_inc(&(nx_ctx
->stats
->sha512_ops
));
171 atomic64_add(csbcpb
->cpb
.sha512
.message_bit_length_lo
,
172 &(nx_ctx
->stats
->sha512_bytes
));
174 memcpy(out
, csbcpb
->cpb
.sha512
.message_digest
, SHA512_DIGEST_SIZE
);
179 static int nx_sha512_export(struct shash_desc
*desc
, void *out
)
181 struct sha512_state
*sctx
= shash_desc_ctx(desc
);
182 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
183 struct nx_csbcpb
*csbcpb
= (struct nx_csbcpb
*)nx_ctx
->csbcpb
;
184 struct sha512_state
*octx
= out
;
186 /* move message_bit_length (128 bits) into count and convert its value
188 octx
->count
[0] = csbcpb
->cpb
.sha512
.message_bit_length_lo
>> 3 |
189 ((csbcpb
->cpb
.sha512
.message_bit_length_hi
& 7) << 61);
190 octx
->count
[1] = csbcpb
->cpb
.sha512
.message_bit_length_hi
>> 3;
192 octx
->count
[0] += sctx
->count
[0];
193 if (octx
->count
[0] < sctx
->count
[0])
196 memcpy(octx
->buf
, sctx
->buf
, sizeof(octx
->buf
));
198 /* if no data has been processed yet, we need to export SHA512's
199 * initial data, in case this context gets imported into a software
201 if (csbcpb
->cpb
.sha512
.message_bit_length_hi
||
202 csbcpb
->cpb
.sha512
.message_bit_length_lo
)
203 memcpy(octx
->state
, csbcpb
->cpb
.sha512
.message_digest
,
206 octx
->state
[0] = SHA512_H0
;
207 octx
->state
[1] = SHA512_H1
;
208 octx
->state
[2] = SHA512_H2
;
209 octx
->state
[3] = SHA512_H3
;
210 octx
->state
[4] = SHA512_H4
;
211 octx
->state
[5] = SHA512_H5
;
212 octx
->state
[6] = SHA512_H6
;
213 octx
->state
[7] = SHA512_H7
;
219 static int nx_sha512_import(struct shash_desc
*desc
, const void *in
)
221 struct sha512_state
*sctx
= shash_desc_ctx(desc
);
222 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
223 struct nx_csbcpb
*csbcpb
= (struct nx_csbcpb
*)nx_ctx
->csbcpb
;
224 const struct sha512_state
*ictx
= in
;
226 memcpy(sctx
->buf
, ictx
->buf
, sizeof(ictx
->buf
));
227 sctx
->count
[0] = ictx
->count
[0] & 0x3f;
228 csbcpb
->cpb
.sha512
.message_bit_length_lo
= (ictx
->count
[0] & ~0x3f)
230 csbcpb
->cpb
.sha512
.message_bit_length_hi
= ictx
->count
[1] << 3 |
231 ictx
->count
[0] >> 61;
233 if (csbcpb
->cpb
.sha512
.message_bit_length_hi
||
234 csbcpb
->cpb
.sha512
.message_bit_length_lo
) {
235 memcpy(csbcpb
->cpb
.sha512
.message_digest
, ictx
->state
,
238 NX_CPB_FDM(csbcpb
) |= NX_FDM_CONTINUATION
;
239 NX_CPB_FDM(csbcpb
) |= NX_FDM_INTERMEDIATE
;
245 struct shash_alg nx_shash_sha512_alg
= {
246 .digestsize
= SHA512_DIGEST_SIZE
,
247 .init
= nx_sha512_init
,
248 .update
= nx_sha512_update
,
249 .final
= nx_sha512_final
,
250 .export
= nx_sha512_export
,
251 .import
= nx_sha512_import
,
252 .descsize
= sizeof(struct sha512_state
),
253 .statesize
= sizeof(struct sha512_state
),
255 .cra_name
= "sha512",
256 .cra_driver_name
= "sha512-nx",
258 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
,
259 .cra_blocksize
= SHA512_BLOCK_SIZE
,
260 .cra_module
= THIS_MODULE
,
261 .cra_ctxsize
= sizeof(struct nx_crypto_ctx
),
262 .cra_init
= nx_crypto_ctx_sha_init
,
263 .cra_exit
= nx_crypto_ctx_exit
,