OSDN Git Service

Jumbo patch:
[pf3gnuchains/gcc-fork.git] / libjava / gnu / gcj / io / shs.cc
1
2 /* --------------------------------- SHS.CC ------------------------------- */
3
4 /*
5  * NIST proposed Secure Hash Standard.
6  *
7  * Written 2 September 1992, Peter C. Gutmann.
8  * This implementation placed in the public domain.
9  *
10  * Comments to pgut1@cs.aukuni.ac.nz
11  */
12
13 #include <string.h>
14 #include "shs.h"
15
16 /* The SHS f()-functions */
17
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 */
22
23 /* The SHS Mysterious Constants */
24
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 */
29
30 /* SHS initial values */
31
32 #define h0init  0x67452301L
33 #define h1init  0xEFCDAB89L
34 #define h2init  0x98BADCFEL
35 #define h3init  0x10325476L
36 #define h4init  0xC3D2E1F0L
37
38 /* 32-bit rotate - kludged with shifts */
39
40 #define S(n,X)  ((X << n) | (X >> (32 - n)))
41
42 /* The initial expanding function */
43
44 #define expand(count)   W [count] = W [count - 3] ^ W [count - 8] ^ W [count - 14] ^ W [count - 16]
45
46 /* The four SHS sub-rounds */
47
48 #define subRound1(count)    \
49         { \
50                 temp = S (5, A) + f1 (B, C, D) + E + W [count] + K1; \
51                 E = D; \
52                 D = C; \
53                 C = S (30, B); \
54                 B = A; \
55                 A = temp; \
56         }
57
58 #define subRound2(count)    \
59         { \
60                 temp = S (5, A) + f2 (B, C, D) + E + W [count] + K2; \
61                 E = D; \
62                 D = C; \
63                 C = S (30, B); \
64                 B = A; \
65                 A = temp; \
66         }
67
68 #define subRound3(count)    \
69         { \
70                 temp = S (5, A) + f3 (B, C, D) + E + W [count] + K3; \
71                 E = D; \
72                 D = C; \
73                 C = S (30, B); \
74                 B = A; \
75                 A = temp; \
76         }
77
78 #define subRound4(count)    \
79         { \
80                 temp = S (5, A) + f4 (B, C, D) + E + W [count] + K4; \
81                 E = D; \
82                 D = C; \
83                 C = S (30, B); \
84                 B = A; \
85                 A = temp; \
86         }
87
88 /* The two buffers of 5 32-bit words */
89
90 LONG h0, h1, h2, h3, h4;
91 LONG A, B, C, D, E;
92
93 local void byteReverse OF((LONG *buffer, int byteCount));
94 void shsTransform OF((SHS_INFO *shsInfo));
95
96 /* Initialize the SHS values */
97
98 void shsInit (SHS_INFO *shsInfo)
99 {
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;
106
107         /* Initialise bit count */
108         shsInfo->countLo = shsInfo->countHi = 0L;
109 }
110
111 /*
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
115  */
116
117 void shsTransform (SHS_INFO *shsInfo)
118 {
119         LONG W [80], temp;
120         int i;
121
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];
125
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);
140
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];
147
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);
154
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);
160
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);
166
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);
172
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;
179 }
180
181 local void byteReverse (LONG *buffer, int byteCount)
182 {
183         LONG value;
184         int count;
185
186         /*
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).
191          *
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
196          * endian'.
197          *
198          *                          -- Shawn A. Clifford (sac@eng.ufl.edu)
199          */
200
201         /*
202          * Several bugs fixed       -- Pat Myrto (pat@rwing.uucp)
203          */
204
205         if ((*(unsigned short *) ("@P") >> 8) == '@')
206                 return;
207
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);
212         }
213 }
214
215 /*
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()
220  */
221
222 void shsUpdate (SHS_INFO *shsInfo, BYTE *buffer, int count)
223 {
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);
229
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;
237         }
238
239         /*
240          * Handle any remaining bytes of data.
241          * This should only happen once on the final lot of data
242          */
243         memcpy (shsInfo->data, buffer, count);
244 }
245
246 void shsFinal (SHS_INFO *shsInfo)
247 {
248         int count;
249         LONG lowBitcount = shsInfo->countLo, highBitcount = shsInfo->countHi;
250
251         /* Compute number of bytes mod 64 */
252         count = (int) ((shsInfo->countLo >> 3) & 0x3F);
253
254         /*
255          * Set the first char of padding to 0x80.
256          * This is safe since there is always at least one byte free
257          */
258         ((BYTE *) shsInfo->data) [count++] = 0x80;
259
260         /* Pad out to 56 mod 64 */
261         if (count > 56) {
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);
266
267                 /* Now fill the next block with 56 bytes */
268                 memset (shsInfo->data, 0, 56);
269         } else
270                 /* Pad block to 56 bytes */
271                 memset ((BYTE *) shsInfo->data + count, 0, 56 - count);
272         byteReverse (shsInfo->data, SHS_BLOCKSIZE);
273
274         /* Append length in bits and transform */
275         shsInfo->data [14] = highBitcount;
276         shsInfo->data [15] = lowBitcount;
277
278         shsTransform (shsInfo);
279         byteReverse (shsInfo->data, SHS_DIGESTSIZE);
280 }