minor cleanup

This commit is contained in:
bodea 2006-12-05 05:22:59 +00:00
parent 426335628c
commit 007f5d2cac
2 changed files with 190 additions and 191 deletions

52
md5.c
View file

@ -15,10 +15,7 @@
* and avoid compile-time configuration.
*/
#ifndef HAVE_OPENSSL
#include <string.h>
#include "md5.h"
/*
@ -49,18 +46,16 @@
* doesn't work.
*/
#if defined(__i386__) || defined(__vax__)
#define SET(n) \
(*(MD5_u32plus *)&ptr[(n) * 4])
#define GET(n) \
SET(n)
# define SET(n) (*(MD5_u32plus *)&ptr[(n) * 4])
# define GET(n) SET(n)
#else
#define SET(n) \
# define SET(n) \
(ctx->block[(n)] = \
(MD5_u32plus)ptr[(n) * 4] | \
((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \
((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \
((MD5_u32plus)ptr[(n) * 4 + 3] << 24))
#define GET(n) \
# define GET(n) \
(ctx->block[(n)])
#endif
@ -87,7 +82,7 @@ static void *body(MD5_CTX *ctx, void *data, unsigned long size)
saved_c = c;
saved_d = d;
/* Round 1 */
/* Round 1 */
STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
@ -105,7 +100,7 @@ static void *body(MD5_CTX *ctx, void *data, unsigned long size)
STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
/* Round 2 */
/* Round 2 */
STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
@ -123,7 +118,7 @@ static void *body(MD5_CTX *ctx, void *data, unsigned long size)
STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
/* Round 3 */
/* Round 3 */
STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
@ -141,7 +136,7 @@ static void *body(MD5_CTX *ctx, void *data, unsigned long size)
STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
/* Round 4 */
/* Round 4 */
STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
@ -164,8 +159,8 @@ static void *body(MD5_CTX *ctx, void *data, unsigned long size)
c += saved_c;
d += saved_d;
ptr += 64;
} while (size -= 64);
ptr += MD5_BLOCK_SZ;
} while (size -= MD5_BLOCK_SZ);
ctx->a = a;
ctx->b = b;
@ -194,14 +189,17 @@ void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
saved_lo = ctx->lo;
if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
ctx->hi++;
ctx->hi += size >> 29;
used = saved_lo & 0x3f;
if (used) {
free = 64 - used;
if (used)
{
free = MD5_BLOCK_SZ - used;
if (size < free) {
if (size < free)
{
memcpy(&ctx->buffer[used], data, size);
return;
}
@ -209,10 +207,11 @@ void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size)
memcpy(&ctx->buffer[used], data, free);
data = (unsigned char *)data + free;
size -= free;
body(ctx, ctx->buffer, 64);
body(ctx, ctx->buffer, MD5_BLOCK_SZ);
}
if (size >= 64) {
if (size >= MD5_BLOCK_SZ)
{
data = body(ctx, data, size & ~(unsigned long)0x3f);
size &= 0x3f;
}
@ -228,13 +227,14 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx)
ctx->buffer[used++] = 0x80;
free = 64 - used;
free = MD5_BLOCK_SZ - used;
if (free < 8) {
if (free < 8)
{
memset(&ctx->buffer[used], 0, free);
body(ctx, ctx->buffer, 64);
body(ctx, ctx->buffer, MD5_BLOCK_SZ);
used = 0;
free = 64;
free = MD5_BLOCK_SZ;
}
memset(&ctx->buffer[used], 0, free - 8);
@ -249,7 +249,7 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx)
ctx->buffer[62] = ctx->hi >> 16;
ctx->buffer[63] = ctx->hi >> 24;
body(ctx, ctx->buffer, 64);
body(ctx, ctx->buffer, MD5_BLOCK_SZ);
result[0] = ctx->a;
result[1] = ctx->a >> 8;
@ -270,5 +270,3 @@ void MD5_Final(unsigned char *result, MD5_CTX *ctx)
memset(ctx, 0, sizeof(*ctx));
}
#endif

15
md5.h
View file

@ -6,10 +6,11 @@
* in the public domain. See md5.c for more information.
*/
#ifdef HAVE_OPENSSL
#include <openssl/md5.h>
#elif !defined(_MD5_H)
#define _MD5_H
#ifndef __MD5_H__
#define __MD5_H__
#define MD5_DIGEST_SZ 16
#define MD5_BLOCK_SZ 64
/* Any 32-bit or wider unsigned integer data type will do */
typedef unsigned long MD5_u32plus;
@ -17,12 +18,12 @@ typedef unsigned long MD5_u32plus;
typedef struct {
MD5_u32plus lo, hi;
MD5_u32plus a, b, c, d;
unsigned char buffer[64];
MD5_u32plus block[16];
unsigned char buffer[MD5_BLOCK_SZ];
MD5_u32plus block[MD5_DIGEST_SZ];
} MD5_CTX;
extern void MD5_Init(MD5_CTX *ctx);
extern void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size);
extern void MD5_Final(unsigned char *result, MD5_CTX *ctx);
#endif
#endif /* __MD5_H__ */