Set mangle to create a base64-encoded hmac-md5
[pwdhash.git] / md5-cl.c
index aee9c1e..4ebe217 100644 (file)
--- a/md5-cl.c
+++ b/md5-cl.c
@@ -5,6 +5,10 @@
 
 #include "md5-cl.h"
 
+const u8 domain[] = "flypig.co.uk";
+const u32x domain_len = 12;
+
+
 #define MD5_STEP(f,a,b,c,d,x,K,s)   \
 {                                   \
   a += K;                           \
@@ -114,7 +118,7 @@ u32x rotl32 (const u32x a, const u32 n)
  // return rotate (a, n);
 }
 
-void md5_transform (const u32x w0[4], const u32x w1[4], const u32x w2[4], const u32x w3[4], u32x digest[4])
+void md5_transform_cl (const u32x w0[4], const u32x w1[4], const u32x w2[4], const u32x w3[4], u32x digest[4])
 {
   u32x a = digest[0];
   u32x b = digest[1];
@@ -354,7 +358,7 @@ void append_0x80_2x4_VV (u32x w0[4], u32x w1[4], const u32x offset)
 
 // HMAC MD5 ///////////////////////////////////////////////////////
 
-void hmac_md5_pad (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4], u32x opad[4])
+void hmac_md5_pad_cl (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4], u32x opad[4])
 {
   w0[0] = w0[0] ^ 0x36363636;
   w0[1] = w0[1] ^ 0x36363636;
@@ -378,7 +382,7 @@ void hmac_md5_pad (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4],
   ipad[2] = MD5M_C;
   ipad[3] = MD5M_D;
 
-  md5_transform (w0, w1, w2, w3, ipad);
+  md5_transform_cl (w0, w1, w2, w3, ipad);
 
   w0[0] = w0[0] ^ 0x6a6a6a6a;
   w0[1] = w0[1] ^ 0x6a6a6a6a;
@@ -402,17 +406,17 @@ void hmac_md5_pad (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4],
   opad[2] = MD5M_C;
   opad[3] = MD5M_D;
 
-  md5_transform (w0, w1, w2, w3, opad);
+  md5_transform_cl (w0, w1, w2, w3, opad);
 }
 
-void hmac_md5_run (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4], u32x opad[4], u32x digest[4])
+void hmac_md5_run_cl (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4], u32x opad[4], u32x digest[4])
 {
   digest[0] = ipad[0];
   digest[1] = ipad[1];
   digest[2] = ipad[2];
   digest[3] = ipad[3];
 
-  md5_transform (w0, w1, w2, w3, digest);
+  md5_transform_cl (w0, w1, w2, w3, digest);
 
   w0[0] = digest[0];
   w0[1] = digest[1];
@@ -436,7 +440,67 @@ void hmac_md5_run (u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], u32x ipad[4],
   digest[2] = opad[2];
   digest[3] = opad[3];
 
-  md5_transform (w0, w1, w2, w3, digest);
+  md5_transform_cl (w0, w1, w2, w3, digest);
+}
+
+void md5hmac_none(u8 const * const inData, const u32x pw_len, u8 outDigest[16])
+{
+        u32 pos;
+        u32 boundary;
+
+               boundary = pw_len;
+               if (boundary > 16) {
+                       boundary = 16;
+               }
+
+        for (pos = 0; pos < boundary; pos++) {
+                outDigest[pos] = inData[pos];
+        }
+        for (pos = boundary; pos < 16; pos++) {
+                outDigest[pos] = 0;
+        }
+}
+
+void md5hmac_domain(u8 const * const inData, const u32x pw_len, u8 outDigest[16])
+{
+        u32 pos;
+
+        // data
+
+        u32x data_buf[16];
+
+        for (pos = 0; pos < pw_len; pos++) {
+                ((u8 *)data_buf)[pos] = inData[pos];
+        }
+        for (pos = pw_len; pos < 64; pos++) {
+                ((u8 *)data_buf)[pos] = 0;
+        }
+
+        // key
+
+        u32x key_buf[16];
+
+        for (pos = 0; pos < domain_len; pos++) {
+                ((u8 *)key_buf)[pos] = domain[pos];
+        }
+        for (pos = domain_len; pos < 64; pos++) {
+                ((u8 *)key_buf)[pos] = 0;
+        }
+
+        // pads
+
+        u32x ipad[4];
+        u32x opad[4];
+
+        hmac_md5_pad_cl (key_buf, key_buf + 4, key_buf + 8, key_buf + 12, ipad, opad);
+
+        // loop
+
+        append_0x80_2x4_VV (data_buf, data_buf + 4, pw_len);
+
+        data_buf[14] = (64 + pw_len) * 8;
+
+        hmac_md5_run_cl (data_buf, data_buf + 4, data_buf + 8, data_buf + 12, ipad, opad, (u32x *)outDigest);
 }
 
 void md5hmac_cl(u8 * inKey, u32 key_len, u8 * inData, u32 pw_len, u8 outDigest[DIGEST_SIZE])
@@ -476,7 +540,7 @@ void md5hmac_cl(u8 * inKey, u32 key_len, u8 * inData, u32 pw_len, u8 outDigest[D
   u32x ipad[4];
   u32x opad[4];
 
-  hmac_md5_pad (key_buf, key_buf + 4, key_buf + 8, key_buf + 12, ipad, opad);
+  hmac_md5_pad_cl (key_buf, key_buf + 4, key_buf + 8, key_buf + 12, ipad, opad);
 
   /**
    * loop
@@ -486,7 +550,7 @@ void md5hmac_cl(u8 * inKey, u32 key_len, u8 * inData, u32 pw_len, u8 outDigest[D
 
   data_buf[14] = (64 + pw_len) * 8;
 
-  hmac_md5_run (data_buf, data_buf + 4, data_buf + 8, data_buf + 12, ipad, opad, (u32x *)outDigest);
+  hmac_md5_run_cl (data_buf, data_buf + 4, data_buf + 8, data_buf + 12, ipad, opad, (u32x *)outDigest);
 }