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