]> git.saurik.com Git - apple/security.git/blob - SecureTransport/sha.c
Security-30.1.tar.gz
[apple/security.git] / SecureTransport / sha.c
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 /* NIST Secure Hash Algorithm */
20 /* heavily modified by Uwe Hollerbach uh@alumni.caltech edu */
21 /* from Peter C. Gutmann's implementation as found in */
22 /* Applied Cryptography by Bruce Schneier */
23
24 /* NIST's proposed modification to SHA of 7/11/94 may be */
25 /* activated by defining USE_MODIFIED_SHA */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include "st_sha.h"
31
32 /* SHA f()-functions */
33
34 #define f1(x,y,z) ((x & y) | (~x & z))
35 #define f2(x,y,z) (x ^ y ^ z)
36 #define f3(x,y,z) ((x & y) | (x & z) | (y & z))
37 #define f4(x,y,z) (x ^ y ^ z)
38
39 /* SHA constants */
40
41 #define CONST1 0x5a827999L
42 #define CONST2 0x6ed9eba1L
43 #define CONST3 0x8f1bbcdcL
44 #define CONST4 0xca62c1d6L
45
46 /* 32-bit rotate */
47
48 #define ROT32(x,n) ((x << n) | (x >> (32 - n)))
49
50 #define FUNC(n,i) \
51 temp = ROT32(A,5) + f##n(B,C,D) + E + W[i] + CONST##n; \
52 E = D; D = C; C = ROT32(B,30); B = A; A = temp
53
54 /* do SHA transformation */
55
56 static void sha_transform(SHA_INFO *sha_info)
57 {
58 int i;
59 LONG temp, A, B, C, D, E, W[80];
60
61 for (i = 0; i < 16; ++i) {
62 W[i] = sha_info->data[i];
63 }
64 for (i = 16; i < 80; ++i) {
65 W[i] = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
66 #ifdef USE_MODIFIED_SHA
67 W[i] = ROT32(W[i], 1);
68 #endif /* USE_MODIFIED_SHA */
69 }
70 A = sha_info->digest[0];
71 B = sha_info->digest[1];
72 C = sha_info->digest[2];
73 D = sha_info->digest[3];
74 E = sha_info->digest[4];
75 #ifdef UNROLL_LOOPS
76 FUNC(1, 0); FUNC(1, 1); FUNC(1, 2); FUNC(1, 3); FUNC(1, 4);
77 FUNC(1, 5); FUNC(1, 6); FUNC(1, 7); FUNC(1, 8); FUNC(1, 9);
78 FUNC(1,10); FUNC(1,11); FUNC(1,12); FUNC(1,13); FUNC(1,14);
79 FUNC(1,15); FUNC(1,16); FUNC(1,17); FUNC(1,18); FUNC(1,19);
80
81 FUNC(2,20); FUNC(2,21); FUNC(2,22); FUNC(2,23); FUNC(2,24);
82 FUNC(2,25); FUNC(2,26); FUNC(2,27); FUNC(2,28); FUNC(2,29);
83 FUNC(2,30); FUNC(2,31); FUNC(2,32); FUNC(2,33); FUNC(2,34);
84 FUNC(2,35); FUNC(2,36); FUNC(2,37); FUNC(2,38); FUNC(2,39);
85
86 FUNC(3,40); FUNC(3,41); FUNC(3,42); FUNC(3,43); FUNC(3,44);
87 FUNC(3,45); FUNC(3,46); FUNC(3,47); FUNC(3,48); FUNC(3,49);
88 FUNC(3,50); FUNC(3,51); FUNC(3,52); FUNC(3,53); FUNC(3,54);
89 FUNC(3,55); FUNC(3,56); FUNC(3,57); FUNC(3,58); FUNC(3,59);
90
91 FUNC(4,60); FUNC(4,61); FUNC(4,62); FUNC(4,63); FUNC(4,64);
92 FUNC(4,65); FUNC(4,66); FUNC(4,67); FUNC(4,68); FUNC(4,69);
93 FUNC(4,70); FUNC(4,71); FUNC(4,72); FUNC(4,73); FUNC(4,74);
94 FUNC(4,75); FUNC(4,76); FUNC(4,77); FUNC(4,78); FUNC(4,79);
95 #else /* !UNROLL_LOOPS */
96 for (i = 0; i < 20; ++i) {
97 FUNC(1,i);
98 }
99 for (i = 20; i < 40; ++i) {
100 FUNC(2,i);
101 }
102 for (i = 40; i < 60; ++i) {
103 FUNC(3,i);
104 }
105 for (i = 60; i < 80; ++i) {
106 FUNC(4,i);
107 }
108 #endif /* !UNROLL_LOOPS */
109 sha_info->digest[0] += A;
110 sha_info->digest[1] += B;
111 sha_info->digest[2] += C;
112 sha_info->digest[3] += D;
113 sha_info->digest[4] += E;
114 }
115
116 /* HACK: OS X #defines LITTLE_ENDIAN (to 1234) in many places.... */
117 #ifdef __APPLE__
118 #undef LITTLE_ENDIAN
119 #endif
120 /* end hack */
121
122 #ifdef LITTLE_ENDIAN
123
124 /* change endianness of data */
125
126 static void byte_reverse(LONG *buffer, int count)
127 {
128 int i;
129 BYTE ct[4], *cp;
130
131 count /= sizeof(LONG);
132 cp = (BYTE *) buffer;
133 for (i = 0; i < count; ++i) {
134 ct[0] = cp[0];
135 ct[1] = cp[1];
136 ct[2] = cp[2];
137 ct[3] = cp[3];
138 cp[0] = ct[3];
139 cp[1] = ct[2];
140 cp[2] = ct[1];
141 cp[3] = ct[0];
142 cp += sizeof(LONG);
143 }
144 }
145
146 #endif /* LITTLE_ENDIAN */
147
148 /* initialize the SHA digest */
149
150 void sha_init(SHA_INFO *sha_info)
151 {
152 sha_info->digest[0] = 0x67452301L;
153 sha_info->digest[1] = 0xefcdab89L;
154 sha_info->digest[2] = 0x98badcfeL;
155 sha_info->digest[3] = 0x10325476L;
156 sha_info->digest[4] = 0xc3d2e1f0L;
157 sha_info->count_lo = 0L;
158 sha_info->count_hi = 0L;
159 }
160
161 /* update the SHA digest */
162
163 void sha_update(SHA_INFO *sha_info, BYTE *buffer, int count)
164 {
165 if ((sha_info->count_lo + ((LONG) count << 3)) < sha_info->count_lo) {
166 ++sha_info->count_hi;
167 }
168 sha_info->count_lo += (LONG) count << 3;
169 sha_info->count_hi += (LONG) count >> 29;
170 while (count >= SHA_BLOCKSIZE) {
171 memcpy(sha_info->data, buffer, SHA_BLOCKSIZE);
172 #ifdef LITTLE_ENDIAN
173 byte_reverse(sha_info->data, SHA_BLOCKSIZE);
174 #endif /* LITTLE_ENDIAN */
175 sha_transform(sha_info);
176 buffer += SHA_BLOCKSIZE;
177 count -= SHA_BLOCKSIZE;
178 }
179 memcpy(sha_info->data, buffer, count);
180 }
181
182 /* finish computing the SHA digest */
183
184 void sha_final(SHA_INFO *sha_info)
185 {
186 int count;
187 LONG lo_bit_count, hi_bit_count;
188
189 lo_bit_count = sha_info->count_lo;
190 hi_bit_count = sha_info->count_hi;
191 count = (int) ((lo_bit_count >> 3) & 0x3f);
192 ((BYTE *) sha_info->data)[count++] = 0x80;
193 if (count > 56) {
194 memset((BYTE *) &sha_info->data + count, 0, 64 - count);
195 #ifdef LITTLE_ENDIAN
196 byte_reverse(sha_info->data, SHA_BLOCKSIZE);
197 #endif /* LITTLE_ENDIAN */
198 sha_transform(sha_info);
199 memset(&sha_info->data, 0, 56);
200 } else {
201 memset((BYTE *) &sha_info->data + count, 0, 56 - count);
202 }
203 #ifdef LITTLE_ENDIAN
204 byte_reverse(sha_info->data, SHA_BLOCKSIZE);
205 #endif /* LITTLE_ENDIAN */
206 sha_info->data[14] = hi_bit_count;
207 sha_info->data[15] = lo_bit_count;
208 sha_transform(sha_info);
209 #ifdef LITTLE_ENDIAN
210 byte_reverse(sha_info->digest, SHA_DIGESTSIZE);
211 #endif /* LITTLE_ENDIAN */
212 }
213
214 /* compute the SHA digest of a FILE stream */
215
216 #define BLOCK_SIZE 8192
217
218 void sha_stream(SHA_INFO *sha_info, FILE *fin)
219 {
220 int i;
221 BYTE data[BLOCK_SIZE];
222
223 sha_init(sha_info);
224 while ((i = fread(data, 1, BLOCK_SIZE, fin)) > 0) {
225 sha_update(sha_info, data, i);
226 }
227 sha_final(sha_info);
228 }
229
230 /* print a SHA digest */
231
232 void sha_print(SHA_INFO *sha_info)
233 {
234 printf("%08lx %08lx %08lx %08lx %08lx\n",
235 sha_info->digest[0], sha_info->digest[1], sha_info->digest[2],
236 sha_info->digest[3], sha_info->digest[4]);
237 }