5 hashfunc_t hfunc, int hlen, int hblock,
7 const unsigned char *in, int n,
8 const unsigned char *key, int k
11 unsigned char hashbuf[32];
12 unsigned char k_xor_ipad[128];
13 unsigned char k_xor_opad[128];
14 unsigned char *catbuf;
17 if(sizeof(hashbuf) < (size_t) hlen)
19 if(sizeof(k_xor_ipad) < (size_t) hblock)
21 if(sizeof(k_xor_ipad) < (size_t) hlen)
24 catbuf = (unsigned char *)Mem_Alloc(tempmempool, (size_t) hblock + max((size_t) hlen, (size_t) n));
28 // hash the key if it is too long
29 hfunc(k_xor_opad, key, k);
36 // zero pad the key if it is too short
38 memcpy(k_xor_opad, key, k);
39 for(i = k; i < hblock; ++i)
45 for(i = 0; i < hblock; ++i)
47 k_xor_ipad[i] = key[i] ^ 0x36;
48 k_xor_opad[i] = key[i] ^ 0x5c;
51 memcpy(catbuf, k_xor_ipad, hblock);
52 memcpy(catbuf + hblock, in, n);
53 hfunc(hashbuf, catbuf, hblock + n);
54 memcpy(catbuf, k_xor_opad, hblock);
55 memcpy(catbuf + hblock, hashbuf, hlen);
56 hfunc(out, catbuf, hblock + hlen);