limit fstBC to 30bp in Python3 ver.
[GalaxyCodeBases.git] / tools / torrent / mktorrent_crc / sha1.c
blob78f79883a2e70fb077bd9affde7e809e0c95aedc
1 /*
2 * SHA-1 in C
3 * By Steve Reid <sreid@sea-to-sky.net>
4 * 100% Public Domain
6 * Modifications by
7 * James H. Brown <jbrown@burgoyne.com>
8 * Saul Kravitz <Saul.Kravitz@celera.com>
9 * Ralph Giles <giles@artofcode.com>
10 * Emil Renner Berthing <esmil@mailme.dk>
11 * Still 100% Public Domain
14 /* #define SHA1_TEST */
15 /* #define SHA1_HANDSOFF */
16 /* #define SHA1_WIPE_VARS */
17 /* #define SHA1_VERBOSE */
19 #ifndef ALLINONE
20 #ifdef SHA1_TEST
21 #include <stdio.h>
22 #endif
23 #include <string.h>
24 #include <inttypes.h>
26 #define EXPORT
27 #endif /* ALLINONE */
29 #include "sha1.h"
31 #define rol(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
33 /* blk0() and blk() perform the initial expand. */
34 /* I got the idea of expanding during the round function from SSLeay */
35 /* FIXME: can we do this in an endian-proof way? */
36 #ifdef WORDS_BIGENDIAN
37 #define blk0(i) block->l[i]
38 #else
39 #define blk0(i) (block->l[i] = (rol(block->l[i],24)&0xFF00FF00) \
40 |(rol(block->l[i],8)&0x00FF00FF))
41 #endif
42 #define blk(i) (block->l[i&15] = rol(block->l[(i+13)&15]^block->l[(i+8)&15] \
43 ^block->l[(i+2)&15]^block->l[i&15],1))
45 /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */
46 #define R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk0(i)+0x5A827999+rol(v,5);w=rol(w,30);
47 #define R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+blk(i)+0x5A827999+rol(v,5);w=rol(w,30);
48 #define R2(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0x6ED9EBA1+rol(v,5);w=rol(w,30);
49 #define R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+blk(i)+0x8F1BBCDC+rol(v,5);w=rol(w,30);
50 #define R4(v,w,x,y,z,i) z+=(w^x^y)+blk(i)+0xCA62C1D6+rol(v,5);w=rol(w,30);
53 #ifdef SHA1_VERBOSE
54 static void SHAPrintContext(SHA_CTX *context, char *msg){
55 printf("%s (%d,%d) %x %x %x %x %x\n",
56 msg,
57 context->count[0],
58 context->count[1],
59 context->state[0],
60 context->state[1],
61 context->state[2],
62 context->state[3],
63 context->state[4]);
65 #endif /* SHA1_VERBOSE */
67 /* Hash a single 512-bit block. This is the core of the algorithm. */
68 static void SHA1_Transform(uint32_t state[5], const uint8_t buffer[64])
70 uint32_t a, b, c, d, e;
71 typedef union {
72 uint8_t c[64];
73 uint32_t l[16];
74 } CHAR64LONG16;
75 CHAR64LONG16 *block;
76 #ifdef SHA1_HANDSOFF
77 static uint8_t workspace[64];
79 block = (CHAR64LONG16 *)workspace;
80 memcpy(block, buffer, 64);
81 #else
82 block = (CHAR64LONG16 *)buffer;
83 #endif
85 /* Copy context->state[] to working vars */
86 a = state[0];
87 b = state[1];
88 c = state[2];
89 d = state[3];
90 e = state[4];
92 /* 4 rounds of 20 operations each. Loop unrolled. */
93 R0(a,b,c,d,e, 0); R0(e,a,b,c,d, 1); R0(d,e,a,b,c, 2); R0(c,d,e,a,b, 3);
94 R0(b,c,d,e,a, 4); R0(a,b,c,d,e, 5); R0(e,a,b,c,d, 6); R0(d,e,a,b,c, 7);
95 R0(c,d,e,a,b, 8); R0(b,c,d,e,a, 9); R0(a,b,c,d,e,10); R0(e,a,b,c,d,11);
96 R0(d,e,a,b,c,12); R0(c,d,e,a,b,13); R0(b,c,d,e,a,14); R0(a,b,c,d,e,15);
97 R1(e,a,b,c,d,16); R1(d,e,a,b,c,17); R1(c,d,e,a,b,18); R1(b,c,d,e,a,19);
98 R2(a,b,c,d,e,20); R2(e,a,b,c,d,21); R2(d,e,a,b,c,22); R2(c,d,e,a,b,23);
99 R2(b,c,d,e,a,24); R2(a,b,c,d,e,25); R2(e,a,b,c,d,26); R2(d,e,a,b,c,27);
100 R2(c,d,e,a,b,28); R2(b,c,d,e,a,29); R2(a,b,c,d,e,30); R2(e,a,b,c,d,31);
101 R2(d,e,a,b,c,32); R2(c,d,e,a,b,33); R2(b,c,d,e,a,34); R2(a,b,c,d,e,35);
102 R2(e,a,b,c,d,36); R2(d,e,a,b,c,37); R2(c,d,e,a,b,38); R2(b,c,d,e,a,39);
103 R3(a,b,c,d,e,40); R3(e,a,b,c,d,41); R3(d,e,a,b,c,42); R3(c,d,e,a,b,43);
104 R3(b,c,d,e,a,44); R3(a,b,c,d,e,45); R3(e,a,b,c,d,46); R3(d,e,a,b,c,47);
105 R3(c,d,e,a,b,48); R3(b,c,d,e,a,49); R3(a,b,c,d,e,50); R3(e,a,b,c,d,51);
106 R3(d,e,a,b,c,52); R3(c,d,e,a,b,53); R3(b,c,d,e,a,54); R3(a,b,c,d,e,55);
107 R3(e,a,b,c,d,56); R3(d,e,a,b,c,57); R3(c,d,e,a,b,58); R3(b,c,d,e,a,59);
108 R4(a,b,c,d,e,60); R4(e,a,b,c,d,61); R4(d,e,a,b,c,62); R4(c,d,e,a,b,63);
109 R4(b,c,d,e,a,64); R4(a,b,c,d,e,65); R4(e,a,b,c,d,66); R4(d,e,a,b,c,67);
110 R4(c,d,e,a,b,68); R4(b,c,d,e,a,69); R4(a,b,c,d,e,70); R4(e,a,b,c,d,71);
111 R4(d,e,a,b,c,72); R4(c,d,e,a,b,73); R4(b,c,d,e,a,74); R4(a,b,c,d,e,75);
112 R4(e,a,b,c,d,76); R4(d,e,a,b,c,77); R4(c,d,e,a,b,78); R4(b,c,d,e,a,79);
114 /* Add the working vars back into context.state[] */
115 state[0] += a;
116 state[1] += b;
117 state[2] += c;
118 state[3] += d;
119 state[4] += e;
121 #ifdef SHA1_WIPE_VARS
122 a = b = c = d = e = 0;
123 #endif
126 /* SHA1Init - Initialize new context */
127 EXPORT void SHA1_Init(SHA_CTX *context)
129 /* SHA1 initialization constants */
130 context->state[0] = 0x67452301;
131 context->state[1] = 0xEFCDAB89;
132 context->state[2] = 0x98BADCFE;
133 context->state[3] = 0x10325476;
134 context->state[4] = 0xC3D2E1F0;
135 context->count[0] = context->count[1] = 0;
138 /* Run your data through this. */
139 EXPORT void SHA1_Update(SHA_CTX *context, const uint8_t *data, unsigned long len)
141 size_t i, j;
143 #ifdef SHA1_VERBOSE
144 SHAPrintContext(context, "before");
145 #endif
146 j = (context->count[0] >> 3) & 63;
148 if ((context->count[0] += len << 3) < (len << 3))
149 context->count[1]++;
151 context->count[1] += (len >> 29);
153 if ((j + len) > 63) {
154 memcpy(&context->buffer[j], data, (i = 64-j));
155 SHA1_Transform(context->state, context->buffer);
156 for ( ; i + 63 < len; i += 64) {
157 SHA1_Transform(context->state, data + i);
159 j = 0;
160 } else
161 i = 0;
163 memcpy(&context->buffer[j], &data[i], len - i);
165 #ifdef SHA1_VERBOSE
166 SHAPrintContext(context, "after ");
167 #endif
170 /* Add padding and return the message digest. */
171 EXPORT void SHA1_Final(uint8_t *digest, SHA_CTX *context)
173 uint32_t i;
174 uint8_t finalcount[8];
176 for (i = 0; i < 8; i++) {
177 /* Endian independent */
178 finalcount[i] = (uint8_t)
179 ((context->count[(i >= 4 ? 0 : 1)]
180 >> ((3-(i & 3)) * 8)) & 255);
183 SHA1_Update(context, (uint8_t *)"\200", 1);
184 while ((context->count[0] & 504) != 448) {
185 SHA1_Update(context, (uint8_t *)"\0", 1);
187 SHA1_Update(context, finalcount, 8); /* Should cause a SHA1_Transform() */
188 for (i = 0; i < SHA_DIGEST_LENGTH; i++) {
189 digest[i] = (uint8_t)
190 ((context->state[i>>2] >> ((3-(i & 3)) * 8)) & 255);
193 #ifdef SHA1_WIPE_VARS
194 i = 0;
195 memset(context->buffer, 0, 64);
196 memset(context->state, 0, 20);
197 memset(context->count, 0, 8);
198 memset(finalcount, 0, 8);
199 #endif
201 #ifdef SHA1_HANDSOFF /* make SHA1Transform overwrite its own static vars */
202 SHA1_Transform(context->state, context->buffer);
203 #endif
207 /*************************************************************\
208 * Self Test *
209 \*************************************************************/
210 #ifdef SHA1_TEST
212 Test Vectors (from FIPS PUB 180-1)
214 "abc"
215 A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D
216 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
217 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1
218 A million repetitions of "a"
219 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F
221 static char *test_data[] = {
222 "abc",
223 "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
224 "A million repetitions of 'a'"};
225 static char *test_results[] = {
226 "A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D",
227 "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1",
228 "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F"};
231 void digest_to_hex(const uint8_t *digest, char *output)
233 int i,j;
234 char *c = output;
236 for (i = 0; i < SHA_DIGEST_LENGTH/4; i++) {
237 for (j = 0; j < 4; j++) {
238 sprintf(c,"%02X", digest[i*4+j]);
239 c += 2;
241 sprintf(c, " ");
242 c += 1;
244 *(c - 1) = '\0';
247 int main(int argc, char *argv[])
249 int k;
250 SHA_CTX context;
251 uint8_t digest[SHA_DIGEST_LENGTH];
252 char output[80];
254 fprintf(stdout, "Verifying SHA-1 implementation... ");
255 fflush(stdout);
257 for (k = 0; k < 2; k++){
258 SHA1_Init(&context);
259 SHA1_Update(&context, (uint8_t *)test_data[k], strlen(test_data[k]));
260 SHA1_Final(digest, &context);
261 digest_to_hex(digest, output);
263 if (strcmp(output, test_results[k])) {
264 fprintf(stdout, "FAIL\n");
265 fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[k]);
266 fprintf(stderr,"\t%s returned\n", output);
267 fprintf(stderr,"\t%s is correct\n", test_results[k]);
268 return 1;
272 /* The million 'a' vector we feed separately */
273 SHA1_Init(&context);
274 for (k = 0; k < 1000000; k++)
275 SHA1_Update(&context, (uint8_t *)"a", 1);
276 SHA1_Final(digest, &context);
277 digest_to_hex(digest, output);
278 if (strcmp(output, test_results[2])) {
279 fprintf(stdout, "FAIL\n");
280 fprintf(stderr,"* hash of \"%s\" incorrect:\n", test_data[2]);
281 fprintf(stderr,"\t%s returned\n", output);
282 fprintf(stderr,"\t%s is correct\n", test_results[2]);
283 return 1;
286 /* success */
287 fprintf(stdout, "OK\n");
288 fflush(stdout);
289 return 0;
291 #endif /* SHA1_TEST */