md5.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
#pragma ident "%Z%%M% %I% %E% SMI"
#include "md5.h"
/*
**************************************************************************
** md5.c -- the source code for MD5 routines **
** RSA Data Security, Inc. MD5 Message-Digest Algorithm **
** Created: 2/17/90 RLR **
** Revised: 1/91 SRD, AJ, BSK, JT Reference C ver., 7/10 constant corr. **
**************************************************************************
*/
/*
**************************************************************************
** Copyright (C) 1990, RSA Data Security, Inc. All rights reserved. **
** **
** License to copy and use this software is granted provided that **
** it is identified as the "RSA Data Security, Inc. MD5 Message- **
** Digest Algorithm" in all material mentioning or referencing this **
** software or this function. **
** **
** License is also granted to make and use derivative works **
** provided that such works are identified as "derived from the RSA **
** Data Security, Inc. MD5 Message-Digest Algorithm" in all **
** material mentioning or referencing the derived work. **
** **
** RSA Data Security, Inc. makes no representations concerning **
** either the merchantability of this software or the suitability **
** of this software for any particular purpose. It is provided "as **
** is" without express or implied warranty of any kind. **
** **
** These notices must be retained in any copies of any part of this **
**************************************************************************
*/
/*
**************************************************************************
** Message-digest routines: **
** To form the message digest for a message M **
** (1) Initialize a context buffer mdContext using MD5Init **
** (2) Call MD5Update on mdContext and M **
** (3) Call MD5Final on mdContext **
** The message digest is now in mdContext->digest[0...15] **
**************************************************************************
*/
/* forward declaration */
static unsigned char PADDING[64] = {
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/* F, G, H and I are basic MD5 functions */
#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | (~z)))
/* ROTATE_LEFT rotates x left n bits */
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
/* Rotation is separate from addition to prevent recomputation */
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
(a) = ROTATE_LEFT ((a), (s)); \
(a) += (b); \
}
/*
* The routine MD5Init initializes the message-digest context
* mdContext. All fields are set to zero.
*/
{
/*
* Load magic initialization constants.
*/
}
/*
* The routine MD5Update updates the message-digest context to
* account for the presence of each of the characters inBuf[0..inLen-1]
* in the message whose digest is being computed.
*/
{
int mdi;
unsigned int i, ii;
/* compute number of bytes mod 64 */
/* update number of bits */
mdContext->i[1]++;
while (inLen--) {
/* add new character to buffer, increment mdi */
/* transform if necessary */
if (mdi == 0x40) {
mdi = 0;
}
}
}
/*
* The routine MD5Final terminates the message-digest computation and
* ends with the desired message digest in mdContext->digest[0...15].
*/
{
int mdi;
unsigned int i, ii;
unsigned int padLen;
/* save number of bits */
/* compute number of bytes mod 64 */
/* pad out to 56 mod 64 */
/* append length in bits and transform */
/* store buffer in digest */
}
}
/*
* Basic MD5 step. Transforms buf based on in.
*/
{
/* Round 1 */
#define S11 7
#define S12 12
#define S13 17
#define S14 22
/* Round 2 */
#define S21 5
#define S22 9
#define S23 14
#define S24 20
/* Round 3 */
#define S31 4
#define S32 11
#define S33 16
#define S34 23
/* Round 4 */
#define S41 6
#define S42 10
#define S43 15
#define S44 21
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}