update md5 module
This commit is contained in:
parent
8bdab8534c
commit
ad03540394
@ -1,8 +1,8 @@
|
|||||||
/* $OpenBSD: md5.c,v 1.7 2004/05/28 15:10:27 millert Exp $ */
|
/* $OpenBSD: md5.c,v 1.4 2014/12/28 10:04:35 tedu Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This code implements the MD5 message-digest algorithm.
|
* This code implements the MD5 message-digest algorithm.
|
||||||
* The algorithm is due to Ron Rivest. This code was
|
* The algorithm is due to Ron Rivest. This code was
|
||||||
* written by Colin Plumb in 1993, no copyright is claimed.
|
* written by Colin Plumb in 1993, no copyright is claimed.
|
||||||
* This code is in the public domain; do with it what you wish.
|
* This code is in the public domain; do with it what you wish.
|
||||||
*
|
*
|
||||||
@ -17,26 +17,37 @@
|
|||||||
* will fill a supplied 16-byte array with the digest.
|
* will fill a supplied 16-byte array with the digest.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifdef BUILDING_RECOLL
|
||||||
|
#include "autoconfig.h"
|
||||||
|
#else
|
||||||
|
#include "config.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "md5.h"
|
#include "md5.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define PUT_BIT_LE(i, cp, value) do { \
|
||||||
|
(cp)[i] = (uint8_t)(((value) >> 8 * i) & 0xFF); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#define PUT_64BIT_LE(cp, value) do { \
|
#define PUT_64BIT_LE(cp, value) do { \
|
||||||
(cp)[7] = (value) >> 56; \
|
PUT_BIT_LE(7, cp, value); \
|
||||||
(cp)[6] = (value) >> 48; \
|
PUT_BIT_LE(6, cp, value); \
|
||||||
(cp)[5] = (value) >> 40; \
|
PUT_BIT_LE(5, cp, value); \
|
||||||
(cp)[4] = (value) >> 32; \
|
PUT_BIT_LE(4, cp, value); \
|
||||||
(cp)[3] = (value) >> 24; \
|
PUT_BIT_LE(3, cp, value); \
|
||||||
(cp)[2] = (value) >> 16; \
|
PUT_BIT_LE(2, cp, value); \
|
||||||
(cp)[1] = (value) >> 8; \
|
PUT_BIT_LE(1, cp, value); \
|
||||||
(cp)[0] = (value); } while (0)
|
PUT_BIT_LE(0, cp, value); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
#define PUT_32BIT_LE(cp, value) do { \
|
#define PUT_32BIT_LE(cp, value) do { \
|
||||||
(cp)[3] = (value) >> 24; \
|
PUT_BIT_LE(3, cp, value); \
|
||||||
(cp)[2] = (value) >> 16; \
|
PUT_BIT_LE(2, cp, value); \
|
||||||
(cp)[1] = (value) >> 8; \
|
PUT_BIT_LE(1, cp, value); \
|
||||||
(cp)[0] = (value); } while (0)
|
PUT_BIT_LE(0, cp, value); \
|
||||||
|
} while (0)
|
||||||
|
|
||||||
static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
|
static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
|
||||||
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||||
@ -44,9 +55,6 @@ static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
|
|||||||
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
|
||||||
};
|
};
|
||||||
|
|
||||||
static void MD5Pad(MD5_CTX *);
|
|
||||||
static void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
* Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
|
||||||
* initialization constants.
|
* initialization constants.
|
||||||
@ -66,8 +74,9 @@ MD5Init(MD5_CTX *ctx)
|
|||||||
* of bytes.
|
* of bytes.
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
|
MD5Update(MD5_CTX *ctx, const void *inputptr, size_t len)
|
||||||
{
|
{
|
||||||
|
const uint8_t *input = (const uint8_t *)inputptr;
|
||||||
size_t have, need;
|
size_t have, need;
|
||||||
|
|
||||||
/* Check how many bytes we already have and how many more we need. */
|
/* Check how many bytes we already have and how many more we need. */
|
||||||
@ -100,14 +109,15 @@ MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Pad pad to 64-byte boundary with the bit pattern
|
* Final wrapup - pad to 64-byte boundary with the bit pattern
|
||||||
* 1 0* (64-bit count of bits processed, MSB-first)
|
* 1 0* (64-bit count of bits processed, MSB-first)
|
||||||
*/
|
*/
|
||||||
static void
|
void
|
||||||
MD5Pad(MD5_CTX *ctx)
|
MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
|
||||||
{
|
{
|
||||||
uint8_t count[8];
|
uint8_t count[8];
|
||||||
size_t padlen;
|
size_t padlen;
|
||||||
|
int i;
|
||||||
|
|
||||||
/* Convert count to 8 bytes in little endian order. */
|
/* Convert count to 8 bytes in little endian order. */
|
||||||
PUT_64BIT_LE(count, ctx->count);
|
PUT_64BIT_LE(count, ctx->count);
|
||||||
@ -119,22 +129,10 @@ MD5Pad(MD5_CTX *ctx)
|
|||||||
padlen += MD5_BLOCK_LENGTH;
|
padlen += MD5_BLOCK_LENGTH;
|
||||||
MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
|
MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */
|
||||||
MD5Update(ctx, count, 8);
|
MD5Update(ctx, count, 8);
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
for (i = 0; i < 4; i++)
|
||||||
* Final wrapup--call MD5Pad, fill in digest and zero out ctx.
|
PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
|
||||||
*/
|
memset(ctx, 0, sizeof(*ctx)); /* in case it's sensitive */
|
||||||
void
|
|
||||||
MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
MD5Pad(ctx);
|
|
||||||
if (digest != NULL) {
|
|
||||||
for (i = 0; i < 4; i++)
|
|
||||||
PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
|
|
||||||
memset(ctx, 0, sizeof(*ctx));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -155,12 +153,12 @@ MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
|
|||||||
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
* reflect the addition of 16 longwords of new data. MD5Update blocks
|
||||||
* the data and converts bytes into longwords for this routine.
|
* the data and converts bytes into longwords for this routine.
|
||||||
*/
|
*/
|
||||||
static void
|
void
|
||||||
MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
||||||
{
|
{
|
||||||
uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
|
uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
|
||||||
|
|
||||||
#ifndef WORDS_BIGENDIAN
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
||||||
memcpy(in, block, sizeof(in));
|
memcpy(in, block, sizeof(in));
|
||||||
#else
|
#else
|
||||||
for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
|
for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
|
||||||
@ -251,7 +249,6 @@ MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
|
|||||||
state[3] += d;
|
state[3] += d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// C++ utilities
|
// C++ utilities
|
||||||
using std::string;
|
using std::string;
|
||||||
|
|
||||||
@ -283,6 +280,7 @@ string& MD5HexPrint(const string& digest, string &out)
|
|||||||
}
|
}
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
string& MD5HexScan(const string& xdigest, string& digest)
|
string& MD5HexScan(const string& xdigest, string& digest)
|
||||||
{
|
{
|
||||||
digest.erase();
|
digest.erase();
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
/* $OpenBSD: md5.h,v 1.15 2004/05/03 17:30:14 millert Exp $ */
|
/* $OpenBSD: md5.h,v 1.3 2014/11/16 17:39:09 tedu Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This code implements the MD5 message-digest algorithm.
|
* This code implements the MD5 message-digest algorithm.
|
||||||
@ -15,22 +15,22 @@
|
|||||||
#ifndef _MD5_H_
|
#ifndef _MD5_H_
|
||||||
#define _MD5_H_
|
#define _MD5_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#define MD5_BLOCK_LENGTH 64
|
#define MD5_BLOCK_LENGTH 64
|
||||||
#define MD5_DIGEST_LENGTH 16
|
#define MD5_DIGEST_LENGTH 16
|
||||||
#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)
|
|
||||||
|
|
||||||
typedef struct MD5Context {
|
typedef struct MD5Context {
|
||||||
uint32_t state[4]; /* state */
|
uint32_t state[4]; /* state */
|
||||||
uint64_t count; /* number of bits, mod 2^64 */
|
uint64_t count; /* number of bits, mod 2^64 */
|
||||||
uint8_t buffer[MD5_BLOCK_LENGTH]; /* input buffer */
|
uint8_t buffer[MD5_BLOCK_LENGTH]; /* input buffer */
|
||||||
} MD5_CTX;
|
} MD5_CTX;
|
||||||
|
|
||||||
void MD5Init(MD5_CTX *);
|
void MD5Init(MD5_CTX *);
|
||||||
void MD5Update(MD5_CTX *, const uint8_t *, size_t);
|
void MD5Update(MD5_CTX *, const void *, size_t);
|
||||||
void MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *);
|
void MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *);
|
||||||
|
void MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH]);
|
||||||
|
|
||||||
/** md5 c++ utility wrappers */
|
/** md5 c++ utility wrappers */
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user