Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
revoxhere
GitHub Repository: revoxhere/duino-coin
Path: blob/master/Unofficial miners/STM32/src/sha1.h
922 views
1
/**
2
* @file sha1.c
3
* @date 09.09.2017
4
* @author Steve Reid <[email protected]>
5
*
6
* from: http://www.virtualbox.org/svn/vbox/trunk/src/recompiler/tests/sha1.c
7
*/
8
9
/* from valgrind tests */
10
11
/* ================ sha1.c ================ */
12
/*
13
SHA-1 in C
14
By Steve Reid <[email protected]>
15
Maintained by Charlie Wade
16
100% Public Domain
17
18
Test Vectors (from FIPS PUB 180-1)
19
"abc"
20
A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
21
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
22
84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
23
A million repetitions of "a"
24
34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
25
*/
26
27
/* #define LITTLE_ENDIAN * This should be #define'd already, if true. */
28
/* #define SHA1HANDSOFF * Copies data before messing with it. */
29
30
#include <Arduino.h>
31
32
typedef struct
33
{
34
uint32_t state[5];
35
uint32_t count[2];
36
unsigned char buffer[64];
37
} SHA1_CTX;
38
39
SHA1_CTX SHA1Copy(SHA1_CTX ctx)
40
{
41
SHA1_CTX temp;
42
memcpy(&temp, &ctx, sizeof(ctx));
43
return temp;
44
}
45
46
#define SHA1HANDSOFF
47
48
//#include <stdio.h>
49
//#include <string.h>
50
//#include <stdint.h>
51
52
//#include <endian.h>
53
54
#define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
55
56
/* blk0() and blk() perform the initial expand. */
57
/* I got the idea of expanding during the round function from SSLeay */
58
#if BYTE_ORDER == LITTLE_ENDIAN
59
#define blk0(i) (block->l[i] = (rol(block->l[i], 24) & 0xFF00FF00) | (rol(block->l[i], 8) & 0x00FF00FF))
60
#elif BYTE_ORDER == BIG_ENDIAN
61
#define blk0(i) block->l[i]
62
#else
63
#error "Endianness not defined!"
64
#endif
65
#define blk(i) (block->l[i & 15] = rol(block->l[(i + 13) & 15] ^ block->l[(i + 8) & 15] ^ block->l[(i + 2) & 15] ^ block->l[i & 15], 1))
66
67
/* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
68
#define R0(v, w, x, y, z, i) \
69
z += ((w & (x ^ y)) ^ y) + blk0(i) + 0x5A827999 + rol(v, 5); \
70
w = rol(w, 30);
71
#define R1(v, w, x, y, z, i) \
72
z += ((w & (x ^ y)) ^ y) + blk(i) + 0x5A827999 + rol(v, 5); \
73
w = rol(w, 30);
74
#define R2(v, w, x, y, z, i) \
75
z += (w ^ x ^ y) + blk(i) + 0x6ED9EBA1 + rol(v, 5); \
76
w = rol(w, 30);
77
#define R3(v, w, x, y, z, i) \
78
z += (((w | x) & y) | (w & x)) + blk(i) + 0x8F1BBCDC + rol(v, 5); \
79
w = rol(w, 30);
80
#define R4(v, w, x, y, z, i) \
81
z += (w ^ x ^ y) + blk(i) + 0xCA62C1D6 + rol(v, 5); \
82
w = rol(w, 30);
83
84
/* Hash a single 512-bit block. This is the core of the algorithm. */
85
86
void SHA1Transform(uint32_t state[5], uint8_t buffer[64])
87
{
88
uint32_t a, b, c, d, e;
89
typedef union
90
{
91
unsigned char c[64];
92
uint32_t l[16];
93
} CHAR64LONG16;
94
#ifdef SHA1HANDSOFF
95
CHAR64LONG16 block[1]; /* use array to appear as a pointer */
96
memcpy(block, buffer, 64);
97
#else
98
/* The following had better never be used because it causes the
99
* pointer-to-const buffer to be cast into a pointer to non-const.
100
* And the result is written through. I threw a "const" in, hoping
101
* this will cause a diagnostic.
102
*/
103
CHAR64LONG16 *block = (const CHAR64LONG16 *)buffer;
104
#endif
105
/* Copy context->state[] to working vars */
106
a = state[0];
107
b = state[1];
108
c = state[2];
109
d = state[3];
110
e = state[4];
111
/* 4 rounds of 20 operations each. Loop unrolled. */
112
R0(a, b, c, d, e, 0);
113
R0(e, a, b, c, d, 1);
114
R0(d, e, a, b, c, 2);
115
R0(c, d, e, a, b, 3);
116
R0(b, c, d, e, a, 4);
117
R0(a, b, c, d, e, 5);
118
R0(e, a, b, c, d, 6);
119
R0(d, e, a, b, c, 7);
120
R0(c, d, e, a, b, 8);
121
R0(b, c, d, e, a, 9);
122
R0(a, b, c, d, e, 10);
123
R0(e, a, b, c, d, 11);
124
R0(d, e, a, b, c, 12);
125
R0(c, d, e, a, b, 13);
126
R0(b, c, d, e, a, 14);
127
R0(a, b, c, d, e, 15);
128
R1(e, a, b, c, d, 16);
129
R1(d, e, a, b, c, 17);
130
R1(c, d, e, a, b, 18);
131
R1(b, c, d, e, a, 19);
132
R2(a, b, c, d, e, 20);
133
R2(e, a, b, c, d, 21);
134
R2(d, e, a, b, c, 22);
135
R2(c, d, e, a, b, 23);
136
R2(b, c, d, e, a, 24);
137
R2(a, b, c, d, e, 25);
138
R2(e, a, b, c, d, 26);
139
R2(d, e, a, b, c, 27);
140
R2(c, d, e, a, b, 28);
141
R2(b, c, d, e, a, 29);
142
R2(a, b, c, d, e, 30);
143
R2(e, a, b, c, d, 31);
144
R2(d, e, a, b, c, 32);
145
R2(c, d, e, a, b, 33);
146
R2(b, c, d, e, a, 34);
147
R2(a, b, c, d, e, 35);
148
R2(e, a, b, c, d, 36);
149
R2(d, e, a, b, c, 37);
150
R2(c, d, e, a, b, 38);
151
R2(b, c, d, e, a, 39);
152
R3(a, b, c, d, e, 40);
153
R3(e, a, b, c, d, 41);
154
R3(d, e, a, b, c, 42);
155
R3(c, d, e, a, b, 43);
156
R3(b, c, d, e, a, 44);
157
R3(a, b, c, d, e, 45);
158
R3(e, a, b, c, d, 46);
159
R3(d, e, a, b, c, 47);
160
R3(c, d, e, a, b, 48);
161
R3(b, c, d, e, a, 49);
162
R3(a, b, c, d, e, 50);
163
R3(e, a, b, c, d, 51);
164
R3(d, e, a, b, c, 52);
165
R3(c, d, e, a, b, 53);
166
R3(b, c, d, e, a, 54);
167
R3(a, b, c, d, e, 55);
168
R3(e, a, b, c, d, 56);
169
R3(d, e, a, b, c, 57);
170
R3(c, d, e, a, b, 58);
171
R3(b, c, d, e, a, 59);
172
R4(a, b, c, d, e, 60);
173
R4(e, a, b, c, d, 61);
174
R4(d, e, a, b, c, 62);
175
R4(c, d, e, a, b, 63);
176
R4(b, c, d, e, a, 64);
177
R4(a, b, c, d, e, 65);
178
R4(e, a, b, c, d, 66);
179
R4(d, e, a, b, c, 67);
180
R4(c, d, e, a, b, 68);
181
R4(b, c, d, e, a, 69);
182
R4(a, b, c, d, e, 70);
183
R4(e, a, b, c, d, 71);
184
R4(d, e, a, b, c, 72);
185
R4(c, d, e, a, b, 73);
186
R4(b, c, d, e, a, 74);
187
R4(a, b, c, d, e, 75);
188
R4(e, a, b, c, d, 76);
189
R4(d, e, a, b, c, 77);
190
R4(c, d, e, a, b, 78);
191
R4(b, c, d, e, a, 79);
192
/* Add the working vars back into context.state[] */
193
state[0] += a;
194
state[1] += b;
195
state[2] += c;
196
state[3] += d;
197
state[4] += e;
198
/* Wipe variables */
199
a = b = c = d = e = 0;
200
#ifdef SHA1HANDSOFF
201
memset(block, '\0', sizeof(block));
202
#endif
203
}
204
205
/* SHA1Init - Initialize new context */
206
207
void SHA1Init(SHA1_CTX *context)
208
{
209
/* SHA1 initialization constants */
210
context->state[0] = 0x67452301;
211
context->state[1] = 0xEFCDAB89;
212
context->state[2] = 0x98BADCFE;
213
context->state[3] = 0x10325476;
214
context->state[4] = 0xC3D2E1F0;
215
context->count[0] = context->count[1] = 0;
216
}
217
218
/* Run your data through this. */
219
220
void SHA1Update(SHA1_CTX *context, uint8_t *data, uint32_t len)
221
{
222
uint32_t i;
223
uint32_t j;
224
225
j = context->count[0];
226
if ((context->count[0] += len << 3) < j)
227
context->count[1]++;
228
context->count[1] += (len >> 29);
229
j = (j >> 3) & 63;
230
if ((j + len) > 63)
231
{
232
memcpy(&context->buffer[j], data, (i = 64 - j));
233
SHA1Transform(context->state, context->buffer);
234
for (; i + 63 < len; i += 64)
235
{
236
SHA1Transform(context->state, &data[i]);
237
}
238
j = 0;
239
}
240
else
241
i = 0;
242
memcpy(&context->buffer[j], &data[i], len - i);
243
}
244
245
/* Add padding and return the message digest. */
246
247
void SHA1Final(unsigned char digest[20], SHA1_CTX *context)
248
{
249
unsigned i;
250
unsigned char finalcount[8];
251
unsigned char c;
252
253
#if 0 /* untested "improvement" by DHR */
254
/* Convert context->count to a sequence of bytes
255
* in finalcount. Second element first, but
256
* big-endian order within element.
257
* But we do it all backwards.
258
*/
259
unsigned char *fcp = &finalcount[8];
260
261
for (i = 0; i < 2; i++)
262
{
263
uint32_t t = context->count[i];
264
int j;
265
266
for (j = 0; j < 4; t >>= 8, j++)
267
*--fcp = (unsigned char) t;
268
}
269
#else
270
for (i = 0; i < 8; i++)
271
{
272
finalcount[i] = (unsigned char)((context->count[(i >= 4 ? 0 : 1)] >> ((3 - (i & 3)) * 8)) & 255); /* Endian independent */
273
}
274
#endif
275
c = 0200;
276
SHA1Update(context, &c, 1);
277
while ((context->count[0] & 504) != 448)
278
{
279
c = 0000;
280
SHA1Update(context, &c, 1);
281
}
282
SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */
283
for (i = 0; i < 20; i++)
284
{
285
digest[i] = (unsigned char)((context->state[i >> 2] >> ((3 - (i & 3)) * 8)) & 255);
286
}
287
/* Wipe variables */
288
memset(context, '\0', sizeof(*context));
289
memset(&finalcount, '\0', sizeof(finalcount));
290
}
291
/* ================ end of sha1.c ================ */
292
293
void sha1(uint8_t *data, uint32_t size, uint8_t hash[20])
294
{
295
SHA1_CTX ctx;
296
SHA1Init(&ctx);
297
SHA1Update(&ctx, data, size);
298
SHA1Final(hash, &ctx);
299
}
300