]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
Add my own HMAC implementation (verified using the test vectors). Planning to use...
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 9 Apr 2009 06:23:27 +0000 (06:23 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Thu, 9 Apr 2009 06:23:27 +0000 (06:23 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8884 d7cf8633-e32d-0410-b094-e92efae38249

hmac.c [new file with mode: 0644]
hmac.h [new file with mode: 0644]
makefile.inc

diff --git a/hmac.c b/hmac.c
new file mode 100644 (file)
index 0000000..eb254e2
--- /dev/null
+++ b/hmac.c
@@ -0,0 +1,57 @@
+#include "quakedef.h"
+#include "hmac.h"
+
+void hmac(
+       hashfunc_t hfunc, int hlen, int hblock,
+       unsigned char *out,
+       unsigned char *in, int n,
+       unsigned char *key, int k
+)
+{
+       unsigned char hashbuf[32];
+       unsigned char k_xor_ipad[128];
+       unsigned char k_xor_opad[128];
+       unsigned char catbuf[256];
+       int i;
+
+       if(sizeof(hashbuf) < (size_t) hlen)
+               Host_Error("Invalid hash function used for HMAC - too long hash length");
+       if(sizeof(k_xor_ipad) < (size_t) hblock)
+               Host_Error("Invalid hash function used for HMAC - too long hash block length");
+       if(sizeof(catbuf) < (size_t) hblock + (size_t) hlen)
+               Host_Error("Invalid hash function used for HMAC - too long hash block length");
+       if(sizeof(catbuf) < (size_t) hblock + (size_t) n)
+               Host_Error("Invalid hash function used for HMAC - too long message length");
+
+       if(k > hblock)
+       {
+               // hash the key if it is too long
+               // NO! that makes it too short if hblock != hlen
+               // just shorten it, then
+               // hfunc(hashbuf, key, k);
+               // key = hashbuf;
+               k = hblock;
+       }
+       else if(k < hblock)
+       {
+               // zero pad the key if it is too short
+               memcpy(k_xor_opad, key, k);
+               for(i = k; i < hblock; ++i)
+                       k_xor_opad[i] = 0;
+               key = k_xor_opad;
+               k = hblock;
+       }
+
+       for(i = 0; i < hblock; ++i)
+       {
+               k_xor_ipad[i] = key[i] ^ 0x36;
+               k_xor_opad[i] = key[i] ^ 0x5c;
+       }
+
+       memcpy(catbuf, k_xor_ipad, hblock);
+       memcpy(catbuf + hblock, in, n);
+       hfunc(hashbuf, catbuf, hblock + n);
+       memcpy(catbuf, k_xor_opad, hblock);
+       memcpy(catbuf + hblock, hashbuf, hlen);
+       hfunc(out, catbuf, hblock + hlen);
+}
diff --git a/hmac.h b/hmac.h
new file mode 100644 (file)
index 0000000..d2a327b
--- /dev/null
+++ b/hmac.h
@@ -0,0 +1,14 @@
+#ifndef HMAC_H
+#define HMAC_H
+
+typedef void (*hashfunc_t) (unsigned char *out, unsigned char *in, int n);
+void hmac(
+       hashfunc_t hfunc, int hlen, int hblock,
+       unsigned char *out,
+       unsigned char *in, int n,
+       unsigned char *key, int k
+);
+
+#define HMAC_MDFOUR_16BYTES(out, in, n, key, k) hmac(mdfour, 16, 64, out, in, n, key, k)
+
+#endif
index 5bd7de85bcecbc691fed86de97ef55219006daf0..f5e3e6280765717cf495b8da889ebdb4b0ba7bf2 100644 (file)
@@ -107,6 +107,7 @@ OBJ_COMMON= \
        gl_rmain.o \
        gl_rsurf.o \
        gl_textures.o \
+       hmac.o \
        host.o \
        host_cmd.o \
        image.o \