projects/linux·blob master · refs

165 lines · 4.6 KiB

1/* SPDX-License-Identifier: GPL-2.0 OR MIT */
2
3#ifndef _CRYPTO_BLAKE2B_H
4#define _CRYPTO_BLAKE2B_H
5
6#include <linux/bug.h>
7#include <linux/types.h>
8#include <linux/string.h>
9
10enum blake2b_lengths {
11 BLAKE2B_BLOCK_SIZE = 128,
12 BLAKE2B_HASH_SIZE = 64,
13 BLAKE2B_KEY_SIZE = 64,
14
15 BLAKE2B_160_HASH_SIZE = 20,
16 BLAKE2B_256_HASH_SIZE = 32,
17 BLAKE2B_384_HASH_SIZE = 48,
18 BLAKE2B_512_HASH_SIZE = 64,
19};
20
21/**
22 * struct blake2b_ctx - Context for hashing a message with BLAKE2b
23 * @h: compression function state
24 * @t: block counter
25 * @f: finalization indicator
26 * @buf: partial block buffer; 'buflen' bytes are valid
27 * @buflen: number of bytes buffered in @buf
28 * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
29 */
30struct blake2b_ctx {
31 /* 'h', 't', and 'f' are used in assembly code, so keep them as-is. */
32 u64 h[8];
33 u64 t[2];
34 u64 f[2];
35 u8 buf[BLAKE2B_BLOCK_SIZE];
36 unsigned int buflen;
37 unsigned int outlen;
38};
39
40/**
41 * blake2b_zeroize_ctx() - Zeroize a blake2b_ctx structure
42 * @ctx: The blake2b_ctx to zeroize
43 */
44static inline void blake2b_zeroize_ctx(struct blake2b_ctx *ctx)
45{
46 memzero_explicit(ctx, sizeof(*ctx));
47}
48
49enum blake2b_iv {
50 BLAKE2B_IV0 = 0x6A09E667F3BCC908ULL,
51 BLAKE2B_IV1 = 0xBB67AE8584CAA73BULL,
52 BLAKE2B_IV2 = 0x3C6EF372FE94F82BULL,
53 BLAKE2B_IV3 = 0xA54FF53A5F1D36F1ULL,
54 BLAKE2B_IV4 = 0x510E527FADE682D1ULL,
55 BLAKE2B_IV5 = 0x9B05688C2B3E6C1FULL,
56 BLAKE2B_IV6 = 0x1F83D9ABFB41BD6BULL,
57 BLAKE2B_IV7 = 0x5BE0CD19137E2179ULL,
58};
59
60static inline void __blake2b_init(struct blake2b_ctx *ctx, size_t outlen,
61 const void *key, size_t keylen)
62{
63 ctx->h[0] = BLAKE2B_IV0 ^ (0x01010000 | keylen << 8 | outlen);
64 ctx->h[1] = BLAKE2B_IV1;
65 ctx->h[2] = BLAKE2B_IV2;
66 ctx->h[3] = BLAKE2B_IV3;
67 ctx->h[4] = BLAKE2B_IV4;
68 ctx->h[5] = BLAKE2B_IV5;
69 ctx->h[6] = BLAKE2B_IV6;
70 ctx->h[7] = BLAKE2B_IV7;
71 ctx->t[0] = 0;
72 ctx->t[1] = 0;
73 ctx->f[0] = 0;
74 ctx->f[1] = 0;
75 ctx->buflen = 0;
76 ctx->outlen = outlen;
77 if (keylen) {
78 memcpy(ctx->buf, key, keylen);
79 memset(&ctx->buf[keylen], 0, BLAKE2B_BLOCK_SIZE - keylen);
80 ctx->buflen = BLAKE2B_BLOCK_SIZE;
81 }
82}
83
84/**
85 * blake2b_init() - Initialize a BLAKE2b context for a new message (unkeyed)
86 * @ctx: the context to initialize
87 * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
88 *
89 * Context: Any context.
90 */
91static inline void blake2b_init(struct blake2b_ctx *ctx, size_t outlen)
92{
93 __blake2b_init(ctx, outlen, NULL, 0);
94}
95
96/**
97 * blake2b_init_key() - Initialize a BLAKE2b context for a new message (keyed)
98 * @ctx: the context to initialize
99 * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
100 * @key: the key
101 * @keylen: the key length in bytes, at most BLAKE2B_KEY_SIZE
102 *
103 * Context: Any context.
104 */
105static inline void blake2b_init_key(struct blake2b_ctx *ctx, size_t outlen,
106 const void *key, size_t keylen)
107{
108 WARN_ON(IS_ENABLED(DEBUG) && (!outlen || outlen > BLAKE2B_HASH_SIZE ||
109 !key || !keylen || keylen > BLAKE2B_KEY_SIZE));
110
111 __blake2b_init(ctx, outlen, key, keylen);
112}
113
114/**
115 * blake2b_update() - Update a BLAKE2b context with message data
116 * @ctx: the context to update; must have been initialized
117 * @in: the message data
118 * @inlen: the data length in bytes
119 *
120 * This can be called any number of times.
121 *
122 * Context: Any context.
123 */
124void blake2b_update(struct blake2b_ctx *ctx, const u8 *in, size_t inlen);
125
126/**
127 * blake2b_final() - Finish computing a BLAKE2b hash
128 * @ctx: the context to finalize; must have been initialized
129 * @out: (output) the resulting BLAKE2b hash. Its length will be equal to the
130 * @outlen that was passed to blake2b_init() or blake2b_init_key().
131 *
132 * After finishing, this zeroizes @ctx. So the caller does not need to do it.
133 *
134 * Context: Any context.
135 */
136void blake2b_final(struct blake2b_ctx *ctx, u8 *out);
137
138/**
139 * blake2b() - Compute BLAKE2b hash in one shot
140 * @key: the key, or NULL for an unkeyed hash
141 * @keylen: the key length in bytes (at most BLAKE2B_KEY_SIZE), or 0 for an
142 * unkeyed hash
143 * @in: the message data
144 * @inlen: the data length in bytes
145 * @out: (output) the resulting BLAKE2b hash, with length @outlen
146 * @outlen: length of output hash value in bytes, at most BLAKE2B_HASH_SIZE
147 *
148 * Context: Any context.
149 */
150static inline void blake2b(const u8 *key, size_t keylen,
151 const u8 *in, size_t inlen,
152 u8 *out, size_t outlen)
153{
154 struct blake2b_ctx ctx;
155
156 WARN_ON(IS_ENABLED(DEBUG) && ((!in && inlen > 0) || !out || !outlen ||
157 outlen > BLAKE2B_HASH_SIZE || keylen > BLAKE2B_KEY_SIZE ||
158 (!key && keylen)));
159
160 __blake2b_init(&ctx, outlen, key, keylen);
161 blake2b_update(&ctx, in, inlen);
162 blake2b_final(&ctx, out);
163}
164
165#endif /* _CRYPTO_BLAKE2B_H */