home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 November / PCO_1198.ISO / filesbbs / os2 / fn127os2.arj / FN127OS2.ZIP / fn127os2 / src / md5_locl.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-25  |  5.9 KB  |  180 lines

  1. /* crypto/md/md5_locl.h */
  2. /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include "md5.h"
  51.  
  52. #define ULONG    unsigned long
  53. #define UCHAR    unsigned char
  54. #define UINT    unsigned int
  55.  
  56. #if defined(NOCONST)
  57. #define const
  58. #endif
  59.  
  60. #undef c2l
  61. #define c2l(c,l)    (l = ((unsigned long)(*((c)++)))     , \
  62.              l|=(((unsigned long)(*((c)++)))<< 8), \
  63.              l|=(((unsigned long)(*((c)++)))<<16), \
  64.              l|=(((unsigned long)(*((c)++)))<<24))
  65.  
  66. #undef p_c2l
  67. #define p_c2l(c,l,n)    { \
  68.             switch (n) { \
  69.             case 0: l =((unsigned long)(*((c)++))); \
  70.             case 1: l|=((unsigned long)(*((c)++)))<< 8; \
  71.             case 2: l|=((unsigned long)(*((c)++)))<<16; \
  72.             case 3: l|=((unsigned long)(*((c)++)))<<24; \
  73.                 } \
  74.             }
  75.  
  76. /* NOTE the pointer is not incremented at the end of this */
  77. #undef c2l_p
  78. #define c2l_p(c,l,n)    { \
  79.             l=0; \
  80.             (c)+=n; \
  81.             switch (n) { \
  82.             case 3: l =((unsigned long)(*(--(c))))<<16; \
  83.             case 2: l|=((unsigned long)(*(--(c))))<< 8; \
  84.             case 1: l|=((unsigned long)(*(--(c))))    ; \
  85.                 } \
  86.             }
  87.  
  88. #undef p_c2l_p
  89. #define p_c2l_p(c,l,sc,len) { \
  90.             switch (sc) \
  91.                 { \
  92.             case 0: l =((unsigned long)(*((c)++))); \
  93.                 if (--len == 0) break; \
  94.             case 1: l|=((unsigned long)(*((c)++)))<< 8; \
  95.                 if (--len == 0) break; \
  96.             case 2: l|=((unsigned long)(*((c)++)))<<16; \
  97.                 } \
  98.             }
  99.  
  100. #undef l2c
  101. #define l2c(l,c)    (*((c)++)=(unsigned char)(((l)    )&0xff), \
  102.              *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  103.              *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  104.              *((c)++)=(unsigned char)(((l)>>24)&0xff))
  105.  
  106. /* NOTE - c is not incremented as per l2c */
  107. #undef l2cn
  108. #define l2cn(l1,l2,c,n)    { \
  109.             c+=n; \
  110.             switch (n) { \
  111.             case 8: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
  112.             case 7: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
  113.             case 6: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
  114.             case 5: *(--(c))=(unsigned char)(((l2)    )&0xff); \
  115.             case 4: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
  116.             case 3: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
  117.             case 2: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
  118.             case 1: *(--(c))=(unsigned char)(((l1)    )&0xff); \
  119.                 } \
  120.             }
  121.  
  122. /* A nice byte order reversal from Wei Dai <weidai@eskimo.com> */
  123. #if defined(WIN32) || defined(GNU_WIN32)
  124. /* 5 instructions with rotate instruction, else 9 */
  125. #define Endian_Reverse32(a) \
  126.     { \
  127.     unsigned long l=(a); \
  128.     ((ROTATE(l,8)&0xFF00FF00)|(ROTATE(l,24)&0x00FF00FF)); \
  129.     }
  130. #else
  131. /* 6 instructions with rotate instruction, else 8 */
  132. #define Endian_Reverse32(a) \
  133.     { \
  134.     unsigned long l=(a); \
  135.     l=(((l&0xFF00FF00)>>8L)|((l&0x00FF00FF)<<8L)); \
  136.     (a)=ROTATE(l,16L); \
  137.     }
  138. #endif
  139. /*
  140. #define    F(x,y,z)    (((x) & (y))  |  ((~(x)) & (z)))
  141. #define    G(x,y,z)    (((x) & (z))  |  ((y) & (~(z))))
  142. */
  143.  
  144. /* As pointed out by Wei Dai <weidai@eskimo.com>, the above can be
  145.  * simplified to the code below.  Wei attributes these optimisations
  146.  * to Peter Gutmann's SHS code, and he attributes it to Rich Schroeppel.
  147.  */
  148. #define    F(x,y,z)    ((((y) ^ (z)) & (x)) ^ (z))
  149. #define    G(x,y,z)    ((((x) ^ (y)) & (z)) ^ (y))
  150. #define    H(x,y,z)    ((x) ^ (y) ^ (z))
  151. #define    I(x,y,z)    (((x) | (~(z))) ^ (y))
  152.  
  153. #undef ROTATE
  154. #if defined(WIN32)
  155. #define ROTATE(a,n)     _lrotl(a,n)
  156. #else
  157. #define ROTATE(a,n)     (((a)<<(n))|(((a)&0xffffffff)>>(32-(n))))
  158. #endif
  159.  
  160.  
  161. #define R0(a,b,c,d,k,s,t) { \
  162.     a+=((k)+(t)+F((b),(c),(d))); \
  163.     a=ROTATE(a,s); \
  164.     a+=b; };\
  165.  
  166. #define R1(a,b,c,d,k,s,t) { \
  167.     a+=((k)+(t)+G((b),(c),(d))); \
  168.     a=ROTATE(a,s); \
  169.     a+=b; };
  170.  
  171. #define R2(a,b,c,d,k,s,t) { \
  172.     a+=((k)+(t)+H((b),(c),(d))); \
  173.     a=ROTATE(a,s); \
  174.     a+=b; };
  175.  
  176. #define R3(a,b,c,d,k,s,t) { \
  177.     a+=((k)+(t)+I((b),(c),(d))); \
  178.     a=ROTATE(a,s); \
  179.     a+=b; };
  180.