Commit f23a2145a019776f827c11c03474761ba0b44e48

Guillem Jover 2018-10-06T03:46:51

Add compatibility with Aladdin Enterprises MD5 implementation This will be guarded by a LIBMD_MD5_ALADDIN macro check, to avoid polluting the namespace by unsuspecting code. The macro can always be defined unconditionally, and the guard could even potentially be removed in the future if required. Prompted-by: Yangfl <mmyangfl@gmail.com> Ref: https://bugs.debian.org/909116

diff --git a/include/md5.h b/include/md5.h
index dee2bf4..596b276 100644
--- a/include/md5.h
+++ b/include/md5.h
@@ -47,4 +47,24 @@ char	*MD5Data(const uint8_t *, size_t, char *);
 }
 #endif
 
+/* Avoid polluting the namespace. Even though this makes this usage
+ * implementation-specific, defining it unconditionally should not be
+ * a problem, and better than possibly breaking unexpecting code. */
+#ifdef LIBMD_MD5_ALADDIN
+
+/*
+ * Interface compatibility with Aladdin Enterprises independent
+ * implementation from RFC 1321.
+ */
+
+typedef uint8_t md5_byte_t;
+typedef uint32_t md5_word_t;
+typedef MD5_CTX md5_state_t;
+
+#define md5_init(pms) MD5Init(pms)
+#define md5_append(pms, data, nbytes) MD5Update(pms, data, nbytes)
+#define md5_finish(pms, digest) MD5Final(digest, pms)
+
+#endif /* LIBMD_MD5_ALADDIN */
+
 #endif /* _MD5_H_ */
diff --git a/test/md5.c b/test/md5.c
index 080d285..345c257 100644
--- a/test/md5.c
+++ b/test/md5.c
@@ -24,6 +24,8 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#define LIBMD_MD5_ALADDIN 1
+
 #include <config.h>
 
 #include <assert.h>
@@ -34,12 +36,34 @@
 
 DEF_TEST_DIGEST(MD5, MD5)
 
+void
+test_MD5_aladdin(const char *hash_str_ref, const char *data)
+{
+	uint8_t hash_bin_ref[MD5_DIGEST_LENGTH];
+	uint8_t hash_bin_got[MD5_DIGEST_LENGTH];
+	md5_state_t pms;
+
+	hex2bin(hash_bin_ref, hash_str_ref, MD5_DIGEST_LENGTH);
+
+	md5_init(&pms);
+	md5_append(&pms, data, strlen(data));
+	md5_finish(&pms, hash_bin_got);
+	assert(memcmp(hash_bin_ref, hash_bin_got, MD5_DIGEST_LENGTH) == 0);
+}
+
+void
+test_MD5_all(const char *hash_str_ref, const char *data)
+{
+	test_MD5(hash_str_ref, data);
+	test_MD5_aladdin(hash_str_ref, data);
+}
+
 int
 main()
 {
-	test_MD5("d41d8cd98f00b204e9800998ecf8427e", "");
-	test_MD5("900150983cd24fb0d6963f7d28e17f72", "abc");
-	test_MD5("827ccb0eea8a706c4c34a16891f84e7b", "12345");
+	test_MD5_all("d41d8cd98f00b204e9800998ecf8427e", "");
+	test_MD5_all("900150983cd24fb0d6963f7d28e17f72", "abc");
+	test_MD5_all("827ccb0eea8a706c4c34a16891f84e7b", "12345");
 
 	return 0;
 }