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