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-2011 Mark Shelor, All Rights Reserved
9 * Wed Mar 9 05:26:36 MST 2011
19 /* hmacopen: creates a new HMAC-SHA digest object */
20 HMAC *hmacopen(int alg, unsigned char *key, unsigned int keylen)
25 SHA_newz(0, h, 1, HMAC);
28 if ((h->isha = shaopen(alg)) == NULL) {
32 if ((h->osha = shaopen(alg)) == NULL) {
37 if (keylen <= h->osha->blocksize / 8)
38 memcpy(h->key, key, keylen);
40 if ((h->ksha = shaopen(alg)) == NULL) {
46 shawrite(key, keylen * 8, h->ksha);
48 memcpy(h->key, shadigest(h->ksha), h->ksha->digestlen);
51 for (i = 0; i < h->osha->blocksize / 8; i++)
53 shawrite(h->key, h->osha->blocksize, h->osha);
54 for (i = 0; i < h->isha->blocksize / 8; i++)
55 h->key[i] ^= (0x5c ^ 0x36);
56 shawrite(h->key, h->isha->blocksize, h->isha);
57 memset(h->key, 0, sizeof(h->key));
61 /* hmacwrite: triggers a state update using data in bitstr/bitcnt */
62 unsigned long hmacwrite(unsigned char *bitstr, unsigned long bitcnt, HMAC *h)
64 return(shawrite(bitstr, bitcnt, h->isha));
67 /* hmacfinish: computes final digest state */
68 void hmacfinish(HMAC *h)
71 shawrite(shadigest(h->isha), h->isha->digestlen * 8, h->osha);
76 /* hmacdigest: returns pointer to digest (binary) */
77 unsigned char *hmacdigest(HMAC *h)
79 return(shadigest(h->osha));
82 /* hmachex: returns pointer to digest (hexadecimal) */
83 char *hmachex(HMAC *h)
85 return(shahex(h->osha));
88 /* hmacbase64: returns pointer to digest (Base 64) */
89 char *hmacbase64(HMAC *h)
91 return(shabase64(h->osha));
94 /* hmacclose: de-allocates digest object */
95 int hmacclose(HMAC *h)
99 memset(h, 0, sizeof(HMAC));