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: 01/08/2005
32 This is a byte oriented version of SHA1 that operates on arrays of bytes
36 #include <string.h> /* for memcpy() etc. */
37 #include "endian-util.h"
40 #if defined(__cplusplus)
45 #define SHA1_BLOCK_SIZE 64
47 #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
48 #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
50 #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
52 #if (UTIL_BYTE_ORDER == UTIL_ORDER_LITTLE_ENDIAN)
53 # define bsw_32(p,n) \
54 { int _i = (n); while(_i--) ((uint32_t*)p)[_i] = bswap_32(((uint32_t*)p)[_i]); }
55 #elif (UTIL_BYTE_ORDER == UTIL_ORDER_BIG_ENDIAN)
58 # error "Unsupported byte order"
61 #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
65 #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
66 #define parity(x,y,z) ((x) ^ (y) ^ (z))
67 #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
69 #else /* Discovered by Rich Schroeppel and Colin Plumb */
71 #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
72 #define parity(x,y,z) ((x) ^ (y) ^ (z))
73 #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
77 /* Compile 64 bytes of hash data into SHA1 context. Note */
78 /* that this routine assumes that the byte order in the */
79 /* ctx->wbuf[] at this point is in such an order that low */
80 /* address bytes in the ORIGINAL byte stream will go in */
81 /* this buffer to the high end of 32-bit words on BOTH big */
82 /* and little endian systems */
90 #define one_cycle(v,a,b,c,d,e,f,k,h) \
91 q(v,e) += rotr32(q(v,a),27) + \
92 f(q(v,b),q(v,c),q(v,d)) + k + h; \
93 q(v,b) = rotr32(q(v,b), 2)
95 #define five_cycle(v,f,k,i) \
96 one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \
97 one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \
98 one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \
99 one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \
100 one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
102 static void sha1_compile(sha1_ctx ctx[1])
103 { uint32_t *w = ctx->wbuf;
107 memcpy(v, ctx->hash, 5 * sizeof(uint32_t));
109 uint32_t v0, v1, v2, v3, v4;
110 v0 = ctx->hash[0]; v1 = ctx->hash[1];
111 v2 = ctx->hash[2]; v3 = ctx->hash[3];
117 five_cycle(v, ch, 0x5a827999, 0);
118 five_cycle(v, ch, 0x5a827999, 5);
119 five_cycle(v, ch, 0x5a827999, 10);
120 one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
123 #define hf(i) (w[(i) & 15] = rotl32( \
124 w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
125 ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1))
127 one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
128 one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
129 one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
130 one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
132 five_cycle(v, parity, 0x6ed9eba1, 20);
133 five_cycle(v, parity, 0x6ed9eba1, 25);
134 five_cycle(v, parity, 0x6ed9eba1, 30);
135 five_cycle(v, parity, 0x6ed9eba1, 35);
137 five_cycle(v, maj, 0x8f1bbcdc, 40);
138 five_cycle(v, maj, 0x8f1bbcdc, 45);
139 five_cycle(v, maj, 0x8f1bbcdc, 50);
140 five_cycle(v, maj, 0x8f1bbcdc, 55);
142 five_cycle(v, parity, 0xca62c1d6, 60);
143 five_cycle(v, parity, 0xca62c1d6, 65);
144 five_cycle(v, parity, 0xca62c1d6, 70);
145 five_cycle(v, parity, 0xca62c1d6, 75);
148 ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
149 ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
150 ctx->hash[4] += v[4];
152 ctx->hash[0] += v0; ctx->hash[1] += v1;
153 ctx->hash[2] += v2; ctx->hash[3] += v3;
158 void sha1_begin(sha1_ctx ctx[1])
160 ctx->count[0] = ctx->count[1] = 0;
161 ctx->hash[0] = 0x67452301;
162 ctx->hash[1] = 0xefcdab89;
163 ctx->hash[2] = 0x98badcfe;
164 ctx->hash[3] = 0x10325476;
165 ctx->hash[4] = 0xc3d2e1f0;
168 /* SHA1 hash data in an array of bytes into hash buffer and */
169 /* call the hash_compile function as required. */
171 void sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1])
172 { uint32_t pos = (uint32_t)(ctx->count[0] & SHA1_MASK),
173 space = SHA1_BLOCK_SIZE - pos;
174 const unsigned char *sp = data;
176 if((ctx->count[0] += len) < len)
179 while(len >= space) /* transfer whole blocks if possible */
181 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
182 sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
183 bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2);
187 memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
190 /* SHA1 final padding and digest calculation */
192 void sha1_end(unsigned char hval[], sha1_ctx ctx[1])
193 { uint32_t i = (uint32_t)(ctx->count[0] & SHA1_MASK);
195 /* put bytes in the buffer in an order in which references to */
196 /* 32-bit words will put bytes with lower addresses into the */
197 /* top of 32 bit words on BOTH big and little endian machines */
198 bsw_32(ctx->wbuf, (i + 3) >> 2);
200 /* we now need to mask valid bytes and add the padding which is */
201 /* a single 1 bit and as many zero bits as necessary. Note that */
202 /* we can always add the first padding byte here because the */
203 /* buffer always has at least one empty slot */
204 ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
205 ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
207 /* we need 9 or more empty positions, one for the padding byte */
208 /* (above) and eight for the length count. If there is not */
209 /* enough space, pad and empty the buffer */
210 if(i > SHA1_BLOCK_SIZE - 9)
212 if(i < 60) ctx->wbuf[15] = 0;
216 else /* compute a word index for the empty buffer positions */
219 while(i < 14) /* and zero pad all but last two positions */
222 /* the following 32-bit length fields are assembled in the */
223 /* wrong byte order on little endian machines but this is */
224 /* corrected later since they are only ever used as 32-bit */
226 ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
227 ctx->wbuf[15] = ctx->count[0] << 3;
230 /* extract the hash value as bytes in case the hash buffer is */
231 /* misaligned for 32-bit words */
232 for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
233 hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
236 void sha1(unsigned char hval[], const unsigned char data[], unsigned long len)
239 sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
242 #if defined(__cplusplus)