From c00d8b7fd277441232af65aa87da89b27ef2e7f0 Mon Sep 17 00:00:00 2001 From: divverent Date: Thu, 9 Apr 2009 06:23:27 +0000 Subject: [PATCH] Add my own HMAC implementation (verified using the test vectors). Planning to use HMAC-MD4 for an alternate rcon authentication. git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8884 d7cf8633-e32d-0410-b094-e92efae38249 --- hmac.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++ hmac.h | 14 +++++++++++++ makefile.inc | 1 + 3 files changed, 72 insertions(+) create mode 100644 hmac.c create mode 100644 hmac.h diff --git a/hmac.c b/hmac.c new file mode 100644 index 00000000..eb254e21 --- /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 index 00000000..d2a327b6 --- /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 diff --git a/makefile.inc b/makefile.inc index 5bd7de85..f5e3e628 100644 --- a/makefile.inc +++ b/makefile.inc @@ -107,6 +107,7 @@ OBJ_COMMON= \ gl_rmain.o \ gl_rsurf.o \ gl_textures.o \ + hmac.o \ host.o \ host_cmd.o \ image.o \ -- 2.39.2