Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * hmac.c |
michael@0 | 3 | * |
michael@0 | 4 | * implementation of hmac auth_type_t |
michael@0 | 5 | * |
michael@0 | 6 | * David A. McGrew |
michael@0 | 7 | * Cisco Systems, Inc. |
michael@0 | 8 | */ |
michael@0 | 9 | /* |
michael@0 | 10 | * |
michael@0 | 11 | * Copyright(c) 2001-2006 Cisco Systems, Inc. |
michael@0 | 12 | * All rights reserved. |
michael@0 | 13 | * |
michael@0 | 14 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 15 | * modification, are permitted provided that the following conditions |
michael@0 | 16 | * are met: |
michael@0 | 17 | * |
michael@0 | 18 | * Redistributions of source code must retain the above copyright |
michael@0 | 19 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 20 | * |
michael@0 | 21 | * Redistributions in binary form must reproduce the above |
michael@0 | 22 | * copyright notice, this list of conditions and the following |
michael@0 | 23 | * disclaimer in the documentation and/or other materials provided |
michael@0 | 24 | * with the distribution. |
michael@0 | 25 | * |
michael@0 | 26 | * Neither the name of the Cisco Systems, Inc. nor the names of its |
michael@0 | 27 | * contributors may be used to endorse or promote products derived |
michael@0 | 28 | * from this software without specific prior written permission. |
michael@0 | 29 | * |
michael@0 | 30 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 31 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 32 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
michael@0 | 33 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
michael@0 | 34 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
michael@0 | 35 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
michael@0 | 36 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
michael@0 | 37 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
michael@0 | 38 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
michael@0 | 39 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
michael@0 | 40 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
michael@0 | 41 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 42 | * |
michael@0 | 43 | */ |
michael@0 | 44 | |
michael@0 | 45 | #include "hmac.h" |
michael@0 | 46 | #include "alloc.h" |
michael@0 | 47 | |
michael@0 | 48 | /* the debug module for authentiation */ |
michael@0 | 49 | |
michael@0 | 50 | debug_module_t mod_hmac = { |
michael@0 | 51 | 0, /* debugging is off by default */ |
michael@0 | 52 | "hmac sha-1" /* printable name for module */ |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | |
michael@0 | 56 | err_status_t |
michael@0 | 57 | hmac_alloc(auth_t **a, int key_len, int out_len) { |
michael@0 | 58 | extern auth_type_t hmac; |
michael@0 | 59 | uint8_t *pointer; |
michael@0 | 60 | |
michael@0 | 61 | debug_print(mod_hmac, "allocating auth func with key length %d", key_len); |
michael@0 | 62 | debug_print(mod_hmac, " tag length %d", out_len); |
michael@0 | 63 | |
michael@0 | 64 | /* |
michael@0 | 65 | * check key length - note that we don't support keys larger |
michael@0 | 66 | * than 20 bytes yet |
michael@0 | 67 | */ |
michael@0 | 68 | if (key_len > 20) |
michael@0 | 69 | return err_status_bad_param; |
michael@0 | 70 | |
michael@0 | 71 | /* check output length - should be less than 20 bytes */ |
michael@0 | 72 | if (out_len > 20) |
michael@0 | 73 | return err_status_bad_param; |
michael@0 | 74 | |
michael@0 | 75 | /* allocate memory for auth and hmac_ctx_t structures */ |
michael@0 | 76 | pointer = (uint8_t*)crypto_alloc(sizeof(hmac_ctx_t) + sizeof(auth_t)); |
michael@0 | 77 | if (pointer == NULL) |
michael@0 | 78 | return err_status_alloc_fail; |
michael@0 | 79 | |
michael@0 | 80 | /* set pointers */ |
michael@0 | 81 | *a = (auth_t *)pointer; |
michael@0 | 82 | (*a)->type = &hmac; |
michael@0 | 83 | (*a)->state = pointer + sizeof(auth_t); |
michael@0 | 84 | (*a)->out_len = out_len; |
michael@0 | 85 | (*a)->key_len = key_len; |
michael@0 | 86 | (*a)->prefix_len = 0; |
michael@0 | 87 | |
michael@0 | 88 | /* increment global count of all hmac uses */ |
michael@0 | 89 | hmac.ref_count++; |
michael@0 | 90 | |
michael@0 | 91 | return err_status_ok; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | err_status_t |
michael@0 | 95 | hmac_dealloc(auth_t *a) { |
michael@0 | 96 | extern auth_type_t hmac; |
michael@0 | 97 | |
michael@0 | 98 | /* zeroize entire state*/ |
michael@0 | 99 | octet_string_set_to_zero((uint8_t *)a, |
michael@0 | 100 | sizeof(hmac_ctx_t) + sizeof(auth_t)); |
michael@0 | 101 | |
michael@0 | 102 | /* free memory */ |
michael@0 | 103 | crypto_free(a); |
michael@0 | 104 | |
michael@0 | 105 | /* decrement global count of all hmac uses */ |
michael@0 | 106 | hmac.ref_count--; |
michael@0 | 107 | |
michael@0 | 108 | return err_status_ok; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | err_status_t |
michael@0 | 112 | hmac_init(hmac_ctx_t *state, const uint8_t *key, int key_len) { |
michael@0 | 113 | int i; |
michael@0 | 114 | uint8_t ipad[64]; |
michael@0 | 115 | |
michael@0 | 116 | /* |
michael@0 | 117 | * check key length - note that we don't support keys larger |
michael@0 | 118 | * than 20 bytes yet |
michael@0 | 119 | */ |
michael@0 | 120 | if (key_len > 20) |
michael@0 | 121 | return err_status_bad_param; |
michael@0 | 122 | |
michael@0 | 123 | /* |
michael@0 | 124 | * set values of ipad and opad by exoring the key into the |
michael@0 | 125 | * appropriate constant values |
michael@0 | 126 | */ |
michael@0 | 127 | for (i=0; i < key_len; i++) { |
michael@0 | 128 | ipad[i] = key[i] ^ 0x36; |
michael@0 | 129 | state->opad[i] = key[i] ^ 0x5c; |
michael@0 | 130 | } |
michael@0 | 131 | /* set the rest of ipad, opad to constant values */ |
michael@0 | 132 | for ( ; i < 64; i++) { |
michael@0 | 133 | ipad[i] = 0x36; |
michael@0 | 134 | ((uint8_t *)state->opad)[i] = 0x5c; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | debug_print(mod_hmac, "ipad: %s", octet_string_hex_string(ipad, 64)); |
michael@0 | 138 | |
michael@0 | 139 | /* initialize sha1 context */ |
michael@0 | 140 | sha1_init(&state->init_ctx); |
michael@0 | 141 | |
michael@0 | 142 | /* hash ipad ^ key */ |
michael@0 | 143 | sha1_update(&state->init_ctx, ipad, 64); |
michael@0 | 144 | memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t)); |
michael@0 | 145 | |
michael@0 | 146 | return err_status_ok; |
michael@0 | 147 | } |
michael@0 | 148 | |
michael@0 | 149 | err_status_t |
michael@0 | 150 | hmac_start(hmac_ctx_t *state) { |
michael@0 | 151 | |
michael@0 | 152 | memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t)); |
michael@0 | 153 | |
michael@0 | 154 | return err_status_ok; |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | err_status_t |
michael@0 | 158 | hmac_update(hmac_ctx_t *state, const uint8_t *message, int msg_octets) { |
michael@0 | 159 | |
michael@0 | 160 | debug_print(mod_hmac, "input: %s", |
michael@0 | 161 | octet_string_hex_string(message, msg_octets)); |
michael@0 | 162 | |
michael@0 | 163 | /* hash message into sha1 context */ |
michael@0 | 164 | sha1_update(&state->ctx, message, msg_octets); |
michael@0 | 165 | |
michael@0 | 166 | return err_status_ok; |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | err_status_t |
michael@0 | 170 | hmac_compute(hmac_ctx_t *state, const void *message, |
michael@0 | 171 | int msg_octets, int tag_len, uint8_t *result) { |
michael@0 | 172 | uint32_t hash_value[5]; |
michael@0 | 173 | uint32_t H[5]; |
michael@0 | 174 | int i; |
michael@0 | 175 | |
michael@0 | 176 | /* check tag length, return error if we can't provide the value expected */ |
michael@0 | 177 | if (tag_len > 20) |
michael@0 | 178 | return err_status_bad_param; |
michael@0 | 179 | |
michael@0 | 180 | /* hash message, copy output into H */ |
michael@0 | 181 | hmac_update(state, (const uint8_t*)message, msg_octets); |
michael@0 | 182 | sha1_final(&state->ctx, H); |
michael@0 | 183 | |
michael@0 | 184 | /* |
michael@0 | 185 | * note that we don't need to debug_print() the input, since the |
michael@0 | 186 | * function hmac_update() already did that for us |
michael@0 | 187 | */ |
michael@0 | 188 | debug_print(mod_hmac, "intermediate state: %s", |
michael@0 | 189 | octet_string_hex_string((uint8_t *)H, 20)); |
michael@0 | 190 | |
michael@0 | 191 | /* re-initialize hash context */ |
michael@0 | 192 | sha1_init(&state->ctx); |
michael@0 | 193 | |
michael@0 | 194 | /* hash opad ^ key */ |
michael@0 | 195 | sha1_update(&state->ctx, (uint8_t *)state->opad, 64); |
michael@0 | 196 | |
michael@0 | 197 | /* hash the result of the inner hash */ |
michael@0 | 198 | sha1_update(&state->ctx, (uint8_t *)H, 20); |
michael@0 | 199 | |
michael@0 | 200 | /* the result is returned in the array hash_value[] */ |
michael@0 | 201 | sha1_final(&state->ctx, hash_value); |
michael@0 | 202 | |
michael@0 | 203 | /* copy hash_value to *result */ |
michael@0 | 204 | for (i=0; i < tag_len; i++) |
michael@0 | 205 | result[i] = ((uint8_t *)hash_value)[i]; |
michael@0 | 206 | |
michael@0 | 207 | debug_print(mod_hmac, "output: %s", |
michael@0 | 208 | octet_string_hex_string((uint8_t *)hash_value, tag_len)); |
michael@0 | 209 | |
michael@0 | 210 | return err_status_ok; |
michael@0 | 211 | } |
michael@0 | 212 | |
michael@0 | 213 | |
michael@0 | 214 | /* begin test case 0 */ |
michael@0 | 215 | |
michael@0 | 216 | uint8_t |
michael@0 | 217 | hmac_test_case_0_key[20] = { |
michael@0 | 218 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
michael@0 | 219 | 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, |
michael@0 | 220 | 0x0b, 0x0b, 0x0b, 0x0b |
michael@0 | 221 | }; |
michael@0 | 222 | |
michael@0 | 223 | uint8_t |
michael@0 | 224 | hmac_test_case_0_data[8] = { |
michael@0 | 225 | 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 /* "Hi There" */ |
michael@0 | 226 | }; |
michael@0 | 227 | |
michael@0 | 228 | uint8_t |
michael@0 | 229 | hmac_test_case_0_tag[20] = { |
michael@0 | 230 | 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, |
michael@0 | 231 | 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, |
michael@0 | 232 | 0xf1, 0x46, 0xbe, 0x00 |
michael@0 | 233 | }; |
michael@0 | 234 | |
michael@0 | 235 | auth_test_case_t |
michael@0 | 236 | hmac_test_case_0 = { |
michael@0 | 237 | 20, /* octets in key */ |
michael@0 | 238 | hmac_test_case_0_key, /* key */ |
michael@0 | 239 | 8, /* octets in data */ |
michael@0 | 240 | hmac_test_case_0_data, /* data */ |
michael@0 | 241 | 20, /* octets in tag */ |
michael@0 | 242 | hmac_test_case_0_tag, /* tag */ |
michael@0 | 243 | NULL /* pointer to next testcase */ |
michael@0 | 244 | }; |
michael@0 | 245 | |
michael@0 | 246 | /* end test case 0 */ |
michael@0 | 247 | |
michael@0 | 248 | char hmac_description[] = "hmac sha-1 authentication function"; |
michael@0 | 249 | |
michael@0 | 250 | /* |
michael@0 | 251 | * auth_type_t hmac is the hmac metaobject |
michael@0 | 252 | */ |
michael@0 | 253 | |
michael@0 | 254 | auth_type_t |
michael@0 | 255 | hmac = { |
michael@0 | 256 | (auth_alloc_func) hmac_alloc, |
michael@0 | 257 | (auth_dealloc_func) hmac_dealloc, |
michael@0 | 258 | (auth_init_func) hmac_init, |
michael@0 | 259 | (auth_compute_func) hmac_compute, |
michael@0 | 260 | (auth_update_func) hmac_update, |
michael@0 | 261 | (auth_start_func) hmac_start, |
michael@0 | 262 | (char *) hmac_description, |
michael@0 | 263 | (int) 0, /* instance count */ |
michael@0 | 264 | (auth_test_case_t *) &hmac_test_case_0, |
michael@0 | 265 | (debug_module_t *) &mod_hmac, |
michael@0 | 266 | (auth_type_id_t) HMAC_SHA1 |
michael@0 | 267 | }; |
michael@0 | 268 |