unsigned char* md4_MemBlock(const unsigned char* buf, size_t size, HASH_ctx* ctx) { md4_init(ctx); md4_update(ctx, buf, size); md4_final(ctx, buf, size); return ctx->MD4_result; }
/*! Resets the object. */ void QCryptographicHash::reset() { switch (d->method) { case Sha1: sha1InitState(&d->sha1Context); break; #ifdef QT_CRYPTOGRAPHICHASH_ONLY_SHA1 default: Q_ASSERT_X(false, "QCryptographicHash", "Method not compiled in"); Q_UNREACHABLE(); break; #else case Md4: md4_init(&d->md4Context); break; case Md5: MD5Init(&d->md5Context); break; case Sha224: SHA224Reset(&d->sha224Context); break; case Sha256: SHA256Reset(&d->sha256Context); break; case Sha384: SHA384Reset(&d->sha384Context); break; case Sha512: SHA512Reset(&d->sha512Context); break; case RealSha3_224: case Keccak_224: sha3Init(&d->sha3Context, 224); break; case RealSha3_256: case Keccak_256: sha3Init(&d->sha3Context, 256); break; case RealSha3_384: case Keccak_384: sha3Init(&d->sha3Context, 384); break; case RealSha3_512: case Keccak_512: sha3Init(&d->sha3Context, 512); break; #endif } d->result.clear(); }
/*! Resets the object. */ void QCryptographicHash::reset() { switch (d->method) { case Md4: md4_init(&d->md4Context); break; case Md5: MD5Init(&d->md5Context); break; case Sha1: sha1InitState(&d->sha1Context); break; } d->result.clear(); }
/** Self-test the hash @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled */ int md4_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else static const struct md4_test_case { const char *input; unsigned char hash[16]; } tests[] = { { "", {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; unsigned char tmp[16]; hash_state md; for(i = 0; i < (int)(sizeof(tests) / sizeof(tests[0])); i++) { md4_init(&md); md4_process(&md, (unsigned char *)tests[i].input, (unsigned long)strlen(tests[i].input)); md4_done(&md, tmp); if (compare_testvector(tmp, sizeof(tmp), tests[i].hash, sizeof(tests[i].hash), "MD4", i)) { return CRYPT_FAIL_TESTVECTOR; } } return CRYPT_OK; #endif }
/** Self-test the hash @return CRYPT_OK if successful, CRYPT_NOP if self-tests have been disabled */ INT md4_test(void) { #ifndef LTC_TEST return CRYPT_NOP; #else static const struct md4_test_case { char *input; UCHAR 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; hash_state md; UCHAR digest[16]; for(i = 0; i < (INT)(sizeof(cases) / sizeof(cases[0])); i++) { md4_init(&md); md4_process(&md, (UCHAR *)cases[i].input, (unsigned long)strlen(cases[i].input)); md4_done(&md, digest); if (XMEMCMP(digest, cases[i].digest, 16) != 0) { return CRYPT_FAIL_TESTVECTOR; } } return CRYPT_OK; #endif }
void handle_md4(array_t *out, bio_t *in) { md4_ctx_t ctx; char hex[sizeof(ctx.hash) * 2]; size_t r; md4_init(&ctx); do { r = bio_feed(in); md4_update(&ctx, BIO_PEEK(in), r); BIO_SEEK(in, r); } while (r > 0); md4_finish(&ctx); array_append(out, hex, fmt_hex_pad(hex, ctx.hash, sizeof(ctx.hash))); array_append_null(out); }
int main() { md4_ctx_t ctx; char hex_str[33]; char digest[16]; int i; uint32_t vector[][4] = { { 0x31d6cfe0, 0xd16ae931, 0xb73c59d7, 0xe0c089c0 }, { 0xbde52cb3, 0x1de33e46, 0x245e05fb, 0xdbd6fb24 }, { 0xa448017a, 0xaf21d852, 0x5fc10ae8, 0x7aa6729d }, { 0xd9130a81, 0x64549fe8, 0x18874806, 0xe1c7014b }, { 0xd79e1c30, 0x8aa5bbcd, 0xeea8ed63, 0xdf412da9 }, { 0x043f8582, 0xf241db35, 0x1ce627e1, 0x53e7f0e4 }, { 0xe33b4ddc, 0x9c38f219, 0x9c3e7b16, 0x4fcc0536 }, { 0xe33b4ddc, 0x9c38f219, 0x9c3e7b16, 0x4fcc0536 } }; md4s(&ctx, ""); for (i = 0; i < 4; i++) { if (vector[0][i] != be32toh(ctx.hash[i])) return 1; } md4s(&ctx, "a"); for (i = 0; i < 4; i++) { if (vector[1][i] != be32toh(ctx.hash[i])) return 2; } md4s(&ctx, "abc"); for (i = 0; i < 4; i++) { if (vector[2][i] != be32toh(ctx.hash[i])) return 3; } md4s(&ctx, "message digest"); for (i = 0; i < 4; i++) { if (vector[3][i] != be32toh(ctx.hash[i])) return 4; } md4s(&ctx, "abcdefghijklmnopqrstuvwxyz"); for (i = 0; i < 4; i++) { if (vector[4][i] != be32toh(ctx.hash[i])) return 5; } md4s(&ctx, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"); for (i = 0; i < 4; i++) { if (vector[5][i] != be32toh(ctx.hash[i])) return 6; } md4s(&ctx, "12345678901234567890123456789012345678901234567890123456789012345678901234567890"); for (i = 0; i < 4; i++) { if (vector[6][i] != be32toh(ctx.hash[i])) return 7; } md4_init(&ctx); md4_update(&ctx, "1234567890", 10); md4_update(&ctx, "1234567890", 10); md4_update(&ctx, "1234567890", 10); md4_update(&ctx, "1234567890", 10); md4_update(&ctx, "1234567890", 10); md4_update(&ctx, "1234567890", 10); md4_update(&ctx, "1234567890", 10); md4_update(&ctx, "1234567890", 10); md4_finish(&ctx); for (i = 0; i < 4; i++) { if (vector[7][i] != be32toh(ctx.hash[i])) return 8; } return 0; }