11/*
2- ---------------------------------------------------------------------------
3- Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
2+ ---------------------------------------------------------------------------
3+ Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
44
5- LICENSE TERMS
5+ The redistribution and use of this software (with or without changes)
6+ is allowed without the payment of fees or royalties provided that:
67
7- The free distribution and use of this software in both source and binary
8- form is allowed (with or without changes) provided that:
8+ source code distributions include the above copyright notice, this
9+ list of conditions and the following disclaimer;
910
10- 1. distributions of this source code include the above copyright
11- notice, this list of conditions and the following disclaimer;
11+ binary distributions include the above copyright notice, this list
12+ of conditions and the following disclaimer in their documentation.
1213
13- 2. distributions in binary form include the above copyright
14- notice, this list of conditions and the following disclaimer
15- in the documentation and/or other associated materials;
16-
17- 3. the copyright holder's name is not used to endorse products
18- built using this software without specific written permission.
19-
20- ALTERNATIVELY, provided that this notice is retained in full, this product
21- may be distributed under the terms of the GNU General Public License (GPL),
22- in which case the provisions of the GPL apply INSTEAD OF those given above.
23-
24- DISCLAIMER
25-
26- This software is provided 'as is' with no explicit or implied warranties
27- in respect of its properties, including, but not limited to, correctness
28- and/or fitness for purpose.
29- ---------------------------------------------------------------------------
30- Issue Date: 01/08/2005
31-
32- This is a byte oriented version of SHA1 that operates on arrays of bytes
33- stored in memory.
34-
35- Source code origin listed in README file.
14+ This software is provided 'as is' with no explicit or implied warranties
15+ in respect of its operation, including, but not limited to, correctness
16+ and fitness for purpose.
17+ ---------------------------------------------------------------------------
18+ Issue Date: 20/12/2007
3619*/
3720
3821#include <string.h> /* for memcpy() etc. */
39- #include <stdio.h>
4022
4123#include "brg_sha1.h"
4224#include "brg_endian.h"
@@ -46,86 +28,9 @@ extern "C"
4628{
4729#endif
4830
49- /** BEGIN: UTS RNG Harness **/
50-
51- void rng_init (RNG_state * newstate , int seed )
52- {
53- struct sha1_context ctx ;
54- struct state_t gen ;
55- int i ;
56-
57- for (i = 0 ; i < 16 ; i ++ )
58- gen .state [i ] = 0 ;
59- gen .state [16 ] = (uint_8t )(0xFF & (seed >> 24 ));
60- gen .state [17 ] = (uint_8t )(0xFF & (seed >> 16 ));
61- gen .state [18 ] = (uint_8t )(0xFF & (seed >> 8 ));
62- gen .state [19 ] = (uint_8t )(0xFF & (seed >> 0 ));
63-
64- sha1_begin (& ctx );
65- sha1_hash (gen .state , 20 , & ctx );
66- sha1_end (newstate , & ctx );
67- }
68-
69- void rng_spawn (RNG_state * mystate , RNG_state * newstate , int spawnnumber )
70- {
71- struct sha1_context ctx ;
72- uint_8t bytes [4 ];
73-
74- bytes [0 ] = (uint_8t )(0xFF & (spawnnumber >> 24 ));
75- bytes [1 ] = (uint_8t )(0xFF & (spawnnumber >> 16 ));
76- bytes [2 ] = (uint_8t )(0xFF & (spawnnumber >> 8 ));
77- bytes [3 ] = (uint_8t )(0xFF & spawnnumber );
78-
79- sha1_begin (& ctx );
80- sha1_hash (mystate , 20 , & ctx );
81- sha1_hash (bytes , 4 , & ctx );
82- sha1_end (newstate , & ctx );
83- }
84-
85- int rng_rand (RNG_state * mystate ){
86- int r ;
87- uint_32t b = (mystate [16 ] << 24 ) | (mystate [17 ] << 16 )
88- | (mystate [18 ] << 8 ) | (mystate [19 ] << 0 );
89- b = b & POS_MASK ;
90-
91- r = (int ) b ;
92- //printf("b: %d\t, r: %d\n", b, r);
93- return r ;
94- }
95-
96- int rng_nextrand (RNG_state * mystate ){
97- struct sha1_context ctx ;
98- int r ;
99- uint_32t b ;
100-
101- sha1_begin (& ctx );
102- sha1_hash (mystate , 20 , & ctx );
103- sha1_end (mystate , & ctx );
104- b = (mystate [16 ] << 24 ) | (mystate [17 ] << 16 )
105- | (mystate [18 ] << 8 ) | (mystate [19 ] << 0 );
106- b = b & POS_MASK ;
107-
108- r = (int ) b ;
109- return r ;
110- }
111-
112- /* condense state into string to display during debugging */
113- char * rng_showstate (RNG_state * state , char * s ){
114- snprintf (s , 8 * sizeof (char ), "%.2X%.2X..." , state [0 ],state [1 ]);
115- return s ;
116- }
117-
118- /* describe random number generator type into string */
119- int rng_showtype (char * strBuf , int ind ) {
120- ind += snprintf (strBuf + ind , 25 * sizeof (char ), "SHA-1 (state size = %uB)" ,
121- (unsigned )sizeof (struct state_t ));
122- return ind ;
123- }
124-
125- /** END: UTS RNG Harness **/
126-
12731#if defined( _MSC_VER ) && ( _MSC_VER > 800 )
12832#pragma intrinsic(memcpy)
33+ #pragma intrinsic(memset)
12934#endif
13035
13136#if 0 && defined(_MSC_VER )
@@ -148,7 +53,7 @@ int rng_showtype(char *strBuf, int ind) {
14853
14954#if defined(SWAP_BYTES )
15055#define bsw_32 (p ,n ) \
151- { int _i = (n); while(_i--) ((uint_32t *)p)[_i] = bswap_32(((uint_32t *)p)[_i]); }
56+ { int _i = (n); while(_i--) ((uint32_t *)p)[_i] = bswap_32(((uint32_t *)p)[_i]); }
15257#else
15358#define bsw_32 (p ,n )
15459#endif
@@ -195,13 +100,13 @@ int rng_showtype(char *strBuf, int ind) {
195100 one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
196101
197102VOID_RETURN sha1_compile (sha1_ctx ctx [1 ])
198- { uint_32t * w = ctx -> wbuf ;
103+ { uint32_t * w = ctx -> wbuf ;
199104
200105#ifdef ARRAY
201- uint_32t v [5 ];
202- memcpy (v , ctx -> hash , 5 * sizeof (uint_32t ));
106+ uint32_t v [5 ];
107+ memcpy (v , ctx -> hash , sizeof (ctx -> hash ));
203108#else
204- uint_32t v0 , v1 , v2 , v3 , v4 ;
109+ uint32_t v0 , v1 , v2 , v3 , v4 ;
205110 v0 = ctx -> hash [0 ]; v1 = ctx -> hash [1 ];
206111 v2 = ctx -> hash [2 ]; v3 = ctx -> hash [3 ];
207112 v4 = ctx -> hash [4 ];
@@ -252,7 +157,7 @@ VOID_RETURN sha1_compile(sha1_ctx ctx[1])
252157
253158VOID_RETURN sha1_begin (sha1_ctx ctx [1 ])
254159{
255- ctx -> count [ 0 ] = ctx -> count [ 1 ] = 0 ;
160+ memset ( ctx , 0 , sizeof ( sha1_ctx )) ;
256161 ctx -> hash [0 ] = 0x67452301 ;
257162 ctx -> hash [1 ] = 0xefcdab89 ;
258163 ctx -> hash [2 ] = 0x98badcfe ;
@@ -261,43 +166,78 @@ VOID_RETURN sha1_begin(sha1_ctx ctx[1])
261166}
262167
263168/* SHA1 hash data in an array of bytes into hash buffer and */
264- /* call the hash_compile function as required. */
169+ /* call the hash_compile function as required. For both the */
170+ /* bit and byte orientated versions, the block length 'len' */
171+ /* must not be greater than 2^32 - 1 bits (2^29 - 1 bytes) */
265172
266173VOID_RETURN sha1_hash (const unsigned char data [], unsigned long len , sha1_ctx ctx [1 ])
267- { uint_32t pos = (uint_32t )(ctx -> count [0 ] & SHA1_MASK ),
268- space = SHA1_BLOCK_SIZE - pos ;
174+ { uint32_t pos = (uint32_t )((ctx -> count [0 ] >> 3 ) & SHA1_MASK );
269175 const unsigned char * sp = data ;
270-
176+ unsigned char * w = (unsigned char * )ctx -> wbuf ;
177+ #if SHA1_BITS == 1
178+ uint32_t ofs = (ctx -> count [0 ] & 7 );
179+ #else
180+ len <<= 3 ;
181+ #endif
271182 if ((ctx -> count [0 ] += len ) < len )
272183 ++ (ctx -> count [1 ]);
273-
274- while ( len >= space ) /* tranfer whole blocks if possible */
184+ #if SHA1_BITS == 1
185+ if ( ofs ) /* if not on a byte boundary */
275186 {
276- memcpy (((unsigned char * )ctx -> wbuf ) + pos , sp , space );
277- sp += space ; len -= space ; space = SHA1_BLOCK_SIZE ; pos = 0 ;
278- bsw_32 (ctx -> wbuf , SHA1_BLOCK_SIZE >> 2 );
279- sha1_compile (ctx );
187+ if (ofs + len < 8 ) /* if no added bytes are needed */
188+ {
189+ w [pos ] |= (* sp >> ofs );
190+ }
191+ else /* otherwise and add bytes */
192+ { unsigned char part = w [pos ];
193+
194+ while ((int )(ofs + (len -= 8 )) >= 0 )
195+ {
196+ w [pos ++ ] = part | (* sp >> ofs );
197+ part = * sp ++ << (8 - ofs );
198+ if (pos == SHA1_BLOCK_SIZE )
199+ {
200+ bsw_32 (w , SHA1_BLOCK_SIZE >> 2 );
201+ sha1_compile (ctx ); pos = 0 ;
202+ }
203+ }
204+
205+ w [pos ] = part ;
206+ }
207+ }
208+ else /* data is byte aligned */
209+ #endif
210+ { uint32_t space = SHA1_BLOCK_SIZE - pos ;
211+
212+ while (len >= (space << 3 ))
213+ {
214+ memcpy (w + pos , sp , space );
215+ bsw_32 (w , SHA1_BLOCK_SIZE >> 2 );
216+ sha1_compile (ctx );
217+ sp += space ; len -= (space << 3 );
218+ space = SHA1_BLOCK_SIZE ; pos = 0 ;
219+ }
220+ memcpy (w + pos , sp , (len + 7 * SHA1_BITS ) >> 3 );
280221 }
281-
282- memcpy (((unsigned char * )ctx -> wbuf ) + pos , sp , len );
283222}
284223
285224/* SHA1 final padding and digest calculation */
286225
287226VOID_RETURN sha1_end (unsigned char hval [], sha1_ctx ctx [1 ])
288- { uint_32t i = (uint_32t )( ctx -> count [0 ] & SHA1_MASK );
227+ { uint32_t i = (uint32_t )(( ctx -> count [0 ] >> 3 ) & SHA1_MASK ), m1 ;
289228
290229 /* put bytes in the buffer in an order in which references to */
291230 /* 32-bit words will put bytes with lower addresses into the */
292231 /* top of 32 bit words on BOTH big and little endian machines */
293- bsw_32 (ctx -> wbuf , (i + 3 ) >> 2 );
232+ bsw_32 (ctx -> wbuf , (i + 3 + SHA1_BITS ) >> 2 );
294233
295234 /* we now need to mask valid bytes and add the padding which is */
296235 /* a single 1 bit and as many zero bits as necessary. Note that */
297236 /* we can always add the first padding byte here because the */
298237 /* buffer always has at least one empty slot */
299- ctx -> wbuf [i >> 2 ] &= 0xffffff80 << 8 * (~i & 3 );
300- ctx -> wbuf [i >> 2 ] |= 0x00000080 << 8 * (~i & 3 );
238+ m1 = (unsigned char )0x80 >> (ctx -> count [0 ] & 7 );
239+ ctx -> wbuf [i >> 2 ] &= ((0xffffff00 | (~m1 + 1 )) << 8 * (~i & 3 ));
240+ ctx -> wbuf [i >> 2 ] |= (m1 << 8 * (~i & 3 ));
301241
302242 /* we need 9 or more empty positions, one for the padding byte */
303243 /* (above) and eight for the length count. If there is not */
@@ -318,14 +258,14 @@ VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1])
318258 /* wrong byte order on little endian machines but this is */
319259 /* corrected later since they are only ever used as 32-bit */
320260 /* word values. */
321- ctx -> wbuf [14 ] = ( ctx -> count [1 ] << 3 ) | ( ctx -> count [ 0 ] >> 29 ) ;
322- ctx -> wbuf [15 ] = ctx -> count [0 ] << 3 ;
261+ ctx -> wbuf [14 ] = ctx -> count [1 ];
262+ ctx -> wbuf [15 ] = ctx -> count [0 ];
323263 sha1_compile (ctx );
324264
325265 /* extract the hash value as bytes in case the hash buffer is */
326266 /* misaligned for 32-bit words */
327267 for (i = 0 ; i < SHA1_DIGEST_SIZE ; ++ i )
328- hval [i ] = (unsigned char ) (ctx -> hash [i >> 2 ] >> (8 * (~i & 3 )));
268+ hval [i ] = ((ctx -> hash [i >> 2 ] >> (8 * (~i & 3 ))) & 0xff );
329269}
330270
331271VOID_RETURN sha1 (unsigned char hval [], const unsigned char data [], unsigned long len )
0 commit comments