2 ---------------------------------------------------------------------------
3 Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
7 The free distribution and use of this software in both source and binary
8 form is allowed (with or without changes) provided that:
10 1. distributions of this source code include the above copyright
11 notice, this list of conditions and the following disclaimer;
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;
17 3. the copyright holder's name is not used to endorse products
18 built using this software without specific written permission.
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.
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: 16/01/2004
32 This is a byte oriented version of SHA1 that operates on arrays of bytes
33 stored in memory. It runs at 22 cycles per byte on a Pentium P4 processor
36 #include <string.h> /* for memcpy() etc. */
37 #include <stdlib.h> /* for _lrotl with VC++ */
41 #if defined(__cplusplus)
47 To obtain the highest speed on processors with 32-bit words, this code
48 needs to determine the order in which bytes are packed into such words.
49 The following block of code is an attempt to capture the most obvious
50 ways in which various environemnts specify their endian definitions.
51 It may well fail, in which case the definitions will need to be set by
52 editing at the points marked **** EDIT HERE IF NECESSARY **** below.
55 /* PLATFORM SPECIFIC INCLUDES */
57 #define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
58 #define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
60 #if defined(__GNUC__) || defined(__GNU_LIBRARY__)
61 # if defined(__FreeBSD__) || defined(__OpenBSD__)
62 # include <sys/endian.h>
63 # elif defined( BSD ) && ( BSD >= 199103 )
64 # include <machine/endian.h>
65 # elif defined(__APPLE__)
66 # if defined(__BIG_ENDIAN__) && !defined( BIG_ENDIAN )
68 # elif defined(__LITTLE_ENDIAN__) && !defined( LITTLE_ENDIAN )
69 # define LITTLE_ENDIAN
73 # if !defined(__BEOS__)
74 # include <byteswap.h>
79 #if !defined(PLATFORM_BYTE_ORDER)
80 # if defined(LITTLE_ENDIAN) || defined(BIG_ENDIAN)
81 # if defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
82 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
83 # elif !defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
84 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
85 # elif defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
86 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
87 # elif defined(BYTE_ORDER) && (BYTE_ORDER == BIG_ENDIAN)
88 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
90 # elif defined(_LITTLE_ENDIAN) || defined(_BIG_ENDIAN)
91 # if defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
92 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
93 # elif !defined(_LITTLE_ENDIAN) && defined(_BIG_ENDIAN)
94 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
95 # elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _LITTLE_ENDIAN)
96 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
97 # elif defined(_BYTE_ORDER) && (_BYTE_ORDER == _BIG_ENDIAN)
98 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
100 # elif defined(__LITTLE_ENDIAN__) || defined(__BIG_ENDIAN__)
101 # if defined(__LITTLE_ENDIAN__) && !defined(__BIG_ENDIAN__)
102 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
103 # elif !defined(__LITTLE_ENDIAN__) && defined(__BIG_ENDIAN__)
104 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
105 # elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __LITTLE_ENDIAN__)
106 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
107 # elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __BIG_ENDIAN__)
108 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
113 /* if the platform is still unknown, try to find its byte order */
114 /* from commonly used machine defines */
116 #if !defined(PLATFORM_BYTE_ORDER)
118 #if defined( __alpha__ ) || defined( __alpha ) || defined( i386 ) || \
119 defined( __i386__ ) || defined( _M_I86 ) || defined( _M_IX86 ) || \
120 defined( __OS2__ ) || defined( sun386 ) || defined( __TURBOC__ ) || \
121 defined( vax ) || defined( vms ) || defined( VMS ) || \
123 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
125 #elif defined( AMIGA ) || defined( applec ) || defined( __AS400__ ) || \
126 defined( _CRAY ) || defined( __hppa ) || defined( __hp9000 ) || \
127 defined( ibm370 ) || defined( mc68000 ) || defined( m68k ) || \
128 defined( __MRC__ ) || defined( __MVS__ ) || defined( __MWERKS__ ) || \
129 defined( sparc ) || defined( __sparc) || defined( SYMANTEC_C ) || \
130 defined( __TANDEM ) || defined( THINK_C ) || defined( __VMCMS__ )
131 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
133 #elif 0 /* **** EDIT HERE IF NECESSARY **** */
134 # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
135 #elif 0 /* **** EDIT HERE IF NECESSARY **** */
136 # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
138 # error Please edit sha1.c (line 134 or 136) to set the platform byte order
144 #pragma intrinsic(memcpy)
147 #if 0 && defined(_MSC_VER)
148 #define rotl32 _lrotl
149 #define rotr32 _lrotr
151 #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
152 #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
155 #if !defined(bswap_32)
156 #define bswap_32(x) (rotr32((x), 24) & 0x00ff00ff | rotr32((x), 8) & 0xff00ff00)
159 #if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)
165 #if defined(SWAP_BYTES)
166 #define bsw_32(p,n) \
167 { int _i = (n); while(_i--) ((sha1_32t*)p)[_i] = bswap_32(((sha1_32t*)p)[_i]); }
172 #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
176 #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
177 #define parity(x,y,z) ((x) ^ (y) ^ (z))
178 #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
180 #else /* Discovered by Rich Schroeppel and Colin Plumb */
182 #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
183 #define parity(x,y,z) ((x) ^ (y) ^ (z))
184 #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
188 /* Compile 64 bytes of hash data into SHA1 context. Note */
189 /* that this routine assumes that the byte order in the */
190 /* ctx->wbuf[] at this point is in such an order that low */
191 /* address bytes in the ORIGINAL byte stream in this buffer */
192 /* will go to the high end of 32-bit words on BOTH big and */
193 /* little endian systems */
201 #define one_cycle(v,a,b,c,d,e,f,k,h) \
202 q(v,e) += rotr32(q(v,a),27) + \
203 f(q(v,b),q(v,c),q(v,d)) + k + h; \
204 q(v,b) = rotr32(q(v,b), 2)
206 #define five_cycle(v,f,k,i) \
207 one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \
208 one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \
209 one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \
210 one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \
211 one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
213 void sha1_compile(sha1_ctx ctx[1])
214 { sha1_32t *w = ctx->wbuf;
218 memcpy(v, ctx->hash, 5 * sizeof(sha1_32t));
220 sha1_32t v0, v1, v2, v3, v4;
221 v0 = ctx->hash[0]; v1 = ctx->hash[1];
222 v2 = ctx->hash[2]; v3 = ctx->hash[3];
228 five_cycle(v, ch, 0x5a827999, 0);
229 five_cycle(v, ch, 0x5a827999, 5);
230 five_cycle(v, ch, 0x5a827999, 10);
231 one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
234 #define hf(i) (w[(i) & 15] = rotl32( \
235 w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
236 ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1))
238 one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
239 one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
240 one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
241 one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
243 five_cycle(v, parity, 0x6ed9eba1, 20);
244 five_cycle(v, parity, 0x6ed9eba1, 25);
245 five_cycle(v, parity, 0x6ed9eba1, 30);
246 five_cycle(v, parity, 0x6ed9eba1, 35);
248 five_cycle(v, maj, 0x8f1bbcdc, 40);
249 five_cycle(v, maj, 0x8f1bbcdc, 45);
250 five_cycle(v, maj, 0x8f1bbcdc, 50);
251 five_cycle(v, maj, 0x8f1bbcdc, 55);
253 five_cycle(v, parity, 0xca62c1d6, 60);
254 five_cycle(v, parity, 0xca62c1d6, 65);
255 five_cycle(v, parity, 0xca62c1d6, 70);
256 five_cycle(v, parity, 0xca62c1d6, 75);
259 ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
260 ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
261 ctx->hash[4] += v[4];
263 ctx->hash[0] += v0; ctx->hash[1] += v1;
264 ctx->hash[2] += v2; ctx->hash[3] += v3;
269 void sha1_begin(sha1_ctx ctx[1])
271 ctx->count[0] = ctx->count[1] = 0;
272 ctx->hash[0] = 0x67452301;
273 ctx->hash[1] = 0xefcdab89;
274 ctx->hash[2] = 0x98badcfe;
275 ctx->hash[3] = 0x10325476;
276 ctx->hash[4] = 0xc3d2e1f0;
279 /* SHA1 hash data in an array of bytes into hash buffer and */
280 /* call the hash_compile function as required. */
282 void sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1])
283 { sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK),
284 space = SHA1_BLOCK_SIZE - pos;
285 const unsigned char *sp = data;
287 if((ctx->count[0] += len) < len)
290 while(len >= space) /* tranfer whole blocks if possible */
292 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
293 sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
294 bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2);
298 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
301 /* SHA1 final padding and digest calculation */
303 void sha1_end(unsigned char hval[], sha1_ctx ctx[1])
304 { sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1_MASK);
306 /* put bytes in the buffer in an order in which references to */
307 /* 32-bit words will put bytes with lower addresses into the */
308 /* top of 32 bit words on BOTH big and little endian machines */
309 bsw_32(ctx->wbuf, (i + 3) >> 2);
311 /* we now need to mask valid bytes and add the padding which is */
312 /* a single 1 bit and as many zero bits as necessary. Note that */
313 /* we can always add the first padding byte here because the */
314 /* buffer always has at least one empty slot */
315 ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
316 ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
318 /* we need 9 or more empty positions, one for the padding byte */
319 /* (above) and eight for the length count. If there is not */
320 /* enough space, pad and empty the buffer */
321 if(i > SHA1_BLOCK_SIZE - 9)
323 if(i < 60) ctx->wbuf[15] = 0;
327 else /* compute a word index for the empty buffer positions */
330 while(i < 14) /* and zero pad all but last two positions */
333 /* the following 32-bit length fields are assembled in the */
334 /* wrong byte order on little endian machines but this is */
335 /* corrected later since they are only ever used as 32-bit */
337 ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
338 ctx->wbuf[15] = ctx->count[0] << 3;
341 /* extract the hash value as bytes in case the hash buffer is */
342 /* misaligned for 32-bit words */
343 for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
344 hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
347 void sha1(unsigned char hval[], const unsigned char data[], unsigned long len)
350 sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
353 #if defined(__cplusplus)