4
* implementation of hmac auth_type_t
11
* Copyright(c) 2001-2006 Cisco Systems, Inc.
12
* All rights reserved.
14
* Redistribution and use in source and binary forms, with or without
15
* modification, are permitted provided that the following conditions
18
* Redistributions of source code must retain the above copyright
19
* notice, this list of conditions and the following disclaimer.
21
* Redistributions in binary form must reproduce the above
22
* copyright notice, this list of conditions and the following
23
* disclaimer in the documentation and/or other materials provided
24
* with the distribution.
26
* Neither the name of the Cisco Systems, Inc. nor the names of its
27
* contributors may be used to endorse or promote products derived
28
* from this software without specific prior written permission.
30
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
33
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
35
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
37
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
41
* OF THE POSSIBILITY OF SUCH DAMAGE.
48
/* the debug module for authentiation */
50
debug_module_t mod_hmac = {
51
0, /* debugging is off by default */
52
"hmac sha-1" /* printable name for module */
57
hmac_alloc(auth_t **a, int key_len, int out_len) {
58
extern auth_type_t hmac;
61
debug_print(mod_hmac, "allocating auth func with key length %d", key_len);
62
debug_print(mod_hmac, " tag length %d", out_len);
65
* check key length - note that we don't support keys larger
69
return err_status_bad_param;
71
/* check output length - should be less than 20 bytes */
73
return err_status_bad_param;
75
/* allocate memory for auth and hmac_ctx_t structures */
76
pointer = (uint8_t*)crypto_alloc(sizeof(hmac_ctx_t) + sizeof(auth_t));
78
return err_status_alloc_fail;
81
*a = (auth_t *)pointer;
83
(*a)->state = pointer + sizeof(auth_t);
84
(*a)->out_len = out_len;
85
(*a)->key_len = key_len;
88
/* increment global count of all hmac uses */
95
hmac_dealloc(auth_t *a) {
96
extern auth_type_t hmac;
98
/* zeroize entire state*/
99
octet_string_set_to_zero((uint8_t *)a,
100
sizeof(hmac_ctx_t) + sizeof(auth_t));
105
/* decrement global count of all hmac uses */
108
return err_status_ok;
112
hmac_init(hmac_ctx_t *state, const uint8_t *key, int key_len) {
117
* check key length - note that we don't support keys larger
121
return err_status_bad_param;
124
* set values of ipad and opad by exoring the key into the
125
* appropriate constant values
127
for (i=0; i < key_len; i++) {
128
ipad[i] = key[i] ^ 0x36;
129
state->opad[i] = key[i] ^ 0x5c;
131
/* set the rest of ipad, opad to constant values */
132
for ( ; i < 64; i++) {
134
((uint8_t *)state->opad)[i] = 0x5c;
137
debug_print(mod_hmac, "ipad: %s", octet_string_hex_string(ipad, 64));
139
/* initialize sha1 context */
140
sha1_init(&state->init_ctx);
142
/* hash ipad ^ key */
143
sha1_update(&state->init_ctx, ipad, 64);
144
memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t));
146
return err_status_ok;
150
hmac_start(hmac_ctx_t *state) {
152
memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t));
154
return err_status_ok;
158
hmac_update(hmac_ctx_t *state, const uint8_t *message, int msg_octets) {
160
debug_print(mod_hmac, "input: %s",
161
octet_string_hex_string(message, msg_octets));
163
/* hash message into sha1 context */
164
sha1_update(&state->ctx, message, msg_octets);
166
return err_status_ok;
170
hmac_compute(hmac_ctx_t *state, const void *message,
171
int msg_octets, int tag_len, uint8_t *result) {
172
uint32_t hash_value[5];
176
/* check tag length, return error if we can't provide the value expected */
178
return err_status_bad_param;
180
/* hash message, copy output into H */
181
hmac_update(state, (const uint8_t*)message, msg_octets);
182
sha1_final(&state->ctx, H);
185
* note that we don't need to debug_print() the input, since the
186
* function hmac_update() already did that for us
188
debug_print(mod_hmac, "intermediate state: %s",
189
octet_string_hex_string((uint8_t *)H, 20));
191
/* re-initialize hash context */
192
sha1_init(&state->ctx);
194
/* hash opad ^ key */
195
sha1_update(&state->ctx, (uint8_t *)state->opad, 64);
197
/* hash the result of the inner hash */
198
sha1_update(&state->ctx, (uint8_t *)H, 20);
200
/* the result is returned in the array hash_value[] */
201
sha1_final(&state->ctx, hash_value);
203
/* copy hash_value to *result */
204
for (i=0; i < tag_len; i++)
205
result[i] = ((uint8_t *)hash_value)[i];
207
debug_print(mod_hmac, "output: %s",
208
octet_string_hex_string((uint8_t *)hash_value, tag_len));
210
return err_status_ok;
214
/* begin test case 0 */
217
hmac_test_case_0_key[20] = {
218
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
219
0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
220
0x0b, 0x0b, 0x0b, 0x0b
224
hmac_test_case_0_data[8] = {
225
0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 /* "Hi There" */
229
hmac_test_case_0_tag[20] = {
230
0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
231
0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
232
0xf1, 0x46, 0xbe, 0x00
237
20, /* octets in key */
238
hmac_test_case_0_key, /* key */
239
8, /* octets in data */
240
hmac_test_case_0_data, /* data */
241
20, /* octets in tag */
242
hmac_test_case_0_tag, /* tag */
243
NULL /* pointer to next testcase */
246
/* end test case 0 */
248
char hmac_description[] = "hmac sha-1 authentication function";
251
* auth_type_t hmac is the hmac metaobject
256
(auth_alloc_func) hmac_alloc,
257
(auth_dealloc_func) hmac_dealloc,
258
(auth_init_func) hmac_init,
259
(auth_compute_func) hmac_compute,
260
(auth_update_func) hmac_update,
261
(auth_start_func) hmac_start,
262
(char *) hmac_description,
263
(int) 0, /* instance count */
264
(auth_test_case_t *) &hmac_test_case_0,
265
(debug_module_t *) &mod_hmac