2 * Multi buffer SHA1 algorithm Glue Code
4 * This file is provided under a dual BSD/GPLv2 license. When using or
5 * redistributing this file, you may do so under either license.
9 * Copyright(c) 2014 Intel Corporation.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * Contact Information:
21 * Tim Chen <tim.c.chen@linux.intel.com>
25 * Copyright(c) 2014 Intel Corporation.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
31 * * Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * * Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in
35 * the documentation and/or other materials provided with the
37 * * Neither the name of Intel Corporation nor the names of its
38 * contributors may be used to endorse or promote products derived
39 * from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
42 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
43 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
44 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
45 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
48 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
49 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
50 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
51 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
54 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
56 #include <crypto/internal/hash.h>
57 #include <linux/init.h>
58 #include <linux/module.h>
60 #include <linux/cryptohash.h>
61 #include <linux/types.h>
62 #include <linux/list.h>
63 #include <crypto/scatterwalk.h>
64 #include <crypto/sha.h>
65 #include <crypto/mcryptd.h>
66 #include <crypto/crypto_wq.h>
67 #include <asm/byteorder.h>
68 #include <linux/hardirq.h>
69 #include <asm/fpu/api.h>
70 #include "sha_mb_ctx.h"
72 #define FLUSH_INTERVAL 1000 /* in usec */
74 static struct mcryptd_alg_state sha1_mb_alg_state
;
77 struct mcryptd_ahash
*mcryptd_tfm
;
80 static inline struct mcryptd_hash_request_ctx
*cast_hash_to_mcryptd_ctx(struct sha1_hash_ctx
*hash_ctx
)
82 struct shash_desc
*desc
;
84 desc
= container_of((void *) hash_ctx
, struct shash_desc
, __ctx
);
85 return container_of(desc
, struct mcryptd_hash_request_ctx
, desc
);
88 static inline struct ahash_request
*cast_mcryptd_ctx_to_req(struct mcryptd_hash_request_ctx
*ctx
)
90 return container_of((void *) ctx
, struct ahash_request
, __ctx
);
93 static void req_ctx_init(struct mcryptd_hash_request_ctx
*rctx
,
94 struct shash_desc
*desc
)
96 rctx
->flag
= HASH_UPDATE
;
99 static asmlinkage
void (*sha1_job_mgr_init
)(struct sha1_mb_mgr
*state
);
100 static asmlinkage
struct job_sha1
* (*sha1_job_mgr_submit
)(struct sha1_mb_mgr
*state
,
101 struct job_sha1
*job
);
102 static asmlinkage
struct job_sha1
* (*sha1_job_mgr_flush
)(struct sha1_mb_mgr
*state
);
103 static asmlinkage
struct job_sha1
* (*sha1_job_mgr_get_comp_job
)(struct sha1_mb_mgr
*state
);
105 inline void sha1_init_digest(uint32_t *digest
)
107 static const uint32_t initial_digest
[SHA1_DIGEST_LENGTH
] = {SHA1_H0
,
108 SHA1_H1
, SHA1_H2
, SHA1_H3
, SHA1_H4
};
109 memcpy(digest
, initial_digest
, sizeof(initial_digest
));
112 inline uint32_t sha1_pad(uint8_t padblock
[SHA1_BLOCK_SIZE
* 2],
115 uint32_t i
= total_len
& (SHA1_BLOCK_SIZE
- 1);
117 memset(&padblock
[i
], 0, SHA1_BLOCK_SIZE
);
120 i
+= ((SHA1_BLOCK_SIZE
- 1) &
121 (0 - (total_len
+ SHA1_PADLENGTHFIELD_SIZE
+ 1)))
122 + 1 + SHA1_PADLENGTHFIELD_SIZE
;
124 #if SHA1_PADLENGTHFIELD_SIZE == 16
125 *((uint64_t *) &padblock
[i
- 16]) = 0;
128 *((uint64_t *) &padblock
[i
- 8]) = cpu_to_be64(total_len
<< 3);
130 /* Number of extra blocks to hash */
131 return i
>> SHA1_LOG2_BLOCK_SIZE
;
134 static struct sha1_hash_ctx
*sha1_ctx_mgr_resubmit(struct sha1_ctx_mgr
*mgr
, struct sha1_hash_ctx
*ctx
)
137 if (ctx
->status
& HASH_CTX_STS_COMPLETE
) {
138 /* Clear PROCESSING bit */
139 ctx
->status
= HASH_CTX_STS_COMPLETE
;
144 * If the extra blocks are empty, begin hashing what remains
145 * in the user's buffer.
147 if (ctx
->partial_block_buffer_length
== 0 &&
148 ctx
->incoming_buffer_length
) {
150 const void *buffer
= ctx
->incoming_buffer
;
151 uint32_t len
= ctx
->incoming_buffer_length
;
155 * Only entire blocks can be hashed.
156 * Copy remainder to extra blocks buffer.
158 copy_len
= len
& (SHA1_BLOCK_SIZE
-1);
162 memcpy(ctx
->partial_block_buffer
,
163 ((const char *) buffer
+ len
),
165 ctx
->partial_block_buffer_length
= copy_len
;
168 ctx
->incoming_buffer_length
= 0;
170 /* len should be a multiple of the block size now */
171 assert((len
% SHA1_BLOCK_SIZE
) == 0);
173 /* Set len to the number of blocks to be hashed */
174 len
>>= SHA1_LOG2_BLOCK_SIZE
;
178 ctx
->job
.buffer
= (uint8_t *) buffer
;
180 ctx
= (struct sha1_hash_ctx
*) sha1_job_mgr_submit(&mgr
->mgr
,
187 * If the extra blocks are not empty, then we are
188 * either on the last block(s) or we need more
189 * user input before continuing.
191 if (ctx
->status
& HASH_CTX_STS_LAST
) {
193 uint8_t *buf
= ctx
->partial_block_buffer
;
194 uint32_t n_extra_blocks
= sha1_pad(buf
, ctx
->total_length
);
196 ctx
->status
= (HASH_CTX_STS_PROCESSING
|
197 HASH_CTX_STS_COMPLETE
);
198 ctx
->job
.buffer
= buf
;
199 ctx
->job
.len
= (uint32_t) n_extra_blocks
;
200 ctx
= (struct sha1_hash_ctx
*) sha1_job_mgr_submit(&mgr
->mgr
, &ctx
->job
);
204 ctx
->status
= HASH_CTX_STS_IDLE
;
211 static struct sha1_hash_ctx
*sha1_ctx_mgr_get_comp_ctx(struct sha1_ctx_mgr
*mgr
)
214 * If get_comp_job returns NULL, there are no jobs complete.
215 * If get_comp_job returns a job, verify that it is safe to return to the user.
216 * If it is not ready, resubmit the job to finish processing.
217 * If sha1_ctx_mgr_resubmit returned a job, it is ready to be returned.
218 * Otherwise, all jobs currently being managed by the hash_ctx_mgr still need processing.
220 struct sha1_hash_ctx
*ctx
;
222 ctx
= (struct sha1_hash_ctx
*) sha1_job_mgr_get_comp_job(&mgr
->mgr
);
223 return sha1_ctx_mgr_resubmit(mgr
, ctx
);
226 static void sha1_ctx_mgr_init(struct sha1_ctx_mgr
*mgr
)
228 sha1_job_mgr_init(&mgr
->mgr
);
231 static struct sha1_hash_ctx
*sha1_ctx_mgr_submit(struct sha1_ctx_mgr
*mgr
,
232 struct sha1_hash_ctx
*ctx
,
237 if (flags
& (~HASH_ENTIRE
)) {
238 /* User should not pass anything other than FIRST, UPDATE, or LAST */
239 ctx
->error
= HASH_CTX_ERROR_INVALID_FLAGS
;
243 if (ctx
->status
& HASH_CTX_STS_PROCESSING
) {
244 /* Cannot submit to a currently processing job. */
245 ctx
->error
= HASH_CTX_ERROR_ALREADY_PROCESSING
;
249 if ((ctx
->status
& HASH_CTX_STS_COMPLETE
) && !(flags
& HASH_FIRST
)) {
250 /* Cannot update a finished job. */
251 ctx
->error
= HASH_CTX_ERROR_ALREADY_COMPLETED
;
256 if (flags
& HASH_FIRST
) {
258 sha1_init_digest(ctx
->job
.result_digest
);
260 /* Reset byte counter */
261 ctx
->total_length
= 0;
263 /* Clear extra blocks */
264 ctx
->partial_block_buffer_length
= 0;
267 /* If we made it here, there were no errors during this call to submit */
268 ctx
->error
= HASH_CTX_ERROR_NONE
;
270 /* Store buffer ptr info from user */
271 ctx
->incoming_buffer
= buffer
;
272 ctx
->incoming_buffer_length
= len
;
274 /* Store the user's request flags and mark this ctx as currently being processed. */
275 ctx
->status
= (flags
& HASH_LAST
) ?
276 (HASH_CTX_STS_PROCESSING
| HASH_CTX_STS_LAST
) :
277 HASH_CTX_STS_PROCESSING
;
279 /* Advance byte counter */
280 ctx
->total_length
+= len
;
283 * If there is anything currently buffered in the extra blocks,
284 * append to it until it contains a whole block.
285 * Or if the user's buffer contains less than a whole block,
286 * append as much as possible to the extra block.
288 if ((ctx
->partial_block_buffer_length
) | (len
< SHA1_BLOCK_SIZE
)) {
289 /* Compute how many bytes to copy from user buffer into extra block */
290 uint32_t copy_len
= SHA1_BLOCK_SIZE
- ctx
->partial_block_buffer_length
;
295 /* Copy and update relevant pointers and counters */
296 memcpy(&ctx
->partial_block_buffer
[ctx
->partial_block_buffer_length
],
299 ctx
->partial_block_buffer_length
+= copy_len
;
300 ctx
->incoming_buffer
= (const void *)((const char *)buffer
+ copy_len
);
301 ctx
->incoming_buffer_length
= len
- copy_len
;
304 /* The extra block should never contain more than 1 block here */
305 assert(ctx
->partial_block_buffer_length
<= SHA1_BLOCK_SIZE
);
307 /* If the extra block buffer contains exactly 1 block, it can be hashed. */
308 if (ctx
->partial_block_buffer_length
>= SHA1_BLOCK_SIZE
) {
309 ctx
->partial_block_buffer_length
= 0;
311 ctx
->job
.buffer
= ctx
->partial_block_buffer
;
313 ctx
= (struct sha1_hash_ctx
*) sha1_job_mgr_submit(&mgr
->mgr
, &ctx
->job
);
317 return sha1_ctx_mgr_resubmit(mgr
, ctx
);
320 static struct sha1_hash_ctx
*sha1_ctx_mgr_flush(struct sha1_ctx_mgr
*mgr
)
322 struct sha1_hash_ctx
*ctx
;
325 ctx
= (struct sha1_hash_ctx
*) sha1_job_mgr_flush(&mgr
->mgr
);
327 /* If flush returned 0, there are no more jobs in flight. */
332 * If flush returned a job, resubmit the job to finish processing.
334 ctx
= sha1_ctx_mgr_resubmit(mgr
, ctx
);
337 * If sha1_ctx_mgr_resubmit returned a job, it is ready to be returned.
338 * Otherwise, all jobs currently being managed by the sha1_ctx_mgr
339 * still need processing. Loop.
346 static int sha1_mb_init(struct shash_desc
*desc
)
348 struct sha1_hash_ctx
*sctx
= shash_desc_ctx(desc
);
351 sctx
->job
.result_digest
[0] = SHA1_H0
;
352 sctx
->job
.result_digest
[1] = SHA1_H1
;
353 sctx
->job
.result_digest
[2] = SHA1_H2
;
354 sctx
->job
.result_digest
[3] = SHA1_H3
;
355 sctx
->job
.result_digest
[4] = SHA1_H4
;
356 sctx
->total_length
= 0;
357 sctx
->partial_block_buffer_length
= 0;
358 sctx
->status
= HASH_CTX_STS_IDLE
;
363 static int sha1_mb_set_results(struct mcryptd_hash_request_ctx
*rctx
)
366 struct sha1_hash_ctx
*sctx
= shash_desc_ctx(&rctx
->desc
);
367 __be32
*dst
= (__be32
*) rctx
->out
;
369 for (i
= 0; i
< 5; ++i
)
370 dst
[i
] = cpu_to_be32(sctx
->job
.result_digest
[i
]);
375 static int sha_finish_walk(struct mcryptd_hash_request_ctx
**ret_rctx
,
376 struct mcryptd_alg_cstate
*cstate
, bool flush
)
378 int flag
= HASH_UPDATE
;
380 struct mcryptd_hash_request_ctx
*rctx
= *ret_rctx
;
381 struct sha1_hash_ctx
*sha_ctx
;
384 while (!(rctx
->flag
& HASH_DONE
)) {
385 nbytes
= crypto_ahash_walk_done(&rctx
->walk
, 0);
390 /* check if the walk is done */
391 if (crypto_ahash_walk_last(&rctx
->walk
)) {
392 rctx
->flag
|= HASH_DONE
;
393 if (rctx
->flag
& HASH_FINAL
)
397 sha_ctx
= (struct sha1_hash_ctx
*) shash_desc_ctx(&rctx
->desc
);
399 sha_ctx
= sha1_ctx_mgr_submit(cstate
->mgr
, sha_ctx
, rctx
->walk
.data
, nbytes
, flag
);
402 sha_ctx
= sha1_ctx_mgr_flush(cstate
->mgr
);
406 rctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
413 /* copy the results */
414 if (rctx
->flag
& HASH_FINAL
)
415 sha1_mb_set_results(rctx
);
422 static int sha_complete_job(struct mcryptd_hash_request_ctx
*rctx
,
423 struct mcryptd_alg_cstate
*cstate
,
426 struct ahash_request
*req
= cast_mcryptd_ctx_to_req(rctx
);
427 struct sha1_hash_ctx
*sha_ctx
;
428 struct mcryptd_hash_request_ctx
*req_ctx
;
431 /* remove from work list */
432 spin_lock(&cstate
->work_lock
);
433 list_del(&rctx
->waiter
);
434 spin_unlock(&cstate
->work_lock
);
437 rctx
->complete(&req
->base
, err
);
440 rctx
->complete(&req
->base
, err
);
444 /* check to see if there are other jobs that are done */
445 sha_ctx
= sha1_ctx_mgr_get_comp_ctx(cstate
->mgr
);
447 req_ctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
448 ret
= sha_finish_walk(&req_ctx
, cstate
, false);
450 spin_lock(&cstate
->work_lock
);
451 list_del(&req_ctx
->waiter
);
452 spin_unlock(&cstate
->work_lock
);
454 req
= cast_mcryptd_ctx_to_req(req_ctx
);
456 rctx
->complete(&req
->base
, ret
);
459 rctx
->complete(&req
->base
, ret
);
463 sha_ctx
= sha1_ctx_mgr_get_comp_ctx(cstate
->mgr
);
469 static void sha1_mb_add_list(struct mcryptd_hash_request_ctx
*rctx
,
470 struct mcryptd_alg_cstate
*cstate
)
472 unsigned long next_flush
;
473 unsigned long delay
= usecs_to_jiffies(FLUSH_INTERVAL
);
476 rctx
->tag
.arrival
= jiffies
; /* tag the arrival time */
477 rctx
->tag
.seq_num
= cstate
->next_seq_num
++;
478 next_flush
= rctx
->tag
.arrival
+ delay
;
479 rctx
->tag
.expire
= next_flush
;
481 spin_lock(&cstate
->work_lock
);
482 list_add_tail(&rctx
->waiter
, &cstate
->work_list
);
483 spin_unlock(&cstate
->work_lock
);
485 mcryptd_arm_flusher(cstate
, delay
);
488 static int sha1_mb_update(struct shash_desc
*desc
, const u8
*data
,
491 struct mcryptd_hash_request_ctx
*rctx
=
492 container_of(desc
, struct mcryptd_hash_request_ctx
, desc
);
493 struct mcryptd_alg_cstate
*cstate
=
494 this_cpu_ptr(sha1_mb_alg_state
.alg_cstate
);
496 struct ahash_request
*req
= cast_mcryptd_ctx_to_req(rctx
);
497 struct sha1_hash_ctx
*sha_ctx
;
502 if (rctx
->tag
.cpu
!= smp_processor_id()) {
503 pr_err("mcryptd error: cpu clash\n");
507 /* need to init context */
508 req_ctx_init(rctx
, desc
);
510 nbytes
= crypto_ahash_walk_first(req
, &rctx
->walk
);
517 if (crypto_ahash_walk_last(&rctx
->walk
))
518 rctx
->flag
|= HASH_DONE
;
521 sha_ctx
= (struct sha1_hash_ctx
*) shash_desc_ctx(desc
);
522 sha1_mb_add_list(rctx
, cstate
);
524 sha_ctx
= sha1_ctx_mgr_submit(cstate
->mgr
, sha_ctx
, rctx
->walk
.data
, nbytes
, HASH_UPDATE
);
527 /* check if anything is returned */
531 if (sha_ctx
->error
) {
532 ret
= sha_ctx
->error
;
533 rctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
537 rctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
538 ret
= sha_finish_walk(&rctx
, cstate
, false);
543 sha_complete_job(rctx
, cstate
, ret
);
547 static int sha1_mb_finup(struct shash_desc
*desc
, const u8
*data
,
548 unsigned int len
, u8
*out
)
550 struct mcryptd_hash_request_ctx
*rctx
=
551 container_of(desc
, struct mcryptd_hash_request_ctx
, desc
);
552 struct mcryptd_alg_cstate
*cstate
=
553 this_cpu_ptr(sha1_mb_alg_state
.alg_cstate
);
555 struct ahash_request
*req
= cast_mcryptd_ctx_to_req(rctx
);
556 struct sha1_hash_ctx
*sha_ctx
;
557 int ret
= 0, flag
= HASH_UPDATE
, nbytes
;
560 if (rctx
->tag
.cpu
!= smp_processor_id()) {
561 pr_err("mcryptd error: cpu clash\n");
565 /* need to init context */
566 req_ctx_init(rctx
, desc
);
568 nbytes
= crypto_ahash_walk_first(req
, &rctx
->walk
);
575 if (crypto_ahash_walk_last(&rctx
->walk
)) {
576 rctx
->flag
|= HASH_DONE
;
582 rctx
->flag
|= HASH_FINAL
;
583 sha_ctx
= (struct sha1_hash_ctx
*) shash_desc_ctx(desc
);
584 sha1_mb_add_list(rctx
, cstate
);
587 sha_ctx
= sha1_ctx_mgr_submit(cstate
->mgr
, sha_ctx
, rctx
->walk
.data
, nbytes
, flag
);
590 /* check if anything is returned */
594 if (sha_ctx
->error
) {
595 ret
= sha_ctx
->error
;
599 rctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
600 ret
= sha_finish_walk(&rctx
, cstate
, false);
604 sha_complete_job(rctx
, cstate
, ret
);
608 static int sha1_mb_final(struct shash_desc
*desc
, u8
*out
)
610 struct mcryptd_hash_request_ctx
*rctx
=
611 container_of(desc
, struct mcryptd_hash_request_ctx
, desc
);
612 struct mcryptd_alg_cstate
*cstate
=
613 this_cpu_ptr(sha1_mb_alg_state
.alg_cstate
);
615 struct sha1_hash_ctx
*sha_ctx
;
620 if (rctx
->tag
.cpu
!= smp_processor_id()) {
621 pr_err("mcryptd error: cpu clash\n");
625 /* need to init context */
626 req_ctx_init(rctx
, desc
);
629 rctx
->flag
|= HASH_DONE
| HASH_FINAL
;
631 sha_ctx
= (struct sha1_hash_ctx
*) shash_desc_ctx(desc
);
632 /* flag HASH_FINAL and 0 data size */
633 sha1_mb_add_list(rctx
, cstate
);
635 sha_ctx
= sha1_ctx_mgr_submit(cstate
->mgr
, sha_ctx
, &data
, 0, HASH_LAST
);
638 /* check if anything is returned */
642 if (sha_ctx
->error
) {
643 ret
= sha_ctx
->error
;
644 rctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
648 rctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
649 ret
= sha_finish_walk(&rctx
, cstate
, false);
653 sha_complete_job(rctx
, cstate
, ret
);
657 static int sha1_mb_export(struct shash_desc
*desc
, void *out
)
659 struct sha1_hash_ctx
*sctx
= shash_desc_ctx(desc
);
661 memcpy(out
, sctx
, sizeof(*sctx
));
666 static int sha1_mb_import(struct shash_desc
*desc
, const void *in
)
668 struct sha1_hash_ctx
*sctx
= shash_desc_ctx(desc
);
670 memcpy(sctx
, in
, sizeof(*sctx
));
676 static struct shash_alg sha1_mb_shash_alg
= {
677 .digestsize
= SHA1_DIGEST_SIZE
,
678 .init
= sha1_mb_init
,
679 .update
= sha1_mb_update
,
680 .final
= sha1_mb_final
,
681 .finup
= sha1_mb_finup
,
682 .export
= sha1_mb_export
,
683 .import
= sha1_mb_import
,
684 .descsize
= sizeof(struct sha1_hash_ctx
),
685 .statesize
= sizeof(struct sha1_hash_ctx
),
687 .cra_name
= "__sha1-mb",
688 .cra_driver_name
= "__intel_sha1-mb",
691 * use ASYNC flag as some buffers in multi-buffer
692 * algo may not have completed before hashing thread sleep
694 .cra_flags
= CRYPTO_ALG_TYPE_SHASH
| CRYPTO_ALG_ASYNC
|
696 .cra_blocksize
= SHA1_BLOCK_SIZE
,
697 .cra_module
= THIS_MODULE
,
698 .cra_list
= LIST_HEAD_INIT(sha1_mb_shash_alg
.base
.cra_list
),
702 static int sha1_mb_async_init(struct ahash_request
*req
)
704 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
705 struct sha1_mb_ctx
*ctx
= crypto_ahash_ctx(tfm
);
706 struct ahash_request
*mcryptd_req
= ahash_request_ctx(req
);
707 struct mcryptd_ahash
*mcryptd_tfm
= ctx
->mcryptd_tfm
;
709 memcpy(mcryptd_req
, req
, sizeof(*req
));
710 ahash_request_set_tfm(mcryptd_req
, &mcryptd_tfm
->base
);
711 return crypto_ahash_init(mcryptd_req
);
714 static int sha1_mb_async_update(struct ahash_request
*req
)
716 struct ahash_request
*mcryptd_req
= ahash_request_ctx(req
);
718 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
719 struct sha1_mb_ctx
*ctx
= crypto_ahash_ctx(tfm
);
720 struct mcryptd_ahash
*mcryptd_tfm
= ctx
->mcryptd_tfm
;
722 memcpy(mcryptd_req
, req
, sizeof(*req
));
723 ahash_request_set_tfm(mcryptd_req
, &mcryptd_tfm
->base
);
724 return crypto_ahash_update(mcryptd_req
);
727 static int sha1_mb_async_finup(struct ahash_request
*req
)
729 struct ahash_request
*mcryptd_req
= ahash_request_ctx(req
);
731 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
732 struct sha1_mb_ctx
*ctx
= crypto_ahash_ctx(tfm
);
733 struct mcryptd_ahash
*mcryptd_tfm
= ctx
->mcryptd_tfm
;
735 memcpy(mcryptd_req
, req
, sizeof(*req
));
736 ahash_request_set_tfm(mcryptd_req
, &mcryptd_tfm
->base
);
737 return crypto_ahash_finup(mcryptd_req
);
740 static int sha1_mb_async_final(struct ahash_request
*req
)
742 struct ahash_request
*mcryptd_req
= ahash_request_ctx(req
);
744 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
745 struct sha1_mb_ctx
*ctx
= crypto_ahash_ctx(tfm
);
746 struct mcryptd_ahash
*mcryptd_tfm
= ctx
->mcryptd_tfm
;
748 memcpy(mcryptd_req
, req
, sizeof(*req
));
749 ahash_request_set_tfm(mcryptd_req
, &mcryptd_tfm
->base
);
750 return crypto_ahash_final(mcryptd_req
);
753 static int sha1_mb_async_digest(struct ahash_request
*req
)
755 struct crypto_ahash
*tfm
= crypto_ahash_reqtfm(req
);
756 struct sha1_mb_ctx
*ctx
= crypto_ahash_ctx(tfm
);
757 struct ahash_request
*mcryptd_req
= ahash_request_ctx(req
);
758 struct mcryptd_ahash
*mcryptd_tfm
= ctx
->mcryptd_tfm
;
760 memcpy(mcryptd_req
, req
, sizeof(*req
));
761 ahash_request_set_tfm(mcryptd_req
, &mcryptd_tfm
->base
);
762 return crypto_ahash_digest(mcryptd_req
);
765 static int sha1_mb_async_init_tfm(struct crypto_tfm
*tfm
)
767 struct mcryptd_ahash
*mcryptd_tfm
;
768 struct sha1_mb_ctx
*ctx
= crypto_tfm_ctx(tfm
);
769 struct mcryptd_hash_ctx
*mctx
;
771 mcryptd_tfm
= mcryptd_alloc_ahash("__intel_sha1-mb",
773 CRYPTO_ALG_INTERNAL
);
774 if (IS_ERR(mcryptd_tfm
))
775 return PTR_ERR(mcryptd_tfm
);
776 mctx
= crypto_ahash_ctx(&mcryptd_tfm
->base
);
777 mctx
->alg_state
= &sha1_mb_alg_state
;
778 ctx
->mcryptd_tfm
= mcryptd_tfm
;
779 crypto_ahash_set_reqsize(__crypto_ahash_cast(tfm
),
780 sizeof(struct ahash_request
) +
781 crypto_ahash_reqsize(&mcryptd_tfm
->base
));
786 static void sha1_mb_async_exit_tfm(struct crypto_tfm
*tfm
)
788 struct sha1_mb_ctx
*ctx
= crypto_tfm_ctx(tfm
);
790 mcryptd_free_ahash(ctx
->mcryptd_tfm
);
793 static struct ahash_alg sha1_mb_async_alg
= {
794 .init
= sha1_mb_async_init
,
795 .update
= sha1_mb_async_update
,
796 .final
= sha1_mb_async_final
,
797 .finup
= sha1_mb_async_finup
,
798 .digest
= sha1_mb_async_digest
,
800 .digestsize
= SHA1_DIGEST_SIZE
,
803 .cra_driver_name
= "sha1_mb",
805 .cra_flags
= CRYPTO_ALG_TYPE_AHASH
| CRYPTO_ALG_ASYNC
,
806 .cra_blocksize
= SHA1_BLOCK_SIZE
,
807 .cra_type
= &crypto_ahash_type
,
808 .cra_module
= THIS_MODULE
,
809 .cra_list
= LIST_HEAD_INIT(sha1_mb_async_alg
.halg
.base
.cra_list
),
810 .cra_init
= sha1_mb_async_init_tfm
,
811 .cra_exit
= sha1_mb_async_exit_tfm
,
812 .cra_ctxsize
= sizeof(struct sha1_mb_ctx
),
818 static unsigned long sha1_mb_flusher(struct mcryptd_alg_cstate
*cstate
)
820 struct mcryptd_hash_request_ctx
*rctx
;
821 unsigned long cur_time
;
822 unsigned long next_flush
= 0;
823 struct sha1_hash_ctx
*sha_ctx
;
828 while (!list_empty(&cstate
->work_list
)) {
829 rctx
= list_entry(cstate
->work_list
.next
,
830 struct mcryptd_hash_request_ctx
, waiter
);
831 if (time_before(cur_time
, rctx
->tag
.expire
))
834 sha_ctx
= (struct sha1_hash_ctx
*) sha1_ctx_mgr_flush(cstate
->mgr
);
837 pr_err("sha1_mb error: nothing got flushed for non-empty list\n");
840 rctx
= cast_hash_to_mcryptd_ctx(sha_ctx
);
841 sha_finish_walk(&rctx
, cstate
, true);
842 sha_complete_job(rctx
, cstate
, 0);
845 if (!list_empty(&cstate
->work_list
)) {
846 rctx
= list_entry(cstate
->work_list
.next
,
847 struct mcryptd_hash_request_ctx
, waiter
);
848 /* get the hash context and then flush time */
849 next_flush
= rctx
->tag
.expire
;
850 mcryptd_arm_flusher(cstate
, get_delay(next_flush
));
855 static int __init
sha1_mb_mod_init(void)
860 struct mcryptd_alg_cstate
*cpu_state
;
862 /* check for dependent cpu features */
863 if (!boot_cpu_has(X86_FEATURE_AVX2
) ||
864 !boot_cpu_has(X86_FEATURE_BMI2
))
867 /* initialize multibuffer structures */
868 sha1_mb_alg_state
.alg_cstate
= alloc_percpu(struct mcryptd_alg_cstate
);
870 sha1_job_mgr_init
= sha1_mb_mgr_init_avx2
;
871 sha1_job_mgr_submit
= sha1_mb_mgr_submit_avx2
;
872 sha1_job_mgr_flush
= sha1_mb_mgr_flush_avx2
;
873 sha1_job_mgr_get_comp_job
= sha1_mb_mgr_get_comp_job_avx2
;
875 if (!sha1_mb_alg_state
.alg_cstate
)
877 for_each_possible_cpu(cpu
) {
878 cpu_state
= per_cpu_ptr(sha1_mb_alg_state
.alg_cstate
, cpu
);
879 cpu_state
->next_flush
= 0;
880 cpu_state
->next_seq_num
= 0;
881 cpu_state
->flusher_engaged
= false;
882 INIT_DELAYED_WORK(&cpu_state
->flush
, mcryptd_flusher
);
883 cpu_state
->cpu
= cpu
;
884 cpu_state
->alg_state
= &sha1_mb_alg_state
;
885 cpu_state
->mgr
= kzalloc(sizeof(struct sha1_ctx_mgr
),
889 sha1_ctx_mgr_init(cpu_state
->mgr
);
890 INIT_LIST_HEAD(&cpu_state
->work_list
);
891 spin_lock_init(&cpu_state
->work_lock
);
893 sha1_mb_alg_state
.flusher
= &sha1_mb_flusher
;
895 err
= crypto_register_shash(&sha1_mb_shash_alg
);
898 err
= crypto_register_ahash(&sha1_mb_async_alg
);
905 crypto_unregister_shash(&sha1_mb_shash_alg
);
907 for_each_possible_cpu(cpu
) {
908 cpu_state
= per_cpu_ptr(sha1_mb_alg_state
.alg_cstate
, cpu
);
909 kfree(cpu_state
->mgr
);
911 free_percpu(sha1_mb_alg_state
.alg_cstate
);
915 static void __exit
sha1_mb_mod_fini(void)
918 struct mcryptd_alg_cstate
*cpu_state
;
920 crypto_unregister_ahash(&sha1_mb_async_alg
);
921 crypto_unregister_shash(&sha1_mb_shash_alg
);
922 for_each_possible_cpu(cpu
) {
923 cpu_state
= per_cpu_ptr(sha1_mb_alg_state
.alg_cstate
, cpu
);
924 kfree(cpu_state
->mgr
);
926 free_percpu(sha1_mb_alg_state
.alg_cstate
);
929 module_init(sha1_mb_mod_init
);
930 module_exit(sha1_mb_mod_fini
);
932 MODULE_LICENSE("GPL");
933 MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm, multi buffer accelerated");
935 MODULE_ALIAS_CRYPTO("sha1");