summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/sha/sha_dgst.c
diff options
context:
space:
mode:
authorbeck <>1999-09-29 04:37:45 +0000
committerbeck <>1999-09-29 04:37:45 +0000
commitde8f24ea083384bb66b32ec105dc4743c5663cdf (patch)
tree1412176ae62a3cab2cf2b0b92150fcbceaac6092 /src/lib/libcrypto/sha/sha_dgst.c
parentcb929d29896bcb87c2a97417fbd03e50078fc178 (diff)
downloadopenbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.gz
openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.tar.bz2
openbsd-de8f24ea083384bb66b32ec105dc4743c5663cdf.zip
OpenSSL 0.9.4 merge
Diffstat (limited to 'src/lib/libcrypto/sha/sha_dgst.c')
-rw-r--r--src/lib/libcrypto/sha/sha_dgst.c250
1 files changed, 150 insertions, 100 deletions
diff --git a/src/lib/libcrypto/sha/sha_dgst.c b/src/lib/libcrypto/sha/sha_dgst.c
index 8ed533ea26..4df535360f 100644
--- a/src/lib/libcrypto/sha/sha_dgst.c
+++ b/src/lib/libcrypto/sha/sha_dgst.c
@@ -60,39 +60,44 @@
60#include <string.h> 60#include <string.h>
61#define SHA_0 61#define SHA_0
62#undef SHA_1 62#undef SHA_1
63#include "sha.h" 63#include <openssl/sha.h>
64#include "sha_locl.h" 64#include "sha_locl.h"
65#include <openssl/opensslv.h>
65 66
66char *SHA_version="SHA part of SSLeay 0.9.0b 29-Jun-1998"; 67#ifndef NO_SHA0
68char *SHA_version="SHA" OPENSSL_VERSION_PTEXT;
67 69
68/* Implemented from SHA-0 document - The Secure Hash Algorithm 70/* Implemented from SHA-0 document - The Secure Hash Algorithm
69 */ 71 */
70 72
71#define INIT_DATA_h0 (unsigned long)0x67452301L 73#define INIT_DATA_h0 0x67452301UL
72#define INIT_DATA_h1 (unsigned long)0xefcdab89L 74#define INIT_DATA_h1 0xefcdab89UL
73#define INIT_DATA_h2 (unsigned long)0x98badcfeL 75#define INIT_DATA_h2 0x98badcfeUL
74#define INIT_DATA_h3 (unsigned long)0x10325476L 76#define INIT_DATA_h3 0x10325476UL
75#define INIT_DATA_h4 (unsigned long)0xc3d2e1f0L 77#define INIT_DATA_h4 0xc3d2e1f0UL
76 78
77#define K_00_19 0x5a827999L 79#define K_00_19 0x5a827999UL
78#define K_20_39 0x6ed9eba1L 80#define K_20_39 0x6ed9eba1UL
79#define K_40_59 0x8f1bbcdcL 81#define K_40_59 0x8f1bbcdcUL
80#define K_60_79 0xca62c1d6L 82#define K_60_79 0xca62c1d6UL
81 83
82#ifndef NOPROTO 84static void sha_block(SHA_CTX *c, register SHA_LONG *p, int num);
83 void sha_block(SHA_CTX *c, register unsigned long *p, int num); 85
86#if !defined(B_ENDIAN) && defined(SHA_ASM)
87# define M_c2nl c2l
88# define M_p_c2nl p_c2l
89# define M_c2nl_p c2l_p
90# define M_p_c2nl_p p_c2l_p
91# define M_nl2c l2c
84#else 92#else
85 void sha_block(); 93# define M_c2nl c2nl
94# define M_p_c2nl p_c2nl
95# define M_c2nl_p c2nl_p
96# define M_p_c2nl_p p_c2nl_p
97# define M_nl2c nl2c
86#endif 98#endif
87 99
88#define M_c2nl c2nl 100void SHA_Init(SHA_CTX *c)
89#define M_p_c2nl p_c2nl
90#define M_c2nl_p c2nl_p
91#define M_p_c2nl_p p_c2nl_p
92#define M_nl2c nl2c
93
94void SHA_Init(c)
95SHA_CTX *c;
96 { 101 {
97 c->h0=INIT_DATA_h0; 102 c->h0=INIT_DATA_h0;
98 c->h1=INIT_DATA_h1; 103 c->h1=INIT_DATA_h1;
@@ -104,14 +109,12 @@ SHA_CTX *c;
104 c->num=0; 109 c->num=0;
105 } 110 }
106 111
107void SHA_Update(c, data, len) 112void SHA_Update(SHA_CTX *c, register const unsigned char *data,
108SHA_CTX *c; 113 unsigned long len)
109register unsigned char *data;
110unsigned long len;
111 { 114 {
112 register ULONG *p; 115 register SHA_LONG *p;
113 int ew,ec,sw,sc; 116 int ew,ec,sw,sc;
114 ULONG l; 117 SHA_LONG l;
115 118
116 if (len == 0) return; 119 if (len == 0) return;
117 120
@@ -139,7 +142,7 @@ unsigned long len;
139 } 142 }
140 len-=(SHA_CBLOCK-c->num); 143 len-=(SHA_CBLOCK-c->num);
141 144
142 sha_block(c,p,64); 145 sha_block(c,p,1);
143 c->num=0; 146 c->num=0;
144 /* drop through and do the rest */ 147 /* drop through and do the rest */
145 } 148 }
@@ -176,15 +179,15 @@ unsigned long len;
176 * copies it to a local array. I should be able to do this for 179 * copies it to a local array. I should be able to do this for
177 * the C version as well.... 180 * the C version as well....
178 */ 181 */
179#if 1 182#if SHA_LONG_LOG2==2
180#if defined(B_ENDIAN) || defined(SHA_ASM) 183#if defined(B_ENDIAN) || defined(SHA_ASM)
181 if ((((unsigned int)data)%sizeof(ULONG)) == 0) 184 if ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)
182 { 185 {
183 sw=len/SHA_CBLOCK; 186 sw=len/SHA_CBLOCK;
184 if (sw) 187 if (sw)
185 { 188 {
189 sha_block(c,(SHA_LONG *)data,sw);
186 sw*=SHA_CBLOCK; 190 sw*=SHA_CBLOCK;
187 sha_block(c,(ULONG *)data,sw);
188 data+=sw; 191 data+=sw;
189 len-=sw; 192 len-=sw;
190 } 193 }
@@ -196,35 +199,61 @@ unsigned long len;
196 p=c->data; 199 p=c->data;
197 while (len >= SHA_CBLOCK) 200 while (len >= SHA_CBLOCK)
198 { 201 {
199#if defined(B_ENDIAN) || defined(L_ENDIAN) 202#if SHA_LONG_LOG2==2
200 if (p != (unsigned long *)data) 203#if defined(B_ENDIAN) || defined(SHA_ASM)
204#define SHA_NO_TAIL_CODE
205 /*
206 * Basically we get here only when data happens
207 * to be unaligned.
208 */
209 if (p != (SHA_LONG *)data)
201 memcpy(p,data,SHA_CBLOCK); 210 memcpy(p,data,SHA_CBLOCK);
202 data+=SHA_CBLOCK; 211 data+=SHA_CBLOCK;
203# ifdef L_ENDIAN 212 sha_block(c,p=c->data,1);
204# ifndef SHA_ASM /* Will not happen */ 213 len-=SHA_CBLOCK;
205 for (sw=(SHA_LBLOCK/4); sw; sw--) 214#elif defined(L_ENDIAN)
215#define BE_COPY(dst,src,i) { \
216 l = ((SHA_LONG *)src)[i]; \
217 Endian_Reverse32(l); \
218 dst[i] = l; \
219 }
220 if ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)
206 { 221 {
207 Endian_Reverse32(p[0]); 222 for (sw=(SHA_LBLOCK/4); sw; sw--)
208 Endian_Reverse32(p[1]); 223 {
209 Endian_Reverse32(p[2]); 224 BE_COPY(p,data,0);
210 Endian_Reverse32(p[3]); 225 BE_COPY(p,data,1);
211 p+=4; 226 BE_COPY(p,data,2);
227 BE_COPY(p,data,3);
228 p+=4;
229 data += 4*sizeof(SHA_LONG);
230 }
231 sha_block(c,p=c->data,1);
232 len-=SHA_CBLOCK;
233 continue;
212 } 234 }
235#endif
236#endif
237#ifndef SHA_NO_TAIL_CODE
238 /*
239 * In addition to "sizeof(SHA_LONG)!= 4" case the
240 * following code covers unaligned access cases on
241 * little-endian machines.
242 * <appro@fy.chalmers.se>
243 */
213 p=c->data; 244 p=c->data;
214# endif 245 for (sw=(SHA_LBLOCK/4); sw; sw--)
215# endif
216#else
217 for (sw=(SHA_BLOCK/4); sw; sw--)
218 { 246 {
219 M_c2nl(data,l); *(p++)=l; 247 M_c2nl(data,l); p[0]=l;
220 M_c2nl(data,l); *(p++)=l; 248 M_c2nl(data,l); p[1]=l;
221 M_c2nl(data,l); *(p++)=l; 249 M_c2nl(data,l); p[2]=l;
222 M_c2nl(data,l); *(p++)=l; 250 M_c2nl(data,l); p[3]=l;
251 p+=4;
223 } 252 }
224 p=c->data; 253 p=c->data;
225#endif 254 sha_block(c,p,1);
226 sha_block(c,p,64);
227 len-=SHA_CBLOCK; 255 len-=SHA_CBLOCK;
256#endif
228 } 257 }
229 ec=(int)len; 258 ec=(int)len;
230 c->num=ec; 259 c->num=ec;
@@ -237,50 +266,61 @@ unsigned long len;
237 p[sw]=l; 266 p[sw]=l;
238 } 267 }
239 268
240void SHA_Transform(c,b) 269void SHA_Transform(SHA_CTX *c, unsigned char *b)
241SHA_CTX *c;
242unsigned char *b;
243 { 270 {
244 ULONG p[16]; 271 SHA_LONG p[SHA_LBLOCK];
245#if !defined(B_ENDIAN)
246 ULONG *q;
247 int i;
248#endif
249 272
250#if defined(B_ENDIAN) || defined(L_ENDIAN) 273#if SHA_LONG_LOG2==2
251 memcpy(p,b,64); 274#if defined(B_ENDIAN) || defined(SHA_ASM)
252#ifdef L_ENDIAN 275 memcpy(p,b,SHA_CBLOCK);
253 q=p; 276 sha_block(c,p,1);
254 for (i=(SHA_LBLOCK/4); i; i--) 277 return;
278#elif defined(L_ENDIAN)
279 if (((unsigned long)b%sizeof(SHA_LONG)) == 0)
255 { 280 {
256 Endian_Reverse32(q[0]); 281 SHA_LONG *q;
257 Endian_Reverse32(q[1]); 282 int i;
258 Endian_Reverse32(q[2]); 283
259 Endian_Reverse32(q[3]); 284 q=p;
260 q+=4; 285 for (i=(SHA_LBLOCK/4); i; i--)
286 {
287 unsigned long l;
288 BE_COPY(q,b,0); /* BE_COPY was defined above */
289 BE_COPY(q,b,1);
290 BE_COPY(q,b,2);
291 BE_COPY(q,b,3);
292 q+=4;
293 b+=4*sizeof(SHA_LONG);
294 }
295 sha_block(c,p,1);
296 return;
261 } 297 }
262#endif 298#endif
263#else 299#endif
264 q=p; 300#ifndef SHA_NO_TAIL_CODE /* defined above, see comment */
265 for (i=(SHA_LBLOCK/4); i; i--)
266 { 301 {
267 ULONG l; 302 SHA_LONG *q;
268 c2nl(b,l); *(q++)=l; 303 int i;
269 c2nl(b,l); *(q++)=l; 304
270 c2nl(b,l); *(q++)=l; 305 q=p;
271 c2nl(b,l); *(q++)=l; 306 for (i=(SHA_LBLOCK/4); i; i--)
272 } 307 {
308 SHA_LONG l;
309 c2nl(b,l); *(q++)=l;
310 c2nl(b,l); *(q++)=l;
311 c2nl(b,l); *(q++)=l;
312 c2nl(b,l); *(q++)=l;
313 }
314 sha_block(c,p,1);
315 }
273#endif 316#endif
274 sha_block(c,p,64);
275 } 317 }
276 318
277void sha_block(c, W, num) 319#ifndef SHA_ASM
278SHA_CTX *c; 320static void sha_block(SHA_CTX *c, register SHA_LONG *W, int num)
279register unsigned long *W;
280int num;
281 { 321 {
282 register ULONG A,B,C,D,E,T; 322 register SHA_LONG A,B,C,D,E,T;
283 ULONG X[16]; 323 SHA_LONG X[SHA_LBLOCK];
284 324
285 A=c->h0; 325 A=c->h0;
286 B=c->h1; 326 B=c->h1;
@@ -380,8 +420,7 @@ int num;
380 c->h3=(c->h3+B)&0xffffffffL; 420 c->h3=(c->h3+B)&0xffffffffL;
381 c->h4=(c->h4+C)&0xffffffffL; 421 c->h4=(c->h4+C)&0xffffffffL;
382 422
383 num-=64; 423 if (--num <= 0) break;
384 if (num <= 0) break;
385 424
386 A=c->h0; 425 A=c->h0;
387 B=c->h1; 426 B=c->h1;
@@ -389,17 +428,21 @@ int num;
389 D=c->h3; 428 D=c->h3;
390 E=c->h4; 429 E=c->h4;
391 430
392 W+=16; 431 W+=SHA_LBLOCK; /* Note! This can happen only when sizeof(SHA_LONG)
432 * is 4. Whenever it's not the actual case this
433 * function is never called with num larger than 1
434 * and we never advance down here.
435 * <appro@fy.chalmers.se>
436 */
393 } 437 }
394 } 438 }
439#endif
395 440
396void SHA_Final(md, c) 441void SHA_Final(unsigned char *md, SHA_CTX *c)
397unsigned char *md;
398SHA_CTX *c;
399 { 442 {
400 register int i,j; 443 register int i,j;
401 register ULONG l; 444 register SHA_LONG l;
402 register ULONG *p; 445 register SHA_LONG *p;
403 static unsigned char end[4]={0x80,0x00,0x00,0x00}; 446 static unsigned char end[4]={0x80,0x00,0x00,0x00};
404 unsigned char *cp=end; 447 unsigned char *cp=end;
405 448
@@ -419,14 +462,20 @@ SHA_CTX *c;
419 { 462 {
420 for (; i<SHA_LBLOCK; i++) 463 for (; i<SHA_LBLOCK; i++)
421 p[i]=0; 464 p[i]=0;
422 sha_block(c,p,64); 465 sha_block(c,p,1);
423 i=0; 466 i=0;
424 } 467 }
425 for (; i<(SHA_LBLOCK-2); i++) 468 for (; i<(SHA_LBLOCK-2); i++)
426 p[i]=0; 469 p[i]=0;
427 p[SHA_LBLOCK-2]=c->Nh; 470 p[SHA_LBLOCK-2]=c->Nh;
428 p[SHA_LBLOCK-1]=c->Nl; 471 p[SHA_LBLOCK-1]=c->Nl;
429 sha_block(c,p,64); 472#if SHA_LONG_LOG2==2
473#if !defined(B_ENDIAN) && defined(SHA_ASM)
474 Endian_Reverse32(p[SHA_LBLOCK-2]);
475 Endian_Reverse32(p[SHA_LBLOCK-1]);
476#endif
477#endif
478 sha_block(c,p,1);
430 cp=md; 479 cp=md;
431 l=c->h0; nl2c(l,cp); 480 l=c->h0; nl2c(l,cp);
432 l=c->h1; nl2c(l,cp); 481 l=c->h1; nl2c(l,cp);
@@ -434,9 +483,10 @@ SHA_CTX *c;
434 l=c->h3; nl2c(l,cp); 483 l=c->h3; nl2c(l,cp);
435 l=c->h4; nl2c(l,cp); 484 l=c->h4; nl2c(l,cp);
436 485
437 /* clear stuff, sha_block may be leaving some stuff on the stack
438 * but I'm not worried :-) */
439 c->num=0; 486 c->num=0;
440/* memset((char *)&c,0,sizeof(c));*/ 487 /* sha_block may be leaving some stuff on the stack
488 * but I'm not worried :-)
489 memset((void *)c,0,sizeof(SHA_CTX));
490 */
441 } 491 }
442 492#endif