hmac_link.c revision 1.2.2.2 1 1.2.2.2 christos /* $NetBSD: hmac_link.c,v 1.2.2.2 2012/11/16 02:16:39 christos Exp $ */
2 1.2.2.2 christos
3 1.2.2.2 christos /*
4 1.2.2.2 christos * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
5 1.2.2.2 christos *
6 1.2.2.2 christos * Permission to use, copy modify, and distribute this software for any
7 1.2.2.2 christos * purpose with or without fee is hereby granted, provided that the above
8 1.2.2.2 christos * copyright notice and this permission notice appear in all copies.
9 1.2.2.2 christos *
10 1.2.2.2 christos * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
11 1.2.2.2 christos * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
12 1.2.2.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
13 1.2.2.2 christos * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
14 1.2.2.2 christos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
15 1.2.2.2 christos * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
16 1.2.2.2 christos * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
17 1.2.2.2 christos * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
18 1.2.2.2 christos */
19 1.2.2.2 christos #include <sys/cdefs.h>
20 1.2.2.2 christos #if 0
21 1.2.2.2 christos static const char rcsid[] = "Header: /proj/cvs/prod/libbind/dst/hmac_link.c,v 1.8 2007/09/24 17:18:25 each Exp ";
22 1.2.2.2 christos #else
23 1.2.2.2 christos __RCSID("$NetBSD: hmac_link.c,v 1.2.2.2 2012/11/16 02:16:39 christos Exp $");
24 1.2.2.2 christos #endif
25 1.2.2.2 christos
26 1.2.2.2 christos /*%
27 1.2.2.2 christos * This file contains an implementation of the HMAC-MD5 algorithm.
28 1.2.2.2 christos */
29 1.2.2.2 christos #include "port_before.h"
30 1.2.2.2 christos
31 1.2.2.2 christos #include <stdio.h>
32 1.2.2.2 christos #include <unistd.h>
33 1.2.2.2 christos #include <stdlib.h>
34 1.2.2.2 christos #include <string.h>
35 1.2.2.2 christos #include <memory.h>
36 1.2.2.2 christos #include <sys/param.h>
37 1.2.2.2 christos #include <sys/time.h>
38 1.2.2.2 christos #include <netinet/in.h>
39 1.2.2.2 christos #include <arpa/nameser.h>
40 1.2.2.2 christos #include <resolv.h>
41 1.2.2.2 christos
42 1.2.2.2 christos #include "dst_internal.h"
43 1.2.2.2 christos
44 1.2.2.2 christos #include <md5.h>
45 1.2.2.2 christos #include "port_after.h"
46 1.2.2.2 christos
47 1.2.2.2 christos
48 1.2.2.2 christos #define HMAC_LEN 64
49 1.2.2.2 christos #define HMAC_IPAD 0x36
50 1.2.2.2 christos #define HMAC_OPAD 0x5c
51 1.2.2.2 christos #define MD5_LEN 16
52 1.2.2.2 christos
53 1.2.2.2 christos
54 1.2.2.2 christos typedef struct hmackey {
55 1.2.2.2 christos u_char hk_ipad[64], hk_opad[64];
56 1.2.2.2 christos } HMAC_Key;
57 1.2.2.2 christos
58 1.2.2.2 christos
59 1.2.2.2 christos /**************************************************************************
60 1.2.2.2 christos * dst_hmac_md5_sign
61 1.2.2.2 christos * Call HMAC signing functions to sign a block of data.
62 1.2.2.2 christos * There are three steps to signing, INIT (initialize structures),
63 1.2.2.2 christos * UPDATE (hash (more) data), FINAL (generate a signature). This
64 1.2.2.2 christos * routine performs one or more of these steps.
65 1.2.2.2 christos * Parameters
66 1.2.2.2 christos * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
67 1.2.2.2 christos * priv_key key to use for signing.
68 1.2.2.2 christos * context the context to be used in this digest
69 1.2.2.2 christos * data data to be signed.
70 1.2.2.2 christos * len length in bytes of data.
71 1.2.2.2 christos * signature location to store signature.
72 1.2.2.2 christos * sig_len size of the signature location
73 1.2.2.2 christos * returns
74 1.2.2.2 christos * N Success on SIG_MODE_FINAL = returns signature length in bytes
75 1.2.2.2 christos * 0 Success on SIG_MODE_INIT and UPDATE
76 1.2.2.2 christos * <0 Failure
77 1.2.2.2 christos */
78 1.2.2.2 christos
79 1.2.2.2 christos static int
80 1.2.2.2 christos dst_hmac_md5_sign(const int mode, DST_KEY *d_key, void **context,
81 1.2.2.2 christos const u_char *data, const int len,
82 1.2.2.2 christos u_char *signature, const int sig_len)
83 1.2.2.2 christos {
84 1.2.2.2 christos HMAC_Key *key;
85 1.2.2.2 christos int sign_len = 0;
86 1.2.2.2 christos MD5_CTX *ctx = NULL;
87 1.2.2.2 christos
88 1.2.2.2 christos if (d_key == NULL || d_key->dk_KEY_struct == NULL)
89 1.2.2.2 christos return (-1);
90 1.2.2.2 christos
91 1.2.2.2 christos if (mode & SIG_MODE_INIT)
92 1.2.2.2 christos ctx = (MD5_CTX *) malloc(sizeof(*ctx));
93 1.2.2.2 christos else if (context)
94 1.2.2.2 christos ctx = (MD5_CTX *) *context;
95 1.2.2.2 christos if (ctx == NULL)
96 1.2.2.2 christos return (-1);
97 1.2.2.2 christos
98 1.2.2.2 christos key = (HMAC_Key *) d_key->dk_KEY_struct;
99 1.2.2.2 christos
100 1.2.2.2 christos if (mode & SIG_MODE_INIT) {
101 1.2.2.2 christos MD5Init(ctx);
102 1.2.2.2 christos MD5Update(ctx, key->hk_ipad, HMAC_LEN);
103 1.2.2.2 christos }
104 1.2.2.2 christos
105 1.2.2.2 christos if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
106 1.2.2.2 christos MD5Update(ctx, data, (unsigned int)len);
107 1.2.2.2 christos
108 1.2.2.2 christos if (mode & SIG_MODE_FINAL) {
109 1.2.2.2 christos if (signature == NULL || sig_len < MD5_LEN)
110 1.2.2.2 christos return (SIGN_FINAL_FAILURE);
111 1.2.2.2 christos MD5Final(signature, ctx);
112 1.2.2.2 christos
113 1.2.2.2 christos /* perform outer MD5 */
114 1.2.2.2 christos MD5Init(ctx);
115 1.2.2.2 christos MD5Update(ctx, key->hk_opad, HMAC_LEN);
116 1.2.2.2 christos MD5Update(ctx, signature, MD5_LEN);
117 1.2.2.2 christos MD5Final(signature, ctx);
118 1.2.2.2 christos sign_len = MD5_LEN;
119 1.2.2.2 christos SAFE_FREE(ctx);
120 1.2.2.2 christos }
121 1.2.2.2 christos else {
122 1.2.2.2 christos if (context == NULL)
123 1.2.2.2 christos return (-1);
124 1.2.2.2 christos *context = (void *) ctx;
125 1.2.2.2 christos }
126 1.2.2.2 christos return (sign_len);
127 1.2.2.2 christos }
128 1.2.2.2 christos
129 1.2.2.2 christos
130 1.2.2.2 christos /**************************************************************************
131 1.2.2.2 christos * dst_hmac_md5_verify()
132 1.2.2.2 christos * Calls HMAC verification routines. There are three steps to
133 1.2.2.2 christos * verification, INIT (initialize structures), UPDATE (hash (more) data),
134 1.2.2.2 christos * FINAL (generate a signature). This routine performs one or more of
135 1.2.2.2 christos * these steps.
136 1.2.2.2 christos * Parameters
137 1.2.2.2 christos * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
138 1.2.2.2 christos * dkey key to use for verify.
139 1.2.2.2 christos * data data signed.
140 1.2.2.2 christos * len length in bytes of data.
141 1.2.2.2 christos * signature signature.
142 1.2.2.2 christos * sig_len length in bytes of signature.
143 1.2.2.2 christos * returns
144 1.2.2.2 christos * 0 Success
145 1.2.2.2 christos * <0 Failure
146 1.2.2.2 christos */
147 1.2.2.2 christos
148 1.2.2.2 christos static int
149 1.2.2.2 christos dst_hmac_md5_verify(const int mode, DST_KEY *d_key, void **context,
150 1.2.2.2 christos const u_char *data, const int len,
151 1.2.2.2 christos const u_char *signature, const int sig_len)
152 1.2.2.2 christos {
153 1.2.2.2 christos HMAC_Key *key;
154 1.2.2.2 christos MD5_CTX *ctx = NULL;
155 1.2.2.2 christos
156 1.2.2.2 christos if (d_key == NULL || d_key->dk_KEY_struct == NULL)
157 1.2.2.2 christos return (-1);
158 1.2.2.2 christos
159 1.2.2.2 christos if (mode & SIG_MODE_INIT)
160 1.2.2.2 christos ctx = (MD5_CTX *) malloc(sizeof(*ctx));
161 1.2.2.2 christos else if (context)
162 1.2.2.2 christos ctx = (MD5_CTX *) *context;
163 1.2.2.2 christos if (ctx == NULL)
164 1.2.2.2 christos return (-1);
165 1.2.2.2 christos
166 1.2.2.2 christos key = (HMAC_Key *) d_key->dk_KEY_struct;
167 1.2.2.2 christos if (mode & SIG_MODE_INIT) {
168 1.2.2.2 christos MD5Init(ctx);
169 1.2.2.2 christos MD5Update(ctx, key->hk_ipad, HMAC_LEN);
170 1.2.2.2 christos }
171 1.2.2.2 christos if ((mode & SIG_MODE_UPDATE) && (data && len > 0))
172 1.2.2.2 christos MD5Update(ctx, data, (unsigned int)len);
173 1.2.2.2 christos
174 1.2.2.2 christos if (mode & SIG_MODE_FINAL) {
175 1.2.2.2 christos u_char digest[MD5_LEN];
176 1.2.2.2 christos if (signature == NULL || key == NULL || sig_len != MD5_LEN)
177 1.2.2.2 christos return (VERIFY_FINAL_FAILURE);
178 1.2.2.2 christos MD5Final(digest, ctx);
179 1.2.2.2 christos
180 1.2.2.2 christos /* perform outer MD5 */
181 1.2.2.2 christos MD5Init(ctx);
182 1.2.2.2 christos MD5Update(ctx, key->hk_opad, HMAC_LEN);
183 1.2.2.2 christos MD5Update(ctx, digest, MD5_LEN);
184 1.2.2.2 christos MD5Final(digest, ctx);
185 1.2.2.2 christos
186 1.2.2.2 christos SAFE_FREE(ctx);
187 1.2.2.2 christos if (memcmp(digest, signature, MD5_LEN) != 0)
188 1.2.2.2 christos return (VERIFY_FINAL_FAILURE);
189 1.2.2.2 christos }
190 1.2.2.2 christos else {
191 1.2.2.2 christos if (context == NULL)
192 1.2.2.2 christos return (-1);
193 1.2.2.2 christos *context = (void *) ctx;
194 1.2.2.2 christos }
195 1.2.2.2 christos return (0);
196 1.2.2.2 christos }
197 1.2.2.2 christos
198 1.2.2.2 christos
199 1.2.2.2 christos /**************************************************************************
200 1.2.2.2 christos * dst_buffer_to_hmac_md5
201 1.2.2.2 christos * Converts key from raw data to an HMAC Key
202 1.2.2.2 christos * This function gets in a pointer to the data
203 1.2.2.2 christos * Parameters
204 1.2.2.2 christos * hkey the HMAC key to be filled in
205 1.2.2.2 christos * key the key in raw format
206 1.2.2.2 christos * keylen the length of the key
207 1.2.2.2 christos * Return
208 1.2.2.2 christos * 0 Success
209 1.2.2.2 christos * <0 Failure
210 1.2.2.2 christos */
211 1.2.2.2 christos static int
212 1.2.2.2 christos dst_buffer_to_hmac_md5(DST_KEY *dkey, const u_char *key, const int keylen)
213 1.2.2.2 christos {
214 1.2.2.2 christos int i;
215 1.2.2.2 christos HMAC_Key *hkey = NULL;
216 1.2.2.2 christos MD5_CTX ctx;
217 1.2.2.2 christos int local_keylen = keylen;
218 1.2.2.2 christos u_char tk[MD5_LEN];
219 1.2.2.2 christos
220 1.2.2.2 christos if (dkey == NULL || key == NULL || keylen < 0)
221 1.2.2.2 christos return (-1);
222 1.2.2.2 christos
223 1.2.2.2 christos if ((hkey = (HMAC_Key *) malloc(sizeof(HMAC_Key))) == NULL)
224 1.2.2.2 christos return (-2);
225 1.2.2.2 christos
226 1.2.2.2 christos memset(hkey->hk_ipad, 0, sizeof(hkey->hk_ipad));
227 1.2.2.2 christos memset(hkey->hk_opad, 0, sizeof(hkey->hk_opad));
228 1.2.2.2 christos
229 1.2.2.2 christos /* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
230 1.2.2.2 christos if (keylen > HMAC_LEN) {
231 1.2.2.2 christos MD5Init(&ctx);
232 1.2.2.2 christos MD5Update(&ctx, key, (unsigned int)keylen);
233 1.2.2.2 christos MD5Final(tk, &ctx);
234 1.2.2.2 christos memset((void *) &ctx, 0, sizeof(ctx));
235 1.2.2.2 christos key = tk;
236 1.2.2.2 christos local_keylen = MD5_LEN;
237 1.2.2.2 christos }
238 1.2.2.2 christos /* start out by storing key in pads */
239 1.2.2.2 christos memcpy(hkey->hk_ipad, key, local_keylen);
240 1.2.2.2 christos memcpy(hkey->hk_opad, key, local_keylen);
241 1.2.2.2 christos
242 1.2.2.2 christos /* XOR key with hk_ipad and opad values */
243 1.2.2.2 christos for (i = 0; i < HMAC_LEN; i++) {
244 1.2.2.2 christos hkey->hk_ipad[i] ^= HMAC_IPAD;
245 1.2.2.2 christos hkey->hk_opad[i] ^= HMAC_OPAD;
246 1.2.2.2 christos }
247 1.2.2.2 christos dkey->dk_key_size = local_keylen;
248 1.2.2.2 christos dkey->dk_KEY_struct = (void *) hkey;
249 1.2.2.2 christos return (1);
250 1.2.2.2 christos }
251 1.2.2.2 christos
252 1.2.2.2 christos
253 1.2.2.2 christos /**************************************************************************
254 1.2.2.2 christos * dst_hmac_md5_key_to_file_format
255 1.2.2.2 christos * Encodes an HMAC Key into the portable file format.
256 1.2.2.2 christos * Parameters
257 1.2.2.2 christos * hkey HMAC KEY structure
258 1.2.2.2 christos * buff output buffer
259 1.2.2.2 christos * buff_len size of output buffer
260 1.2.2.2 christos * Return
261 1.2.2.2 christos * 0 Failure - null input hkey
262 1.2.2.2 christos * -1 Failure - not enough space in output area
263 1.2.2.2 christos * N Success - Length of data returned in buff
264 1.2.2.2 christos */
265 1.2.2.2 christos
266 1.2.2.2 christos static int
267 1.2.2.2 christos dst_hmac_md5_key_to_file_format(const DST_KEY *dkey, char *buff,
268 1.2.2.2 christos const int buff_len)
269 1.2.2.2 christos {
270 1.2.2.2 christos char *bp;
271 1.2.2.2 christos int len, i, key_len;
272 1.2.2.2 christos u_char key[HMAC_LEN];
273 1.2.2.2 christos HMAC_Key *hkey;
274 1.2.2.2 christos
275 1.2.2.2 christos if (dkey == NULL || dkey->dk_KEY_struct == NULL)
276 1.2.2.2 christos return (0);
277 1.2.2.2 christos /*
278 1.2.2.2 christos * Using snprintf() would be so much simpler here.
279 1.2.2.2 christos */
280 1.2.2.2 christos if (buff == NULL ||
281 1.2.2.2 christos buff_len <= (int)(strlen(KEY_FILE_FMT_STR) +
282 1.2.2.2 christos strlen(KEY_FILE_FORMAT) + 4))
283 1.2.2.2 christos return (-1); /*%< no OR not enough space in output area */
284 1.2.2.2 christos hkey = (HMAC_Key *) dkey->dk_KEY_struct;
285 1.2.2.2 christos memset(buff, 0, buff_len); /*%< just in case */
286 1.2.2.2 christos /* write file header */
287 1.2.2.2 christos snprintf(buff, buff_len, KEY_FILE_FMT_STR, KEY_FILE_FORMAT,
288 1.2.2.2 christos KEY_HMAC_MD5, "HMAC");
289 1.2.2.2 christos
290 1.2.2.2 christos bp = buff + strlen(buff);
291 1.2.2.2 christos
292 1.2.2.2 christos memset(key, 0, HMAC_LEN);
293 1.2.2.2 christos for (i = 0; i < HMAC_LEN; i++)
294 1.2.2.2 christos key[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
295 1.2.2.2 christos for (i = HMAC_LEN - 1; i >= 0; i--)
296 1.2.2.2 christos if (key[i] != 0)
297 1.2.2.2 christos break;
298 1.2.2.2 christos key_len = i + 1;
299 1.2.2.2 christos
300 1.2.2.2 christos if (buff_len - (bp - buff) < 6)
301 1.2.2.2 christos return (-1);
302 1.2.2.2 christos strcat(bp, "Key: ");
303 1.2.2.2 christos bp += strlen("Key: ");
304 1.2.2.2 christos
305 1.2.2.2 christos len = b64_ntop(key, key_len, bp, (size_t)(buff_len - (bp - buff)));
306 1.2.2.2 christos if (len < 0)
307 1.2.2.2 christos return (-1);
308 1.2.2.2 christos bp += len;
309 1.2.2.2 christos if (buff_len - (bp - buff) < 2)
310 1.2.2.2 christos return (-1);
311 1.2.2.2 christos *(bp++) = '\n';
312 1.2.2.2 christos *bp = '\0';
313 1.2.2.2 christos
314 1.2.2.2 christos return (int)(bp - buff);
315 1.2.2.2 christos }
316 1.2.2.2 christos
317 1.2.2.2 christos
318 1.2.2.2 christos /**************************************************************************
319 1.2.2.2 christos * dst_hmac_md5_key_from_file_format
320 1.2.2.2 christos * Converts contents of a key file into an HMAC key.
321 1.2.2.2 christos * Parameters
322 1.2.2.2 christos * hkey structure to put key into
323 1.2.2.2 christos * buff buffer containing the encoded key
324 1.2.2.2 christos * buff_len the length of the buffer
325 1.2.2.2 christos * Return
326 1.2.2.2 christos * n >= 0 Foot print of the key converted
327 1.2.2.2 christos * n < 0 Error in conversion
328 1.2.2.2 christos */
329 1.2.2.2 christos
330 1.2.2.2 christos static int
331 1.2.2.2 christos dst_hmac_md5_key_from_file_format(DST_KEY *dkey, const char *buff,
332 1.2.2.2 christos const int buff_len)
333 1.2.2.2 christos {
334 1.2.2.2 christos const char *p = buff, *eol;
335 1.2.2.2 christos u_char key[HMAC_LEN+1]; /* b64_pton needs more than 64 bytes do decode
336 1.2.2.2 christos * it should probably be fixed rather than doing
337 1.2.2.2 christos * this
338 1.2.2.2 christos */
339 1.2.2.2 christos u_char *tmp;
340 1.2.2.2 christos int key_len, len;
341 1.2.2.2 christos
342 1.2.2.2 christos if (dkey == NULL)
343 1.2.2.2 christos return (-2);
344 1.2.2.2 christos if (buff == NULL || buff_len < 0)
345 1.2.2.2 christos return (-1);
346 1.2.2.2 christos
347 1.2.2.2 christos memset(key, 0, sizeof(key));
348 1.2.2.2 christos
349 1.2.2.2 christos if (!dst_s_verify_str(&p, "Key: "))
350 1.2.2.2 christos return (-3);
351 1.2.2.2 christos
352 1.2.2.2 christos eol = strchr(p, '\n');
353 1.2.2.2 christos if (eol == NULL)
354 1.2.2.2 christos return (-4);
355 1.2.2.2 christos len = (int)(eol - p);
356 1.2.2.2 christos tmp = malloc(len + 2);
357 1.2.2.2 christos if (tmp == NULL)
358 1.2.2.2 christos return (-5);
359 1.2.2.2 christos memcpy(tmp, p, len);
360 1.2.2.2 christos *(tmp + len) = 0x0;
361 1.2.2.2 christos key_len = b64_pton((char *)tmp, key, HMAC_LEN+1); /*%< see above */
362 1.2.2.2 christos SAFE_FREE2(tmp, len + 2);
363 1.2.2.2 christos
364 1.2.2.2 christos if (dst_buffer_to_hmac_md5(dkey, key, key_len) < 0) {
365 1.2.2.2 christos return (-6);
366 1.2.2.2 christos }
367 1.2.2.2 christos return (0);
368 1.2.2.2 christos }
369 1.2.2.2 christos
370 1.2.2.2 christos /*%
371 1.2.2.2 christos * dst_hmac_md5_to_dns_key()
372 1.2.2.2 christos * function to extract hmac key from DST_KEY structure
373 1.2.2.2 christos * intput:
374 1.2.2.2 christos * in_key: HMAC-MD5 key
375 1.2.2.2 christos * output:
376 1.2.2.2 christos * out_str: buffer to write ot
377 1.2.2.2 christos * out_len: size of output buffer
378 1.2.2.2 christos * returns:
379 1.2.2.2 christos * number of bytes written to output buffer
380 1.2.2.2 christos */
381 1.2.2.2 christos static int
382 1.2.2.2 christos dst_hmac_md5_to_dns_key(const DST_KEY *in_key, u_char *out_str,
383 1.2.2.2 christos const int out_len)
384 1.2.2.2 christos {
385 1.2.2.2 christos
386 1.2.2.2 christos HMAC_Key *hkey;
387 1.2.2.2 christos int i;
388 1.2.2.2 christos
389 1.2.2.2 christos if (in_key == NULL || in_key->dk_KEY_struct == NULL ||
390 1.2.2.2 christos out_len <= in_key->dk_key_size || out_str == NULL)
391 1.2.2.2 christos return (-1);
392 1.2.2.2 christos
393 1.2.2.2 christos hkey = (HMAC_Key *) in_key->dk_KEY_struct;
394 1.2.2.2 christos for (i = 0; i < in_key->dk_key_size; i++)
395 1.2.2.2 christos out_str[i] = hkey->hk_ipad[i] ^ HMAC_IPAD;
396 1.2.2.2 christos return (i);
397 1.2.2.2 christos }
398 1.2.2.2 christos
399 1.2.2.2 christos /**************************************************************************
400 1.2.2.2 christos * dst_hmac_md5_compare_keys
401 1.2.2.2 christos * Compare two keys for equality.
402 1.2.2.2 christos * Return
403 1.2.2.2 christos * 0 The keys are equal
404 1.2.2.2 christos * NON-ZERO The keys are not equal
405 1.2.2.2 christos */
406 1.2.2.2 christos
407 1.2.2.2 christos static int
408 1.2.2.2 christos dst_hmac_md5_compare_keys(const DST_KEY *key1, const DST_KEY *key2)
409 1.2.2.2 christos {
410 1.2.2.2 christos HMAC_Key *hkey1 = (HMAC_Key *) key1->dk_KEY_struct;
411 1.2.2.2 christos HMAC_Key *hkey2 = (HMAC_Key *) key2->dk_KEY_struct;
412 1.2.2.2 christos return memcmp(hkey1->hk_ipad, hkey2->hk_ipad, HMAC_LEN);
413 1.2.2.2 christos }
414 1.2.2.2 christos
415 1.2.2.2 christos /**************************************************************************
416 1.2.2.2 christos * dst_hmac_md5_free_key_structure
417 1.2.2.2 christos * Frees all (none) dynamically allocated structures in hkey
418 1.2.2.2 christos */
419 1.2.2.2 christos
420 1.2.2.2 christos static void *
421 1.2.2.2 christos dst_hmac_md5_free_key_structure(void *key)
422 1.2.2.2 christos {
423 1.2.2.2 christos HMAC_Key *hkey = key;
424 1.2.2.2 christos SAFE_FREE(hkey);
425 1.2.2.2 christos return (NULL);
426 1.2.2.2 christos }
427 1.2.2.2 christos
428 1.2.2.2 christos
429 1.2.2.2 christos /***************************************************************************
430 1.2.2.2 christos * dst_hmac_md5_generate_key
431 1.2.2.2 christos * Creates a HMAC key of size size with a maximum size of 63 bytes
432 1.2.2.2 christos * generating a HMAC key larger than 63 bytes makes no sense as that key
433 1.2.2.2 christos * is digested before use.
434 1.2.2.2 christos */
435 1.2.2.2 christos
436 1.2.2.2 christos static int
437 1.2.2.2 christos /*ARGSUSED*/
438 1.2.2.2 christos dst_hmac_md5_generate_key(DST_KEY *key, const int nothing)
439 1.2.2.2 christos {
440 1.2.2.2 christos return (-1);
441 1.2.2.2 christos }
442 1.2.2.2 christos
443 1.2.2.2 christos /*%
444 1.2.2.2 christos * dst_hmac_md5_init() Function to answer set up function pointers for HMAC
445 1.2.2.2 christos * related functions
446 1.2.2.2 christos */
447 1.2.2.2 christos int
448 1.2.2.2 christos #ifdef SUNW_LIBMD5
449 1.2.2.2 christos dst_md5_hmac_init(void)
450 1.2.2.2 christos #else
451 1.2.2.2 christos dst_hmac_md5_init(void)
452 1.2.2.2 christos #endif
453 1.2.2.2 christos {
454 1.2.2.2 christos if (dst_t_func[KEY_HMAC_MD5] != NULL)
455 1.2.2.2 christos return (1);
456 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5] = malloc(sizeof(struct dst_func));
457 1.2.2.2 christos if (dst_t_func[KEY_HMAC_MD5] == NULL)
458 1.2.2.2 christos return (0);
459 1.2.2.2 christos memset(dst_t_func[KEY_HMAC_MD5], 0, sizeof(struct dst_func));
460 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->sign = dst_hmac_md5_sign;
461 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->verify = dst_hmac_md5_verify;
462 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->compare = dst_hmac_md5_compare_keys;
463 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->generate = dst_hmac_md5_generate_key;
464 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->destroy = dst_hmac_md5_free_key_structure;
465 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->to_dns_key = dst_hmac_md5_to_dns_key;
466 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->from_dns_key = dst_buffer_to_hmac_md5;
467 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->to_file_fmt = dst_hmac_md5_key_to_file_format;
468 1.2.2.2 christos dst_t_func[KEY_HMAC_MD5]->from_file_fmt = dst_hmac_md5_key_from_file_format;
469 1.2.2.2 christos return (1);
470 1.2.2.2 christos }
471 1.2.2.2 christos
472 1.2.2.2 christos /*! \file */
473