2 /* --------------------------------- SHS.CC ------------------------------- */
5 * NIST proposed Secure Hash Standard.
7 * Written 2 September 1992, Peter C. Gutmann.
8 * This implementation placed in the public domain.
10 * Comments to pgut1@cs.aukuni.ac.nz
16 /* The SHS f()-functions */
18 #define f1(x,y,z) ( ( x & y ) | ( ~x & z ) ) /* Rounds 0-19 */
19 #define f2(x,y,z) ( x ^ y ^ z ) /* Rounds 20-39 */
20 #define f3(x,y,z) ( ( x & y ) | ( x & z ) | ( y & z ) ) /* Rounds 40-59 */
21 #define f4(x,y,z) ( x ^ y ^ z ) /* Rounds 60-79 */
23 /* The SHS Mysterious Constants */
25 #define K1 0x5A827999L /* Rounds 0-19 */
26 #define K2 0x6ED9EBA1L /* Rounds 20-39 */
27 #define K3 0x8F1BBCDCL /* Rounds 40-59 */
28 #define K4 0xCA62C1D6L /* Rounds 60-79 */
30 /* SHS initial values */
32 #define h0init 0x67452301L
33 #define h1init 0xEFCDAB89L
34 #define h2init 0x98BADCFEL
35 #define h3init 0x10325476L
36 #define h4init 0xC3D2E1F0L
38 /* 32-bit rotate - kludged with shifts */
40 #define S(n,X) ((X << n) | (X >> (32 - n)))
42 /* The initial expanding function */
44 #define expand(count) W [count] = W [count - 3] ^ W [count - 8] ^ W [count - 14] ^ W [count - 16]
46 /* The four SHS sub-rounds */
48 #define subRound1(count) \
50 temp = S (5, A) + f1 (B, C, D) + E + W [count] + K1; \
58 #define subRound2(count) \
60 temp = S (5, A) + f2 (B, C, D) + E + W [count] + K2; \
68 #define subRound3(count) \
70 temp = S (5, A) + f3 (B, C, D) + E + W [count] + K3; \
78 #define subRound4(count) \
80 temp = S (5, A) + f4 (B, C, D) + E + W [count] + K4; \
88 /* The two buffers of 5 32-bit words */
90 LONG h0, h1, h2, h3, h4;
93 local void byteReverse OF((LONG *buffer, int byteCount));
94 void shsTransform OF((SHS_INFO *shsInfo));
96 /* Initialize the SHS values */
98 void shsInit (SHS_INFO *shsInfo)
100 /* Set the h-vars to their initial values */
101 shsInfo->digest [0] = h0init;
102 shsInfo->digest [1] = h1init;
103 shsInfo->digest [2] = h2init;
104 shsInfo->digest [3] = h3init;
105 shsInfo->digest [4] = h4init;
107 /* Initialise bit count */
108 shsInfo->countLo = shsInfo->countHi = 0L;
112 * Perform the SHS transformation. Note that this code, like MD5, seems to
113 * break some optimizing compilers - it may be necessary to split it into
114 * sections, eg based on the four subrounds
117 void shsTransform (SHS_INFO *shsInfo)
122 /* Step A. Copy the data buffer into the local work buffer */
123 for (i = 0; i < 16; i++)
124 W [i] = shsInfo->data [i];
126 /* Step B. Expand the 16 words into 64 temporary data words */
127 expand (16); expand (17); expand (18); expand (19); expand (20);
128 expand (21); expand (22); expand (23); expand (24); expand (25);
129 expand (26); expand (27); expand (28); expand (29); expand (30);
130 expand (31); expand (32); expand (33); expand (34); expand (35);
131 expand (36); expand (37); expand (38); expand (39); expand (40);
132 expand (41); expand (42); expand (43); expand (44); expand (45);
133 expand (46); expand (47); expand (48); expand (49); expand (50);
134 expand (51); expand (52); expand (53); expand (54); expand (55);
135 expand (56); expand (57); expand (58); expand (59); expand (60);
136 expand (61); expand (62); expand (63); expand (64); expand (65);
137 expand (66); expand (67); expand (68); expand (69); expand (70);
138 expand (71); expand (72); expand (73); expand (74); expand (75);
139 expand (76); expand (77); expand (78); expand (79);
141 /* Step C. Set up first buffer */
142 A = shsInfo->digest [0];
143 B = shsInfo->digest [1];
144 C = shsInfo->digest [2];
145 D = shsInfo->digest [3];
146 E = shsInfo->digest [4];
148 /* Step D. Serious mangling, divided into four sub-rounds */
149 subRound1 (0); subRound1 (1); subRound1 (2); subRound1 (3);
150 subRound1 (4); subRound1 (5); subRound1 (6); subRound1 (7);
151 subRound1 (8); subRound1 (9); subRound1 (10); subRound1 (11);
152 subRound1 (12); subRound1 (13); subRound1 (14); subRound1 (15);
153 subRound1 (16); subRound1 (17); subRound1 (18); subRound1 (19);
155 subRound2 (20); subRound2 (21); subRound2 (22); subRound2 (23);
156 subRound2 (24); subRound2 (25); subRound2 (26); subRound2 (27);
157 subRound2 (28); subRound2 (29); subRound2 (30); subRound2 (31);
158 subRound2 (32); subRound2 (33); subRound2 (34); subRound2 (35);
159 subRound2 (36); subRound2 (37); subRound2 (38); subRound2 (39);
161 subRound3 (40); subRound3 (41); subRound3 (42); subRound3 (43);
162 subRound3 (44); subRound3 (45); subRound3 (46); subRound3 (47);
163 subRound3 (48); subRound3 (49); subRound3 (50); subRound3 (51);
164 subRound3 (52); subRound3 (53); subRound3 (54); subRound3 (55);
165 subRound3 (56); subRound3 (57); subRound3 (58); subRound3 (59);
167 subRound4 (60); subRound4 (61); subRound4 (62); subRound4 (63);
168 subRound4 (64); subRound4 (65); subRound4 (66); subRound4 (67);
169 subRound4 (68); subRound4 (69); subRound4 (70); subRound4 (71);
170 subRound4 (72); subRound4 (73); subRound4 (74); subRound4 (75);
171 subRound4 (76); subRound4 (77); subRound4 (78); subRound4 (79);
173 /* Step E. Build message digest */
174 shsInfo->digest [0] += A;
175 shsInfo->digest [1] += B;
176 shsInfo->digest [2] += C;
177 shsInfo->digest [3] += D;
178 shsInfo->digest [4] += E;
181 local void byteReverse (LONG *buffer, int byteCount)
187 * Find out what the byte order is on this machine.
188 * Big endian is for machines that place the most significant byte
189 * first (eg. Sun SPARC). Little endian is for machines that place
190 * the least significant byte first (eg. VAX).
192 * We figure out the byte order by stuffing a 2 byte string into a
193 * short and examining the left byte. '@' = 0x40 and 'P' = 0x50
194 * If the left byte is the 'high' byte, then it is 'big endian'.
195 * If the left byte is the 'low' byte, then the machine is 'little
198 * -- Shawn A. Clifford (sac@eng.ufl.edu)
202 * Several bugs fixed -- Pat Myrto (pat@rwing.uucp)
205 if ((*(unsigned short *) ("@P") >> 8) == '@')
208 byteCount /= sizeof (LONG);
209 for (count = 0; count < byteCount; count++) {
210 value = (buffer [count] << 16) | (buffer [count] >> 16);
211 buffer [count] = ((value & 0xFF00FF00L) >> 8) | ((value & 0x00FF00FFL) << 8);
216 * Update SHS for a block of data. This code assumes that the buffer size is
217 * a multiple of SHS_BLOCKSIZE bytes long, which makes the code a lot more
218 * efficient since it does away with the need to handle partial blocks
219 * between calls to shsUpdate()
222 void shsUpdate (SHS_INFO *shsInfo, BYTE *buffer, int count)
224 /* Update bitcount */
225 if ((shsInfo->countLo + ((LONG) count << 3)) < shsInfo->countLo)
226 shsInfo->countHi++; /* Carry from low to high bitCount */
227 shsInfo->countLo += ((LONG) count << 3);
228 shsInfo->countHi += ((LONG) count >> 29);
230 /* Process data in SHS_BLOCKSIZE chunks */
231 while (count >= SHS_BLOCKSIZE) {
232 memcpy (shsInfo->data, buffer, SHS_BLOCKSIZE);
233 byteReverse (shsInfo->data, SHS_BLOCKSIZE);
234 shsTransform (shsInfo);
235 buffer += SHS_BLOCKSIZE;
236 count -= SHS_BLOCKSIZE;
240 * Handle any remaining bytes of data.
241 * This should only happen once on the final lot of data
243 memcpy (shsInfo->data, buffer, count);
246 void shsFinal (SHS_INFO *shsInfo)
249 LONG lowBitcount = shsInfo->countLo, highBitcount = shsInfo->countHi;
251 /* Compute number of bytes mod 64 */
252 count = (int) ((shsInfo->countLo >> 3) & 0x3F);
255 * Set the first char of padding to 0x80.
256 * This is safe since there is always at least one byte free
258 ((BYTE *) shsInfo->data) [count++] = 0x80;
260 /* Pad out to 56 mod 64 */
262 /* Two lots of padding: Pad the first block to 64 bytes */
263 memset ((BYTE *) shsInfo->data + count, 0, 64 - count);
264 byteReverse (shsInfo->data, SHS_BLOCKSIZE);
265 shsTransform (shsInfo);
267 /* Now fill the next block with 56 bytes */
268 memset (shsInfo->data, 0, 56);
270 /* Pad block to 56 bytes */
271 memset ((BYTE *) shsInfo->data + count, 0, 56 - count);
272 byteReverse (shsInfo->data, SHS_BLOCKSIZE);
274 /* Append length in bits and transform */
275 shsInfo->data [14] = highBitcount;
276 shsInfo->data [15] = lowBitcount;
278 shsTransform (shsInfo);
279 byteReverse (shsInfo->data, SHS_DIGESTSIZE);