]>
git.saurik.com Git - apple/security.git/blob - SecureTransport/sha.c
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
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
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.
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 */
24 /* NIST's proposed modification to SHA of 7/11/94 may be */
25 /* activated by defining USE_MODIFIED_SHA */
32 /* SHA f()-functions */
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)
41 #define CONST1 0x5a827999L
42 #define CONST2 0x6ed9eba1L
43 #define CONST3 0x8f1bbcdcL
44 #define CONST4 0xca62c1d6L
48 #define ROT32(x,n) ((x << n) | (x >> (32 - n)))
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
54 /* do SHA transformation */
56 static void sha_transform(SHA_INFO
*sha_info
)
59 LONG temp
, A
, B
, C
, D
, E
, W
[80];
61 for (i
= 0; i
< 16; ++i
) {
62 W
[i
] = sha_info
->data
[i
];
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 */
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];
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);
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);
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);
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
) {
99 for (i
= 20; i
< 40; ++i
) {
102 for (i
= 40; i
< 60; ++i
) {
105 for (i
= 60; i
< 80; ++i
) {
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
;
116 /* HACK: OS X #defines LITTLE_ENDIAN (to 1234) in many places.... */
124 /* change endianness of data */
126 static void byte_reverse(LONG
*buffer
, int count
)
131 count
/= sizeof(LONG
);
132 cp
= (BYTE
*) buffer
;
133 for (i
= 0; i
< count
; ++i
) {
146 #endif /* LITTLE_ENDIAN */
148 /* initialize the SHA digest */
150 void sha_init(SHA_INFO
*sha_info
)
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;
161 /* update the SHA digest */
163 void sha_update(SHA_INFO
*sha_info
, BYTE
*buffer
, int count
)
165 if ((sha_info
->count_lo
+ ((LONG
) count
<< 3)) < sha_info
->count_lo
) {
166 ++sha_info
->count_hi
;
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
);
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
;
179 memcpy(sha_info
->data
, buffer
, count
);
182 /* finish computing the SHA digest */
184 void sha_final(SHA_INFO
*sha_info
)
187 LONG lo_bit_count
, hi_bit_count
;
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;
194 memset((BYTE
*) &sha_info
->data
+ count
, 0, 64 - count
);
196 byte_reverse(sha_info
->data
, SHA_BLOCKSIZE
);
197 #endif /* LITTLE_ENDIAN */
198 sha_transform(sha_info
);
199 memset(&sha_info
->data
, 0, 56);
201 memset((BYTE
*) &sha_info
->data
+ count
, 0, 56 - count
);
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
);
210 byte_reverse(sha_info
->digest
, SHA_DIGESTSIZE
);
211 #endif /* LITTLE_ENDIAN */
214 /* compute the SHA digest of a FILE stream */
216 #define BLOCK_SIZE 8192
218 void sha_stream(SHA_INFO
*sha_info
, FILE *fin
)
221 BYTE data
[BLOCK_SIZE
];
224 while ((i
= fread(data
, 1, BLOCK_SIZE
, fin
)) > 0) {
225 sha_update(sha_info
, data
, i
);
230 /* print a SHA digest */
232 void sha_print(SHA_INFO
*sha_info
)
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]);