[Olsr-cvs] olsrd-current/lib/secure/src md5.h, 1.2, 1.3 md5.c, 1.3, 1.4

Bernd Petrovitsch (spam-protected)
Fri Nov 2 16:51:48 CET 2007


Update of /cvsroot/olsrd/olsrd-current/lib/secure/src
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv28055/lib/secure/src

Modified Files:
	md5.h md5.c 
Log Message:
While working on something else, I stumbled over this. Obviously no one
ever used the "secure" plugin on 64bit hardware:
- fixed lib/secure/src/md5.h: This was broken as it had
  "typedef unsigned long int UINT4;". "unsigned long int" is 8 bytes on
  x86_64.
  We are using now the standardized types from <inttypes.h>.
- fixed warnings and improved lib/secure/src/md5.c:
  * we are using memcpy() and memset() instead of the open-coded loops as
    suggested 
  * const'ified
  * added function prototypes for static functions
  * moved static functions to the top so that their declaration is before
    their use to allow gcc to inline if only used once.
  It remains ugly - God knows why there are that so many useless
  type-casts.


Index: md5.c
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/lib/secure/src/md5.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -C2 -d -r1.3 -r1.4
*** md5.c	30 Jan 2007 17:11:52 -0000	1.3
--- md5.c	2 Nov 2007 15:51:46 -0000	1.4
***************
*** 26,29 ****
--- 26,31 ----
  #include "md5.h"
  
+ #include <string.h>
+ 
  /* Constants for MD5Transform routine.
   */
***************
*** 45,55 ****
  #define S44 21
  
! static void MD5Transform(UINT4 [4], const unsigned char [64]);
! static void Encode(unsigned char *, UINT4 *, unsigned int);
! static void Decode(UINT4 *, const unsigned char *, const unsigned int);
! static void MD5_memcpy(POINTER, POINTER, unsigned int);
! static void MD5_memset(POINTER, int, unsigned int);
! 
! static unsigned char PADDING[64] = {
    0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
--- 47,51 ----
  #define S44 21
  
! static const unsigned char PADDING[64] = {
    0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
***************
*** 92,175 ****
    }
  
! /* MD5 initialization. Begins an MD5 operation, writing a new context.
   */
! void MD5Init (MD5_CTX *context)
  {
!   context->count[0] = context->count[1] = 0;
! 
!   /* Load magic initialization constants.
!    */
!   context->state[0] = 0x67452301;
!   context->state[1] = 0xefcdab89;
!   context->state[2] = 0x98badcfe;
!   context->state[3] = 0x10325476;
  }
  
! /* MD5 block update operation. Continues an MD5 message-digest
!      operation, processing another message block, and updating the
!      context.
   */
! void MD5Update (MD5_CTX *context, const unsigned char *input, const unsigned int inputLen)
  {
!   unsigned int i, index, partLen;
! 
!   /* Compute number of bytes mod 64 */
!   index = (unsigned int)((context->count[0] >> 3) & 0x3F);
! 
!   /* Update number of bits */
!   if ((context->count[0] += ((UINT4)inputLen << 3))
!       < ((UINT4)inputLen << 3))
!     context->count[1]++;
!   context->count[1] += ((UINT4)inputLen >> 29);
!   
!   partLen = 64 - index;
!   
!   /* Transform as many times as possible.
!    */
!   if (inputLen >= partLen) {
!     MD5_memcpy
!       ((POINTER)&context->buffer[index], (POINTER)input, partLen);
!     MD5Transform (context->state, context->buffer);
!   
!     for (i = partLen; i + 63 < inputLen; i += 64)
!       MD5Transform (context->state, &input[i]);
!     
!     index = 0;
!   }
!   else
!     i = 0;
    
!   /* Buffer remaining input */
!   MD5_memcpy 
!     ((POINTER)&context->buffer[index], (POINTER)&input[i],
!      inputLen-i);
  }
  
! /* MD5 finalization. Ends an MD5 message-digest operation, writing the
!      the message digest and zeroizing the context.
   */
! void MD5Final (unsigned char digest[16], MD5_CTX *context)
  {
!   unsigned char bits[8];
!   unsigned int index, padLen;
  
!   /* Save number of bits */
!   Encode (bits, context->count, 8);
  
!   /* Pad out to 56 mod 64.
!    */
!   index = (unsigned int)((context->count[0] >> 3) & 0x3f);
!   padLen = (index < 56) ? (56 - index) : (120 - index);
!   MD5Update (context, PADDING, padLen);
!   
!   /* Append length (before padding) */
!   MD5Update (context, bits, 8);
  
!   /* Store state in digest */
!   Encode (digest, context->state, 16);
!   
!   /* Zeroize sensitive information.
!    */
!   MD5_memset ((POINTER)context, 0, sizeof (*context));
  }
  
--- 88,142 ----
    }
  
