Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(164)

Side by Side Diff: srtp/crypto/hash/hmac.c

Issue 2344973002: Update libsrtp to version 2.0 (Closed)
Patch Set: Add '.' back to include_dirs Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « srtp/crypto/hash/auth.c ('k') | srtp/crypto/hash/hmac_ossl.c » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * hmac.c
3 *
4 * implementation of hmac auth_type_t
5 *
6 * David A. McGrew
7 * Cisco Systems, Inc.
8 */
9 /*
10 *
11 * Copyright(c) 2001-2006 Cisco Systems, Inc.
12 * All rights reserved.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
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.
25 *
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.
29 *
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.
42 *
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif
48
49 #include "hmac.h"
50 #include "alloc.h"
51
52 /* the debug module for authentiation */
53
54 debug_module_t mod_hmac = {
55 0, /* debugging is off by default */
56 "hmac sha-1" /* printable name for module */
57 };
58
59
60 err_status_t
61 hmac_alloc(auth_t **a, int key_len, int out_len) {
62 extern auth_type_t hmac;
63 uint8_t *pointer;
64
65 debug_print(mod_hmac, "allocating auth func with key length %d", key_len);
66 debug_print(mod_hmac, " tag length %d", out_len);
67
68 /*
69 * check key length - note that we don't support keys larger
70 * than 20 bytes yet
71 */
72 if (key_len > 20)
73 return err_status_bad_param;
74
75 /* check output length - should be less than 20 bytes */
76 if (out_len > 20)
77 return err_status_bad_param;
78
79 /* allocate memory for auth and hmac_ctx_t structures */
80 pointer = (uint8_t*)crypto_alloc(sizeof(hmac_ctx_t) + sizeof(auth_t));
81 if (pointer == NULL)
82 return err_status_alloc_fail;
83
84 /* set pointers */
85 *a = (auth_t *)pointer;
86 (*a)->type = &hmac;
87 (*a)->state = pointer + sizeof(auth_t);
88 (*a)->out_len = out_len;
89 (*a)->key_len = key_len;
90 (*a)->prefix_len = 0;
91
92 /* increment global count of all hmac uses */
93 hmac.ref_count++;
94
95 return err_status_ok;
96 }
97
98 err_status_t
99 hmac_dealloc(auth_t *a) {
100 extern auth_type_t hmac;
101
102 /* zeroize entire state*/
103 octet_string_set_to_zero((uint8_t *)a,
104 sizeof(hmac_ctx_t) + sizeof(auth_t));
105
106 /* free memory */
107 crypto_free(a);
108
109 /* decrement global count of all hmac uses */
110 hmac.ref_count--;
111
112 return err_status_ok;
113 }
114
115 err_status_t
116 hmac_init(hmac_ctx_t *state, const uint8_t *key, int key_len) {
117 int i;
118 uint8_t ipad[64];
119
120 /*
121 * check key length - note that we don't support keys larger
122 * than 20 bytes yet
123 */
124 if (key_len > 20)
125 return err_status_bad_param;
126
127 /*
128 * set values of ipad and opad by exoring the key into the
129 * appropriate constant values
130 */
131 for (i=0; i < key_len; i++) {
132 ipad[i] = key[i] ^ 0x36;
133 state->opad[i] = key[i] ^ 0x5c;
134 }
135 /* set the rest of ipad, opad to constant values */
136 for ( ; i < 64; i++) {
137 ipad[i] = 0x36;
138 ((uint8_t *)state->opad)[i] = 0x5c;
139 }
140
141 debug_print(mod_hmac, "ipad: %s", octet_string_hex_string(ipad, 64));
142
143 /* initialize sha1 context */
144 sha1_init(&state->init_ctx);
145
146 /* hash ipad ^ key */
147 sha1_update(&state->init_ctx, ipad, 64);
148 memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t));
149
150 return err_status_ok;
151 }
152
153 err_status_t
154 hmac_start(hmac_ctx_t *state) {
155
156 memcpy(&state->ctx, &state->init_ctx, sizeof(sha1_ctx_t));
157
158 return err_status_ok;
159 }
160
161 err_status_t
162 hmac_update(hmac_ctx_t *state, const uint8_t *message, int msg_octets) {
163
164 debug_print(mod_hmac, "input: %s",
165 octet_string_hex_string(message, msg_octets));
166
167 /* hash message into sha1 context */
168 sha1_update(&state->ctx, message, msg_octets);
169
170 return err_status_ok;
171 }
172
173 err_status_t
174 hmac_compute(hmac_ctx_t *state, const void *message,
175 int msg_octets, int tag_len, uint8_t *result) {
176 uint32_t hash_value[5];
177 uint32_t H[5];
178 int i;
179
180 /* check tag length, return error if we can't provide the value expected */
181 if (tag_len > 20)
182 return err_status_bad_param;
183
184 /* hash message, copy output into H */
185 hmac_update(state, (const uint8_t*)message, msg_octets);
186 sha1_final(&state->ctx, H);
187
188 /*
189 * note that we don't need to debug_print() the input, since the
190 * function hmac_update() already did that for us
191 */
192 debug_print(mod_hmac, "intermediate state: %s",
193 octet_string_hex_string((uint8_t *)H, 20));
194
195 /* re-initialize hash context */
196 sha1_init(&state->ctx);
197
198 /* hash opad ^ key */
199 sha1_update(&state->ctx, (uint8_t *)state->opad, 64);
200
201 /* hash the result of the inner hash */
202 sha1_update(&state->ctx, (uint8_t *)H, 20);
203
204 /* the result is returned in the array hash_value[] */
205 sha1_final(&state->ctx, hash_value);
206
207 /* copy hash_value to *result */
208 for (i=0; i < tag_len; i++)
209 result[i] = ((uint8_t *)hash_value)[i];
210
211 debug_print(mod_hmac, "output: %s",
212 octet_string_hex_string((uint8_t *)hash_value, tag_len));
213
214 return err_status_ok;
215 }
216
217
218 /* begin test case 0 */
219
220 uint8_t
221 hmac_test_case_0_key[20] = {
222 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
223 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
224 0x0b, 0x0b, 0x0b, 0x0b
225 };
226
227 uint8_t
228 hmac_test_case_0_data[8] = {
229 0x48, 0x69, 0x20, 0x54, 0x68, 0x65, 0x72, 0x65 /* "Hi There" */
230 };
231
232 uint8_t
233 hmac_test_case_0_tag[20] = {
234 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64,
235 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e,
236 0xf1, 0x46, 0xbe, 0x00
237 };
238
239 auth_test_case_t
240 hmac_test_case_0 = {
241 20, /* octets in key */
242 hmac_test_case_0_key, /* key */
243 8, /* octets in data */
244 hmac_test_case_0_data, /* data */
245 20, /* octets in tag */
246 hmac_test_case_0_tag, /* tag */
247 NULL /* pointer to next testcase */
248 };
249
250 /* end test case 0 */
251
252 char hmac_description[] = "hmac sha-1 authentication function";
253
254 /*
255 * auth_type_t hmac is the hmac metaobject
256 */
257
258 auth_type_t
259 hmac = {
260 (auth_alloc_func) hmac_alloc,
261 (auth_dealloc_func) hmac_dealloc,
262 (auth_init_func) hmac_init,
263 (auth_compute_func) hmac_compute,
264 (auth_update_func) hmac_update,
265 (auth_start_func) hmac_start,
266 (char *) hmac_description,
267 (int) 0, /* instance count */
268 (auth_test_case_t *) &hmac_test_case_0,
269 (debug_module_t *) &mod_hmac,
270 (auth_type_id_t) HMAC_SHA1
271 };
272
OLDNEW
« no previous file with comments | « srtp/crypto/hash/auth.c ('k') | srtp/crypto/hash/hmac_ossl.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698