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 $";
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.
23 * This file contains an implementation of the HMAC-MD5 algorithm.
25 #include "port_before.h"
32 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <arpa/nameser.h>
38 #include "dst_internal.h"
42 # define _MD5_H_ 1 /* make sure we do not include rsaref md5.h file */
46 #include "port_after.h"
50 #define HMAC_IPAD 0x36
51 #define HMAC_OPAD 0x5c
55 typedef struct hmackey
{
56 u_char hk_ipad
[64], hk_opad
[64];
60 /**************************************************************************
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.
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
75 * N Success on SIG_MODE_FINAL = returns signature length in bytes
76 * 0 Success on SIG_MODE_INIT and UPDATE
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
)
89 if (mode
& SIG_MODE_INIT
)
90 ctx
= (MD5_CTX
*) malloc(sizeof(*ctx
));
92 ctx
= (MD5_CTX
*) *context
;
96 if (d_key
== NULL
|| d_key
->dk_KEY_struct
== NULL
)
98 key
= (HMAC_Key
*) d_key
->dk_KEY_struct
;
100 if (mode
& SIG_MODE_INIT
) {
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 */
115 MD5Update(ctx
, key
->hk_opad
, HMAC_LEN
);
116 MD5Update(ctx
, signature
, MD5_LEN
);
117 MD5Final(signature
, ctx
);
124 *context
= (void *) ctx
;
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
137 * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
138 * dkey key to use for verify.
140 * len length in bytes of data.
141 * signature signature.
142 * sig_len length in bytes of signature.
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
)
156 if (mode
& SIG_MODE_INIT
)
157 ctx
= (MD5_CTX
*) malloc(sizeof(*ctx
));
159 ctx
= (MD5_CTX
*) *context
;
163 if (d_key
== NULL
|| d_key
->dk_KEY_struct
== NULL
)
166 key
= (HMAC_Key
*) d_key
->dk_KEY_struct
;
167 if (mode
& SIG_MODE_INIT
) {
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 */
182 MD5Update(ctx
, key
->hk_opad
, HMAC_LEN
);
183 MD5Update(ctx
, digest
, MD5_LEN
);
184 MD5Final(digest
, ctx
);
187 if (memcmp(digest
, signature
, MD5_LEN
) != 0)
188 return (VERIFY_FINAL_FAILURE
);
193 *context
= (void *) ctx
;
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
204 * hkey the HMAC key to be filled in
205 * key the key in raw format
206 * keylen the length of the key
212 dst_buffer_to_hmac_md5(DST_KEY
*dkey
, const u_char
*key
, const int keylen
)
215 HMAC_Key
*hkey
= NULL
;
217 int local_keylen
= keylen
;
219 if (dkey
== NULL
|| key
== NULL
|| keylen
< 0)
222 if ((hkey
= (HMAC_Key
*) malloc(sizeof(HMAC_Key
))) == NULL
)
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
) {
232 MD5Update(&ctx
, key
, keylen
);
234 memset((void *) &ctx
, 0, sizeof(ctx
));
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
;
253 /**************************************************************************
254 * dst_hmac_md5_key_to_file_format
255 * Encodes an HMAC Key into the portable file format.
257 * hkey HMAC KEY structure
259 * buff_len size of output buffer
261 * 0 Failure - null input hkey
262 * -1 Failure - not enough space in output area
263 * N Success - Length of data returned in buff
267 dst_hmac_md5_key_to_file_format(const DST_KEY
*dkey
, char *buff
,
271 int len
, b_len
, i
, key_len
;
272 u_char key
[HMAC_LEN
];
275 if (dkey
== NULL
|| dkey
->dk_KEY_struct
== NULL
)
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
--)
297 bp
+= strlen("Key: ");
298 b_len
= buff_len
- (bp
- buff
);
300 len
= b64_ntop(key
, key_len
, bp
, b_len
);
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.
316 * hkey structure to put key into
317 * buff buffer containing the encoded key
318 * buff_len the length of the buffer
320 * n >= 0 Foot print of the key converted
321 * n < 0 Error in conversion
325 dst_hmac_md5_key_from_file_format(DST_KEY
*dkey
, const char *buff
,
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
338 if (buff
== NULL
|| buff_len
< 0)
341 memset(key
, 0, sizeof(key
));
343 if (!dst_s_verify_str(&p
, "Key: "))
346 eol
= strchr(p
, '\n');
350 tmp
= malloc(len
+ 2);
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) {
363 * dst_hmac_md5_to_dns_key()
364 * function to extract hmac key from DST_KEY structure
366 * in_key: HMAC-MD5 key
368 * out_str: buffer to write ot
369 * out_len: size of output buffer
371 * number of bytes written to output buffer
374 dst_hmac_md5_to_dns_key(const DST_KEY
*in_key
, u_char
*out_str
,
381 if (in_key
== NULL
|| in_key
->dk_KEY_struct
== NULL
||
382 out_len
<= in_key
->dk_key_size
|| out_str
== NULL
)
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
;
391 /**************************************************************************
392 * dst_hmac_md5_compare_keys
393 * Compare two keys for equality.
395 * 0 The keys are equal
396 * NON-ZERO The keys are not equal
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
413 dst_hmac_md5_free_key_structure(void *key
)
415 HMAC_Key
*hkey
= key
;
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.
429 dst_hmac_md5_generate_key(DST_KEY
*key
, const int nothing
)
437 * dst_hmac_md5_init() Function to answer set up function pointers for HMAC
443 if (dst_t_func
[KEY_HMAC_MD5
] != NULL
)
445 dst_t_func
[KEY_HMAC_MD5
] = malloc(sizeof(struct dst_func
));
446 if (dst_t_func
[KEY_HMAC_MD5
] == NULL
)
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
;
462 #define dst_hmac_md5_init __dst_hmac_md5_init