void HMAC_FINAL(HMAC_CONTEXT *ctxt, uint8_t hmac[HASH_SIZE]) { uint8_t ihash[HASH_SIZE]; HASH_FINAL(&ctxt->inner, ihash); HASH_UPDATE(&ctxt->outer, ihash, sizeof(ihash)); HASH_FINAL(&ctxt->outer, hmac); memset(ihash, 0, sizeof(ihash)); }
/* Returns a hash value for the N bytes starting at P, starting from BASIS. */ unsigned int hash_bytes (const void *p_, size_t n, unsigned int basis) { const uint8_t *p = p_; uint32_t a, b, c; uint32_t tmp[3]; a = b = c = 0xdeadbeef + n + basis; while (n >= 12) { memcpy (tmp, p, 12); a += tmp[0]; b += tmp[1]; c += tmp[2]; HASH_MIX (a, b, c); n -= 12; p += 12; } if (n > 0) { memset (tmp, 0, 12); memcpy (tmp, p, n); a += tmp[0]; b += tmp[1]; c += tmp[2]; } HASH_FINAL (a, b, c); return c; }
/* Returns a hash value for the N bytes at S, with lowercase and uppercase letters treated as equal, starting from BASIS. */ unsigned int hash_case_bytes (const void *s_, size_t n, unsigned int basis) { const char *s = s_; uint32_t a, b, c; uint32_t tmp[3]; int i; a = b = c = 0xdeadbeef + n + basis; while (n >= 12) { for (i = 0; i < 12; i++) ((unsigned char *)tmp)[i] = toupper ((unsigned char) s[i]); a += tmp[0]; b += tmp[1]; c += tmp[2]; HASH_MIX (a, b, c); n -= 12; s += 12; } if (n > 0) { memset (tmp, 0, 12); for (i = 0; i < n; i++) ((unsigned char *)tmp)[i] = toupper ((unsigned char) s[i]); a += tmp[0]; b += tmp[1]; c += tmp[2]; } HASH_FINAL (a, b, c); return c; }
/* Simple finalization function */ int finalize(thread_t * thread) { SOCK *sock_obj = THREAD_ARG(thread); unsigned char digest_length = HASH_LENGTH(sock_obj); unsigned char digest[digest_length]; int i; /* Compute final hash digest */ HASH_FINAL(sock_obj, digest); if (req->verbose) { printf("\n"); printf(HTML_HASH); dump_buffer((char *) digest, digest_length); printf(HTML_HASH_FINAL); } printf("%s = ", HASH_LABEL(sock_obj)); for (i = 0; i < digest_length; i++) printf("%02x", digest[i]); printf("\n\n"); DBG("Finalize : [%s]\n", req->url); free_all(thread); return 0; }
/* Returns a hash value for double D, starting from BASIS. */ unsigned int hash_double (double d, unsigned int basis) { if (sizeof (double) == 8) { uint32_t tmp[2]; uint32_t a, b, c; a = b = c = 0xdeadbeef + 8 + basis; memcpy (tmp, &d, 8); a += tmp[0]; b += tmp[1]; HASH_FINAL (a, b, c); return c; } else return hash_bytes (&d, sizeof d, basis); }
void HMAC_INIT(HMAC_CONTEXT *ctxt, const void *key, size_t keyLen) { HASH_CONTEXT keyCtxt; unsigned int i; uint8_t pkey[HASH_BLOCK_SIZE], okey[HASH_BLOCK_SIZE], ikey[HASH_BLOCK_SIZE]; /* Ensure key is zero-padded */ memset(pkey, 0, sizeof(pkey)); if (keyLen > sizeof(pkey)) { /* Hash key if > HASH_BLOCK_SIZE */ HASH_INIT(&keyCtxt); HASH_UPDATE(&keyCtxt, key, keyLen); HASH_FINAL(&keyCtxt, pkey); } else { memcpy(pkey, key, keyLen); } /* XOR with opad, ipad */ for (i = 0; i < sizeof(okey); i++) { okey[i] = pkey[i] ^ 0x5c; } for (i = 0; i < sizeof(ikey); i++) { ikey[i] = pkey[i] ^ 0x36; } /* Initialize hash contexts */ HASH_INIT(&ctxt->outer); HASH_UPDATE(&ctxt->outer, okey, sizeof(okey)); HASH_INIT(&ctxt->inner); HASH_UPDATE(&ctxt->inner, ikey, sizeof(ikey)); /* Burn the stack */ memset(ikey, 0, sizeof(ikey)); memset(okey, 0, sizeof(okey)); memset(pkey, 0, sizeof(pkey)); memset(&keyCtxt, 0, sizeof(keyCtxt)); }