2
---------------------------------------------------------------------------
3
Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK.
8
The free distribution and use of this software in both source and binary
9
form is allowed (with or without changes) provided that:
11
1. distributions of this source code include the above copyright
12
notice, this list of conditions and the following disclaimer;
14
2. distributions in binary form include the above copyright
15
notice, this list of conditions and the following disclaimer
16
in the documentation and/or other associated materials;
18
3. the copyright holder's name is not used to endorse products
19
built using this software without specific written permission.
21
ALTERNATIVELY, provided that this notice is retained in full, this product
22
may be distributed under the terms of the GNU General Public License (GPL),
23
in which case the provisions of the GPL apply INSTEAD OF those given above.
27
This software is provided 'as is' with no explicit or implied warranties
28
in respect of its properties, including, but not limited to, correctness
29
and/or fitness for purpose.
30
---------------------------------------------------------------------------
31
Issue Date: 26/08/2003
33
This is an implementation of HMAC, the FIPS standard keyed hash function
38
/* initialise the HMAC context to zero */
39
void hmac_sha_begin(hmac_ctx cx[1])
41
memset(cx, 0, sizeof(hmac_ctx));
44
/* input the HMAC key (can be called multiple times) */
45
int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1])
47
if(cx->klen == HMAC_IN_DATA) /* error if further key input */
48
return HMAC_BAD_MODE; /* is attempted in data mode */
50
if(cx->klen + key_len > HASH_INPUT_SIZE) /* if the key has to be hashed */
52
if(cx->klen <= HASH_INPUT_SIZE) /* if the hash has not yet been */
53
{ /* started, initialise it and */
54
sha_begin(cx->ctx); /* hash stored key characters */
55
sha_hash(cx->key, cx->klen, cx->ctx);
58
sha_hash(key, key_len, cx->ctx); /* hash long key data into hash */
60
else /* otherwise store key data */
61
memcpy(cx->key + cx->klen, key, key_len);
63
cx->klen += key_len; /* update the key length count */
67
/* input the HMAC data (can be called multiple times) - */
68
/* note that this call terminates the key input phase */
69
void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1])
72
if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */
74
if(cx->klen > HASH_INPUT_SIZE) /* if key is being hashed */
75
{ /* complete the hash and */
76
sha_end(cx->key, cx->ctx); /* store the result as the */
77
cx->klen = HASH_OUTPUT_SIZE; /* key and set new length */
80
/* pad the key if necessary */
81
memset(cx->key + cx->klen, 0, HASH_INPUT_SIZE - cx->klen);
83
/* xor ipad into key value */
84
for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i)
85
((unsigned long*)cx->key)[i] ^= 0x36363636;
87
/* and start hash operation */
89
sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx);
91
/* mark as now in data mode */
92
cx->klen = HMAC_IN_DATA;
95
/* hash the data (if any) */
97
sha_hash(data, data_len, cx->ctx);
100
/* compute and output the MAC value */
101
void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1])
102
{ unsigned char dig[HASH_OUTPUT_SIZE];
105
/* if no data has been entered perform a null data phase */
106
if(cx->klen != HMAC_IN_DATA)
107
hmac_sha_data((const unsigned char*)0, 0, cx);
109
sha_end(dig, cx->ctx); /* complete the inner hash */
111
/* set outer key value using opad and removing ipad */
112
for(i = 0; i < (HASH_INPUT_SIZE >> 2); ++i)
113
((unsigned long*)cx->key)[i] ^= 0x36363636 ^ 0x5c5c5c5c;
115
/* perform the outer hash operation */
117
sha_hash(cx->key, HASH_INPUT_SIZE, cx->ctx);
118
sha_hash(dig, HASH_OUTPUT_SIZE, cx->ctx);
119
sha_end(dig, cx->ctx);
121
/* output the hash value */
122
for(i = 0; i < mac_len; ++i)
126
/* 'do it all in one go' subroutine */
127
void hmac_sha(const unsigned char key[], unsigned long key_len,
128
const unsigned char data[], unsigned long data_len,
129
unsigned char mac[], unsigned long mac_len)
133
hmac_sha_key(key, key_len, cx);
134
hmac_sha_data(data, data_len, cx);
135
hmac_sha_end(mac, mac_len, cx);