xref: /linux/lib/crypto/powerpc/sha256.h (revision 13150742b09e720fdf021de14cd2b98b37415a89)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * SHA-256 Secure Hash Algorithm, SPE optimized
4  *
5  * Based on generic implementation. The assembler module takes care
6  * about the SPE registers so it can run from interrupt context.
7  *
8  * Copyright (c) 2015 Markus Stockhausen <stockhausen@collogia.de>
9  */
10 
11 #include <asm/switch_to.h>
12 #include <linux/preempt.h>
13 
14 /*
15  * MAX_BYTES defines the number of bytes that are allowed to be processed
16  * between preempt_disable() and preempt_enable(). SHA256 takes ~2,000
17  * operations per 64 bytes. e500 cores can issue two arithmetic instructions
18  * per clock cycle using one 32/64 bit unit (SU1) and one 32 bit unit (SU2).
19  * Thus 1KB of input data will need an estimated maximum of 18,000 cycles.
20  * Headroom for cache misses included. Even with the low end model clocked
21  * at 667 MHz this equals to a critical time window of less than 27us.
22  *
23  */
24 #define MAX_BYTES 1024
25 
26 extern void ppc_spe_sha256_transform(struct sha256_block_state *state,
27 				     const u8 *src, u32 blocks);
28 
spe_begin(void)29 static void spe_begin(void)
30 {
31 	/* We just start SPE operations and will save SPE registers later. */
32 	preempt_disable();
33 	enable_kernel_spe();
34 }
35 
spe_end(void)36 static void spe_end(void)
37 {
38 	disable_kernel_spe();
39 	/* reenable preemption */
40 	preempt_enable();
41 }
42 
sha256_blocks(struct sha256_block_state * state,const u8 * data,size_t nblocks)43 static void sha256_blocks(struct sha256_block_state *state,
44 			  const u8 *data, size_t nblocks)
45 {
46 	do {
47 		/* cut input data into smaller blocks */
48 		u32 unit = min_t(size_t, nblocks,
49 				 MAX_BYTES / SHA256_BLOCK_SIZE);
50 
51 		spe_begin();
52 		ppc_spe_sha256_transform(state, data, unit);
53 		spe_end();
54 
55 		data += unit * SHA256_BLOCK_SIZE;
56 		nblocks -= unit;
57 	} while (nblocks);
58 }
59