1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 2002, Dr Brian Gladman, Worcester, UK.   All rights reserved.
4 
5  LICENSE TERMS
6 
7  The free distribution and use of this software in both source and binary
8  form is allowed (with or without changes) provided that:
9 
10    1. distributions of this source code include the above copyright
11       notice, this list of conditions and the following disclaimer;
12 
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 
36 #include <string.h>     /* for memcpy() etc.        */
37 
38 #include "sha1.h"
39 #include "brg_endian.h"
40 
41 #if defined(__cplusplus)
42 extern "C"
43 {
44 #endif
45 
46 #if defined( _MSC_VER ) && ( _MSC_VER > 800 )
47 #pragma intrinsic(memcpy)
48 #endif
49 
50 #if 0 && defined(_MSC_VER)
51 #define rotl32  _lrotl
52 #define rotr32  _lrotr
53 #else
54 #define rotl32(x,n)   (((x) << n) | ((x) >> (32 - n)))
55 #define rotr32(x,n)   (((x) >> n) | ((x) << (32 - n)))
56 #endif
57 
58 #if !defined(bswap_32)
59 #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
60 #endif
61 
62 #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
63 #define SWAP_BYTES
64 #else
65 #undef  SWAP_BYTES
66 #endif
67 
68 #if defined(SWAP_BYTES)
69 #define bsw_32(p,n) \
70     { int _i = (n); while(_i--) ((uint_32t*)p)[_i] = bswap_32(((uint_32t*)p)[_i]); }
71 #else
72 #define bsw_32(p,n)
73 #endif
74 
75 #define SHA1_MASK   (SHA1_BLOCK_SIZE - 1)
76 
77 #if 0
78 
79 #define ch(x,y,z)       (((x) & (y)) ^ (~(x) & (z)))
80 #define parity(x,y,z)   ((x) ^ (y) ^ (z))
81 #define maj(x,y,z)      (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
82 
83 #else   /* Discovered by Rich Schroeppel and Colin Plumb   */
84 
85 #define ch(x,y,z)       ((z) ^ ((x) & ((y) ^ (z))))
86 #define parity(x,y,z)   ((x) ^ (y) ^ (z))
87 #define maj(x,y,z)      (((x) & (y)) | ((z) & ((x) ^ (y))))
88 
89 #endif
90 
91 /* Compile 64 bytes of hash data into SHA1 context. Note    */
92 /* that this routine assumes that the byte order in the     */
93 /* ctx->wbuf[] at this point is in such an order that low   */
94 /* address bytes in the ORIGINAL byte stream will go in     */
95 /* this buffer to the high end of 32-bit words on BOTH big  */
96 /* and little endian systems                                */
97 
98 #ifdef ARRAY
99 #define q(v,n)  v[n]
100 #else
101 #define q(v,n)  v##n
102 #endif
103 
104 #define one_cycle(v,a,b,c,d,e,f,k,h)            \
105     q(v,e) += rotr32(q(v,a),27) +               \
106               f(q(v,b),q(v,c),q(v,d)) + k + h;  \
107     q(v,b)  = rotr32(q(v,b), 2)
108 
109 #define five_cycle(v,f,k,i)                 \
110     one_cycle(v, 0,1,2,3,4, f,k,hf(i  ));   \
111     one_cycle(v, 4,0,1,2,3, f,k,hf(i+1));   \
112     one_cycle(v, 3,4,0,1,2, f,k,hf(i+2));   \
113     one_cycle(v, 2,3,4,0,1, f,k,hf(i+3));   \
114     one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
115 
sha1_compile(sha1_ctx ctx[1])116 VOID_RETURN sha1_compile(sha1_ctx ctx[1])
117 {   uint_32t    *w = ctx->wbuf;
118 
119 #ifdef ARRAY
120     uint_32t    v[5];
121     memcpy(v, ctx->hash, 5 * sizeof(uint_32t));
122 #else
123     uint_32t    v0, v1, v2, v3, v4;
124     v0 = ctx->hash[0]; v1 = ctx->hash[1];
125     v2 = ctx->hash[2]; v3 = ctx->hash[3];
126     v4 = ctx->hash[4];
127 #endif
128 
129 #define hf(i)   w[i]
130 
131     five_cycle(v, ch, 0x5a827999,  0);
132     five_cycle(v, ch, 0x5a827999,  5);
133     five_cycle(v, ch, 0x5a827999, 10);
134     one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
135 
136 #undef  hf
137 #define hf(i) (w[(i) & 15] = rotl32(                    \
138                  w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
139                ^ w[((i) +  2) & 15] ^ w[(i) & 15], 1))
140 
141     one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
142     one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
143     one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
144     one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
145 
146     five_cycle(v, parity, 0x6ed9eba1,  20);
147     five_cycle(v, parity, 0x6ed9eba1,  25);
148     five_cycle(v, parity, 0x6ed9eba1,  30);
149     five_cycle(v, parity, 0x6ed9eba1,  35);
150 
151     five_cycle(v, maj, 0x8f1bbcdc,  40);
152     five_cycle(v, maj, 0x8f1bbcdc,  45);
153     five_cycle(v, maj, 0x8f1bbcdc,  50);
154     five_cycle(v, maj, 0x8f1bbcdc,  55);
155 
156     five_cycle(v, parity, 0xca62c1d6,  60);
157     five_cycle(v, parity, 0xca62c1d6,  65);
158     five_cycle(v, parity, 0xca62c1d6,  70);
159     five_cycle(v, parity, 0xca62c1d6,  75);
160 
161 #ifdef ARRAY
162     ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
163     ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
164     ctx->hash[4] += v[4];
165 #else
166     ctx->hash[0] += v0; ctx->hash[1] += v1;
167     ctx->hash[2] += v2; ctx->hash[3] += v3;
168     ctx->hash[4] += v4;
169 #endif
170 }
171 
sha1_begin(sha1_ctx ctx[1])172 VOID_RETURN sha1_begin(sha1_ctx ctx[1])
173 {
174     ctx->count[0] = ctx->count[1] = 0;
175     ctx->hash[0] = 0x67452301;
176     ctx->hash[1] = 0xefcdab89;
177     ctx->hash[2] = 0x98badcfe;
178     ctx->hash[3] = 0x10325476;
179     ctx->hash[4] = 0xc3d2e1f0;
180 }
181 
182 /* SHA1 hash data in an array of bytes into hash buffer and */
183 /* call the hash_compile function as required.              */
184 
sha1_hash(const unsigned char data[],unsigned long len,sha1_ctx ctx[1])185 VOID_RETURN sha1_hash(const unsigned char data[], unsigned long len, sha1_ctx ctx[1])
186 {   uint_32t pos = (uint_32t)(ctx->count[0] & SHA1_MASK),
187             space = SHA1_BLOCK_SIZE - pos;
188     const unsigned char *sp = data;
189 
190     if((ctx->count[0] += len) < len)
191         ++(ctx->count[1]);
192 
193     while(len >= space)     /* tranfer whole blocks if possible  */
194     {
195         memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
196         sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
197         bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2);
198         sha1_compile(ctx);
199     }
200 
201     memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
202 }
203 
204 /* SHA1 final padding and digest calculation  */
205 
sha1_end(unsigned char hval[],sha1_ctx ctx[1])206 VOID_RETURN sha1_end(unsigned char hval[], sha1_ctx ctx[1])
207 {   uint_32t    i = (uint_32t)(ctx->count[0] & SHA1_MASK);
208 
209     /* put bytes in the buffer in an order in which references to   */
210     /* 32-bit words will put bytes with lower addresses into the    */
211     /* top of 32 bit words on BOTH big and little endian machines   */
212     bsw_32(ctx->wbuf, (i + 3) >> 2);
213 
214     /* we now need to mask valid bytes and add the padding which is */
215     /* a single 1 bit and as many zero bits as necessary. Note that */
216     /* we can always add the first padding byte here because the    */
217     /* buffer always has at least one empty slot                    */
218     ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
219     ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
220 
221     /* we need 9 or more empty positions, one for the padding byte  */
222     /* (above) and eight for the length count. If there is not      */
223     /* enough space, pad and empty the buffer                       */
224     if(i > SHA1_BLOCK_SIZE - 9)
225     {
226         if(i < 60) ctx->wbuf[15] = 0;
227         sha1_compile(ctx);
228         i = 0;
229     }
230     else    /* compute a word index for the empty buffer positions  */
231         i = (i >> 2) + 1;
232 
233     while(i < 14) /* and zero pad all but last two positions        */
234         ctx->wbuf[i++] = 0;
235 
236     /* the following 32-bit length fields are assembled in the      */
237     /* wrong byte order on little endian machines but this is       */
238     /* corrected later since they are only ever used as 32-bit      */
239     /* word values.                                                 */
240     ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
241     ctx->wbuf[15] = ctx->count[0] << 3;
242     sha1_compile(ctx);
243 
244     /* extract the hash value as bytes in case the hash buffer is   */
245     /* misaligned for 32-bit words                                  */
246     for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
247         hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
248 }
249 
sha1(unsigned char hval[],const unsigned char data[],unsigned long len)250 VOID_RETURN sha1(unsigned char hval[], const unsigned char data[], unsigned long len)
251 {   sha1_ctx    cx[1];
252 
253     sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
254 }
255 
256 #if defined(__cplusplus)
257 }
258 #endif
259