xref: /linux/tools/perf/util/blake2s.c (revision c7decec2f2d2ab0366567f9e30c0e1418cece43f)
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  *
5  * This is an implementation of the BLAKE2s hash and PRF functions.
6  *
7  * Information: https://blake2.net/
8  */
9 
10 #include "blake2s.h"
11 #include <linux/kernel.h>
12 
ror32(u32 v,int n)13 static inline u32 ror32(u32 v, int n)
14 {
15 	return (v >> n) | (v << (32 - n));
16 }
17 
le32_to_cpu_array(u32 a[],size_t n)18 static inline void le32_to_cpu_array(u32 a[], size_t n)
19 {
20 	for (size_t i = 0; i < n; i++)
21 		a[i] = le32_to_cpu((__force __le32)a[i]);
22 }
23 
cpu_to_le32_array(u32 a[],size_t n)24 static inline void cpu_to_le32_array(u32 a[], size_t n)
25 {
26 	for (size_t i = 0; i < n; i++)
27 		a[i] = (__force u32)cpu_to_le32(a[i]);
28 }
29 
30 static const u8 blake2s_sigma[10][16] = {
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 	{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
34 	{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
35 	{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
36 	{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
37 	{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
38 	{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
39 	{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
40 	{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
41 };
42 
blake2s_increment_counter(struct blake2s_ctx * ctx,u32 inc)43 static inline void blake2s_increment_counter(struct blake2s_ctx *ctx, u32 inc)
44 {
45 	ctx->t[0] += inc;
46 	ctx->t[1] += (ctx->t[0] < inc);
47 }
48 
blake2s_compress(struct blake2s_ctx * ctx,const u8 * data,size_t nblocks,u32 inc)49 static void blake2s_compress(struct blake2s_ctx *ctx,
50 			     const u8 *data, size_t nblocks, u32 inc)
51 {
52 	u32 m[16];
53 	u32 v[16];
54 	int i;
55 
56 	while (nblocks > 0) {
57 		blake2s_increment_counter(ctx, inc);
58 		memcpy(m, data, BLAKE2S_BLOCK_SIZE);
59 		le32_to_cpu_array(m, ARRAY_SIZE(m));
60 		memcpy(v, ctx->h, 32);
61 		v[ 8] = BLAKE2S_IV0;
62 		v[ 9] = BLAKE2S_IV1;
63 		v[10] = BLAKE2S_IV2;
64 		v[11] = BLAKE2S_IV3;
65 		v[12] = BLAKE2S_IV4 ^ ctx->t[0];
66 		v[13] = BLAKE2S_IV5 ^ ctx->t[1];
67 		v[14] = BLAKE2S_IV6 ^ ctx->f[0];
68 		v[15] = BLAKE2S_IV7 ^ ctx->f[1];
69 
70 #define G(r, i, a, b, c, d) do { \
71 	a += b + m[blake2s_sigma[r][2 * i + 0]]; \
72 	d = ror32(d ^ a, 16); \
73 	c += d; \
74 	b = ror32(b ^ c, 12); \
75 	a += b + m[blake2s_sigma[r][2 * i + 1]]; \
76 	d = ror32(d ^ a, 8); \
77 	c += d; \
78 	b = ror32(b ^ c, 7); \
79 } while (0)
80 
81 #define ROUND(r) do { \
82 	G(r, 0, v[0], v[ 4], v[ 8], v[12]); \
83 	G(r, 1, v[1], v[ 5], v[ 9], v[13]); \
84 	G(r, 2, v[2], v[ 6], v[10], v[14]); \
85 	G(r, 3, v[3], v[ 7], v[11], v[15]); \
86 	G(r, 4, v[0], v[ 5], v[10], v[15]); \
87 	G(r, 5, v[1], v[ 6], v[11], v[12]); \
88 	G(r, 6, v[2], v[ 7], v[ 8], v[13]); \
89 	G(r, 7, v[3], v[ 4], v[ 9], v[14]); \
90 } while (0)
91 		ROUND(0);
92 		ROUND(1);
93 		ROUND(2);
94 		ROUND(3);
95 		ROUND(4);
96 		ROUND(5);
97 		ROUND(6);
98 		ROUND(7);
99 		ROUND(8);
100 		ROUND(9);
101 
102 #undef G
103 #undef ROUND
104 
105 		for (i = 0; i < 8; ++i)
106 			ctx->h[i] ^= v[i] ^ v[i + 8];
107 
108 		data += BLAKE2S_BLOCK_SIZE;
109 		--nblocks;
110 	}
111 }
112 
blake2s_set_lastblock(struct blake2s_ctx * ctx)113 static inline void blake2s_set_lastblock(struct blake2s_ctx *ctx)
114 {
115 	ctx->f[0] = -1;
116 }
117 
blake2s_update(struct blake2s_ctx * ctx,const u8 * in,size_t inlen)118 void blake2s_update(struct blake2s_ctx *ctx, const u8 *in, size_t inlen)
119 {
120 	const size_t fill = BLAKE2S_BLOCK_SIZE - ctx->buflen;
121 
122 	if (unlikely(!inlen))
123 		return;
124 	if (inlen > fill) {
125 		memcpy(ctx->buf + ctx->buflen, in, fill);
126 		blake2s_compress(ctx, ctx->buf, 1, BLAKE2S_BLOCK_SIZE);
127 		ctx->buflen = 0;
128 		in += fill;
129 		inlen -= fill;
130 	}
131 	if (inlen > BLAKE2S_BLOCK_SIZE) {
132 		const size_t nblocks = DIV_ROUND_UP(inlen, BLAKE2S_BLOCK_SIZE);
133 
134 		blake2s_compress(ctx, in, nblocks - 1, BLAKE2S_BLOCK_SIZE);
135 		in += BLAKE2S_BLOCK_SIZE * (nblocks - 1);
136 		inlen -= BLAKE2S_BLOCK_SIZE * (nblocks - 1);
137 	}
138 	memcpy(ctx->buf + ctx->buflen, in, inlen);
139 	ctx->buflen += inlen;
140 }
141 
blake2s_final(struct blake2s_ctx * ctx,u8 * out)142 void blake2s_final(struct blake2s_ctx *ctx, u8 *out)
143 {
144 	blake2s_set_lastblock(ctx);
145 	memset(ctx->buf + ctx->buflen, 0,
146 	       BLAKE2S_BLOCK_SIZE - ctx->buflen); /* Padding */
147 	blake2s_compress(ctx, ctx->buf, 1, ctx->buflen);
148 	cpu_to_le32_array(ctx->h, ARRAY_SIZE(ctx->h));
149 	memcpy(out, ctx->h, ctx->outlen);
150 	memset(ctx, 0, sizeof(*ctx));
151 }
152