Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
1/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2/*
3 * Helper functions for BLAKE2b implementations.
4 * Keep this in sync with the corresponding BLAKE2s header.
5 */
6
7#ifndef _CRYPTO_INTERNAL_BLAKE2B_H
8#define _CRYPTO_INTERNAL_BLAKE2B_H
9
10#include <asm/byteorder.h>
11#include <crypto/blake2b.h>
12#include <crypto/internal/hash.h>
13#include <linux/array_size.h>
14#include <linux/compiler.h>
15#include <linux/build_bug.h>
16#include <linux/errno.h>
17#include <linux/math.h>
18#include <linux/string.h>
19#include <linux/types.h>
20
21static inline void blake2b_set_lastblock(struct blake2b_state *state)
22{
23 state->f[0] = -1;
24 state->f[1] = 0;
25}
26
27static inline void blake2b_set_nonlast(struct blake2b_state *state)
28{
29 state->f[0] = 0;
30 state->f[1] = 0;
31}
32
33typedef void (*blake2b_compress_t)(struct blake2b_state *state,
34 const u8 *block, size_t nblocks, u32 inc);
35
36/* Helper functions for shash implementations of BLAKE2b */
37
38struct blake2b_tfm_ctx {
39 u8 key[BLAKE2B_BLOCK_SIZE];
40 unsigned int keylen;
41};
42
43static inline int crypto_blake2b_setkey(struct crypto_shash *tfm,
44 const u8 *key, unsigned int keylen)
45{
46 struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(tfm);
47
48 if (keylen > BLAKE2B_KEY_SIZE)
49 return -EINVAL;
50
51 BUILD_BUG_ON(BLAKE2B_KEY_SIZE > BLAKE2B_BLOCK_SIZE);
52
53 memcpy(tctx->key, key, keylen);
54 memset(tctx->key + keylen, 0, BLAKE2B_BLOCK_SIZE - keylen);
55 tctx->keylen = keylen;
56
57 return 0;
58}
59
60static inline int crypto_blake2b_init(struct shash_desc *desc)
61{
62 const struct blake2b_tfm_ctx *tctx = crypto_shash_ctx(desc->tfm);
63 struct blake2b_state *state = shash_desc_ctx(desc);
64 unsigned int outlen = crypto_shash_digestsize(desc->tfm);
65
66 __blake2b_init(state, outlen, tctx->keylen);
67 return tctx->keylen ?
68 crypto_shash_update(desc, tctx->key, BLAKE2B_BLOCK_SIZE) : 0;
69}
70
71static inline int crypto_blake2b_update_bo(struct shash_desc *desc,
72 const u8 *in, unsigned int inlen,
73 blake2b_compress_t compress)
74{
75 struct blake2b_state *state = shash_desc_ctx(desc);
76
77 blake2b_set_nonlast(state);
78 compress(state, in, inlen / BLAKE2B_BLOCK_SIZE, BLAKE2B_BLOCK_SIZE);
79 return inlen - round_down(inlen, BLAKE2B_BLOCK_SIZE);
80}
81
82static inline int crypto_blake2b_finup(struct shash_desc *desc, const u8 *in,
83 unsigned int inlen, u8 *out,
84 blake2b_compress_t compress)
85{
86 struct blake2b_state *state = shash_desc_ctx(desc);
87 u8 buf[BLAKE2B_BLOCK_SIZE];
88 int i;
89
90 memcpy(buf, in, inlen);
91 memset(buf + inlen, 0, BLAKE2B_BLOCK_SIZE - inlen);
92 blake2b_set_lastblock(state);
93 compress(state, buf, 1, inlen);
94 for (i = 0; i < ARRAY_SIZE(state->h); i++)
95 __cpu_to_le64s(&state->h[i]);
96 memcpy(out, state->h, crypto_shash_digestsize(desc->tfm));
97 memzero_explicit(buf, sizeof(buf));
98 return 0;
99}
100
101#endif /* _CRYPTO_INTERNAL_BLAKE2B_H */