2 * hmac.c: routines to compute HMAC-SHA-1/224/256/384/512 digests
4 * Ref: FIPS PUB 198 The Keyed-Hash Message Authentication Code
6 * Copyright (C) 2003-2005 Mark Shelor, All Rights Reserved
9 * Fri Dec 2 02:32:20 MST 2005
19 /* hmacopen: creates a new HMAC-SHA digest object */
20 HMAC *hmacopen(alg, key, keylen)
28 SHA_newz(0, h, 1, HMAC);
31 if ((h->isha = shaopen(alg)) == NULL) {
35 if ((h->osha = shaopen(alg)) == NULL) {
40 if (keylen <= h->osha->blocksize / 8)
41 memcpy(h->key, key, keylen);
43 if ((h->ksha = shaopen(alg)) == NULL) {
49 shawrite(key, keylen * 8, h->ksha);
51 memcpy(h->key, shadigest(h->ksha), h->ksha->digestlen);
54 for (i = 0; i < h->osha->blocksize / 8; i++)
56 shawrite(h->key, h->osha->blocksize, h->osha);
57 for (i = 0; i < h->isha->blocksize / 8; i++)
58 h->key[i] ^= (0x5c ^ 0x36);
59 shawrite(h->key, h->isha->blocksize, h->isha);
60 memset(h->key, 0, sizeof(h->key));
64 /* hmacwrite: triggers a state update using data in bitstr/bitcnt */
65 unsigned long hmacwrite(bitstr, bitcnt, h)
66 unsigned char *bitstr;
70 return(shawrite(bitstr, bitcnt, h->isha));
73 /* hmacfinish: computes final digest state */
78 shawrite(shadigest(h->isha), h->isha->digestlen * 8, h->osha);
83 /* hmacdigest: returns pointer to digest (binary) */
84 unsigned char *hmacdigest(h)
87 return(shadigest(h->osha));
90 /* hmachex: returns pointer to digest (hexadecimal) */
94 return(shahex(h->osha));
97 /* hmacbase64: returns pointer to digest (Base 64) */
101 return(shabase64(h->osha));
104 /* hmacclose: de-allocates digest object */
110 memset(h, 0, sizeof(HMAC));