Line data Source code
1 : #ifndef GPAC_DISABLE_CORE_TOOLS
2 :
3 : #ifndef _CRT_SECURE_NO_DEPRECATE
4 : #define _CRT_SECURE_NO_DEPRECATE 1
5 : #endif
6 :
7 : #include <gpac/tools.h>
8 :
9 : #ifndef PUT_UINT32_BE
10 : #define PUT_UINT32_BE(n,b,i) \
11 : { \
12 : (b)[(i) ] = (u8) ( (n) >> 24 ); \
13 : (b)[(i) + 1] = (u8) ( (n) >> 16 ); \
14 : (b)[(i) + 2] = (u8) ( (n) >> 8 ); \
15 : (b)[(i) + 3] = (u8) ( (n) ); \
16 : }
17 : #endif
18 :
19 : #if 0
20 : /*
21 : * FIPS-180-1 compliant SHA-1 implementation
22 : *
23 : * Copyright (C) 2003-2006 Christophe Devine
24 : *
25 : * This library is free software; you can redistribute it and/or
26 : * modify it under the terms of the GNU Lesser General Public
27 : * License, version 2.1 as published by the Free Software Foundation.
28 : *
29 : * This library is distributed in the hope that it will be useful,
30 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
32 : * Lesser General Public License for more details.
33 : *
34 : * You should have received a copy of the GNU Lesser General Public
35 : * License along with this library; if not, write to the Free Software
36 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
37 : * MA 02110-1301 USA
38 : */
39 : /*
40 : * The SHA-1 standard was published by NIST in 1993.
41 : *
42 : * http://www.itl.nist.gov/fipspubs/fip180-1.htm
43 : */
44 :
45 :
46 : struct __sha1_context
47 : {
48 : u32 total[2];
49 : u32 state[5];
50 : u8 buffer[64];
51 : };
52 :
53 : /*
54 : * 32-bit integer manipulation macros (big endian)
55 : */
56 : #ifndef GET_UINT32_BE
57 : #define GET_UINT32_BE(n,b,i) \
58 : { \
59 : (n) = ( (u32) (b)[(i) ] << 24 ) \
60 : | ( (u32) (b)[(i) + 1] << 16 ) \
61 : | ( (u32) (b)[(i) + 2] << 8 ) \
62 : | ( (u32) (b)[(i) + 3] ); \
63 : }
64 : #endif
65 :
66 : /*
67 : * SHA-1 context setup
68 : */
69 : GF_SHA1Context *gf_sha1_starts()
70 : {
71 : GF_SHA1Context *ctx;
72 : GF_SAFEALLOC(ctx, GF_SHA1Context);
73 : if (!ctx) return NULL;
74 : ctx->total[0] = 0;
75 : ctx->total[1] = 0;
76 :
77 : ctx->state[0] = 0x67452301;
78 : ctx->state[1] = 0xEFCDAB89;
79 : ctx->state[2] = 0x98BADCFE;
80 : ctx->state[3] = 0x10325476;
81 : ctx->state[4] = 0xC3D2E1F0;
82 : return ctx;
83 : }
84 :
85 : static void sha1_process(GF_SHA1Context *ctx, u8 data[64] )
86 : {
87 : u32 temp, W[16], A, B, C, D, E;
88 :
89 : GET_UINT32_BE( W[0], data, 0 );
90 : GET_UINT32_BE( W[1], data, 4 );
91 : GET_UINT32_BE( W[2], data, 8 );
92 : GET_UINT32_BE( W[3], data, 12 );
93 : GET_UINT32_BE( W[4], data, 16 );
94 : GET_UINT32_BE( W[5], data, 20 );
95 : GET_UINT32_BE( W[6], data, 24 );
96 : GET_UINT32_BE( W[7], data, 28 );
97 : GET_UINT32_BE( W[8], data, 32 );
98 : GET_UINT32_BE( W[9], data, 36 );
99 : GET_UINT32_BE( W[10], data, 40 );
100 : GET_UINT32_BE( W[11], data, 44 );
101 : GET_UINT32_BE( W[12], data, 48 );
102 : GET_UINT32_BE( W[13], data, 52 );
103 : GET_UINT32_BE( W[14], data, 56 );
104 : GET_UINT32_BE( W[15], data, 60 );
105 :
106 : #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
107 :
108 : #define R(t) \
109 : ( \
110 : temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
111 : W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
112 : ( W[t & 0x0F] = S(temp,1) ) \
113 : )
114 :
115 : #define P(a,b,c,d,e,x) \
116 : { \
117 : e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
118 : }
119 :
120 : A = ctx->state[0];
121 : B = ctx->state[1];
122 : C = ctx->state[2];
123 : D = ctx->state[3];
124 : E = ctx->state[4];
125 :
126 : #define F(x,y,z) (z ^ (x & (y ^ z)))
127 : #define K 0x5A827999
128 :
129 : P( A, B, C, D, E, W[0] );
130 : P( E, A, B, C, D, W[1] );
131 : P( D, E, A, B, C, W[2] );
132 : P( C, D, E, A, B, W[3] );
133 : P( B, C, D, E, A, W[4] );
134 : P( A, B, C, D, E, W[5] );
135 : P( E, A, B, C, D, W[6] );
136 : P( D, E, A, B, C, W[7] );
137 : P( C, D, E, A, B, W[8] );
138 : P( B, C, D, E, A, W[9] );
139 : P( A, B, C, D, E, W[10] );
140 : P( E, A, B, C, D, W[11] );
141 : P( D, E, A, B, C, W[12] );
142 : P( C, D, E, A, B, W[13] );
143 : P( B, C, D, E, A, W[14] );
144 : P( A, B, C, D, E, W[15] );
145 : P( E, A, B, C, D, R(16) );
146 : P( D, E, A, B, C, R(17) );
147 : P( C, D, E, A, B, R(18) );
148 : P( B, C, D, E, A, R(19) );
149 :
150 : #undef K
151 : #undef F
152 :
153 : #define F(x,y,z) (x ^ y ^ z)
154 : #define K 0x6ED9EBA1
155 :
156 : P( A, B, C, D, E, R(20) );
157 : P( E, A, B, C, D, R(21) );
158 : P( D, E, A, B, C, R(22) );
159 : P( C, D, E, A, B, R(23) );
160 : P( B, C, D, E, A, R(24) );
161 : P( A, B, C, D, E, R(25) );
162 : P( E, A, B, C, D, R(26) );
163 : P( D, E, A, B, C, R(27) );
164 : P( C, D, E, A, B, R(28) );
165 : P( B, C, D, E, A, R(29) );
166 : P( A, B, C, D, E, R(30) );
167 : P( E, A, B, C, D, R(31) );
168 : P( D, E, A, B, C, R(32) );
169 : P( C, D, E, A, B, R(33) );
170 : P( B, C, D, E, A, R(34) );
171 : P( A, B, C, D, E, R(35) );
172 : P( E, A, B, C, D, R(36) );
173 : P( D, E, A, B, C, R(37) );
174 : P( C, D, E, A, B, R(38) );
175 : P( B, C, D, E, A, R(39) );
176 :
177 : #undef K
178 : #undef F
179 :
180 : #define F(x,y,z) ((x & y) | (z & (x | y)))
181 : #define K 0x8F1BBCDC
182 :
183 : P( A, B, C, D, E, R(40) );
184 : P( E, A, B, C, D, R(41) );
185 : P( D, E, A, B, C, R(42) );
186 : P( C, D, E, A, B, R(43) );
187 : P( B, C, D, E, A, R(44) );
188 : P( A, B, C, D, E, R(45) );
189 : P( E, A, B, C, D, R(46) );
190 : P( D, E, A, B, C, R(47) );
191 : P( C, D, E, A, B, R(48) );
192 : P( B, C, D, E, A, R(49) );
193 : P( A, B, C, D, E, R(50) );
194 : P( E, A, B, C, D, R(51) );
195 : P( D, E, A, B, C, R(52) );
196 : P( C, D, E, A, B, R(53) );
197 : P( B, C, D, E, A, R(54) );
198 : P( A, B, C, D, E, R(55) );
199 : P( E, A, B, C, D, R(56) );
200 : P( D, E, A, B, C, R(57) );
201 : P( C, D, E, A, B, R(58) );
202 : P( B, C, D, E, A, R(59) );
203 :
204 : #undef K
205 : #undef F
206 :
207 : #define F(x,y,z) (x ^ y ^ z)
208 : #define K 0xCA62C1D6
209 :
210 : P( A, B, C, D, E, R(60) );
211 : P( E, A, B, C, D, R(61) );
212 : P( D, E, A, B, C, R(62) );
213 : P( C, D, E, A, B, R(63) );
214 : P( B, C, D, E, A, R(64) );
215 : P( A, B, C, D, E, R(65) );
216 : P( E, A, B, C, D, R(66) );
217 : P( D, E, A, B, C, R(67) );
218 : P( C, D, E, A, B, R(68) );
219 : P( B, C, D, E, A, R(69) );
220 : P( A, B, C, D, E, R(70) );
221 : P( E, A, B, C, D, R(71) );
222 : P( D, E, A, B, C, R(72) );
223 : P( C, D, E, A, B, R(73) );
224 : P( B, C, D, E, A, R(74) );
225 : P( A, B, C, D, E, R(75) );
226 : P( E, A, B, C, D, R(76) );
227 : P( D, E, A, B, C, R(77) );
228 : P( C, D, E, A, B, R(78) );
229 : P( B, C, D, E, A, R(79) );
230 :
231 : #undef K
232 : #undef F
233 :
234 : ctx->state[0] += A;
235 : ctx->state[1] += B;
236 : ctx->state[2] += C;
237 : ctx->state[3] += D;
238 : ctx->state[4] += E;
239 : }
240 :
241 : /*
242 : * SHA-1 process buffer
243 : */
244 : void gf_sha1_update(GF_SHA1Context *ctx, u8 *input, u32 ilen )
245 : {
246 : s32 fill;
247 : u32 left;
248 :
249 : if( ilen <= 0 )
250 : return;
251 :
252 : left = ctx->total[0] & 0x3F;
253 : fill = 64 - left;
254 :
255 : ctx->total[0] += ilen;
256 : ctx->total[0] &= 0xFFFFFFFF;
257 :
258 : if( ctx->total[0] < (u32) ilen )
259 : ctx->total[1]++;
260 :
261 : if( left && (s32) ilen >= fill )
262 : {
263 : memcpy( (void *) (ctx->buffer + left),
264 : (void *) input, fill );
265 : sha1_process( ctx, ctx->buffer );
266 : input += fill;
267 : ilen -= fill;
268 : left = 0;
269 : }
270 :
271 : while( ilen >= 64 )
272 : {
273 : sha1_process( ctx, input );
274 : input += 64;
275 : ilen -= 64;
276 : }
277 :
278 : if( ilen > 0 )
279 : {
280 : memcpy( (void *) (ctx->buffer + left),
281 : (void *) input, ilen );
282 : }
283 : }
284 :
285 : static const u8 sha1_padding[64] =
286 : {
287 : 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
288 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
289 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
290 : 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
291 : };
292 :
293 : /*
294 : * SHA-1 final digest
295 : */
296 : void gf_sha1_finish(GF_SHA1Context *ctx, u8 output[GF_SHA1_DIGEST_SIZE] )
297 : {
298 : u32 last, padn;
299 : u32 high, low;
300 : u8 msglen[8];
301 :
302 : high = ( ctx->total[0] >> 29 )
303 : | ( ctx->total[1] << 3 );
304 : low = ( ctx->total[0] << 3 );
305 :
306 : PUT_UINT32_BE( high, msglen, 0 );
307 : PUT_UINT32_BE( low, msglen, 4 );
308 :
309 : last = ctx->total[0] & 0x3F;
310 : padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
311 :
312 : gf_sha1_update( ctx, (u8 *) sha1_padding, padn );
313 : gf_sha1_update( ctx, msglen, 8 );
314 :
315 : PUT_UINT32_BE( ctx->state[0], output, 0 );
316 : PUT_UINT32_BE( ctx->state[1], output, 4 );
317 : PUT_UINT32_BE( ctx->state[2], output, 8 );
318 : PUT_UINT32_BE( ctx->state[3], output, 12 );
319 : PUT_UINT32_BE( ctx->state[4], output, 16 );
320 :
321 : gf_free(ctx);
322 : }
323 : #else
324 :
325 : /*
326 : * sha1.c
327 : *
328 : * Copyright (C) 1998, 2009
329 : * Paul E. Jones <paulej@packetizer.com>
330 : * All Rights Reserved
331 : *
332 : * Freeware Public License (FPL)
333 : *
334 : * This software is licensed as "freeware." Permission to distribute
335 : * this software in source and binary forms, including incorporation
336 : * into other products, is hereby granted without a fee. THIS SOFTWARE
337 : * IS PROVIDED 'AS IS' AND WITHOUT ANY EXPRESSED OR IMPLIED WARRANTIES,
338 : * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
339 : * AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHOR SHALL NOT BE HELD
340 : * LIABLE FOR ANY DAMAGES RESULTING FROM THE USE OF THIS SOFTWARE, EITHER
341 : * DIRECTLY OR INDIRECTLY, INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA
342 : * OR DATA BEING RENDERED INACCURATE.
343 : *
344 : *****************************************************************************
345 : * $Id: sha1.c 12 2009-06-22 19:34:25Z paulej $
346 : *****************************************************************************
347 : *
348 : * Description:
349 : * This file implements the Secure Hashing Standard as defined
350 : * in FIPS PUB 180-1 published April 17, 1995.
351 : *
352 : * The Secure Hashing Standard, which uses the Secure Hashing
353 : * Algorithm (SHA), produces a 160-bit message digest for a
354 : * given data stream. In theory, it is highly improbable that
355 : * two messages will produce the same message digest. Therefore,
356 : * this algorithm can serve as a means of providing a "fingerprint"
357 : * for a message.
358 : *
359 : * Portability Issues:
360 : * SHA-1 is defined in terms of 32-bit "words". This code was
361 : * written with the expectation that the processor has at least
362 : * a 32-bit machine word size. If the machine word size is larger,
363 : * the code should still function properly. One caveat to that
364 : * is that the input functions taking characters and character
365 : * arrays assume that only 8 bits of information are stored in each
366 : * character.
367 : *
368 : * Caveats:
369 : * SHA-1 is designed to work with messages less than 2^64 bits
370 : * long. Although SHA-1 allows a message digest to be generated for
371 : * messages of any number of bits less than 2^64, this
372 : * implementation only works with messages with a length that is a
373 : * multiple of the size of an 8-bit character.
374 : *
375 : */
376 :
377 : /*
378 : * This structure will hold context information for the hashing
379 : * operation
380 : */
381 : struct __sha1_context
382 : {
383 : unsigned Message_Digest[5]; /* Message Digest (output) */
384 :
385 : unsigned Length_Low; /* Message length in bits */
386 : unsigned Length_High; /* Message length in bits */
387 :
388 : unsigned char Message_Block[64]; /* 512-bit message blocks */
389 : int Message_Block_Index; /* Index into message block array */
390 :
391 : int Computed; /* Is the digest computed? */
392 : int Corrupted; /* Is the message digest corruped? */
393 : };
394 :
395 : /*
396 : * Define the circular shift macro
397 : */
398 : #define SHA1CircularShift(bits,word) \
399 : ((((word) << (bits)) & 0xFFFFFFFF) | \
400 : ((word) >> (32-(bits))))
401 :
402 :
403 : /*
404 : * SHA1ProcessMessageBlock
405 : *
406 : * Description:
407 : * This function will process the next 512 bits of the message
408 : * stored in the Message_Block array.
409 : *
410 : * Parameters:
411 : * None.
412 : *
413 : * Returns:
414 : * Nothing.
415 : *
416 : * Comments:
417 : * Many of the variable names in the SHAContext, especially the
418 : * single character names, were used because those were the names
419 : * used in the publication.
420 : *
421 : *
422 : */
423 14860414 : void SHA1ProcessMessageBlock(GF_SHA1Context *context)
424 : {
425 : const unsigned K[] = /* Constants defined in SHA-1 */
426 : {
427 : 0x5A827999,
428 : 0x6ED9EBA1,
429 : 0x8F1BBCDC,
430 : 0xCA62C1D6
431 : };
432 : int t; /* Loop counter */
433 : unsigned temp; /* Temporary word value */
434 : unsigned W[80]; /* Word sequence */
435 : unsigned A, B, C, D, E; /* Word buffers */
436 :
437 : /*
438 : * Initialize the first 16 words in the array W
439 : */
440 252627038 : for(t = 0; t < 16; t++)
441 : {
442 237766624 : W[t] = ((unsigned) context->Message_Block[t * 4]) << 24;
443 237766624 : W[t] |= ((unsigned) context->Message_Block[t * 4 + 1]) << 16;
444 237766624 : W[t] |= ((unsigned) context->Message_Block[t * 4 + 2]) << 8;
445 237766624 : W[t] |= ((unsigned) context->Message_Block[t * 4 + 3]);
446 : }
447 :
448 951065920 : for(t = 16; t < 80; t++)
449 : {
450 951065920 : W[t] = SHA1CircularShift(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
451 : }
452 :
453 14860414 : A = context->Message_Digest[0];
454 14860414 : B = context->Message_Digest[1];
455 14860414 : C = context->Message_Digest[2];
456 14860414 : D = context->Message_Digest[3];
457 14860414 : E = context->Message_Digest[4];
458 :
459 312068574 : for(t = 0; t < 20; t++)
460 : {
461 891624480 : temp = SHA1CircularShift(5,A) +
462 594416320 : ((B & C) | ((~B) & D)) + E + W[t] + K[0];
463 : temp &= 0xFFFFFFFF;
464 : E = D;
465 : D = C;
466 297208160 : C = SHA1CircularShift(30,B);
467 : B = A;
468 : A = temp;
469 : }
470 :
471 297208180 : for(t = 20; t < 40; t++)
472 : {
473 297208180 : temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[1];
474 : temp &= 0xFFFFFFFF;
475 : E = D;
476 : D = C;
477 297208180 : C = SHA1CircularShift(30,B);
478 : B = A;
479 : A = temp;
480 : }
481 :
482 297208060 : for(t = 40; t < 60; t++)
483 : {
484 891624180 : temp = SHA1CircularShift(5,A) +
485 594416120 : ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
486 : temp &= 0xFFFFFFFF;
487 : E = D;
488 : D = C;
489 297208060 : C = SHA1CircularShift(30,B);
490 : B = A;
491 : A = temp;
492 : }
493 :
494 297208160 : for(t = 60; t < 80; t++)
495 : {
496 297208160 : temp = SHA1CircularShift(5,A) + (B ^ C ^ D) + E + W[t] + K[3];
497 : temp &= 0xFFFFFFFF;
498 : E = D;
499 : D = C;
500 297208160 : C = SHA1CircularShift(30,B);
501 : B = A;
502 : A = temp;
503 : }
504 :
505 14860414 : context->Message_Digest[0] =
506 14860414 : (context->Message_Digest[0] + A) & 0xFFFFFFFF;
507 14860414 : context->Message_Digest[1] =
508 14860414 : (context->Message_Digest[1] + B) & 0xFFFFFFFF;
509 14860414 : context->Message_Digest[2] =
510 14860414 : (context->Message_Digest[2] + C) & 0xFFFFFFFF;
511 14860414 : context->Message_Digest[3] =
512 14860414 : (context->Message_Digest[3] + D) & 0xFFFFFFFF;
513 14860414 : context->Message_Digest[4] =
514 14860414 : (context->Message_Digest[4] + E) & 0xFFFFFFFF;
515 :
516 14860414 : context->Message_Block_Index = 0;
517 14860414 : }
518 :
519 : /*
520 : * SHA1PadMessage
521 : *
522 : * Description:
523 : * According to the standard, the message must be padded to an even
524 : * 512 bits. The first padding bit must be a '1'. The last 64
525 : * bits represent the length of the original message. All bits in
526 : * between should be 0. This function will pad the message
527 : * according to those rules by filling the Message_Block array
528 : * accordingly. It will also call SHA1ProcessMessageBlock()
529 : * appropriately. When it returns, it can be assumed that the
530 : * message digest has been computed.
531 : *
532 : * Parameters:
533 : * context: [in/out]
534 : * The context to pad
535 : *
536 : * Returns:
537 : * Nothing.
538 : *
539 : * Comments:
540 : *
541 : */
542 4893 : static void SHA1PadMessage(GF_SHA1Context *context)
543 : {
544 : /*
545 : * Check to see if the current message block is too small to hold
546 : * the initial padding bits and length. If so, we will pad the
547 : * block, process it, and then continue padding into a second
548 : * block.
549 : */
550 4893 : if (context->Message_Block_Index > 55)
551 : {
552 717 : context->Message_Block[context->Message_Block_Index++] = 0x80;
553 4632 : while(context->Message_Block_Index < 64)
554 : {
555 3198 : context->Message_Block[context->Message_Block_Index++] = 0;
556 : }
557 :
558 717 : SHA1ProcessMessageBlock(context);
559 :
560 41586 : while(context->Message_Block_Index < 56)
561 : {
562 40152 : context->Message_Block[context->Message_Block_Index++] = 0;
563 : }
564 : }
565 : else
566 : {
567 4176 : context->Message_Block[context->Message_Block_Index++] = 0x80;
568 132746 : while(context->Message_Block_Index < 56)
569 : {
570 124394 : context->Message_Block[context->Message_Block_Index++] = 0;
571 : }
572 : }
573 :
574 : /*
575 : * Store the message length as the last 8 octets
576 : */
577 4893 : context->Message_Block[56] = (context->Length_High >> 24) & 0xFF;
578 4893 : context->Message_Block[57] = (context->Length_High >> 16) & 0xFF;
579 4893 : context->Message_Block[58] = (context->Length_High >> 8) & 0xFF;
580 4893 : context->Message_Block[59] = (context->Length_High) & 0xFF;
581 4893 : context->Message_Block[60] = (context->Length_Low >> 24) & 0xFF;
582 4893 : context->Message_Block[61] = (context->Length_Low >> 16) & 0xFF;
583 4893 : context->Message_Block[62] = (context->Length_Low >> 8) & 0xFF;
584 4893 : context->Message_Block[63] = (context->Length_Low) & 0xFF;
585 :
586 4893 : SHA1ProcessMessageBlock(context);
587 4893 : }
588 : /*
589 : * SHA1Reset
590 : *
591 : * Description:
592 : * This function will initialize the SHA1Context in preparation
593 : * for computing a new message digest.
594 : *
595 : * Parameters:
596 : * context: [in/out]
597 : * The context to reset.
598 : *
599 : * Returns:
600 : * Nothing.
601 : *
602 : * Comments:
603 : *
604 : */
605 4891 : GF_SHA1Context *gf_sha1_starts()
606 : {
607 : GF_SHA1Context *context;
608 4891 : GF_SAFEALLOC(context, GF_SHA1Context);
609 4893 : if (!context) return NULL;
610 4893 : context->Length_Low = 0;
611 4893 : context->Length_High = 0;
612 4893 : context->Message_Block_Index = 0;
613 :
614 4893 : context->Message_Digest[0] = 0x67452301;
615 4893 : context->Message_Digest[1] = 0xEFCDAB89;
616 4893 : context->Message_Digest[2] = 0x98BADCFE;
617 4893 : context->Message_Digest[3] = 0x10325476;
618 4893 : context->Message_Digest[4] = 0xC3D2E1F0;
619 :
620 4893 : context->Computed = 0;
621 4893 : context->Corrupted = 0;
622 4893 : return context;
623 : }
624 :
625 294201 : void gf_sha1_update(GF_SHA1Context *context, u8 *message_array, u32 length )
626 : {
627 294201 : if (!length)
628 : {
629 : return;
630 : }
631 :
632 294206 : if (context->Computed || context->Corrupted)
633 : {
634 0 : context->Corrupted = 1;
635 0 : return;
636 : }
637 :
638 951148795 : while(length-- && !context->Corrupted)
639 : {
640 1901709230 : context->Message_Block[context->Message_Block_Index++] =
641 950854615 : (*message_array & 0xFF);
642 :
643 950854615 : context->Length_Low += 8;
644 : /* Force it to 32 bits */
645 : context->Length_Low &= 0xFFFFFFFF;
646 950854615 : if (context->Length_Low == 0)
647 : {
648 0 : context->Length_High++;
649 : /* Force it to 32 bits */
650 : context->Length_High &= 0xFFFFFFFF;
651 0 : if (context->Length_High == 0)
652 : {
653 : /* Message is too long */
654 0 : context->Corrupted = 1;
655 : }
656 : }
657 :
658 950854615 : if (context->Message_Block_Index == 64)
659 : {
660 14854804 : SHA1ProcessMessageBlock(context);
661 : }
662 :
663 950854589 : message_array++;
664 : }
665 : }
666 4893 : void gf_sha1_finish(GF_SHA1Context *context, u8 output[GF_SHA1_DIGEST_SIZE] )
667 : {
668 4893 : if (context->Corrupted)
669 : {
670 : return;
671 : }
672 :
673 4893 : if (!context->Computed)
674 : {
675 4893 : SHA1PadMessage(context);
676 4893 : context->Computed = 1;
677 : }
678 4893 : PUT_UINT32_BE( context->Message_Digest[0], output, 0 );
679 4893 : PUT_UINT32_BE( context->Message_Digest[1], output, 4 );
680 4893 : PUT_UINT32_BE( context->Message_Digest[2], output, 8 );
681 4893 : PUT_UINT32_BE( context->Message_Digest[3], output, 12 );
682 4893 : PUT_UINT32_BE( context->Message_Digest[4], output, 16 );
683 :
684 4893 : gf_free(context);
685 : }
686 :
687 : #endif
688 :
689 : /*
690 : * Output = SHA-1( file contents )
691 : */
692 : GF_EXPORT
693 702 : GF_Err gf_sha1_file_ptr(FILE *f, u8 output[GF_SHA1_DIGEST_SIZE] )
694 : {
695 702 : u64 pos = gf_ftell(f);
696 : size_t n;
697 : GF_SHA1Context *ctx;
698 : u8 buf[1024];
699 :
700 702 : ctx = gf_sha1_starts();
701 702 : gf_fseek(f, 0, SEEK_SET);
702 :
703 2360 : while( ( n = gf_fread( buf, sizeof( buf ), f ) ) > 0 )
704 956 : gf_sha1_update(ctx, buf, (s32) n );
705 :
706 702 : gf_sha1_finish(ctx, output );
707 :
708 702 : gf_fseek(f, pos, SEEK_SET);
709 702 : return GF_OK;
710 : }
711 :
712 : GF_EXPORT
713 147 : GF_Err gf_sha1_file( const char *path, u8 output[GF_SHA1_DIGEST_SIZE] )
714 : {
715 : FILE *f;
716 : GF_Err e;
717 :
718 147 : if (!strncmp(path, "gmem://", 7)) {
719 : u32 size;
720 : u8 *mem_address;
721 103 : e = gf_blob_get(path, &mem_address, &size, NULL);
722 103 : if (e) return e;
723 :
724 103 : gf_sha1_csum(mem_address, size, output);
725 103 : gf_blob_release(path);
726 103 : return GF_OK;
727 : }
728 :
729 44 : if( ( f = gf_fopen( path, "rb" ) ) == NULL )
730 : return GF_URL_ERROR;
731 :
732 44 : e = gf_sha1_file_ptr(f, output);
733 44 : gf_fclose( f );
734 44 : return e;
735 : }
736 :
737 : /*
738 : * Output = SHA-1( input buffer )
739 : */
740 : GF_EXPORT
741 1074 : void gf_sha1_csum( u8 *input, u32 ilen, u8 output[GF_SHA1_DIGEST_SIZE] )
742 : {
743 : GF_SHA1Context *ctx;
744 :
745 : memset(output, 0, sizeof(u8)*GF_SHA1_DIGEST_SIZE);
746 1074 : ctx = gf_sha1_starts();
747 1074 : if (ctx) {
748 1074 : gf_sha1_update(ctx, input, ilen );
749 1074 : gf_sha1_finish(ctx, output );
750 : }
751 1074 : }
752 :
753 : #if 0 //unused
754 : #define GF_SHA1_DIGEST_SIZE_HEXA 41
755 : void gf_sha1_csum_hexa(u8 *buf, u32 buflen, u8 digest[GF_SHA1_DIGEST_SIZE_HEXA]) {
756 : u8 tmp[GF_SHA1_DIGEST_SIZE];
757 : gf_sha1_csum (buf, buflen, tmp );
758 : digest[0] = 0;
759 : {
760 : int i;
761 : for ( i=0; i<GF_SHA1_DIGEST_SIZE; i++ )
762 : {
763 : char t[3];
764 : t[2] = 0;
765 : sprintf ( t, "%02X", tmp[i] );
766 : strcat ( (char*)digest, t );
767 : }
768 : }
769 : }
770 : #endif
771 :
772 :
773 : #endif
774 :
|