! #if 0
! /* Note: Replace "for loop" with standard memcpy if possible.
   */
! static void MD5_memcpy (POINTER output, POINTER input, unsigned int len)
  {
!   unsigned int i;
!   
!   for (i = 0; i < len; i++)
!     output[i] = input[i];
  }
  
! /* Note: Replace "for loop" with standard memset if possible.
   */
! static void MD5_memset (POINTER output, int value, unsigned int len)
  {
!   unsigned int i;
    
!   for (i = 0; i < len; i++)
!     ((char *)output)[i] = (char)value;
  }
+ #else
+ #define MD5_memcpy(dst, src, len) memcpy((dst), (src), (len)) 
+ #define MD5_memset(dst, val, len) memset((dst), (val), (len)) 
+ #endif
  
! 
! /* Encodes input (UINT4) into output (unsigned char). Assumes len is
!      a multiple of 4.
   */
! static void Encode (unsigned char *output, UINT4 *input, unsigned int len)
  {
!   unsigned int i, j;
  
!   for (i = 0, j = 0; j < len; i++, j += 4) {
!     output[j] = (unsigned char)(input[i] & 0xff);
!     output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
!     output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
!     output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
!   }
! }
  
! /* Decodes input (unsigned char) into output (UINT4). Assumes len is
!      a multiple of 4.
!  */
! static void Decode (UINT4 *output, const unsigned char *input, unsigned int len)
! {
!   unsigned int i, j;
  
!   for (i = 0, j = 0; j < len; i++, j += 4)
!     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
!       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
  }
  
***************
*** 264,311 ****
  }
  
! /* Encodes input (UINT4) into output (unsigned char). Assumes len is
!      a multiple of 4.
   */
! static void Encode (unsigned char *output, UINT4 *input, unsigned int len)
  {
!   unsigned int i, j;
  
!   for (i = 0, j = 0; j < len; i++, j += 4) {
!     output[j] = (unsigned char)(input[i] & 0xff);
!     output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
!     output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
!     output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
!   }
  }
  
! /* Decodes input (unsigned char) into output (UINT4). Assumes len is
!      a multiple of 4.
   */
! static void Decode (UINT4 *output, const unsigned char *input, const unsigned int len)
  {
!   unsigned int i, j;
  
!   for (i = 0, j = 0; j < len; i++, j += 4)
!     output[i] = ((UINT4)input[j]) | (((UINT4)input[j+1]) << 8) |
!       (((UINT4)input[j+2]) << 16) | (((UINT4)input[j+3]) << 24);
! }
  
! /* Note: Replace "for loop" with standard memcpy if possible.
!  */
! static void MD5_memcpy (POINTER output, POINTER input, unsigned int len)
! {
!   unsigned int i;
    
!   for (i = 0; i < len; i++)
!     output[i] = input[i];
  }
  
! /* Note: Replace "for loop" with standard memset if possible.
   */
