summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/sha/sha1dgst.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/sha1dgst.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/sha1dgst.c')
-rw-r--r--src/lib/libcrypto/sha/sha1dgst.c244
1 files changed, 137 insertions, 107 deletions
diff --git a/src/lib/libcrypto/sha/sha1dgst.c b/src/lib/libcrypto/sha/sha1dgst.c
index 2b0ae1f0d4..66e885dd76 100644
--- a/src/lib/libcrypto/sha/sha1dgst.c
+++ b/src/lib/libcrypto/sha/sha1dgst.c
@@ -60,43 +60,35 @@
60#include <string.h> 60#include <string.h>
61#undef SHA_0 61#undef SHA_0
62#define SHA_1 62#define 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 *SHA1_version="SHA1 part of SSLeay 0.9.0b 29-Jun-1998"; 67#ifndef NO_SHA1
68char *SHA1_version="SHA1" OPENSSL_VERSION_PTEXT;
67 69
68/* Implemented from SHA-1 document - The Secure Hash Algorithm 70/* Implemented from SHA-1 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 84#ifdef SHA1_ASM
83# ifdef SHA1_ASM 85 void sha1_block_x86(SHA_CTX *c, register SHA_LONG *p, int num);
84 void sha1_block_x86(SHA_CTX *c, register unsigned long *p, int num); 86# define sha1_block(c,p,n) sha1_block_x86((c),(p),(n)*SHA_CBLOCK)
85# define sha1_block sha1_block_x86
86# else
87 void sha1_block(SHA_CTX *c, register unsigned long *p, int num);
88# endif
89#else 87#else
90# ifdef SHA1_ASM 88 static void sha1_block(SHA_CTX *c, register SHA_LONG *p, int num);
91 void sha1_block_x86();
92# define sha1_block sha1_block_x86
93# else
94 void sha1_block();
95# endif
96#endif 89#endif
97 90
98 91#if !defined(B_ENDIAN) && defined(SHA1_ASM)
99#if defined(L_ENDIAN) && defined(SHA1_ASM)
100# define M_c2nl c2l 92# define M_c2nl c2l
101# define M_p_c2nl p_c2l 93# define M_p_c2nl p_c2l
102# define M_c2nl_p c2l_p 94# define M_c2nl_p c2l_p
@@ -110,8 +102,7 @@ char *SHA1_version="SHA1 part of SSLeay 0.9.0b 29-Jun-1998";
110# define M_nl2c nl2c 102# define M_nl2c nl2c
111#endif 103#endif
112 104
113void SHA1_Init(c) 105void SHA1_Init(SHA_CTX *c)
114SHA_CTX *c;
115 { 106 {
116 c->h0=INIT_DATA_h0; 107 c->h0=INIT_DATA_h0;
117 c->h1=INIT_DATA_h1; 108 c->h1=INIT_DATA_h1;
@@ -123,14 +114,12 @@ SHA_CTX *c;
123 c->num=0; 114 c->num=0;
124 } 115 }
125 116
126void SHA1_Update(c, data, len) 117void SHA1_Update(SHA_CTX *c, register const unsigned char *data,
127SHA_CTX *c; 118 unsigned long len)
128register unsigned char *data;
129unsigned long len;
130 { 119 {
131 register ULONG *p; 120 register SHA_LONG *p;
132 int ew,ec,sw,sc; 121 int ew,ec,sw,sc;
133 ULONG l; 122 SHA_LONG l;
134 123
135 if (len == 0) return; 124 if (len == 0) return;
136 125
@@ -158,7 +147,7 @@ unsigned long len;
158 } 147 }
159 len-=(SHA_CBLOCK-c->num); 148 len-=(SHA_CBLOCK-c->num);
160 149
161 sha1_block(c,p,64); 150 sha1_block(c,p,1);
162 c->num=0; 151 c->num=0;
163 /* drop through and do the rest */ 152 /* drop through and do the rest */
164 } 153 }
@@ -195,15 +184,15 @@ unsigned long len;
195 * copies it to a local array. I should be able to do this for 184 * copies it to a local array. I should be able to do this for
196 * the C version as well.... 185 * the C version as well....
197 */ 186 */
198#if 1 187#if SHA_LONG_LOG2==2
199#if defined(B_ENDIAN) || defined(SHA1_ASM) 188#if defined(B_ENDIAN) || defined(SHA1_ASM)
200 if ((((unsigned int)data)%sizeof(ULONG)) == 0) 189 if ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)
201 { 190 {
202 sw=len/SHA_CBLOCK; 191 sw=len/SHA_CBLOCK;
203 if (sw) 192 if (sw)
204 { 193 {
194 sha1_block(c,(SHA_LONG *)data,sw);
205 sw*=SHA_CBLOCK; 195 sw*=SHA_CBLOCK;
206 sha1_block(c,(ULONG *)data,sw);
207 data+=sw; 196 data+=sw;
208 len-=sw; 197 len-=sw;
209 } 198 }
@@ -215,35 +204,61 @@ unsigned long len;
215 p=c->data; 204 p=c->data;
216 while (len >= SHA_CBLOCK) 205 while (len >= SHA_CBLOCK)
217 { 206 {
218#if defined(B_ENDIAN) || defined(L_ENDIAN) 207#if SHA_LONG_LOG2==2
219 if (p != (unsigned long *)data) 208#if defined(B_ENDIAN) || defined(SHA1_ASM)
209#define SHA_NO_TAIL_CODE
210 /*
211 * Basically we get here only when data happens
212 * to be unaligned.
213 */
214 if (p != (SHA_LONG *)data)
220 memcpy(p,data,SHA_CBLOCK); 215 memcpy(p,data,SHA_CBLOCK);
221 data+=SHA_CBLOCK; 216 data+=SHA_CBLOCK;
222# ifdef L_ENDIAN 217 sha1_block(c,p=c->data,1);
223# ifndef SHA1_ASM /* Will not happen */ 218 len-=SHA_CBLOCK;
224 for (sw=(SHA_LBLOCK/4); sw; sw--) 219#elif defined(L_ENDIAN)
220#define BE_COPY(dst,src,i) { \
221 l = ((SHA_LONG *)src)[i]; \
222 Endian_Reverse32(l); \
223 dst[i] = l; \
224 }
225 if ((((unsigned long)data)%sizeof(SHA_LONG)) == 0)
225 { 226 {
226 Endian_Reverse32(p[0]); 227 for (sw=(SHA_LBLOCK/4); sw; sw--)
227 Endian_Reverse32(p[1]); 228 {
228 Endian_Reverse32(p[2]); 229 BE_COPY(p,data,0);
229 Endian_Reverse32(p[3]); 230 BE_COPY(p,data,1);
230 p+=4; 231 BE_COPY(p,data,2);
232 BE_COPY(p,data,3);
233 p+=4;
234 data += 4*sizeof(SHA_LONG);
235 }
236 sha1_block(c,p=c->data,1);
237 len-=SHA_CBLOCK;
238 continue;
231 } 239 }
240#endif
241#endif
242#ifndef SHA_NO_TAIL_CODE
243 /*
244 * In addition to "sizeof(SHA_LONG)!= 4" case the
245 * following code covers unaligned access cases on
246 * little-endian machines.
247 * <appro@fy.chalmers.se>
248 */
232 p=c->data; 249 p=c->data;
233# endif 250 for (sw=(SHA_LBLOCK/4); sw; sw--)
234# endif
235#else
236 for (sw=(SHA_BLOCK/4); sw; sw--)
237 { 251 {
238 M_c2nl(data,l); *(p++)=l; 252 M_c2nl(data,l); p[0]=l;
239 M_c2nl(data,l); *(p++)=l; 253 M_c2nl(data,l); p[1]=l;
240 M_c2nl(data,l); *(p++)=l; 254 M_c2nl(data,l); p[2]=l;
241 M_c2nl(data,l); *(p++)=l; 255 M_c2nl(data,l); p[3]=l;
256 p+=4;
242 } 257 }
243 p=c->data; 258 p=c->data;
244#endif 259 sha1_block(c,p,1);
245 sha1_block(c,p,64);
246 len-=SHA_CBLOCK; 260 len-=SHA_CBLOCK;
261#endif
247 } 262 }
248 ec=(int)len; 263 ec=(int)len;
249 c->num=ec; 264 c->num=ec;
@@ -256,52 +271,61 @@ unsigned long len;
256 p[sw]=l; 271 p[sw]=l;
257 } 272 }
258 273
259void SHA1_Transform(c,b) 274void SHA1_Transform(SHA_CTX *c, unsigned char *b)
260SHA_CTX *c;
261unsigned char *b;
262 { 275 {
263 ULONG p[16]; 276 SHA_LONG p[SHA_LBLOCK];
264#ifndef B_ENDIAN
265 ULONG *q;
266 int i;
267#endif
268 277
269#if defined(B_ENDIAN) || defined(L_ENDIAN) 278#if SHA_LONG_LOG2==2
270 memcpy(p,b,64); 279#if defined(B_ENDIAN) || defined(SHA1_ASM)
271#ifdef L_ENDIAN 280 memcpy(p,b,SHA_CBLOCK);
272 q=p; 281 sha1_block(c,p,1);
273 for (i=(SHA_LBLOCK/4); i; i--) 282 return;
283#elif defined(L_ENDIAN)
284 if (((unsigned long)b%sizeof(SHA_LONG)) == 0)
274 { 285 {
275 Endian_Reverse32(q[0]); 286 SHA_LONG *q;
276 Endian_Reverse32(q[1]); 287 int i;
277 Endian_Reverse32(q[2]); 288
278 Endian_Reverse32(q[3]); 289 q=p;
279 q+=4; 290 for (i=(SHA_LBLOCK/4); i; i--)
291 {
292 unsigned long l;
293 BE_COPY(q,b,0); /* BE_COPY was defined above */
294 BE_COPY(q,b,1);
295 BE_COPY(q,b,2);
296 BE_COPY(q,b,3);
297 q+=4;
298 b+=4*sizeof(SHA_LONG);
299 }
300 sha1_block(c,p,1);
301 return;
280 } 302 }
281#endif 303#endif
282#else 304#endif
283 q=p; 305#ifndef SHA_NO_TAIL_CODE /* defined above, see comment */
284 for (i=(SHA_LBLOCK/4); i; i--)
285 { 306 {
286 ULONG l; 307 SHA_LONG *q;
287 c2nl(b,l); *(q++)=l; 308 int i;
288 c2nl(b,l); *(q++)=l; 309
289 c2nl(b,l); *(q++)=l; 310 q=p;
290 c2nl(b,l); *(q++)=l; 311 for (i=(SHA_LBLOCK/4); i; i--)
291 } 312 {
313 SHA_LONG l;
314 c2nl(b,l); *(q++)=l;
315 c2nl(b,l); *(q++)=l;
316 c2nl(b,l); *(q++)=l;
317 c2nl(b,l); *(q++)=l;
318 }
319 sha1_block(c,p,1);
320 }
292#endif 321#endif
293 sha1_block(c,p,64);
294 } 322 }
295 323
296#ifndef SHA1_ASM 324#ifndef SHA1_ASM
297 325static void sha1_block(SHA_CTX *c, register SHA_LONG *W, int num)
298void sha1_block(c, W, num)
299SHA_CTX *c;
300register unsigned long *W;
301int num;
302 { 326 {
303 register ULONG A,B,C,D,E,T; 327 register SHA_LONG A,B,C,D,E,T;
304 ULONG X[16]; 328 SHA_LONG X[SHA_LBLOCK];
305 329
306 A=c->h0; 330 A=c->h0;
307 B=c->h1; 331 B=c->h1;
@@ -401,8 +425,7 @@ int num;
401 c->h3=(c->h3+B)&0xffffffffL; 425 c->h3=(c->h3+B)&0xffffffffL;
402 c->h4=(c->h4+C)&0xffffffffL; 426 c->h4=(c->h4+C)&0xffffffffL;
403 427
404 num-=64; 428 if (--num <= 0) break;
405 if (num <= 0) break;
406 429
407 A=c->h0; 430 A=c->h0;
408 B=c->h1; 431 B=c->h1;
@@ -410,18 +433,21 @@ int num;
410 D=c->h3; 433 D=c->h3;
411 E=c->h4; 434 E=c->h4;
412 435
413 W+=16; 436 W+=SHA_LBLOCK; /* Note! This can happen only when sizeof(SHA_LONG)
437 * is 4. Whenever it's not the actual case this
438 * function is never called with num larger than 1
439 * and we never advance down here.
440 * <appro@fy.chalmers.se>
441 */
414 } 442 }
415 } 443 }
416#endif 444#endif
417 445
418void SHA1_Final(md, c) 446void SHA1_Final(unsigned char *md, SHA_CTX *c)
419unsigned char *md;
420SHA_CTX *c;
421 { 447 {
422 register int i,j; 448 register int i,j;
423 register ULONG l; 449 register SHA_LONG l;
424 register ULONG *p; 450 register SHA_LONG *p;
425 static unsigned char end[4]={0x80,0x00,0x00,0x00}; 451 static unsigned char end[4]={0x80,0x00,0x00,0x00};
426 unsigned char *cp=end; 452 unsigned char *cp=end;
427 453
@@ -441,18 +467,20 @@ SHA_CTX *c;
441 { 467 {
442 for (; i<SHA_LBLOCK; i++) 468 for (; i<SHA_LBLOCK; i++)
443 p[i]=0; 469 p[i]=0;
444 sha1_block(c,p,64); 470 sha1_block(c,p,1);
445 i=0; 471 i=0;
446 } 472 }
447 for (; i<(SHA_LBLOCK-2); i++) 473 for (; i<(SHA_LBLOCK-2); i++)
448 p[i]=0; 474 p[i]=0;
449 p[SHA_LBLOCK-2]=c->Nh; 475 p[SHA_LBLOCK-2]=c->Nh;
450 p[SHA_LBLOCK-1]=c->Nl; 476 p[SHA_LBLOCK-1]=c->Nl;
451#if defined(L_ENDIAN) && defined(SHA1_ASM) 477#if SHA_LONG_LOG2==2
478#if !defined(B_ENDIAN) && defined(SHA1_ASM)
452 Endian_Reverse32(p[SHA_LBLOCK-2]); 479 Endian_Reverse32(p[SHA_LBLOCK-2]);
453 Endian_Reverse32(p[SHA_LBLOCK-1]); 480 Endian_Reverse32(p[SHA_LBLOCK-1]);
454#endif 481#endif
455 sha1_block(c,p,64); 482#endif
483 sha1_block(c,p,1);
456 cp=md; 484 cp=md;
457 l=c->h0; nl2c(l,cp); 485 l=c->h0; nl2c(l,cp);
458 l=c->h1; nl2c(l,cp); 486 l=c->h1; nl2c(l,cp);
@@ -460,9 +488,11 @@ SHA_CTX *c;
460 l=c->h3; nl2c(l,cp); 488 l=c->h3; nl2c(l,cp);
461 l=c->h4; nl2c(l,cp); 489 l=c->h4; nl2c(l,cp);
462 490
463 /* clear stuff, sha1_block may be leaving some stuff on the stack
464 * but I'm not worried :-) */
465 c->num=0; 491 c->num=0;
466/* memset((char *)&c,0,sizeof(c));*/ 492 /* sha_block may be leaving some stuff on the stack
493 * but I'm not worried :-)
494 memset((void *)c,0,sizeof(SHA_CTX));
495 */
467 } 496 }
497#endif
468 498