diff --git a/Makefile.am b/Makefile.am
index 2396a6f7..b04cee1a 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -44,6 +44,8 @@ UTIL_FILES = \
hash.hpp \
hash/md2.cpp \
hash/md2.hpp \
+ hash/md4.cpp \
+ hash/md4.hpp \
image.cpp \
image.hpp \
io.cpp \
diff --git a/hash/md4.cpp b/hash/md4.cpp
new file mode 100644
index 00000000..dae617b8
--- /dev/null
+++ b/hash/md4.cpp
@@ -0,0 +1,244 @@
+/*
+ * This file is part of libgim.
+ *
+ * libgim is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libgim is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libgim. If not, see .
+ *
+ * Copyright 2013 Danny Robson
+ */
+
+#include "md4.hpp"
+
+#include "bitwise.hpp"
+#include "types.hpp"
+#include "endian.hpp"
+
+#include
+
+
+using util::hash::MD4;
+using std::array;
+
+
+// Auxiliary functions for each set of rounds
+static inline uint32_t
+F (uint32_t X, uint32_t Y, uint32_t Z)
+ { return (X & Y) | (~X & Z); }
+
+static inline uint32_t
+G (uint32_t X, uint32_t Y, uint32_t Z)
+ { return (X & Y) | (X & Z) | (Y & Z); }
+
+static inline uint32_t
+H (uint32_t X, uint32_t Y, uint32_t Z)
+ { return X ^ Y ^ Z; }
+
+
+// Constructors and setup functions
+static const uint32_t DEFAULT_A = 0x67452301;
+static const uint32_t DEFAULT_B = 0xefcdab89;
+static const uint32_t DEFAULT_C = 0x98badcfe;
+static const uint32_t DEFAULT_D = 0x10325476;
+
+
+MD4::MD4 ()
+{
+ reset ();
+ static_assert (sizeof (MD4::X) == sizeof (MD4::Xb),
+ "Byte and word buffer size must match exactly");
+ static_assert (sizeof (MD4::ABCD) == sizeof (MD4::digest_t),
+ "Internal state must match the size of the digest");
+}
+
+
+void
+MD4::reset (void) {
+ m_total = 0;
+
+ ABCD[0] = DEFAULT_A;
+ ABCD[1] = DEFAULT_B;
+ ABCD[2] = DEFAULT_C;
+ ABCD[3] = DEFAULT_D;
+
+ memset (Xb, 0, sizeof (Xb));
+}
+
+
+void
+MD4::update (const void *data, size_t size)
+ { update (static_cast (data), size); }
+
+
+void
+MD4::update (const uint8_t *data, size_t size) {
+ size_t offset = m_total % sizeof (Xb);
+ size_t remain = sizeof (Xb) - offset;
+
+ if (size > remain) {
+ memcpy (Xb + offset, data, remain);
+ transform ();
+
+ m_total += remain;
+ size -= remain;
+ data += remain;
+
+ while (size >= sizeof (Xb)) {
+ memcpy (Xb, data, sizeof (Xb));
+ transform ();
+
+ m_total += sizeof (Xb);
+ size -= sizeof (Xb);
+ data += sizeof (Xb);
+ }
+
+ offset = 0;
+ }
+
+ memcpy (Xb + offset, data, size);
+ m_total += size;
+}
+
+
+MD4::digest_t
+MD4::digest (void) {
+ finish ();
+
+ digest_t d;
+ memcpy (d.data (), ABCD.data(), sizeof (ABCD));
+ return d;
+}
+
+
+void
+MD4::transform (void) {
+ uint32_t A = ABCD[0],
+ B = ABCD[1],
+ C = ABCD[2],
+ D = ABCD[3];
+
+ #define ROUND1(a,b,c,d,k,s) do { \
+ (a) += F((b), (c), (d)) + X[k]; \
+ (a) = rotatel ((a), (s)); \
+ } while (0)
+
+ ROUND1(A,B,C,D, 0, 3);
+ ROUND1(D,A,B,C, 1, 7);
+ ROUND1(C,D,A,B, 2, 11);
+ ROUND1(B,C,D,A, 3, 19);
+
+ ROUND1(A,B,C,D, 4, 3);
+ ROUND1(D,A,B,C, 5, 7);
+ ROUND1(C,D,A,B, 6, 11);
+ ROUND1(B,C,D,A, 7, 19);
+
+ ROUND1(A,B,C,D, 8, 3);
+ ROUND1(D,A,B,C, 9, 7);
+ ROUND1(C,D,A,B, 10, 11);
+ ROUND1(B,C,D,A, 11, 19);
+
+ ROUND1(A,B,C,D, 12, 3);
+ ROUND1(D,A,B,C, 13, 7);
+ ROUND1(C,D,A,B, 14, 11);
+ ROUND1(B,C,D,A, 15, 19);
+
+ #define ROUND2(a,b,c,d,k,s) do { \
+ (a) += G((b),(c),(d)) + X[k] + 0x5A827999u; \
+ (a) = rotatel ((a), (s)); \
+ } while (0)
+
+ ROUND2(A,B,C,D, 0, 3);
+ ROUND2(D,A,B,C, 4, 5);
+ ROUND2(C,D,A,B, 8, 9);
+ ROUND2(B,C,D,A, 12, 13);
+
+ ROUND2(A,B,C,D, 1, 3);
+ ROUND2(D,A,B,C, 5, 5);
+ ROUND2(C,D,A,B, 9, 9);
+ ROUND2(B,C,D,A, 13, 13);
+
+ ROUND2(A,B,C,D, 2, 3);
+ ROUND2(D,A,B,C, 6, 5);
+ ROUND2(C,D,A,B, 10, 9);
+ ROUND2(B,C,D,A, 14, 13);
+
+ ROUND2(A,B,C,D, 3, 3);
+ ROUND2(D,A,B,C, 7, 5);
+ ROUND2(C,D,A,B, 11, 9);
+ ROUND2(B,C,D,A, 15, 13);
+
+ #define ROUND3(a,b,c,d,k,s) do { \
+ (a) += H((b),(c),(d)) + X[k] + 0x6ED9EBA1u; \
+ (a) = rotatel ((a), (s)); \
+ } while (0)
+
+ ROUND3(A,B,C,D, 0, 3);
+ ROUND3(D,A,B,C, 8, 9);
+ ROUND3(C,D,A,B, 4, 11);
+ ROUND3(B,C,D,A, 12, 15);
+
+ ROUND3(A,B,C,D, 2, 3);
+ ROUND3(D,A,B,C, 10, 9);
+ ROUND3(C,D,A,B, 6, 11);
+ ROUND3(B,C,D,A, 14, 15);
+
+ ROUND3(A,B,C,D, 1, 3);
+ ROUND3(D,A,B,C, 9, 9);
+ ROUND3(C,D,A,B, 5, 11);
+ ROUND3(B,C,D,A, 13, 15);
+
+ ROUND3(A,B,C,D, 3, 3);
+ ROUND3(D,A,B,C, 11, 9);
+ ROUND3(C,D,A,B, 7, 11);
+ ROUND3(B,C,D,A, 15, 15);
+
+ ABCD[0] += A;
+ ABCD[1] += B;
+ ABCD[2] += C;
+ ABCD[3] += D;
+}
+
+
+void
+MD4::finish (void) {
+ uint64_t bits = m_total * 8;
+
+ {
+ // Pad with the mandatory 1 bit
+ size_t offset = m_total % sizeof (Xb);
+ Xb[offset] = 0x80;
+ }
+
+ {
+ // Pad the remainder with 0's, until 56 bytes
+ size_t offset = (m_total + 1) % sizeof (Xb);
+ size_t remain = (56 - offset % sizeof (Xb)) % sizeof (Xb);
+
+ if (offset > 56) {
+ memset (Xb + offset, 0, sizeof (Xb) - offset);
+ transform ();
+ remain -= sizeof (Xb) - offset;
+ offset = 0;
+ }
+
+ memset (Xb + offset, 0, remain);
+
+ // Put in the length (in bits) least significant first
+ for (size_t i = 0; i < sizeof (bits); ++i) {
+ Xb[56 + i] = bits & 0xFF;
+ bits >>= 8;
+ }
+
+ transform ();
+ }
+}
+
diff --git a/hash/md4.hpp b/hash/md4.hpp
new file mode 100644
index 00000000..07816b6c
--- /dev/null
+++ b/hash/md4.hpp
@@ -0,0 +1,56 @@
+/*
+ * This file is part of libgim.
+ *
+ * libgim is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later
+ * version.
+ *
+ * libgim is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with libgim. If not, see .
+ *
+ * Copyright 2013 Danny Robson
+ */
+
+#ifndef __UTIL_HASH_MD4_HPP
+#define __UTIL_HASH_MD4_HPP
+
+#include
+#include
+
+namespace util {
+ namespace hash {
+ class MD4 {
+ public:
+ typedef std::array digest_t;
+
+ public:
+ MD4();
+
+ void update (const void *data, size_t len);
+ void update (const uint8_t *data, size_t len);
+
+ digest_t digest (void);
+ void reset (void);
+
+ private:
+ void transform (void);
+ void finish (void);
+
+ uint64_t m_total;
+
+ std::array ABCD;
+ union {
+ uint32_t X [16];
+ uint8_t Xb[64];
+ };
+ };
+ }
+}
+
+#endif
diff --git a/test/.gitignore b/test/.gitignore
index 0bfff37d..ef5df41d 100644
--- a/test/.gitignore
+++ b/test/.gitignore
@@ -10,7 +10,7 @@
/*.log
/maths*
/matrix*
-/md2*
+/md[24]*
/option
/pool*
/range*
diff --git a/test/Makefile.am b/test/Makefile.am
index 32f51272..42ddc356 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -18,6 +18,7 @@ TEST_BIN = \
maths \
matrix \
md2 \
+ md4 \
option \
pool \
range \
@@ -60,6 +61,9 @@ matrix_SOURCES = matrix.cpp
md2_LDADD = $(builddir)/../libutil.la
md2_SOURCES = md2.cpp
+md4_LDADD = $(builddir)/../libutil.la
+md4_SOURCES = md4.cpp
+
option_LDADD = $(builddir)/../libutil.la
option_SOURCES = options/success.cpp
diff --git a/test/md4.cpp b/test/md4.cpp
new file mode 100644
index 00000000..c9e53f8e
--- /dev/null
+++ b/test/md4.cpp
@@ -0,0 +1,67 @@
+#include "../hash/md4.hpp"
+
+#include
+#include
+
+
+using util::hash::MD4;
+
+
+int
+main (int, char**) {
+ static const struct {
+ const char *input;
+ MD4::digest_t output;
+ } 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 } }
+ }
+ };
+
+ bool success = true;
+
+ for (auto i: TESTS) {
+ MD4 h;
+ h.update (i.input, strlen (i.input));
+ auto out = h.digest ();
+
+ if (out != i.output) {
+ std::cerr << "Failed on '" << i.input << "'\n";
+ success = false;
+ }
+ }
+
+ return success ? 0 : 1;
+}