! static void MD5_memset (POINTER output, int value, unsigned int len)
  {
!   unsigned int i;
    
!   for (i = 0; i < len; i++)
!     ((char *)output)[i] = (char)value;
  }
--- 231,314 ----
  }
  
! 
! /* MD5 initialization. Begins an MD5 operation, writing a new context.
   */
! void MD5Init (MD5_CTX *context)
  {
!   context->count[0] = context->count[1] = 0;
  
!   /* Load magic initialization constants.
!    */
!   context->state[0] = 0x67452301;
!   context->state[1] = 0xefcdab89;
!   context->state[2] = 0x98badcfe;
!   context->state[3] = 0x10325476;
  }
  
! /* MD5 block update operation. Continues an MD5 message-digest
!      operation, processing another message block, and updating the
!      context.
   */
! void MD5Update (MD5_CTX *context, const unsigned char *input, const unsigned int inputLen)
  {
!   unsigned int i, index, partLen;
  
!   /* Compute number of bytes mod 64 */
!   index = (unsigned int)((context->count[0] >> 3) & 0x3F);
  
!   /* Update number of bits */
!   if ((context->count[0] += ((UINT4)inputLen << 3))
!       < ((UINT4)inputLen << 3))
!     context->count[1]++;
!   context->count[1] += ((UINT4)inputLen >> 29);
    
!   partLen = 64 - index;
!   
!   /* Transform as many times as possible.
!    */
!   if (inputLen >= partLen) {
!     MD5_memcpy
!       ((POINTER)&context->buffer[index], (POINTER)input, partLen);
!     MD5Transform (context->state, context->buffer);
!   
!     for (i = partLen; i + 63 < inputLen; i += 64)
!       MD5Transform (context->state, &input[i]);
!     
!     index = 0;
!   }
!   else
!     i = 0;
!   
!   /* Buffer remaining input */
!   MD5_memcpy 
!     ((POINTER)&context->buffer[index], (POINTER)&input[i],
!      inputLen-i);
  }
  
! /* MD5 finalization. Ends an MD5 message-digest operation, writing the
!      the message digest and zeroizing the context.
   */
! void MD5Final (unsigned char digest[16], MD5_CTX *context)
  {
!   unsigned char bits[8];
!   unsigned int index, padLen;
! 
!   /* Save number of bits */
!   Encode (bits, context->count, 8);
! 
!   /* Pad out to 56 mod 64.
!    */
!   index = (unsigned int)((context->count[0] >> 3) & 0x3f);
!   padLen = (index < 56) ? (56 - index) : (120 - index);
!   MD5Update (context, PADDING, padLen);
    
!   /* Append length (before padding) */
!   MD5Update (context, bits, 8);
! 
!   /* Store state in digest */
!   Encode (digest, context->state, 16);
!   
!   /* Zeroize sensitive information.
!    */
!   MD5_memset ((POINTER)context, 0, sizeof (*context));
  }

Index: md5.h
===================================================================
RCS file: /cvsroot/olsrd/olsrd-current/lib/secure/src/md5.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** md5.h	30 Jan 2007 17:11:52 -0000	1.2
--- md5.h	2 Nov 2007 15:51:46 -0000	1.3
***************
*** 27,36 ****
  #define _MD5_H_
  
  /* POINTER defines a generic pointer type */
! typedef unsigned char *POINTER;
  /* UINT2 defines a two byte word */
! typedef unsigned short int UINT2;
  /* UINT4 defines a four byte word */
! typedef unsigned long int UINT4;
  
  /* MD5 context. */
--- 27,38 ----
  #define _MD5_H_
  
+ #include <inttypes.h>
+ 
  /* POINTER defines a generic pointer type */
! typedef uint8_t *POINTER;
  /* UINT2 defines a two byte word */
! typedef uint16_t UINT2;
  /* UINT4 defines a four byte word */
! typedef uint32_t UINT4;
  
  /* MD5 context. */





More information about the Olsr-cvs mailing list