xref: /OK3568_Linux_fs/kernel/include/crypto/internal/blake2b.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 OR MIT */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Helper functions for BLAKE2b implementations.
4*4882a593Smuzhiyun  * Keep this in sync with the corresponding BLAKE2s header.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #ifndef _CRYPTO_INTERNAL_BLAKE2B_H
8*4882a593Smuzhiyun #define _CRYPTO_INTERNAL_BLAKE2B_H
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <crypto/blake2b.h>
11*4882a593Smuzhiyun #include <crypto/internal/hash.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun void blake2b_compress_generic(struct blake2b_state *state,
15*4882a593Smuzhiyun 			      const u8 *block, size_t nblocks, u32 inc);
16*4882a593Smuzhiyun 
blake2b_set_lastblock(struct blake2b_state * state)17*4882a593Smuzhiyun static inline void blake2b_set_lastblock(struct blake2b_state *state)
18*4882a593Smuzhiyun {
19*4882a593Smuzhiyun 	state->f[0] = -1;
20*4882a593Smuzhiyun }
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun typedef void (*blake2b_compress_t)(struct blake2b_state *state,
23*4882a593Smuzhiyun 				   const u8 *block, size_t nblocks, u32 inc);
24*4882a593Smuzhiyun 
__blake2b_update(struct blake2b_state * state,const u8 * in,size_t inlen,blake2b_compress_t compress)25*4882a593Smuzhiyun static inline void __blake2b_update(struct blake2b_state *state,
26*4882a593Smuzhiyun 				    const u8 *in, size_t inlen,
27*4882a593Smuzhiyun 				    blake2b_compress_t compress)
28*4882a593Smuzhiyun {
29*4882a593Smuzhiyun 	const size_t fill = BLAKE2B_BLOCK_SIZE - state->buflen;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	if (unlikely(!inlen))
32*4882a593Smuzhiyun 		return;
33*4882a593Smuzhiyun 	if (inlen > fill) {
34*4882a593Smuzhiyun 		memcpy(state->buf + state->buflen, in, fill);
35*4882a593Smuzhiyun 		(*compress)(state, state->buf, 1, BLAKE2B_BLOCK_SIZE);
36*4882a593Smuzhiyun 		state->buflen = 0;
37*4882a593Smuzhiyun 		in += fill;
38*4882a593Smuzhiyun 		inlen -= fill;
39*4882a593Smuzhiyun 	}
40*4882a593Smuzhiyun 	if (inlen > BLAKE2B_BLOCK_SIZE) {
41*4882a593Smuzhiyun 		const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2B_BLOCK_SIZE);
42*4882a593Smuzhiyun 		/* Hash one less (full) block than strictly possible */
43*4882a593Smuzhiyun 		(*compress)(state, in, nblocks - 1, BLAKE2B_BLOCK_SIZE);
44*4882a593Smuzhiyun 		in += BLAKE2B_BLOCK_SIZE * (nblocks - 1);
45*4882a593Smuzhiyun 		inlen -= BLAKE2B_BLOCK_SIZE * (nblocks - 1);
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 	memcpy(state->buf + state->buflen, in, inlen);
48*4882a593Smuzhiyun 	state->buflen += inlen;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
__blake2b_final(struct blake2b_state * state,u8 * out,blake2b_compress_t compress)51*4882a593Smuzhiyun static inline void __blake2b_final(struct blake2b_state *state, u8 *out,
52*4882a593Smuzhiyun 				   blake2b_compress_t compress)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	int i;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	blake2b_set_lastblock(state);
57*4882a593Smuzhiyun 	memset(state->buf + state->buflen, 0,
58*4882a593Smuzhiyun 	       BLAKE2B_BLOCK_SIZE - state->buflen); /* Padding */
59*4882a593Smuzhiyun 	(*compress)(state, state->buf, 1, state->buflen);
60*4882a593Smuzhiyun 	for (i = 0; i < ARRAY_SIZE(state->h); i++)
61*4882a593Smuzhiyun 		__cpu_to_le64s(&state->h[i]);
62*4882a593Smuzhiyun 	memcpy(out, state->h, state->outlen);
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun /* Helper functions for shash implementations of BLAKE2b */
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun struct blake2b_tfm_ctx {
68*4882a593Smuzhiyun 	u8 key[BLAKE2B_KEY_SIZE];
69*4882a593Smuzhiyun 	unsigned int keylen;
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
crypto_blake2b_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)72*4882a593Smuzhiyun static inline int crypto_blake2b_setkey(struct crypto_shash *tfm,
73*4882a593Smuzhiyun 					const u8 *key, unsigned int keylen)
74*4882a593Smuzhiyun {
75*4882a593Smuzhiyun 	struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(tfm);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (keylen == 0 || keylen > BLAKE2B_KEY_SIZE)
78*4882a593Smuzhiyun 		return -EINVAL;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	memcpy(tctx->key, key, keylen);
81*4882a593Smuzhiyun 	tctx->keylen = keylen;
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	return 0;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
crypto_blake2b_init(struct shash_desc * desc)86*4882a593Smuzhiyun static inline int crypto_blake2b_init(struct shash_desc *desc)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	const struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
89*4882a593Smuzhiyun 	struct blake2b_state *state = shash_desc_ctx(desc);
90*4882a593Smuzhiyun 	unsigned int outlen = crypto_shash_digestsize(desc->tfm);
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	__blake2b_init(state, outlen, tctx->key, tctx->keylen);
93*4882a593Smuzhiyun 	return 0;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
crypto_blake2b_update(struct shash_desc * desc,const u8 * in,unsigned int inlen,blake2b_compress_t compress)96*4882a593Smuzhiyun static inline int crypto_blake2b_update(struct shash_desc *desc,
97*4882a593Smuzhiyun 					const u8 *in, unsigned int inlen,
98*4882a593Smuzhiyun 					blake2b_compress_t compress)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	struct blake2b_state *state = shash_desc_ctx(desc);
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 	__blake2b_update(state, in, inlen, compress);
103*4882a593Smuzhiyun 	return 0;
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun 
crypto_blake2b_final(struct shash_desc * desc,u8 * out,blake2b_compress_t compress)106*4882a593Smuzhiyun static inline int crypto_blake2b_final(struct shash_desc *desc, u8 *out,
107*4882a593Smuzhiyun 				       blake2b_compress_t compress)
108*4882a593Smuzhiyun {
109*4882a593Smuzhiyun 	struct blake2b_state *state = shash_desc_ctx(desc);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	__blake2b_final(state, out, compress);
112*4882a593Smuzhiyun 	return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun #endif /* _CRYPTO_INTERNAL_BLAKE2B_H */
116