Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tpruvot
GitHub Repository: tpruvot/cpuminer-multi
Path: blob/linux/crypto/blake2b.c
1201 views
1
/*
2
* Copyright 2009 Colin Percival, 2014 savale
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
7
* are met:
8
* 1. Redistributions of source code must retain the above copyright
9
* notice, this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
13
*
14
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
* SUCH DAMAGE.
25
*
26
* This file was originally written by Colin Percival as part of the Tarsnap
27
* online backup system.
28
*/
29
30
#include <stdlib.h>
31
#include <stdint.h>
32
#include <string.h>
33
34
#include <sha3/sph_types.h>
35
#include "blake2b.h"
36
37
// Cyclic right rotation.
38
39
#ifndef ROTR64
40
#define ROTR64(x, y) (((x) >> (y)) ^ ((x) << (64 - (y))))
41
#endif
42
43
// Little-endian byte access.
44
45
#define B2B_GET64(p) \
46
(((uint64_t) ((uint8_t *) (p))[0]) ^ \
47
(((uint64_t) ((uint8_t *) (p))[1]) << 8) ^ \
48
(((uint64_t) ((uint8_t *) (p))[2]) << 16) ^ \
49
(((uint64_t) ((uint8_t *) (p))[3]) << 24) ^ \
50
(((uint64_t) ((uint8_t *) (p))[4]) << 32) ^ \
51
(((uint64_t) ((uint8_t *) (p))[5]) << 40) ^ \
52
(((uint64_t) ((uint8_t *) (p))[6]) << 48) ^ \
53
(((uint64_t) ((uint8_t *) (p))[7]) << 56))
54
55
// G Mixing function.
56
57
#define B2B_G(a, b, c, d, x, y) { \
58
v[a] = v[a] + v[b] + x; \
59
v[d] = ROTR64(v[d] ^ v[a], 32); \
60
v[c] = v[c] + v[d]; \
61
v[b] = ROTR64(v[b] ^ v[c], 24); \
62
v[a] = v[a] + v[b] + y; \
63
v[d] = ROTR64(v[d] ^ v[a], 16); \
64
v[c] = v[c] + v[d]; \
65
v[b] = ROTR64(v[b] ^ v[c], 63); }
66
67
// Initialization Vector.
68
69
static const uint64_t blake2b_iv[8] = {
70
0x6A09E667F3BCC908, 0xBB67AE8584CAA73B,
71
0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1,
72
0x510E527FADE682D1, 0x9B05688C2B3E6C1F,
73
0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179
74
};
75
76
// Compression function. "last" flag indicates last block.
77
78
static void blake2b_compress(blake2b_ctx *ctx, int last)
79
{
80
const uint8_t sigma[12][16] = {
81
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
82
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 },
83
{ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4 },
84
{ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8 },
85
{ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13 },
86
{ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9 },
87
{ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11 },
88
{ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10 },
89
{ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5 },
90
{ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0 },
91
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
92
{ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3 }
93
};
94
int i;
95
uint64_t v[16], m[16];
96
97
for (i = 0; i < 8; i++) { // init work variables
98
v[i] = ctx->h[i];
99
v[i + 8] = blake2b_iv[i];
100
}
101
102
v[12] ^= ctx->t[0]; // low 64 bits of offset
103
v[13] ^= ctx->t[1]; // high 64 bits
104
if (last) // last block flag set ?
105
v[14] = ~v[14];
106
107
for (i = 0; i < 16; i++) // get little-endian words
108
m[i] = B2B_GET64(&ctx->b[8 * i]);
109
110
for (i = 0; i < 12; i++) { // twelve rounds
111
B2B_G( 0, 4, 8, 12, m[sigma[i][ 0]], m[sigma[i][ 1]]);
112
B2B_G( 1, 5, 9, 13, m[sigma[i][ 2]], m[sigma[i][ 3]]);
113
B2B_G( 2, 6, 10, 14, m[sigma[i][ 4]], m[sigma[i][ 5]]);
114
B2B_G( 3, 7, 11, 15, m[sigma[i][ 6]], m[sigma[i][ 7]]);
115
B2B_G( 0, 5, 10, 15, m[sigma[i][ 8]], m[sigma[i][ 9]]);
116
B2B_G( 1, 6, 11, 12, m[sigma[i][10]], m[sigma[i][11]]);
117
B2B_G( 2, 7, 8, 13, m[sigma[i][12]], m[sigma[i][13]]);
118
B2B_G( 3, 4, 9, 14, m[sigma[i][14]], m[sigma[i][15]]);
119
}
120
121
for( i = 0; i < 8; ++i )
122
ctx->h[i] ^= v[i] ^ v[i + 8];
123
}
124
125
// Initialize the hashing context "ctx" with optional key "key".
126
// 1 <= outlen <= 64 gives the digest size in bytes.
127
// Secret key (also <= 64 bytes) is optional (keylen = 0).
128
129
int blake2b_init(blake2b_ctx *ctx, size_t outlen,
130
const void *key, size_t keylen) // (keylen=0: no key)
131
{
132
size_t i;
133
134
if (outlen == 0 || outlen > 64 || keylen > 64)
135
return -1; // illegal parameters
136
137
for (i = 0; i < 8; i++) // state, "param block"
138
ctx->h[i] = blake2b_iv[i];
139
ctx->h[0] ^= 0x01010000 ^ (keylen << 8) ^ outlen;
140
141
ctx->t[0] = 0; // input count low word
142
ctx->t[1] = 0; // input count high word
143
ctx->c = 0; // pointer within buffer
144
ctx->outlen = outlen;
145
146
for (i = keylen; i < 128; i++) // zero input block
147
ctx->b[i] = 0;
148
if (keylen > 0) {
149
blake2b_update(ctx, key, keylen);
150
ctx->c = 128; // at the end
151
}
152
153
return 0;
154
}
155
156
// Add "inlen" bytes from "in" into the hash.
157
158
void blake2b_update(blake2b_ctx *ctx,
159
const void *in, size_t inlen) // data bytes
160
{
161
size_t i;
162
163
for (i = 0; i < inlen; i++) {
164
if (ctx->c == 128) { // buffer full ?
165
ctx->t[0] += ctx->c; // add counters
166
if (ctx->t[0] < ctx->c) // carry overflow ?
167
ctx->t[1]++; // high word
168
blake2b_compress(ctx, 0); // compress (not last)
169
ctx->c = 0; // counter to zero
170
}
171
ctx->b[ctx->c++] = ((const uint8_t *) in)[i];
172
}
173
}
174
175
// Generate the message digest (size given in init).
176
// Result placed in "out".
177
178
void blake2b_final(blake2b_ctx *ctx, void *out)
179
{
180
size_t i;
181
182
ctx->t[0] += ctx->c; // mark last block offset
183
if (ctx->t[0] < ctx->c) // carry overflow
184
ctx->t[1]++; // high word
185
186
while (ctx->c < 128) // fill up with zeros
187
ctx->b[ctx->c++] = 0;
188
blake2b_compress(ctx, 1); // final block flag = 1
189
190
// little endian convert and store
191
for (i = 0; i < ctx->outlen; i++) {
192
((uint8_t *) out)[i] =
193
(ctx->h[i >> 3] >> (8 * (i & 7))) & 0xFF;
194
}
195
}
196
197
198