1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/encode/sha256hash.h"
#include "gate/memalloc.h"
/* Public information: http://en.wikipedia.org/wiki/SHA-2 */
static unsigned long uint32_load(unsigned char const* buffer)
{
return (((unsigned long)buffer[0] & 0xff) << 24)
| (((unsigned long)buffer[1] & 0xff) << 16)
| (((unsigned long)buffer[2] & 0xff) << 8)
| (((unsigned long)buffer[3] & 0xff))
;
}
static void uint32_save(unsigned long value, unsigned char* buffer)
{
buffer[0] = (unsigned char)((value >> 24) & 0xff);
buffer[1] = (unsigned char)((value >> 16) & 0xff);
buffer[2] = (unsigned char)((value >> 8) & 0xff);
buffer[3] = (unsigned char)((value) & 0xff);
}
static unsigned long rotate_right32(unsigned long value, unsigned n)
{
return ((value >> n) & 0xffffffff) | ((value << (32 - n)) & 0xffffffff);
}
static unsigned long const k[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
static void gate_sha256_add(gate_sha256_t* context, unsigned char const* block)
{
unsigned n;
unsigned long s0;
unsigned long s1;
unsigned long ch;<--- The scope of the variable 'ch' can be reduced. [+]The scope of the variable 'ch' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned long temp1;<--- The scope of the variable 'temp1' can be reduced. [+]The scope of the variable 'temp1' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned long temp2;<--- The scope of the variable 'temp2' can be reduced. [+]The scope of the variable 'temp2' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned long maj;<--- The scope of the variable 'maj' can be reduced. [+]The scope of the variable 'maj' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned long a = context->h0;
unsigned long b = context->h1;
unsigned long c = context->h2;
unsigned long d = context->h3;
unsigned long e = context->h4;
unsigned long f = context->h5;
unsigned long g = context->h6;
unsigned long h = context->h7;
unsigned long w[64];
for (n = 0; n != 16; ++n)
{
w[n] = uint32_load(&block[n * 4]);
}
for (n = 16; n < 64; ++n)
{
s0 = rotate_right32(w[n - 15], 7) ^ rotate_right32(w[n - 15], 18) ^ (w[n - 15] >> 3);
s1 = rotate_right32(w[n - 2], 17) ^ rotate_right32(w[n - 2], 19) ^ (w[n - 2] >> 10);
w[n] = (w[n - 16] + s0 + w[n - 7] + s1) & 0xffffffff;
}
for (n = 0; n < 64; ++n)
{
s1 = rotate_right32(e, 6) ^ rotate_right32(e, 11) ^ rotate_right32(e, 25);
ch = (e & f) ^ ((~e) & g);
temp1 = (h + s1 + ch + k[n] + w[n]) & 0xffffffff;
s0 = rotate_right32(a, 2) ^ rotate_right32(a, 13) ^ rotate_right32(a, 22);
maj = (a & b) ^ (a & c) ^ (b & c);
temp2 = (s0 + maj) & 0xffffffff;
h = g;
g = f;
f = e;
e = (d + temp1) & 0xffffffff;
d = c;
c = b;
b = a;
a = (temp1 + temp2) & 0xffffffff;
}
context->h0 = (context->h0 + a) & 0xffffffff;
context->h1 = (context->h1 + b) & 0xffffffff;
context->h2 = (context->h2 + c) & 0xffffffff;
context->h3 = (context->h3 + d) & 0xffffffff;
context->h4 = (context->h4 + e) & 0xffffffff;
context->h5 = (context->h5 + f) & 0xffffffff;
context->h6 = (context->h6 + g) & 0xffffffff;
context->h7 = (context->h7 + h) & 0xffffffff;
}
void gate_sha256_init(gate_sha256_t* context)
{
context->h0 = 0x6a09e667;
context->h1 = 0xbb67ae85;
context->h2 = 0x3c6ef372;
context->h3 = 0xa54ff53a;
context->h4 = 0x510e527f;
context->h5 = 0x9b05688c;
context->h6 = 0x1f83d9ab;
context->h7 = 0x5be0cd19;
context->bufferused = 0;
context->msglength = 0;
}
void gate_sha256_update(gate_sha256_t* context, void const* buffer, gate_size_t length)
{
unsigned char const* ptr = (unsigned char const*)buffer;
context->msglength += length;
if (context->bufferused > 0)
{
gate_size_t freebytes = GATE_SHA256_BLOCK_LENGTH - context->bufferused;
if (length < freebytes)
{
gate_mem_copy(&context->buffer[context->bufferused], ptr, length);
context->bufferused += length;
return;
}
else
{
gate_mem_copy(&context->buffer[context->bufferused], ptr, freebytes);
gate_sha256_add(context, &context->buffer[0]);
context->bufferused = 0;
ptr += freebytes;
length -= freebytes;
}
}
while (length >= GATE_SHA256_BLOCK_LENGTH)
{
gate_sha256_add(context, ptr);
ptr += GATE_SHA256_BLOCK_LENGTH;
length -= GATE_SHA256_BLOCK_LENGTH;
}
if (length > 0)
{
gate_mem_copy(context->buffer, ptr, length);
context->bufferused = length;
}
}
void gate_sha256_finish(gate_sha256_t* context, gate_sha256_result_t* result)
{
gate_uint64_t bitlen = context->msglength * 8;
unsigned long bitlen_high = (unsigned long)(bitlen >> 32) & 0xffffffff;
unsigned long bitlen_low = (unsigned long)(bitlen & 0xffffffff);
gate_size_t freebytes = (GATE_SHA256_BLOCK_LENGTH - context->bufferused - 1);
context->buffer[context->bufferused++] = 0x80;
if (freebytes < 8)
{
gate_mem_clear(&context->buffer[context->bufferused], freebytes);
gate_sha256_add(context, &context->buffer[0]);
freebytes = GATE_SHA256_BLOCK_LENGTH;
context->bufferused = 0;
}
gate_mem_clear(&context->buffer[context->bufferused], GATE_SHA256_BLOCK_LENGTH - 8 - context->bufferused);
uint32_save(bitlen_high, &context->buffer[GATE_SHA256_BLOCK_LENGTH - 8]);
uint32_save(bitlen_low, &context->buffer[GATE_SHA256_BLOCK_LENGTH - 4]);
gate_sha256_add(context, context->buffer);
uint32_save(context->h0, &result->hash[0]);
uint32_save(context->h1, &result->hash[4]);
uint32_save(context->h2, &result->hash[8]);
uint32_save(context->h3, &result->hash[12]);
uint32_save(context->h4, &result->hash[16]);
uint32_save(context->h5, &result->hash[20]);
uint32_save(context->h6, &result->hash[24]);
uint32_save(context->h7, &result->hash[28]);
}
|