Imported Upstream version 2.7
[ossec-hids.git] / src / os_crypto / sha1 / md32_common.h
1 /* @(#) $Id: ./src/os_crypto/sha1/md32_common.h, 2011/09/08 dcid Exp $
2  */
3 /* Included on ossec */
4
5 /* crypto/md32_common.h */
6 /* ====================================================================
7  * Copyright (c) 1999-2002 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 /*
61  * This is a generic 32 bit "collector" for message digest algorithms.
62  * Whenever needed it collects input character stream into chunks of
63  * 32 bit values and invokes a block function that performs actual hash
64  * calculations.
65  *
66  * Porting guide.
67  *
68  * Obligatory macros:
69  *
70  * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
71  *      this macro defines byte order of input stream.
72  * HASH_CBLOCK
73  *      size of a unit chunk HASH_BLOCK operates on.
74  * HASH_LONG
75  *      has to be at lest 32 bit wide, if it's wider, then
76  *      HASH_LONG_LOG2 *has to* be defined along
77  * HASH_CTX
78  *      context structure that at least contains following
79  *      members:
80  *              typedef struct {
81  *                      ...
82  *                      HASH_LONG       Nl,Nh;
83  *                      HASH_LONG       data[HASH_LBLOCK];
84  *                      unsigned int    num;
85  *                      ...
86  *                      } HASH_CTX;
87  * HASH_UPDATE
88  *      name of "Update" function, implemented here.
89  * HASH_TRANSFORM
90  *      name of "Transform" function, implemented here.
91  * HASH_FINAL
92  *      name of "Final" function, implemented here.
93  * HASH_BLOCK_HOST_ORDER
94  *      name of "block" function treating *aligned* input message
95  *      in host byte order, implemented externally.
96  * HASH_BLOCK_DATA_ORDER
97  *      name of "block" function treating *unaligned* input message
98  *      in original (data) byte order, implemented externally (it
99  *      actually is optional if data and host are of the same
100  *      "endianess").
101  * HASH_MAKE_STRING
102  *      macro convering context variables to an ASCII hash string.
103  *
104  * Optional macros:
105  *
106  * B_ENDIAN or L_ENDIAN
107  *      defines host byte-order.
108  * HASH_LONG_LOG2
109  *      defaults to 2 if not states otherwise.
110  * HASH_LBLOCK
111  *      assumed to be HASH_CBLOCK/4 if not stated otherwise.
112  * HASH_BLOCK_DATA_ORDER_ALIGNED
113  *      alternative "block" function capable of treating
114  *      aligned input message in original (data) order,
115  *      implemented externally.
116  *
117  * MD5 example:
118  *
119  *      #define DATA_ORDER_IS_LITTLE_ENDIAN
120  *
121  *      #define HASH_LONG               MD5_LONG
122  *      #define HASH_LONG_LOG2          MD5_LONG_LOG2
123  *      #define HASH_CTX                MD5_CTX
124  *      #define HASH_CBLOCK             MD5_CBLOCK
125  *      #define HASH_LBLOCK             MD5_LBLOCK
126  *      #define HASH_UPDATE             MD5_Update
127  *      #define HASH_TRANSFORM          MD5_Transform
128  *      #define HASH_FINAL              MD5_Final
129  *      #define HASH_BLOCK_HOST_ORDER   md5_block_host_order
130  *      #define HASH_BLOCK_DATA_ORDER   md5_block_data_order
131  *
132  *                                      <appro@fy.chalmers.se>
133  */
134
135
136 #ifndef _MD32_COMMON__H
137 #define _MD32_COMMON__H
138
139
140 #if !defined(DATA_ORDER_IS_BIG_ENDIAN) && !defined(DATA_ORDER_IS_LITTLE_ENDIAN)
141 #error "DATA_ORDER must be defined!"
142 #endif
143
144 #ifndef HASH_CBLOCK
145 #error "HASH_CBLOCK must be defined!"
146 #endif
147 #ifndef HASH_LONG
148 #error "HASH_LONG must be defined!"
149 #endif
150 #ifndef HASH_CTX
151 #error "HASH_CTX must be defined!"
152 #endif
153
154 #ifndef HASH_UPDATE
155 #error "HASH_UPDATE must be defined!"
156 #endif
157 #ifndef HASH_TRANSFORM
158 #error "HASH_TRANSFORM must be defined!"
159 #endif
160 #ifndef HASH_FINAL
161 #error "HASH_FINAL must be defined!"
162 #endif
163
164 #ifndef HASH_BLOCK_HOST_ORDER
165 #error "HASH_BLOCK_HOST_ORDER must be defined!"
166 #endif
167
168 #if 0
169 /*
170  * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
171  * isn't defined.
172  */
173 #ifndef HASH_BLOCK_DATA_ORDER
174 #error "HASH_BLOCK_DATA_ORDER must be defined!"
175 #endif
176 #endif
177
178 #ifndef HASH_LBLOCK
179 #define HASH_LBLOCK     (HASH_CBLOCK/4)
180 #endif
181
182 #ifndef HASH_LONG_LOG2
183 #define HASH_LONG_LOG2  2
184 #endif
185
186 /*
187  * Engage compiler specific rotate intrinsic function if available.
188  */
189 #undef ROTATE
190 #ifndef PEDANTIC
191 # if defined(_MSC_VER) || defined(__ICC)
192 #  define ROTATE(a,n)   _lrotl(a,n)
193 # elif defined(__MWERKS__)
194 #  if defined(__POWERPC__)
195 #   define ROTATE(a,n)  __rlwinm(a,n,0,31)
196 #  elif defined(__MC68K__)
197     /* Motorola specific tweak. <appro@fy.chalmers.se> */
198 #   define ROTATE(a,n)  ( n<24 ? __rol(a,n) : __ror(a,32-n) )
199 #  else
200 #   define ROTATE(a,n)  __rol(a,n)
201 #  endif
202 # elif defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
203   /*
204    * Some GNU C inline assembler templates. Note that these are
205    * rotates by *constant* number of bits! But that's exactly
206    * what we need here...
207    *                                    <appro@fy.chalmers.se>
208    */
209 #  if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
210 #   define ROTATE(a,n)  ({ register unsigned int ret;   \
211                                 asm (                   \
212                                 "roll %1,%0"            \
213                                 : "=r"(ret)             \
214                                 : "I"(n), "0"(a)        \
215                                 : "cc");                \
216                            ret;                         \
217                         })
218 #  elif defined(__powerpc) || defined(__ppc__) || defined(__powerpc64__)
219 #   define ROTATE(a,n)  ({ register unsigned int ret;   \
220                                 asm (                   \
221                                 "rlwinm %0,%1,%2,0,31"  \
222                                 : "=r"(ret)             \
223                                 : "r"(a), "I"(n));      \
224                            ret;                         \
225                         })
226 #  endif
227 # endif
228 #endif /* PEDANTIC */
229
230 #if HASH_LONG_LOG2==2   /* Engage only if sizeof(HASH_LONG)== 4 */
231 /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
232 #ifdef ROTATE
233 /* 5 instructions with rotate instruction, else 9 */
234 #define REVERSE_FETCH32(a,l)    (                                       \
235                 l=*(const HASH_LONG *)(a),                              \
236                 ((ROTATE(l,8)&0x00FF00FF)|(ROTATE((l&0x00FF00FF),24)))  \
237                                 )
238 #else
239 /* 6 instructions with rotate instruction, else 8 */
240 #define REVERSE_FETCH32(a,l)    (                               \
241                 l=*(const HASH_LONG *)(a),                      \
242                 l=(((l>>8)&0x00FF00FF)|((l&0x00FF00FF)<<8)),    \
243                 ROTATE(l,16)                                    \
244                                 )
245 /*
246  * Originally the middle line started with l=(((l&0xFF00FF00)>>8)|...
247  * It's rewritten as above for two reasons:
248  *      - RISCs aren't good at long constants and have to explicitely
249  *        compose 'em with several (well, usually 2) instructions in a
250  *        register before performing the actual operation and (as you
251  *        already realized:-) having same constant should inspire the
252  *        compiler to permanently allocate the only register for it;
253  *      - most modern CPUs have two ALUs, but usually only one has
254  *        circuitry for shifts:-( this minor tweak inspires compiler
255  *        to schedule shift instructions in a better way...
256  *
257  *                              <appro@fy.chalmers.se>
258  */
259 #endif
260 #endif
261
262 #ifndef ROTATE
263 #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
264 #endif
265
266 /*
267  * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
268  * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
269  * and host are of the same "endianess". It's possible to mask
270  * this with blank #define HASH_BLOCK_DATA_ORDER though...
271  *
272  *                              <appro@fy.chalmers.se>
273  */
274 #if defined(B_ENDIAN)
275 #  if defined(DATA_ORDER_IS_BIG_ENDIAN)
276 #    if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
277 #      define HASH_BLOCK_DATA_ORDER_ALIGNED     HASH_BLOCK_HOST_ORDER
278 #    endif
279 #  endif
280 #elif defined(L_ENDIAN)
281 #  if defined(DATA_ORDER_IS_LITTLE_ENDIAN)
282 #    if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED) && HASH_LONG_LOG2==2
283 #      define HASH_BLOCK_DATA_ORDER_ALIGNED     HASH_BLOCK_HOST_ORDER
284 #    endif
285 #  endif
286 #endif
287
288 #if !defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
289 #ifndef HASH_BLOCK_DATA_ORDER
290 #error "HASH_BLOCK_DATA_ORDER must be defined!"
291 #endif
292 #endif
293
294 #if defined(DATA_ORDER_IS_BIG_ENDIAN)
295
296 #ifndef PEDANTIC
297 # if defined(__GNUC__) && __GNUC__>=2 && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM)
298 #  if ((defined(__i386) || defined(__i386__)) && !defined(I386_ONLY)) || \
299       (defined(__x86_64) || defined(__x86_64__))
300     /*
301      * This gives ~30-40% performance improvement in SHA-256 compiled
302      * with gcc [on P4]. Well, first macro to be frank. We can pull
303      * this trick on x86* platforms only, because these CPUs can fetch
304      * unaligned data without raising an exception.
305      */
306 #   define HOST_c2l(c,l)        ({ unsigned int r=*((const unsigned int *)(c)); \
307                                    asm ("bswapl %0":"=r"(r):"0"(r));    \
308                                    (c)+=4; (l)=r;                       })
309 #   define HOST_l2c(l,c)        ({ unsigned int r=(l);                  \
310                                    asm ("bswapl %0":"=r"(r):"0"(r));    \
311                                    *((unsigned int *)(c))=r; (c)+=4; r; })
312 #  endif
313 # endif
314 #endif
315
316 #ifndef HOST_c2l
317 #define HOST_c2l(c,l)   (l =(((unsigned long)(*((c)++)))<<24),          \
318                          l|=(((unsigned long)(*((c)++)))<<16),          \
319                          l|=(((unsigned long)(*((c)++)))<< 8),          \
320                          l|=(((unsigned long)(*((c)++)))    ),          \
321                          l)
322 #endif
323 #define HOST_p_c2l(c,l,n)       {                                       \
324                         switch (n) {                                    \
325                         case 0: l =((unsigned long)(*((c)++)))<<24;     \
326                         case 1: l|=((unsigned long)(*((c)++)))<<16;     \
327                         case 2: l|=((unsigned long)(*((c)++)))<< 8;     \
328                         case 3: l|=((unsigned long)(*((c)++)));         \
329                                 } }
330 #define HOST_p_c2l_p(c,l,sc,len) {                                      \
331                         switch (sc) {                                   \
332                         case 0: l =((unsigned long)(*((c)++)))<<24;     \
333                                 if (--len == 0) break;                  \
334                         case 1: l|=((unsigned long)(*((c)++)))<<16;     \
335                                 if (--len == 0) break;                  \
336                         case 2: l|=((unsigned long)(*((c)++)))<< 8;     \
337                                 } }
338 /* NOTE the pointer is not incremented at the end of this */
339 #define HOST_c2l_p(c,l,n)       {                                       \
340                         l=0; (c)+=n;                                    \
341                         switch (n) {                                    \
342                         case 3: l =((unsigned long)(*(--(c))))<< 8;     \
343                         case 2: l|=((unsigned long)(*(--(c))))<<16;     \
344                         case 1: l|=((unsigned long)(*(--(c))))<<24;     \
345                                 } }
346 #ifndef HOST_l2c
347 #define HOST_l2c(l,c)   (*((c)++)=(unsigned char)(((l)>>24)&0xff),      \
348                          *((c)++)=(unsigned char)(((l)>>16)&0xff),      \
349                          *((c)++)=(unsigned char)(((l)>> 8)&0xff),      \
350                          *((c)++)=(unsigned char)(((l)    )&0xff),      \
351                          l)
352 #endif
353
354 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
355
356 #if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
357 # ifndef B_ENDIAN
358    /* See comment in DATA_ORDER_IS_BIG_ENDIAN section. */
359 #  define HOST_c2l(c,l) ((l)=*((const unsigned int *)(c)), (c)+=4, l)
360 #  define HOST_l2c(l,c) (*((unsigned int *)(c))=(l), (c)+=4, l)
361 # endif
362 #endif
363
364 #ifndef HOST_c2l
365 #define HOST_c2l(c,l)   (l =(((unsigned long)(*((c)++)))    ),          \
366                          l|=(((unsigned long)(*((c)++)))<< 8),          \
367                          l|=(((unsigned long)(*((c)++)))<<16),          \
368                          l|=(((unsigned long)(*((c)++)))<<24),          \
369                          l)
370 #endif
371 #define HOST_p_c2l(c,l,n)       {                                       \
372                         switch (n) {                                    \
373                         case 0: l =((unsigned long)(*((c)++)));         \
374                         case 1: l|=((unsigned long)(*((c)++)))<< 8;     \
375                         case 2: l|=((unsigned long)(*((c)++)))<<16;     \
376                         case 3: l|=((unsigned long)(*((c)++)))<<24;     \
377                                 } }
378 #define HOST_p_c2l_p(c,l,sc,len) {                                      \
379                         switch (sc) {                                   \
380                         case 0: l =((unsigned long)(*((c)++)));         \
381                                 if (--len == 0) break;                  \
382                         case 1: l|=((unsigned long)(*((c)++)))<< 8;     \
383                                 if (--len == 0) break;                  \
384                         case 2: l|=((unsigned long)(*((c)++)))<<16;     \
385                                 } }
386 /* NOTE the pointer is not incremented at the end of this */
387 #define HOST_c2l_p(c,l,n)       {                                       \
388                         l=0; (c)+=n;                                    \
389                         switch (n) {                                    \
390                         case 3: l =((unsigned long)(*(--(c))))<<16;     \
391                         case 2: l|=((unsigned long)(*(--(c))))<< 8;     \
392                         case 1: l|=((unsigned long)(*(--(c))));         \
393                                 } }
394 #ifndef HOST_l2c
395 #define HOST_l2c(l,c)   (*((c)++)=(unsigned char)(((l)    )&0xff),      \
396                          *((c)++)=(unsigned char)(((l)>> 8)&0xff),      \
397                          *((c)++)=(unsigned char)(((l)>>16)&0xff),      \
398                          *((c)++)=(unsigned char)(((l)>>24)&0xff),      \
399                          l)
400 #endif
401
402 #endif
403
404 /*
405  * Time for some action:-)
406  */
407
408 int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
409         {
410         const unsigned char *data=data_;
411         register HASH_LONG * p;
412         register HASH_LONG l;
413         size_t sw,sc,ew,ec;
414
415         if (len==0) return 1;
416
417         l=(c->Nl+(((HASH_LONG)len)<<3))&0xffffffffUL;
418         /* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
419          * Wei Dai <weidai@eskimo.com> for pointing it out. */
420         if (l < c->Nl) /* overflow */
421                 c->Nh++;
422         c->Nh+=(len>>29);       /* might cause compiler warning on 16-bit */
423         c->Nl=l;
424
425         if (c->num != 0)
426                 {
427                 p=c->data;
428                 sw=c->num>>2;
429                 sc=c->num&0x03;
430
431                 if ((c->num+len) >= HASH_CBLOCK)
432                         {
433                         l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
434                         for (; sw<HASH_LBLOCK; sw++)
435                                 {
436                                 HOST_c2l(data,l); p[sw]=l;
437                                 }
438                         HASH_BLOCK_HOST_ORDER (c,p,1);
439                         len-=(HASH_CBLOCK-c->num);
440                         c->num=0;
441                         /* drop through and do the rest */
442                         }
443                 else
444                         {
445                         c->num+=(unsigned int)len;
446                         if ((sc+len) < 4) /* ugly, add char's to a word */
447                                 {
448                                 l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
449                                 }
450                         else
451                                 {
452                                 ew=(c->num>>2);
453                                 ec=(c->num&0x03);
454                                 if (sc)
455                                         l=p[sw];
456                                 HOST_p_c2l(data,l,sc);
457                                 p[sw++]=l;
458                                 for (; sw < ew; sw++)
459                                         {
460                                         HOST_c2l(data,l); p[sw]=l;
461                                         }
462                                 if (ec)
463                                         {
464                                         HOST_c2l_p(data,l,ec); p[sw]=l;
465                                         }
466                                 }
467                         return 1;
468                         }
469                 }
470
471         sw=len/HASH_CBLOCK;
472         if (sw > 0)
473                 {
474 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
475                 /*
476                  * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
477                  * only if sizeof(HASH_LONG)==4.
478                  */
479                 if ((((size_t)data)%4) == 0)
480                         {
481                         /* data is properly aligned so that we can cast it: */
482                         HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,sw);
483                         sw*=HASH_CBLOCK;
484                         data+=sw;
485                         len-=sw;
486                         }
487                 else
488 #if !defined(HASH_BLOCK_DATA_ORDER)
489                         while (sw--)
490                                 {
491                                 memcpy (p=c->data,data,HASH_CBLOCK);
492                                 HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
493                                 data+=HASH_CBLOCK;
494                                 len-=HASH_CBLOCK;
495                                 }
496 #endif
497 #endif
498 #if defined(HASH_BLOCK_DATA_ORDER)
499                         {
500                         HASH_BLOCK_DATA_ORDER(c,data,sw);
501                         sw*=HASH_CBLOCK;
502                         data+=sw;
503                         len-=sw;
504                         }
505 #endif
506                 }
507
508         if (len!=0)
509                 {
510                 p = c->data;
511                 c->num = len;
512                 ew=len>>2;      /* words to copy */
513                 ec=len&0x03;
514                 for (; ew; ew--,p++)
515                         {
516                         HOST_c2l(data,l); *p=l;
517                         }
518                 HOST_c2l_p(data,l,ec);
519                 *p=l;
520                 }
521         return 1;
522         }
523
524
525 void HASH_TRANSFORM (HASH_CTX *c, const unsigned char *data)
526         {
527 #if defined(HASH_BLOCK_DATA_ORDER_ALIGNED)
528         if ((((size_t)data)%4) == 0)
529                 /* data is properly aligned so that we can cast it: */
530                 HASH_BLOCK_DATA_ORDER_ALIGNED (c,(const HASH_LONG *)data,1);
531         else
532 #if !defined(HASH_BLOCK_DATA_ORDER)
533                 {
534                 memcpy (c->data,data,HASH_CBLOCK);
535                 HASH_BLOCK_DATA_ORDER_ALIGNED (c,c->data,1);
536                 }
537 #endif
538 #endif
539 #if defined(HASH_BLOCK_DATA_ORDER)
540         HASH_BLOCK_DATA_ORDER (c,data,1);
541 #endif
542         }
543
544
545 int HASH_FINAL (unsigned char *md, HASH_CTX *c)
546         {
547         register HASH_LONG *p;
548         register unsigned long l;
549         register int i,j;
550         static const unsigned char end[4]={0x80,0x00,0x00,0x00};
551         const unsigned char *cp=end;
552
553         /* c->num should definitly have room for at least one more byte. */
554         p=c->data;
555         i=c->num>>2;
556         j=c->num&0x03;
557
558 #if 0
559         /* purify often complains about the following line as an
560          * Uninitialized Memory Read.  While this can be true, the
561          * following p_c2l macro will reset l when that case is true.
562          * This is because j&0x03 contains the number of 'valid' bytes
563          * already in p[i].  If and only if j&0x03 == 0, the UMR will
564          * occur but this is also the only time p_c2l will do
565          * l= *(cp++) instead of l|= *(cp++)
566          * Many thanks to Alex Tang <altitude@cic.net> for pickup this
567          * 'potential bug' */
568 #ifdef PURIFY
569         if (j==0) p[i]=0; /* Yeah, but that's not the way to fix it:-) */
570 #endif
571         l=p[i];
572 #else
573         l = (j==0) ? 0 : p[i];
574 #endif
575         HOST_p_c2l(cp,l,j); p[i++]=l; /* i is the next 'undefined word' */
576
577         if (i>(HASH_LBLOCK-2)) /* save room for Nl and Nh */
578                 {
579                 if (i<HASH_LBLOCK) p[i]=0;
580                 HASH_BLOCK_HOST_ORDER (c,p,1);
581                 i=0;
582                 }
583         for (; i<(HASH_LBLOCK-2); i++)
584                 p[i]=0;
585
586 #if   defined(DATA_ORDER_IS_BIG_ENDIAN)
587         p[HASH_LBLOCK-2]=c->Nh;
588         p[HASH_LBLOCK-1]=c->Nl;
589 #elif defined(DATA_ORDER_IS_LITTLE_ENDIAN)
590         p[HASH_LBLOCK-2]=c->Nl;
591         p[HASH_LBLOCK-1]=c->Nh;
592 #endif
593         HASH_BLOCK_HOST_ORDER (c,p,1);
594
595 #ifndef HASH_MAKE_STRING
596 #error "HASH_MAKE_STRING must be defined!"
597 #else
598         HASH_MAKE_STRING(c,md);
599 #endif
600
601         c->num=0;
602         /* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
603          * but I'm not worried :-)
604         OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
605          */
606         return 1;
607         }
608
609 #ifndef MD32_REG_T
610 #define MD32_REG_T long
611 /*
612  * This comment was originaly written for MD5, which is why it
613  * discusses A-D. But it basically applies to all 32-bit digests,
614  * which is why it was moved to common header file.
615  *
616  * In case you wonder why A-D are declared as long and not
617  * as MD5_LONG. Doing so results in slight performance
618  * boost on LP64 architectures. The catch is we don't
619  * really care if 32 MSBs of a 64-bit register get polluted
620  * with eventual overflows as we *save* only 32 LSBs in
621  * *either* case. Now declaring 'em long excuses the compiler
622  * from keeping 32 MSBs zeroed resulting in 13% performance
623  * improvement under SPARC Solaris7/64 and 5% under AlphaLinux.
624  * Well, to be honest it should say that this *prevents*
625  * performance degradation.
626  *                              <appro@fy.chalmers.se>
627  * Apparently there're LP64 compilers that generate better
628  * code if A-D are declared int. Most notably GCC-x86_64
629  * generates better code.
630  *                              <appro@fy.chalmers.se>
631  */
632 #endif
633
634
635 #endif /* _MD32_COMMON__H */