X-Git-Url: http://git.xonotic.org/?p=xonotic%2Fdarkplaces.git;a=blobdiff_plain;f=hmac.c;h=8b6d2f6234caa8e612d2710e35daaacc16bd40bb;hp=eb254e21bcbc6ef8898f9fd6f1d83464ce21ddd4;hb=90ab66ba6bfed05c0ce331d321ff63eba7912637;hpb=c00d8b7fd277441232af65aa87da89b27ef2e7f0 diff --git a/hmac.c b/hmac.c index eb254e21..8b6d2f62 100644 --- a/hmac.c +++ b/hmac.c @@ -1,41 +1,41 @@ -#include "quakedef.h" +#include "darkplaces.h" #include "hmac.h" -void hmac( +qbool hmac( hashfunc_t hfunc, int hlen, int hblock, unsigned char *out, - unsigned char *in, int n, - unsigned char *key, int k + const unsigned char *in, int n, + const 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]; + unsigned char *catbuf; int i; if(sizeof(hashbuf) < (size_t) hlen) - Host_Error("Invalid hash function used for HMAC - too long hash length"); + return false; 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"); + return false; + if(sizeof(k_xor_ipad) < (size_t) hlen) + return false; + + catbuf = (unsigned char *)Mem_Alloc(tempmempool, (size_t) hblock + max((size_t) hlen, (size_t) n)); 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; + hfunc(k_xor_opad, key, k); + key = k_xor_opad; + k = hlen; } - else if(k < hblock) + + if(k < hblock) { // zero pad the key if it is too short - memcpy(k_xor_opad, key, k); + if(key != k_xor_opad) + memcpy(k_xor_opad, key, k); for(i = k; i < hblock; ++i) k_xor_opad[i] = 0; key = k_xor_opad; @@ -54,4 +54,8 @@ void hmac( memcpy(catbuf, k_xor_opad, hblock); memcpy(catbuf + hblock, hashbuf, hlen); hfunc(out, catbuf, hblock + hlen); + + Mem_Free(catbuf); + + return true; }