1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * sha512_base.h - core logic for SHA-512 implementations
4  *
5  * Copyright (C) 2015 Linaro Ltd <ard.biesheuvel@linaro.org>
6  */
7 
8 #ifndef _CRYPTO_SHA512_BASE_H
9 #define _CRYPTO_SHA512_BASE_H
10 
11 #include <crypto/internal/hash.h>
12 #include <crypto/sha2.h>
13 #include <linux/compiler.h>
14 #include <linux/math.h>
15 #include <linux/string.h>
16 #include <linux/types.h>
17 #include <linux/unaligned.h>
18 
19 typedef void (sha512_block_fn)(struct sha512_state *sst, u8 const *src,
20 			       int blocks);
21 
22 static inline int sha384_base_init(struct shash_desc *desc)
23 {
24 	struct sha512_state *sctx = shash_desc_ctx(desc);
25 
26 	sctx->state[0] = SHA384_H0;
27 	sctx->state[1] = SHA384_H1;
28 	sctx->state[2] = SHA384_H2;
29 	sctx->state[3] = SHA384_H3;
30 	sctx->state[4] = SHA384_H4;
31 	sctx->state[5] = SHA384_H5;
32 	sctx->state[6] = SHA384_H6;
33 	sctx->state[7] = SHA384_H7;
34 	sctx->count[0] = sctx->count[1] = 0;
35 
36 	return 0;
37 }
38 
39 static inline int sha512_base_init(struct shash_desc *desc)
40 {
41 	struct sha512_state *sctx = shash_desc_ctx(desc);
42 
43 	sctx->state[0] = SHA512_H0;
44 	sctx->state[1] = SHA512_H1;
45 	sctx->state[2] = SHA512_H2;
46 	sctx->state[3] = SHA512_H3;
47 	sctx->state[4] = SHA512_H4;
48 	sctx->state[5] = SHA512_H5;
49 	sctx->state[6] = SHA512_H6;
50 	sctx->state[7] = SHA512_H7;
51 	sctx->count[0] = sctx->count[1] = 0;
52 
53 	return 0;
54 }
55 
56 static inline int sha512_base_do_update_blocks(struct shash_desc *desc,
57 					       const u8 *data,
58 					       unsigned int len,
59 					       sha512_block_fn *block_fn)
60 {
61 	unsigned int remain = len - round_down(len, SHA512_BLOCK_SIZE);
62 	struct sha512_state *sctx = shash_desc_ctx(desc);
63 
64 	len -= remain;
65 	sctx->count[0] += len;
66 	if (sctx->count[0] < len)
67 		sctx->count[1]++;
68 	block_fn(sctx, data, len / SHA512_BLOCK_SIZE);
69 	return remain;
70 }
71 
72 static inline int sha512_base_do_finup(struct shash_desc *desc, const u8 *src,
73 				       unsigned int len,
74 				       sha512_block_fn *block_fn)
75 {
76 	unsigned int bit_offset = SHA512_BLOCK_SIZE / 8 - 2;
77 	struct sha512_state *sctx = shash_desc_ctx(desc);
78 	union {
79 		__be64 b64[SHA512_BLOCK_SIZE / 4];
80 		u8 u8[SHA512_BLOCK_SIZE * 2];
81 	} block = {};
82 
83 	if (len >= SHA512_BLOCK_SIZE) {
84 		int remain;
85 
86 		remain = sha512_base_do_update_blocks(desc, src, len, block_fn);
87 		src += len - remain;
88 		len = remain;
89 	}
90 
91 	if (len >= bit_offset * 8)
92 		bit_offset += SHA512_BLOCK_SIZE / 8;
93 	memcpy(&block, src, len);
94 	block.u8[len] = 0x80;
95 	sctx->count[0] += len;
96 	block.b64[bit_offset] = cpu_to_be64(sctx->count[1] << 3 |
97 					    sctx->count[0] >> 61);
98 	block.b64[bit_offset + 1] = cpu_to_be64(sctx->count[0] << 3);
99 	block_fn(sctx, block.u8, (bit_offset + 2) * 8 / SHA512_BLOCK_SIZE);
100 	memzero_explicit(&block, sizeof(block));
101 
102 	return 0;
103 }
104 
105 static inline int sha512_base_finish(struct shash_desc *desc, u8 *out)
106 {
107 	unsigned int digest_size = crypto_shash_digestsize(desc->tfm);
108 	struct sha512_state *sctx = shash_desc_ctx(desc);
109 	__be64 *digest = (__be64 *)out;
110 	int i;
111 
112 	for (i = 0; digest_size > 0; i++, digest_size -= sizeof(__be64))
113 		put_unaligned_be64(sctx->state[i], digest++);
114 	return 0;
115 }
116 
117 void sha512_generic_block_fn(struct sha512_state *sst, u8 const *src,
118 			     int blocks);
119 
120 #endif /* _CRYPTO_SHA512_BASE_H */
121