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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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;
}