#include <stdio.h>
#include "ltc_md4.h"
#include "ltc_hashcommon.h"
#include "tomcrypt_cfg.h"
#include "tomcrypt_macros.h"
#include "tomcrypt_argchk.h"
#include "ccDescriptors.h"
#include "ccErrors.h"
#include "ccMemory.h"
#include "CommonDigest.h"
const ccDescriptor ltc_md4_desc =
{
.implementation_info = &cc_md4_impinfo,
.dtype.digest.hashsize = CC_MD4_DIGEST_LENGTH,
.dtype.digest.blocksize = CC_MD4_BLOCK_BYTES,
.dtype.digest.digest_info = NULL,
.dtype.digest.init = <c_md4_init,
.dtype.digest.process = <c_md4_process,
.dtype.digest.done = <c_md4_done,
};
#define S11 3
#define S12 7
#define S13 11
#define S14 19
#define S21 3
#define S22 5
#define S23 9
#define S24 13
#define S31 3
#define S32 9
#define S33 11
#define S34 15
#define F(x, y, z) (z ^ (x & (y ^ z)))
#define G(x, y, z) ((x & y) | (z & (x | y)))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define ROTATE_LEFT(x, n) LTC_ROLc(x, n)
#define FF(a, b, c, d, x, s) { \
(a) += F ((b), (c), (d)) + (x); \
(a) = ROTATE_LEFT ((a), (s)); \
}
#define GG(a, b, c, d, x, s) { \
(a) += G ((b), (c), (d)) + (x) + 0x5a827999UL; \
(a) = ROTATE_LEFT ((a), (s)); \
}
#define HH(a, b, c, d, x, s) { \
(a) += H ((b), (c), (d)) + (x) + 0x6ed9eba1UL; \
(a) = ROTATE_LEFT ((a), (s)); \
}
#ifdef LTC_CLEAN_STACK
static int _md4_compress(ltc_md4_ctx *ctx, const unsigned char *buf)
#else
static int md4_compress(ltc_md4_ctx *ctx, const unsigned char *buf)
#endif
{
ulong32 x[16], a, b, c, d;
int i;
a = ctx->state[0];
b = ctx->state[1];
c = ctx->state[2];
d = ctx->state[3];
for (i = 0; i < 16; i++) {
LTC_LOAD32L(x[i], buf + (4*i));
}
FF (a, b, c, d, x[ 0], S11);
FF (d, a, b, c, x[ 1], S12);
FF (c, d, a, b, x[ 2], S13);
FF (b, c, d, a, x[ 3], S14);
FF (a, b, c, d, x[ 4], S11);
FF (d, a, b, c, x[ 5], S12);
FF (c, d, a, b, x[ 6], S13);
FF (b, c, d, a, x[ 7], S14);
FF (a, b, c, d, x[ 8], S11);
FF (d, a, b, c, x[ 9], S12);
FF (c, d, a, b, x[10], S13);
FF (b, c, d, a, x[11], S14);
FF (a, b, c, d, x[12], S11);
FF (d, a, b, c, x[13], S12);
FF (c, d, a, b, x[14], S13);
FF (b, c, d, a, x[15], S14);
GG (a, b, c, d, x[ 0], S21);
GG (d, a, b, c, x[ 4], S22);
GG (c, d, a, b, x[ 8], S23);
GG (b, c, d, a, x[12], S24);
GG (a, b, c, d, x[ 1], S21);
GG (d, a, b, c, x[ 5], S22);
GG (c, d, a, b, x[ 9], S23);
GG (b, c, d, a, x[13], S24);
GG (a, b, c, d, x[ 2], S21);
GG (d, a, b, c, x[ 6], S22);
GG (c, d, a, b, x[10], S23);
GG (b, c, d, a, x[14], S24);
GG (a, b, c, d, x[ 3], S21);
GG (d, a, b, c, x[ 7], S22);
GG (c, d, a, b, x[11], S23);
GG (b, c, d, a, x[15], S24);
HH (a, b, c, d, x[ 0], S31);
HH (d, a, b, c, x[ 8], S32);
HH (c, d, a, b, x[ 4], S33);
HH (b, c, d, a, x[12], S34);
HH (a, b, c, d, x[ 2], S31);
HH (d, a, b, c, x[10], S32);
HH (c, d, a, b, x[ 6], S33);
HH (b, c, d, a, x[14], S34);
HH (a, b, c, d, x[ 1], S31);
HH (d, a, b, c, x[ 9], S32);
HH (c, d, a, b, x[ 5], S33);
HH (b, c, d, a, x[13], S34);
HH (a, b, c, d, x[ 3], S31);
HH (d, a, b, c, x[11], S32);
HH (c, d, a, b, x[ 7], S33);
HH (b, c, d, a, x[15], S34);
ctx->state[0] = ctx->state[0] + a;
ctx->state[1] = ctx->state[1] + b;
ctx->state[2] = ctx->state[2] + c;
ctx->state[3] = ctx->state[3] + d;
return CRYPT_OK;
}
#ifdef LTC_CLEAN_STACK
static int md4_compress(ltc_md4_ctx *ctx, const unsigned char *buf)
{
int err;
err = _md4_compress(ctx, buf);
ltc_burn_stack(sizeof(ulong32) * 20 + sizeof(int));
return err;
}
#endif
int ltc_md4_init(ltc_md4_ctx *ctx)
{
LTC_ARGCHK(ctx != NULL);
ctx->state[0] = 0x67452301UL;
ctx->state[1] = 0xefcdab89UL;
ctx->state[2] = 0x98badcfeUL;
ctx->state[3] = 0x10325476UL;
ctx->length = 0;
ctx->curlen = 0;
return CRYPT_OK;
}
LTC_HASH_PROCESS(ltc_md4_process, md4_compress, ltc_md4_ctx, md4, 64)
int ltc_md4_done(ltc_md4_ctx *ctx, unsigned char *out)
{
int i;
LTC_ARGCHK(ctx != NULL);
LTC_ARGCHK(out != NULL);
if (ctx->curlen >= sizeof(ctx->buf)) {
return CRYPT_INVALID_ARG;
}
ctx->length += ctx->curlen * 8;
ctx->buf[ctx->curlen++] = (unsigned char)0x80;
if (ctx->curlen > 56) {
while (ctx->curlen < 64) {
ctx->buf[ctx->curlen++] = (unsigned char)0;
}
md4_compress(ctx, ctx->buf);
ctx->curlen = 0;
}
while (ctx->curlen < 56) {
ctx->buf[ctx->curlen++] = (unsigned char)0;
}
LTC_STORE64L(ctx->length, ctx->buf+56);
md4_compress(ctx, ctx->buf);
for (i = 0; i < 4; i++) {
LTC_STORE32L(ctx->state[i], out+(4*i));
}
#ifdef LTC_CLEAN_STACK
ltc_zeromem(md, sizeof(ltc_hash_state));
#endif
return CRYPT_OK;
}
int ltc_md4_test(void)
{
#ifndef LTC_TEST
return CRYPT_NOP;
#else
static const struct md4_test_case {
const char *input;
unsigned char digest[16];
} cases[] = {
{ "",
{0x31, 0xd6, 0xcf, 0xe0, 0xd1, 0x6a, 0xe9, 0x31,
0xb7, 0x3c, 0x59, 0xd7, 0xe0, 0xc0, 0x89, 0xc0} },
{ "a",
{0xbd, 0xe5, 0x2c, 0xb3, 0x1d, 0xe3, 0x3e, 0x46,
0x24, 0x5e, 0x05, 0xfb, 0xdb, 0xd6, 0xfb, 0x24} },
{ "abc",
{0xa4, 0x48, 0x01, 0x7a, 0xaf, 0x21, 0xd8, 0x52,
0x5f, 0xc1, 0x0a, 0xe8, 0x7a, 0xa6, 0x72, 0x9d} },
{ "message digest",
{0xd9, 0x13, 0x0a, 0x81, 0x64, 0x54, 0x9f, 0xe8,
0x18, 0x87, 0x48, 0x06, 0xe1, 0xc7, 0x01, 0x4b} },
{ "abcdefghijklmnopqrstuvwxyz",
{0xd7, 0x9e, 0x1c, 0x30, 0x8a, 0xa5, 0xbb, 0xcd,
0xee, 0xa8, 0xed, 0x63, 0xdf, 0x41, 0x2d, 0xa9} },
{ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789",
{0x04, 0x3f, 0x85, 0x82, 0xf2, 0x41, 0xdb, 0x35,
0x1c, 0xe6, 0x27, 0xe1, 0x53, 0xe7, 0xf0, 0xe4} },
{ "12345678901234567890123456789012345678901234567890123456789012345678901234567890",
{0xe3, 0x3b, 0x4d, 0xdc, 0x9c, 0x38, 0xf2, 0x19,
0x9c, 0x3e, 0x7b, 0x16, 0x4f, 0xcc, 0x05, 0x36} },
};
int i;
ltc_hash_state md;
unsigned char digest[16];
for(i = 0; i < (int)(sizeof(cases) / sizeof(cases[0])); i++) {
ltc_md4_init(&md);
ltc_md4_process(&md, (const unsigned char *)cases[i].input, (unsigned long)strlen(cases[i].input));
ltc_md4_done(&md, digest);
if (LTC_XMEMCMP(digest, cases[i].digest, 16) != 0) {
return CRYPT_FAIL_TESTVECTOR;
}
}
return CRYPT_OK;
#endif
}