xref: /linux/lib/crypto/blake2b.c (revision 7b8e9264f55a9c320f398e337d215e68cca50131)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright (C) 2015-2019 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  * Copyright 2025 Google LLC
5  *
6  * This is an implementation of the BLAKE2b hash and PRF functions.
7  *
8  * Information: https://blake2.net/
9  */
10 
11 #include <crypto/blake2b.h>
12 #include <linux/bug.h>
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/string.h>
17 #include <linux/unroll.h>
18 #include <linux/types.h>
19 
20 static const u8 blake2b_sigma[12][16] = {
21 	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
22 	{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
23 	{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
24 	{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
25 	{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
26 	{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
27 	{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
28 	{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
29 	{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
30 	{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
31 	{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
32 	{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
33 };
34 
35 static inline void blake2b_increment_counter(struct blake2b_ctx *ctx, u32 inc)
36 {
37 	ctx->t[0] += inc;
38 	ctx->t[1] += (ctx->t[0] < inc);
39 }
40 
41 static void __maybe_unused
42 blake2b_compress_generic(struct blake2b_ctx *ctx,
43 			 const u8 *data, size_t nblocks, u32 inc)
44 {
45 	u64 m[16];
46 	u64 v[16];
47 	int i;
48 
49 	WARN_ON(IS_ENABLED(DEBUG) &&
50 		(nblocks > 1 && inc != BLAKE2B_BLOCK_SIZE));
51 
52 	while (nblocks > 0) {
53 		blake2b_increment_counter(ctx, inc);
54 		memcpy(m, data, BLAKE2B_BLOCK_SIZE);
55 		le64_to_cpu_array(m, ARRAY_SIZE(m));
56 		memcpy(v, ctx->h, 64);
57 		v[ 8] = BLAKE2B_IV0;
58 		v[ 9] = BLAKE2B_IV1;
59 		v[10] = BLAKE2B_IV2;
60 		v[11] = BLAKE2B_IV3;
61 		v[12] = BLAKE2B_IV4 ^ ctx->t[0];
62 		v[13] = BLAKE2B_IV5 ^ ctx->t[1];
63 		v[14] = BLAKE2B_IV6 ^ ctx->f[0];
64 		v[15] = BLAKE2B_IV7 ^ ctx->f[1];
65 
66 #define G(r, i, a, b, c, d) do { \
67 	a += b + m[blake2b_sigma[r][2 * i + 0]]; \
68 	d = ror64(d ^ a, 32); \
69 	c += d; \
70 	b = ror64(b ^ c, 24); \
71 	a += b + m[blake2b_sigma[r][2 * i + 1]]; \
72 	d = ror64(d ^ a, 16); \
73 	c += d; \
74 	b = ror64(b ^ c, 63); \
75 } while (0)
76 
77 #ifdef CONFIG_64BIT
78 		/*
79 		 * Unroll the rounds loop to enable constant-folding of the
80 		 * blake2b_sigma values.  Seems worthwhile on 64-bit kernels.
81 		 * Not worthwhile on 32-bit kernels because the code size is
82 		 * already so large there due to BLAKE2b using 64-bit words.
83 		 */
84 		unrolled_full
85 #endif
86 		for (int r = 0; r < 12; r++) {
87 			G(r, 0, v[0], v[4], v[8], v[12]);
88 			G(r, 1, v[1], v[5], v[9], v[13]);
89 			G(r, 2, v[2], v[6], v[10], v[14]);
90 			G(r, 3, v[3], v[7], v[11], v[15]);
91 			G(r, 4, v[0], v[5], v[10], v[15]);
92 			G(r, 5, v[1], v[6], v[11], v[12]);
93 			G(r, 6, v[2], v[7], v[8], v[13]);
94 			G(r, 7, v[3], v[4], v[9], v[14]);
95 		}
96 #undef G
97 
98 		for (i = 0; i < 8; ++i)
99 			ctx->h[i] ^= v[i] ^ v[i + 8];
100 
101 		data += BLAKE2B_BLOCK_SIZE;
102 		--nblocks;
103 	}
104 }
105 
106 #ifdef CONFIG_CRYPTO_LIB_BLAKE2B_ARCH
107 #include "blake2b.h" /* $(SRCARCH)/blake2b.h */
108 #else
109 #define blake2b_compress blake2b_compress_generic
110 #endif
111 
112 static inline void blake2b_set_lastblock(struct blake2b_ctx *ctx)
113 {
114 	ctx->f[0] = -1;
115 }
116 
117 void blake2b_update(struct blake2b_ctx *ctx, const u8 *in, size_t inlen)
118 {
119 	const size_t fill = BLAKE2B_BLOCK_SIZE - ctx->buflen;
120 
121 	if (unlikely(!inlen))
122 		return;
123 	if (inlen > fill) {
124 		memcpy(ctx->buf + ctx->buflen, in, fill);
125 		blake2b_compress(ctx, ctx->buf, 1, BLAKE2B_BLOCK_SIZE);
126 		ctx->buflen = 0;
127 		in += fill;
128 		inlen -= fill;
129 	}
130 	if (inlen > BLAKE2B_BLOCK_SIZE) {
131 		const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2B_BLOCK_SIZE);
132 
133 		blake2b_compress(ctx, in, nblocks - 1, BLAKE2B_BLOCK_SIZE);
134 		in += BLAKE2B_BLOCK_SIZE * (nblocks - 1);
135 		inlen -= BLAKE2B_BLOCK_SIZE * (nblocks - 1);
136 	}
137 	memcpy(ctx->buf + ctx->buflen, in, inlen);
138 	ctx->buflen += inlen;
139 }
140 EXPORT_SYMBOL(blake2b_update);
141 
142 void blake2b_final(struct blake2b_ctx *ctx, u8 *out)
143 {
144 	WARN_ON(IS_ENABLED(DEBUG) && !out);
145 	blake2b_set_lastblock(ctx);
146 	memset(ctx->buf + ctx->buflen, 0,
147 	       BLAKE2B_BLOCK_SIZE - ctx->buflen); /* Padding */
148 	blake2b_compress(ctx, ctx->buf, 1, ctx->buflen);
149 	cpu_to_le64_array(ctx->h, ARRAY_SIZE(ctx->h));
150 	memcpy(out, ctx->h, ctx->outlen);
151 	memzero_explicit(ctx, sizeof(*ctx));
152 }
153 EXPORT_SYMBOL(blake2b_final);
154 
155 #ifdef blake2b_mod_init_arch
156 static int __init blake2b_mod_init(void)
157 {
158 	blake2b_mod_init_arch();
159 	return 0;
160 }
161 subsys_initcall(blake2b_mod_init);
162 
163 static void __exit blake2b_mod_exit(void)
164 {
165 }
166 module_exit(blake2b_mod_exit);
167 #endif
168 
169 MODULE_DESCRIPTION("BLAKE2b hash function");
170 MODULE_LICENSE("GPL");
171