|
|
1.1.1.3 root 1: /* Deprecated/legacy */
2:
1.1.1.2 root 3: /*
4: ---------------------------------------------------------------------------
5: Copyright (c) 2002, Dr Brian Gladman, Worcester, UK. All rights reserved.
1.1 root 6:
1.1.1.2 root 7: LICENSE TERMS
1.1 root 8:
1.1.1.4 root 9: The free distribution and use of this software is allowed (with or without
10: changes) provided that:
1.1 root 11:
1.1.1.4 root 12: 1. source code distributions include the above copyright notice, this
13: list of conditions and the following disclaimer;
1.1 root 14:
1.1.1.4 root 15: 2. binary distributions include the above copyright notice, this list
16: of conditions and the following disclaimer in their documentation;
17:
18: 3. the name of the copyright holder is not used to endorse products
19: built using this software without specific written permission.
1.1.1.2 root 20:
21: DISCLAIMER
22:
23: This software is provided 'as is' with no explicit or implied warranties
24: in respect of its properties, including, but not limited to, correctness
25: and/or fitness for purpose.
26: ---------------------------------------------------------------------------
27: Issue Date: 18/06/2004
28:
29: This is a byte oriented version of SHA1 that operates on arrays of bytes
30: stored in memory.
31: */
32:
1.1.1.5 ! root 33: /* Adapted for TrueCrypt by the TrueCrypt Foundation */
! 34:
1.1.1.2 root 35: #include <string.h> /* for memcpy() etc. */
36: #include <stdlib.h> /* for _lrotl with VC++ */
37:
38: #include "Sha1.h"
39:
40: #if defined(__cplusplus)
41: extern "C"
42: {
43: #endif
44:
45: /*
46: To obtain the highest speed on processors with 32-bit words, this code
47: needs to determine the order in which bytes are packed into such words.
48: The following block of code is an attempt to capture the most obvious
49: ways in which various environemnts specify their endian definitions.
50: It may well fail, in which case the definitions will need to be set by
51: editing at the points marked **** EDIT HERE IF NECESSARY **** below.
52: */
53:
54: /* PLATFORM SPECIFIC INCLUDES */
55:
56: /* Original byte order detection removed */
57: #include "../Common/Endian.h"
58:
59: #define BRG_LITTLE_ENDIAN 1234 /* byte 0 is least significant (i386) */
60: #define BRG_BIG_ENDIAN 4321 /* byte 0 is most significant (mc68k) */
61:
62: #if BYTE_ORDER == LITTLE_ENDIAN
63: # define PLATFORM_BYTE_ORDER BRG_LITTLE_ENDIAN
64: #endif
65:
66: #if BYTE_ORDER == BIG_ENDIAN
67: # define PLATFORM_BYTE_ORDER BRG_BIG_ENDIAN
68: #endif
69:
70: #ifdef _MSC_VER
71: #pragma intrinsic(memcpy)
72: #endif
73:
74: #if 1 && defined(_MSC_VER) && !defined(_DEBUG)
75: #define rotl32 _rotl
76: #define rotr32 _rotr
77: #else
78: #define rotl32(x,n) (((x) << n) | ((x) >> (32 - n)))
79: #define rotr32(x,n) (((x) >> n) | ((x) << (32 - n)))
80: #endif
81:
82: #if !defined(bswap_32)
1.1.1.5 ! root 83: #define bswap_32(x) ((rotr32((x), 24) & 0x00ff00ff) | (rotr32((x), 8) & 0xff00ff00))
1.1.1.2 root 84: #endif
85:
86: #if (PLATFORM_BYTE_ORDER == BRG_LITTLE_ENDIAN)
87: #define SWAP_BYTES
1.1 root 88: #else
1.1.1.2 root 89: #undef SWAP_BYTES
1.1 root 90: #endif
91:
1.1.1.2 root 92: #if defined(SWAP_BYTES)
93: #define bsw_32(p,n) \
94: { int _i = (n); while(_i--) ((sha1_32t*)p)[_i] = bswap_32(((sha1_32t*)p)[_i]); }
95: #else
96: #define bsw_32(p,n)
97: #endif
1.1 root 98:
1.1.1.2 root 99: #define SHA1_MASK (SHA1_BLOCK_SIZE - 1)
1.1 root 100:
1.1.1.2 root 101: #if 0
1.1 root 102:
1.1.1.2 root 103: #define ch(x,y,z) (((x) & (y)) ^ (~(x) & (z)))
104: #define parity(x,y,z) ((x) ^ (y) ^ (z))
105: #define maj(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
106:
107: #else /* Discovered by Rich Schroeppel and Colin Plumb */
1.1 root 108:
1.1.1.2 root 109: #define ch(x,y,z) ((z) ^ ((x) & ((y) ^ (z))))
110: #define parity(x,y,z) ((x) ^ (y) ^ (z))
111: #define maj(x,y,z) (((x) & (y)) | ((z) & ((x) ^ (y))))
1.1 root 112:
1.1.1.2 root 113: #endif
114:
115: /* Compile 64 bytes of hash data into SHA1 context. Note */
116: /* that this routine assumes that the byte order in the */
117: /* ctx->wbuf[] at this point is in such an order that low */
118: /* address bytes in the ORIGINAL byte stream will go in */
119: /* this buffer to the high end of 32-bit words on BOTH big */
120: /* and little endian systems */
121:
122: #ifdef ARRAY
123: #define q(v,n) v[n]
124: #else
125: #define q(v,n) v##n
126: #endif
127:
128: #define one_cycle(v,a,b,c,d,e,f,k,h) \
129: q(v,e) += rotr32(q(v,a),27) + \
130: f(q(v,b),q(v,c),q(v,d)) + k + h; \
131: q(v,b) = rotr32(q(v,b), 2)
132:
133: #define five_cycle(v,f,k,i) \
134: one_cycle(v, 0,1,2,3,4, f,k,hf(i )); \
135: one_cycle(v, 4,0,1,2,3, f,k,hf(i+1)); \
136: one_cycle(v, 3,4,0,1,2, f,k,hf(i+2)); \
137: one_cycle(v, 2,3,4,0,1, f,k,hf(i+3)); \
138: one_cycle(v, 1,2,3,4,0, f,k,hf(i+4))
139:
140: void sha1_compile(sha1_ctx ctx[1])
141: { sha1_32t *w = ctx->wbuf;
142:
143: #ifdef ARRAY
144: sha1_32t v[5];
145: memcpy(v, ctx->hash, 5 * sizeof(sha1_32t));
146: #else
147: sha1_32t v0, v1, v2, v3, v4;
148: v0 = ctx->hash[0]; v1 = ctx->hash[1];
149: v2 = ctx->hash[2]; v3 = ctx->hash[3];
150: v4 = ctx->hash[4];
151: #endif
152:
153: #define hf(i) w[i]
154:
155: five_cycle(v, ch, 0x5a827999, 0);
156: five_cycle(v, ch, 0x5a827999, 5);
157: five_cycle(v, ch, 0x5a827999, 10);
158: one_cycle(v,0,1,2,3,4, ch, 0x5a827999, hf(15)); \
159:
160: #undef hf
161: #define hf(i) (w[(i) & 15] = rotl32( \
162: w[((i) + 13) & 15] ^ w[((i) + 8) & 15] \
163: ^ w[((i) + 2) & 15] ^ w[(i) & 15], 1))
164:
165: one_cycle(v,4,0,1,2,3, ch, 0x5a827999, hf(16));
166: one_cycle(v,3,4,0,1,2, ch, 0x5a827999, hf(17));
167: one_cycle(v,2,3,4,0,1, ch, 0x5a827999, hf(18));
168: one_cycle(v,1,2,3,4,0, ch, 0x5a827999, hf(19));
169:
170: five_cycle(v, parity, 0x6ed9eba1, 20);
171: five_cycle(v, parity, 0x6ed9eba1, 25);
172: five_cycle(v, parity, 0x6ed9eba1, 30);
173: five_cycle(v, parity, 0x6ed9eba1, 35);
174:
175: five_cycle(v, maj, 0x8f1bbcdc, 40);
176: five_cycle(v, maj, 0x8f1bbcdc, 45);
177: five_cycle(v, maj, 0x8f1bbcdc, 50);
178: five_cycle(v, maj, 0x8f1bbcdc, 55);
179:
180: five_cycle(v, parity, 0xca62c1d6, 60);
181: five_cycle(v, parity, 0xca62c1d6, 65);
182: five_cycle(v, parity, 0xca62c1d6, 70);
183: five_cycle(v, parity, 0xca62c1d6, 75);
184:
185: #ifdef ARRAY
186: ctx->hash[0] += v[0]; ctx->hash[1] += v[1];
187: ctx->hash[2] += v[2]; ctx->hash[3] += v[3];
188: ctx->hash[4] += v[4];
189: #else
190: ctx->hash[0] += v0; ctx->hash[1] += v1;
191: ctx->hash[2] += v2; ctx->hash[3] += v3;
192: ctx->hash[4] += v4;
193: #endif
194: }
195:
196: void sha1_begin(sha1_ctx ctx[1])
1.1 root 197: {
1.1.1.2 root 198: ctx->count[0] = ctx->count[1] = 0;
199: ctx->hash[0] = 0x67452301;
200: ctx->hash[1] = 0xefcdab89;
201: ctx->hash[2] = 0x98badcfe;
202: ctx->hash[3] = 0x10325476;
203: ctx->hash[4] = 0xc3d2e1f0;
1.1 root 204: }
205:
1.1.1.2 root 206: /* SHA1 hash data in an array of bytes into hash buffer and */
207: /* call the hash_compile function as required. */
1.1 root 208:
1.1.1.2 root 209: void sha1_hash(const unsigned char data[], unsigned __int32 len, sha1_ctx ctx[1])
210: { sha1_32t pos = (sha1_32t)(ctx->count[0] & SHA1_MASK),
211: space = SHA1_BLOCK_SIZE - pos;
212: const unsigned char *sp = data;
1.1 root 213:
1.1.1.2 root 214: if((ctx->count[0] += len) < len)
215: ++(ctx->count[1]);
1.1 root 216:
1.1.1.2 root 217: while(len >= space) /* tranfer whole blocks if possible */
1.1 root 218: {
1.1.1.2 root 219: memcpy(((unsigned char*)ctx->wbuf) + pos, sp, space);
220: sp += space; len -= space; space = SHA1_BLOCK_SIZE; pos = 0;
221: bsw_32(ctx->wbuf, SHA1_BLOCK_SIZE >> 2);
222: sha1_compile(ctx);
1.1 root 223: }
224:
1.1.1.2 root 225: memcpy(((unsigned char*)ctx->wbuf) + pos, sp, len);
226: }
1.1 root 227:
1.1.1.2 root 228: /* SHA1 final padding and digest calculation */
1.1 root 229:
1.1.1.2 root 230: void sha1_end(unsigned char hval[], sha1_ctx ctx[1])
231: { sha1_32t i = (sha1_32t)(ctx->count[0] & SHA1_MASK);
1.1 root 232:
1.1.1.2 root 233: /* put bytes in the buffer in an order in which references to */
234: /* 32-bit words will put bytes with lower addresses into the */
235: /* top of 32 bit words on BOTH big and little endian machines */
236: bsw_32(ctx->wbuf, (i + 3) >> 2);
237:
238: /* we now need to mask valid bytes and add the padding which is */
239: /* a single 1 bit and as many zero bits as necessary. Note that */
240: /* we can always add the first padding byte here because the */
241: /* buffer always has at least one empty slot */
242: ctx->wbuf[i >> 2] &= 0xffffff80 << 8 * (~i & 3);
243: ctx->wbuf[i >> 2] |= 0x00000080 << 8 * (~i & 3);
244:
245: /* we need 9 or more empty positions, one for the padding byte */
246: /* (above) and eight for the length count. If there is not */
247: /* enough space, pad and empty the buffer */
248: if(i > SHA1_BLOCK_SIZE - 9)
1.1 root 249: {
1.1.1.2 root 250: if(i < 60) ctx->wbuf[15] = 0;
251: sha1_compile(ctx);
252: i = 0;
1.1 root 253: }
1.1.1.2 root 254: else /* compute a word index for the empty buffer positions */
255: i = (i >> 2) + 1;
256:
257: while(i < 14) /* and zero pad all but last two positions */
258: ctx->wbuf[i++] = 0;
259:
260: /* the following 32-bit length fields are assembled in the */
261: /* wrong byte order on little endian machines but this is */
262: /* corrected later since they are only ever used as 32-bit */
263: /* word values. */
264: ctx->wbuf[14] = (ctx->count[1] << 3) | (ctx->count[0] >> 29);
265: ctx->wbuf[15] = ctx->count[0] << 3;
266: sha1_compile(ctx);
267:
268: /* extract the hash value as bytes in case the hash buffer is */
269: /* misaligned for 32-bit words */
270: for(i = 0; i < SHA1_DIGEST_SIZE; ++i)
271: hval[i] = (unsigned char)(ctx->hash[i >> 2] >> (8 * (~i & 3)));
272: }
1.1 root 273:
1.1.1.2 root 274: void sha1(unsigned char hval[], const unsigned char data[], unsigned __int32 len)
275: { sha1_ctx cx[1];
1.1 root 276:
1.1.1.2 root 277: sha1_begin(cx); sha1_hash(data, len, cx); sha1_end(hval, cx);
1.1 root 278: }
1.1.1.2 root 279:
280: #if defined(__cplusplus)
281: }
282: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.