line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* blake2s.c - an implementation of blake2s hash function. |
2
|
|
|
|
|
|
|
* |
3
|
|
|
|
|
|
|
* Copyright (c) 2012, Samuel Neves |
4
|
|
|
|
|
|
|
* Copyright (c) 2021, Aleksey Kravchenko |
5
|
|
|
|
|
|
|
* |
6
|
|
|
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any |
7
|
|
|
|
|
|
|
* purpose with or without fee is hereby granted. |
8
|
|
|
|
|
|
|
* |
9
|
|
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
10
|
|
|
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
11
|
|
|
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
12
|
|
|
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
13
|
|
|
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE |
14
|
|
|
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
15
|
|
|
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE. |
16
|
|
|
|
|
|
|
*/ |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
#include "blake2s.h" |
19
|
|
|
|
|
|
|
#include "byte_order.h" |
20
|
|
|
|
|
|
|
#include |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
static const uint32_t blake2s_IV[8] = |
23
|
|
|
|
|
|
|
{ |
24
|
|
|
|
|
|
|
0x6A09E667UL, 0xBB67AE85UL, 0x3C6EF372UL, 0xA54FF53AUL, |
25
|
|
|
|
|
|
|
0x510E527FUL, 0x9B05688CUL, 0x1F83D9ABUL, 0x5BE0CD19UL |
26
|
|
|
|
|
|
|
}; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
static const uint8_t blake2s_sigma[10][16] = |
29
|
|
|
|
|
|
|
{ |
30
|
|
|
|
|
|
|
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, |
31
|
|
|
|
|
|
|
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }, |
32
|
|
|
|
|
|
|
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 }, |
33
|
|
|
|
|
|
|
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 }, |
34
|
|
|
|
|
|
|
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 }, |
35
|
|
|
|
|
|
|
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 }, |
36
|
|
|
|
|
|
|
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 }, |
37
|
|
|
|
|
|
|
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 }, |
38
|
|
|
|
|
|
|
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 }, |
39
|
|
|
|
|
|
|
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 }, |
40
|
|
|
|
|
|
|
}; |
41
|
|
|
|
|
|
|
|
42
|
2
|
|
|
|
|
|
void rhash_blake2s_init(blake2s_ctx* ctx) |
43
|
|
|
|
|
|
|
{ |
44
|
2
|
|
|
|
|
|
memset(ctx, 0, sizeof(*ctx)); |
45
|
|
|
|
|
|
|
/* init state by xoring IV with blake2s input parameter block */ |
46
|
2
|
|
|
|
|
|
ctx->hash[0] = blake2s_IV[0] ^ 0x01010020; |
47
|
2
|
|
|
|
|
|
ctx->hash[1] = blake2s_IV[1]; |
48
|
2
|
|
|
|
|
|
ctx->hash[2] = blake2s_IV[2]; |
49
|
2
|
|
|
|
|
|
ctx->hash[3] = blake2s_IV[3]; |
50
|
2
|
|
|
|
|
|
ctx->hash[4] = blake2s_IV[4]; |
51
|
2
|
|
|
|
|
|
ctx->hash[5] = blake2s_IV[5]; |
52
|
2
|
|
|
|
|
|
ctx->hash[6] = blake2s_IV[6]; |
53
|
2
|
|
|
|
|
|
ctx->hash[7] = blake2s_IV[7]; |
54
|
2
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
#define G(r,i,a,b,c,d) \ |
57
|
|
|
|
|
|
|
do { \ |
58
|
|
|
|
|
|
|
a = a + b + m[blake2s_sigma[r][2*i+0]]; \ |
59
|
|
|
|
|
|
|
d = ROTR32(d ^ a, 16); \ |
60
|
|
|
|
|
|
|
c = c + d; \ |
61
|
|
|
|
|
|
|
b = ROTR32(b ^ c, 12); \ |
62
|
|
|
|
|
|
|
a = a + b + m[blake2s_sigma[r][2*i+1]]; \ |
63
|
|
|
|
|
|
|
d = ROTR32(d ^ a, 8); \ |
64
|
|
|
|
|
|
|
c = c + d; \ |
65
|
|
|
|
|
|
|
b = ROTR32(b ^ c, 7); \ |
66
|
|
|
|
|
|
|
} while(0) |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
#define ROUND(r) \ |
69
|
|
|
|
|
|
|
do { \ |
70
|
|
|
|
|
|
|
G(r,0,v[0],v[4],v[ 8],v[12]); \ |
71
|
|
|
|
|
|
|
G(r,1,v[1],v[5],v[ 9],v[13]); \ |
72
|
|
|
|
|
|
|
G(r,2,v[2],v[6],v[10],v[14]); \ |
73
|
|
|
|
|
|
|
G(r,3,v[3],v[7],v[11],v[15]); \ |
74
|
|
|
|
|
|
|
G(r,4,v[0],v[5],v[10],v[15]); \ |
75
|
|
|
|
|
|
|
G(r,5,v[1],v[6],v[11],v[12]); \ |
76
|
|
|
|
|
|
|
G(r,6,v[2],v[7],v[ 8],v[13]); \ |
77
|
|
|
|
|
|
|
G(r,7,v[3],v[4],v[ 9],v[14]); \ |
78
|
|
|
|
|
|
|
} while(0) |
79
|
|
|
|
|
|
|
|
80
|
2
|
|
|
|
|
|
static void rhash_blake2s_process_block(blake2s_ctx* ctx, const uint32_t* m, uint32_t finalization_flag) |
81
|
|
|
|
|
|
|
{ |
82
|
|
|
|
|
|
|
uint32_t v[16]; |
83
|
|
|
|
|
|
|
size_t i; |
84
|
|
|
|
|
|
|
|
85
|
2
|
|
|
|
|
|
memcpy(v, ctx->hash, sizeof(uint32_t) * 8); |
86
|
2
|
|
|
|
|
|
v[ 8] = blake2s_IV[0]; |
87
|
2
|
|
|
|
|
|
v[ 9] = blake2s_IV[1]; |
88
|
2
|
|
|
|
|
|
v[10] = blake2s_IV[2]; |
89
|
2
|
|
|
|
|
|
v[11] = blake2s_IV[3]; |
90
|
2
|
|
|
|
|
|
v[12] = blake2s_IV[4] ^ (uint32_t)ctx->length; |
91
|
2
|
|
|
|
|
|
v[13] = blake2s_IV[5] ^ (uint32_t)(ctx->length >> 32); |
92
|
2
|
|
|
|
|
|
v[14] = blake2s_IV[6] ^ finalization_flag; |
93
|
2
|
|
|
|
|
|
v[15] = blake2s_IV[7]; |
94
|
|
|
|
|
|
|
|
95
|
2
|
|
|
|
|
|
ROUND(0); |
96
|
2
|
|
|
|
|
|
ROUND(1); |
97
|
2
|
|
|
|
|
|
ROUND(2); |
98
|
2
|
|
|
|
|
|
ROUND(3); |
99
|
2
|
|
|
|
|
|
ROUND(4); |
100
|
2
|
|
|
|
|
|
ROUND(5); |
101
|
2
|
|
|
|
|
|
ROUND(6); |
102
|
2
|
|
|
|
|
|
ROUND(7); |
103
|
2
|
|
|
|
|
|
ROUND(8); |
104
|
2
|
|
|
|
|
|
ROUND(9); |
105
|
|
|
|
|
|
|
|
106
|
18
|
100
|
|
|
|
|
for(i = 0; i < 8; ++i) |
107
|
16
|
|
|
|
|
|
ctx->hash[i] ^= v[i] ^ v[i + 8]; |
108
|
2
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
2
|
|
|
|
|
|
void rhash_blake2s_update(blake2s_ctx* ctx, const unsigned char* msg, size_t size) |
111
|
|
|
|
|
|
|
{ |
112
|
2
|
50
|
|
|
|
|
if(size > 0) |
113
|
|
|
|
|
|
|
{ |
114
|
2
|
|
|
|
|
|
size_t index = (size_t)ctx->length & 63; |
115
|
2
|
50
|
|
|
|
|
if(index) |
116
|
|
|
|
|
|
|
{ |
117
|
0
|
|
|
|
|
|
size_t rest = blake2s_block_size - index; |
118
|
0
|
0
|
|
|
|
|
if (size > rest) { |
119
|
0
|
|
|
|
|
|
le32_copy(ctx->message, index, msg, rest); /* fill the block */ |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
/* process the block */ |
122
|
0
|
|
|
|
|
|
size -= rest; |
123
|
0
|
|
|
|
|
|
msg += rest; |
124
|
0
|
|
|
|
|
|
ctx->length += rest; |
125
|
0
|
|
|
|
|
|
index = 0; |
126
|
0
|
|
|
|
|
|
rhash_blake2s_process_block(ctx, ctx->message, 0); |
127
|
|
|
|
|
|
|
} |
128
|
2
|
50
|
|
|
|
|
} else if (ctx->length) { |
129
|
0
|
|
|
|
|
|
rhash_blake2s_process_block(ctx, ctx->message, 0); |
130
|
|
|
|
|
|
|
} |
131
|
2
|
50
|
|
|
|
|
while(size > blake2s_block_size) { |
132
|
|
|
|
|
|
|
uint32_t* aligned_message_block; |
133
|
0
|
0
|
|
|
|
|
if (IS_LITTLE_ENDIAN && IS_ALIGNED_32(msg)) { |
134
|
0
|
|
|
|
|
|
aligned_message_block = (uint32_t*)msg; |
135
|
|
|
|
|
|
|
} else { |
136
|
0
|
|
|
|
|
|
le32_copy(ctx->message, 0, msg, blake2s_block_size); |
137
|
0
|
|
|
|
|
|
aligned_message_block = ctx->message; |
138
|
|
|
|
|
|
|
} |
139
|
0
|
|
|
|
|
|
size -= blake2s_block_size; |
140
|
0
|
|
|
|
|
|
msg += blake2s_block_size; |
141
|
0
|
|
|
|
|
|
ctx->length += blake2s_block_size; |
142
|
0
|
|
|
|
|
|
rhash_blake2s_process_block(ctx, aligned_message_block, 0); |
143
|
|
|
|
|
|
|
} |
144
|
2
|
|
|
|
|
|
le32_copy(ctx->message, index, msg, size); /* save leftovers */ |
145
|
2
|
|
|
|
|
|
ctx->length += size; |
146
|
|
|
|
|
|
|
} |
147
|
2
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
2
|
|
|
|
|
|
void rhash_blake2s_final(blake2s_ctx* ctx, unsigned char *result) |
150
|
|
|
|
|
|
|
{ |
151
|
2
|
|
|
|
|
|
size_t length = (size_t)ctx->length & 63; |
152
|
2
|
50
|
|
|
|
|
if (length) |
153
|
|
|
|
|
|
|
{ |
154
|
|
|
|
|
|
|
/* pad the message with zeros */ |
155
|
2
|
|
|
|
|
|
size_t index = length >> 2; |
156
|
2
|
|
|
|
|
|
unsigned shift = (unsigned)(length & 3) * 8; |
157
|
2
|
|
|
|
|
|
ctx->message[index] &= ~(0xFFFFFFFFu << shift); |
158
|
32
|
100
|
|
|
|
|
for(index++; index < 16; index++) |
159
|
30
|
|
|
|
|
|
ctx->message[index] = 0; |
160
|
|
|
|
|
|
|
} |
161
|
2
|
|
|
|
|
|
rhash_blake2s_process_block(ctx, ctx->message, 0xFFFFFFFFu); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
/* convert hash state to result bytes */ |
164
|
2
|
|
|
|
|
|
le32_copy(result, 0, ctx->hash, blake2s_hash_size); |
165
|
2
|
|
|
|
|
|
} |