1 // SPDX-License-Identifier: GPL-2.0-only
3 * SHA-512 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/internal/hash.h>
11 #include <crypto/sha2.h>
12 #include <linux/module.h>
15 #include "nx_csbcpb.h"
18 struct sha512_state_be
{
19 __be64 state
[SHA512_DIGEST_SIZE
/ 8];
21 u8 buf
[SHA512_BLOCK_SIZE
];
24 static int nx_crypto_ctx_sha512_init(struct crypto_tfm
*tfm
)
26 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(tfm
);
29 err
= nx_crypto_ctx_sha_init(tfm
);
33 nx_ctx_init(nx_ctx
, HCOP_FC_SHA
);
35 nx_ctx
->ap
= &nx_ctx
->props
[NX_PROPS_SHA512
];
37 NX_CPB_SET_DIGEST_SIZE(nx_ctx
->csbcpb
, NX_DS_SHA512
);
42 static int nx_sha512_init(struct shash_desc
*desc
)
44 struct sha512_state_be
*sctx
= shash_desc_ctx(desc
);
46 memset(sctx
, 0, sizeof *sctx
);
48 sctx
->state
[0] = __cpu_to_be64(SHA512_H0
);
49 sctx
->state
[1] = __cpu_to_be64(SHA512_H1
);
50 sctx
->state
[2] = __cpu_to_be64(SHA512_H2
);
51 sctx
->state
[3] = __cpu_to_be64(SHA512_H3
);
52 sctx
->state
[4] = __cpu_to_be64(SHA512_H4
);
53 sctx
->state
[5] = __cpu_to_be64(SHA512_H5
);
54 sctx
->state
[6] = __cpu_to_be64(SHA512_H6
);
55 sctx
->state
[7] = __cpu_to_be64(SHA512_H7
);
61 static int nx_sha512_update(struct shash_desc
*desc
, const u8
*data
,
64 struct sha512_state_be
*sctx
= shash_desc_ctx(desc
);
65 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
66 struct nx_csbcpb
*csbcpb
= (struct nx_csbcpb
*)nx_ctx
->csbcpb
;
68 u64 to_process
, leftover
= 0, total
;
69 unsigned long irq_flags
;
73 u64 buf_len
= (sctx
->count
[0] % SHA512_BLOCK_SIZE
);
75 spin_lock_irqsave(&nx_ctx
->lock
, irq_flags
);
77 /* 2 cases for total data len:
78 * 1: < SHA512_BLOCK_SIZE: copy into state, return 0
79 * 2: >= SHA512_BLOCK_SIZE: process X blocks, copy in leftover
81 total
= (sctx
->count
[0] % SHA512_BLOCK_SIZE
) + len
;
82 if (total
< SHA512_BLOCK_SIZE
) {
83 memcpy(sctx
->buf
+ buf_len
, data
, len
);
84 sctx
->count
[0] += len
;
88 memcpy(csbcpb
->cpb
.sha512
.message_digest
, sctx
->state
, SHA512_DIGEST_SIZE
);
89 NX_CPB_FDM(csbcpb
) |= NX_FDM_INTERMEDIATE
;
90 NX_CPB_FDM(csbcpb
) |= NX_FDM_CONTINUATION
;
92 max_sg_len
= min_t(u64
, nx_ctx
->ap
->sglen
,
93 nx_driver
.of
.max_sg_len
/sizeof(struct nx_sg
));
94 max_sg_len
= min_t(u64
, max_sg_len
,
95 nx_ctx
->ap
->databytelen
/NX_PAGE_SIZE
);
97 data_len
= SHA512_DIGEST_SIZE
;
98 out_sg
= nx_build_sg_list(nx_ctx
->out_sg
, (u8
*)sctx
->state
,
99 &data_len
, max_sg_len
);
100 nx_ctx
->op
.outlen
= (nx_ctx
->out_sg
- out_sg
) * sizeof(struct nx_sg
);
102 if (data_len
!= SHA512_DIGEST_SIZE
) {
109 struct nx_sg
*in_sg
= nx_ctx
->in_sg
;
113 in_sg
= nx_build_sg_list(in_sg
,
115 &data_len
, max_sg_len
);
117 if (data_len
!= buf_len
) {
121 used_sgs
= in_sg
- nx_ctx
->in_sg
;
124 /* to_process: SHA512_BLOCK_SIZE aligned chunk to be
125 * processed in this iteration. This value is restricted
126 * by sg list limits and number of sgs we already used
127 * for leftover data. (see above)
128 * In ideal case, we could allow NX_PAGE_SIZE * max_sg_len,
129 * but because data may not be aligned, we need to account
131 to_process
= min_t(u64
, total
,
132 (max_sg_len
- 1 - used_sgs
) * NX_PAGE_SIZE
);
133 to_process
= to_process
& ~(SHA512_BLOCK_SIZE
- 1);
135 data_len
= to_process
- buf_len
;
136 in_sg
= nx_build_sg_list(in_sg
, (u8
*) data
,
137 &data_len
, max_sg_len
);
139 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) * sizeof(struct nx_sg
);
141 if (data_len
!= (to_process
- buf_len
)) {
146 to_process
= data_len
+ buf_len
;
147 leftover
= total
- to_process
;
150 * we've hit the nx chip previously and we're updating
151 * again, so copy over the partial digest.
153 memcpy(csbcpb
->cpb
.sha512
.input_partial_digest
,
154 csbcpb
->cpb
.sha512
.message_digest
,
157 if (!nx_ctx
->op
.inlen
|| !nx_ctx
->op
.outlen
) {
162 rc
= nx_hcall_sync(nx_ctx
, &nx_ctx
->op
, 0);
166 atomic_inc(&(nx_ctx
->stats
->sha512_ops
));
169 data
+= to_process
- buf_len
;
172 } while (leftover
>= SHA512_BLOCK_SIZE
);
174 /* copy the leftover back into the state struct */
176 memcpy(sctx
->buf
, data
, leftover
);
177 sctx
->count
[0] += len
;
178 memcpy(sctx
->state
, csbcpb
->cpb
.sha512
.message_digest
, SHA512_DIGEST_SIZE
);
180 spin_unlock_irqrestore(&nx_ctx
->lock
, irq_flags
);
184 static int nx_sha512_final(struct shash_desc
*desc
, u8
*out
)
186 struct sha512_state_be
*sctx
= shash_desc_ctx(desc
);
187 struct nx_crypto_ctx
*nx_ctx
= crypto_tfm_ctx(&desc
->tfm
->base
);
188 struct nx_csbcpb
*csbcpb
= (struct nx_csbcpb
*)nx_ctx
->csbcpb
;
189 struct nx_sg
*in_sg
, *out_sg
;
192 unsigned long irq_flags
;
196 spin_lock_irqsave(&nx_ctx
->lock
, irq_flags
);
198 max_sg_len
= min_t(u64
, nx_ctx
->ap
->sglen
,
199 nx_driver
.of
.max_sg_len
/sizeof(struct nx_sg
));
200 max_sg_len
= min_t(u64
, max_sg_len
,
201 nx_ctx
->ap
->databytelen
/NX_PAGE_SIZE
);
203 /* final is represented by continuing the operation and indicating that
204 * this is not an intermediate operation */
205 if (sctx
->count
[0] >= SHA512_BLOCK_SIZE
) {
206 /* we've hit the nx chip previously, now we're finalizing,
207 * so copy over the partial digest */
208 memcpy(csbcpb
->cpb
.sha512
.input_partial_digest
, sctx
->state
,
210 NX_CPB_FDM(csbcpb
) &= ~NX_FDM_INTERMEDIATE
;
211 NX_CPB_FDM(csbcpb
) |= NX_FDM_CONTINUATION
;
213 NX_CPB_FDM(csbcpb
) &= ~NX_FDM_INTERMEDIATE
;
214 NX_CPB_FDM(csbcpb
) &= ~NX_FDM_CONTINUATION
;
217 NX_CPB_FDM(csbcpb
) &= ~NX_FDM_INTERMEDIATE
;
219 count0
= sctx
->count
[0] * 8;
221 csbcpb
->cpb
.sha512
.message_bit_length_lo
= count0
;
223 len
= sctx
->count
[0] & (SHA512_BLOCK_SIZE
- 1);
224 in_sg
= nx_build_sg_list(nx_ctx
->in_sg
, sctx
->buf
, &len
,
227 if (len
!= (sctx
->count
[0] & (SHA512_BLOCK_SIZE
- 1))) {
232 len
= SHA512_DIGEST_SIZE
;
233 out_sg
= nx_build_sg_list(nx_ctx
->out_sg
, out
, &len
,
236 nx_ctx
->op
.inlen
= (nx_ctx
->in_sg
- in_sg
) * sizeof(struct nx_sg
);
237 nx_ctx
->op
.outlen
= (nx_ctx
->out_sg
- out_sg
) * sizeof(struct nx_sg
);
239 if (!nx_ctx
->op
.outlen
) {
244 rc
= nx_hcall_sync(nx_ctx
, &nx_ctx
->op
, 0);
248 atomic_inc(&(nx_ctx
->stats
->sha512_ops
));
249 atomic64_add(sctx
->count
[0], &(nx_ctx
->stats
->sha512_bytes
));
251 memcpy(out
, csbcpb
->cpb
.sha512
.message_digest
, SHA512_DIGEST_SIZE
);
253 spin_unlock_irqrestore(&nx_ctx
->lock
, irq_flags
);
257 static int nx_sha512_export(struct shash_desc
*desc
, void *out
)
259 struct sha512_state_be
*sctx
= shash_desc_ctx(desc
);
261 memcpy(out
, sctx
, sizeof(*sctx
));
266 static int nx_sha512_import(struct shash_desc
*desc
, const void *in
)
268 struct sha512_state_be
*sctx
= shash_desc_ctx(desc
);
270 memcpy(sctx
, in
, sizeof(*sctx
));
275 struct shash_alg nx_shash_sha512_alg
= {
276 .digestsize
= SHA512_DIGEST_SIZE
,
277 .init
= nx_sha512_init
,
278 .update
= nx_sha512_update
,
279 .final
= nx_sha512_final
,
280 .export
= nx_sha512_export
,
281 .import
= nx_sha512_import
,
282 .descsize
= sizeof(struct sha512_state_be
),
283 .statesize
= sizeof(struct sha512_state_be
),
285 .cra_name
= "sha512",
286 .cra_driver_name
= "sha512-nx",
288 .cra_blocksize
= SHA512_BLOCK_SIZE
,
289 .cra_module
= THIS_MODULE
,
290 .cra_ctxsize
= sizeof(struct nx_crypto_ctx
),
291 .cra_init
= nx_crypto_ctx_sha512_init
,
292 .cra_exit
= nx_crypto_ctx_exit
,