3 static const char rcsid
[] = "$Header: /proj/cvs/prod/bind9/lib/bind/dst/hmac_link.c,v 1.2.2.1.4.2 2006/03/10 00:17:21 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"
49 # define _MD5_H_ 1 /* make sure we do not include rsaref md5.h file */
53 #include "port_after.h"
57 #define HMAC_IPAD 0x36
58 #define HMAC_OPAD 0x5c
62 typedef struct hmackey
{
63 u_char hk_ipad
[64], hk_opad
[64];
67 /**************************************************************************
69 * Call HMAC signing functions to sign a block of data.
70 * There are three steps to signing, INIT (initialize structures),
71 * UPDATE (hash (more) data), FINAL (generate a signature). This
72 * routine performs one or more of these steps.
74 * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
75 * priv_key key to use for signing.
76 * context the context to be used in this digest
77 * data data to be signed.
78 * len length in bytes of data.
79 * signature location to store signature.
80 * sig_len size of the signature location
82 * N Success on SIG_MODE_FINAL = returns signature length in bytes
83 * 0 Success on SIG_MODE_INIT and UPDATE
88 dst_hmac_md5_sign(const int mode
, DST_KEY
*d_key
, void **context
,
89 const u_char
*data
, const int len
,
90 u_char
*signature
, const int sig_len
)
96 if (d_key
== NULL
|| d_key
->dk_KEY_struct
== NULL
)
99 if (mode
& SIG_MODE_INIT
)
100 ctx
= (MD5_CTX
*) malloc(sizeof(*ctx
));
102 ctx
= (MD5_CTX
*) *context
;
106 key
= (HMAC_Key
*) d_key
->dk_KEY_struct
;
108 if (mode
& SIG_MODE_INIT
) {
110 MD5Update(ctx
, key
->hk_ipad
, HMAC_LEN
);
113 if ((mode
& SIG_MODE_UPDATE
) && (data
&& len
> 0))
114 MD5Update(ctx
, data
, len
);
116 if (mode
& SIG_MODE_FINAL
) {
117 if (signature
== NULL
|| sig_len
< MD5_LEN
)
118 return (SIGN_FINAL_FAILURE
);
119 MD5Final(signature
, ctx
);
121 /* perform outer MD5 */
123 MD5Update(ctx
, key
->hk_opad
, HMAC_LEN
);
124 MD5Update(ctx
, signature
, MD5_LEN
);
125 MD5Final(signature
, ctx
);
132 *context
= (void *) ctx
;
138 /**************************************************************************
139 * dst_hmac_md5_verify()
140 * Calls HMAC verification routines. There are three steps to
141 * verification, INIT (initialize structures), UPDATE (hash (more) data),
142 * FINAL (generate a signature). This routine performs one or more of
145 * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
146 * dkey key to use for verify.
148 * len length in bytes of data.
149 * signature signature.
150 * sig_len length in bytes of signature.
157 dst_hmac_md5_verify(const int mode
, DST_KEY
*d_key
, void **context
,
158 const u_char
*data
, const int len
,
159 const u_char
*signature
, const int sig_len
)
164 if (d_key
== NULL
|| d_key
->dk_KEY_struct
== NULL
)
167 if (mode
& SIG_MODE_INIT
)
168 ctx
= (MD5_CTX
*) malloc(sizeof(*ctx
));
170 ctx
= (MD5_CTX
*) *context
;
174 key
= (HMAC_Key
*) d_key
->dk_KEY_struct
;
175 if (mode
& SIG_MODE_INIT
) {
177 MD5Update(ctx
, key
->hk_ipad
, HMAC_LEN
);
179 if ((mode
& SIG_MODE_UPDATE
) && (data
&& len
> 0))
180 MD5Update(ctx
, data
, len
);
182 if (mode
& SIG_MODE_FINAL
) {
183 u_char digest
[MD5_LEN
];
184 if (signature
== NULL
|| key
== NULL
|| sig_len
!= MD5_LEN
)
185 return (VERIFY_FINAL_FAILURE
);
186 MD5Final(digest
, ctx
);
188 /* perform outer MD5 */
190 MD5Update(ctx
, key
->hk_opad
, HMAC_LEN
);
191 MD5Update(ctx
, digest
, MD5_LEN
);
192 MD5Final(digest
, ctx
);
195 if (memcmp(digest
, signature
, MD5_LEN
) != 0)
196 return (VERIFY_FINAL_FAILURE
);
201 *context
= (void *) ctx
;
207 /**************************************************************************
208 * dst_buffer_to_hmac_md5
209 * Converts key from raw data to an HMAC Key
210 * This function gets in a pointer to the data
212 * hkey the HMAC key to be filled in
213 * key the key in raw format
214 * keylen the length of the key
220 dst_buffer_to_hmac_md5(DST_KEY
*dkey
, const u_char
*key
, const int keylen
)
223 HMAC_Key
*hkey
= NULL
;
225 int local_keylen
= keylen
;
227 if (dkey
== NULL
|| key
== NULL
|| keylen
< 0)
230 if ((hkey
= (HMAC_Key
*) malloc(sizeof(HMAC_Key
))) == NULL
)
233 memset(hkey
->hk_ipad
, 0, sizeof(hkey
->hk_ipad
));
234 memset(hkey
->hk_opad
, 0, sizeof(hkey
->hk_opad
));
236 /* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
237 if (keylen
> HMAC_LEN
) {
240 MD5Update(&ctx
, key
, keylen
);
242 memset((void *) &ctx
, 0, sizeof(ctx
));
244 local_keylen
= MD5_LEN
;
246 /* start out by storing key in pads */
247 memcpy(hkey
->hk_ipad
, key
, local_keylen
);
248 memcpy(hkey
->hk_opad
, key
, local_keylen
);
250 /* XOR key with hk_ipad and opad values */
251 for (i
= 0; i
< HMAC_LEN
; i
++) {
252 hkey
->hk_ipad
[i
] ^= HMAC_IPAD
;
253 hkey
->hk_opad
[i
] ^= HMAC_OPAD
;
255 dkey
->dk_key_size
= local_keylen
;
256 dkey
->dk_KEY_struct
= (void *) hkey
;
261 /**************************************************************************
262 * dst_hmac_md5_key_to_file_format
263 * Encodes an HMAC Key into the portable file format.
265 * hkey HMAC KEY structure
267 * buff_len size of output buffer
269 * 0 Failure - null input hkey
270 * -1 Failure - not enough space in output area
271 * N Success - Length of data returned in buff
275 dst_hmac_md5_key_to_file_format(const DST_KEY
*dkey
, char *buff
,
279 int len
, b_len
, i
, key_len
;
280 u_char key
[HMAC_LEN
];
283 if (dkey
== NULL
|| dkey
->dk_KEY_struct
== NULL
)
285 if (buff
== NULL
|| buff_len
<= (int) strlen(key_file_fmt_str
))
286 return (-1); /* no OR not enough space in output area */
288 hkey
= (HMAC_Key
*) dkey
->dk_KEY_struct
;
289 memset(buff
, 0, buff_len
); /* just in case */
290 /* write file header */
291 sprintf(buff
, key_file_fmt_str
, KEY_FILE_FORMAT
, KEY_HMAC_MD5
, "HMAC");
293 bp
= buff
+ strlen(buff
);
294 b_len
= buff_len
- (bp
- buff
);
296 memset(key
, 0, HMAC_LEN
);
297 for (i
= 0; i
< HMAC_LEN
; i
++)
298 key
[i
] = hkey
->hk_ipad
[i
] ^ HMAC_IPAD
;
299 for (i
= HMAC_LEN
- 1; i
>= 0; i
--)
305 bp
+= strlen("Key: ");
306 b_len
= buff_len
- (bp
- buff
);
308 len
= b64_ntop(key
, key_len
, bp
, b_len
);
314 b_len
= buff_len
- (bp
- buff
);
316 return (buff_len
- b_len
);
320 /**************************************************************************
321 * dst_hmac_md5_key_from_file_format
322 * Converts contents of a key file into an HMAC key.
324 * hkey structure to put key into
325 * buff buffer containing the encoded key
326 * buff_len the length of the buffer
328 * n >= 0 Foot print of the key converted
329 * n < 0 Error in conversion
333 dst_hmac_md5_key_from_file_format(DST_KEY
*dkey
, const char *buff
,
336 const char *p
= buff
, *eol
;
337 u_char key
[HMAC_LEN
+1]; /* b64_pton needs more than 64 bytes do decode
338 * it should probably be fixed rather than doing
346 if (buff
== NULL
|| buff_len
< 0)
349 memset(key
, 0, sizeof(key
));
351 if (!dst_s_verify_str(&p
, "Key: "))
354 eol
= strchr(p
, '\n');
358 tmp
= malloc(len
+ 2);
363 key_len
= b64_pton((char *)tmp
, key
, HMAC_LEN
+1); /* see above */
364 SAFE_FREE2(tmp
, len
+ 2);
366 if (dst_buffer_to_hmac_md5(dkey
, key
, key_len
) < 0) {
373 * dst_hmac_md5_to_dns_key()
374 * function to extract hmac key from DST_KEY structure
376 * in_key: HMAC-MD5 key
378 * out_str: buffer to write ot
379 * out_len: size of output buffer
381 * number of bytes written to output buffer
384 dst_hmac_md5_to_dns_key(const DST_KEY
*in_key
, u_char
*out_str
,
391 if (in_key
== NULL
|| in_key
->dk_KEY_struct
== NULL
||
392 out_len
<= in_key
->dk_key_size
|| out_str
== NULL
)
395 hkey
= (HMAC_Key
*) in_key
->dk_KEY_struct
;
396 for (i
= 0; i
< in_key
->dk_key_size
; i
++)
397 out_str
[i
] = hkey
->hk_ipad
[i
] ^ HMAC_IPAD
;
401 /**************************************************************************
402 * dst_hmac_md5_compare_keys
403 * Compare two keys for equality.
405 * 0 The keys are equal
406 * NON-ZERO The keys are not equal
410 dst_hmac_md5_compare_keys(const DST_KEY
*key1
, const DST_KEY
*key2
)
412 HMAC_Key
*hkey1
= (HMAC_Key
*) key1
->dk_KEY_struct
;
413 HMAC_Key
*hkey2
= (HMAC_Key
*) key2
->dk_KEY_struct
;
414 return memcmp(hkey1
->hk_ipad
, hkey2
->hk_ipad
, HMAC_LEN
);
417 /**************************************************************************
418 * dst_hmac_md5_free_key_structure
419 * Frees all (none) dynamically allocated structures in hkey
423 dst_hmac_md5_free_key_structure(void *key
)
425 HMAC_Key
*hkey
= key
;
431 /***************************************************************************
432 * dst_hmac_md5_generate_key
433 * Creates a HMAC key of size size with a maximum size of 63 bytes
434 * generating a HMAC key larger than 63 bytes makes no sense as that key
435 * is digested before use.
439 dst_hmac_md5_generate_key(DST_KEY
*key
, const int nothing
)
447 * dst_hmac_md5_init() Function to answer set up function pointers for HMAC
457 if (dst_t_func
[KEY_HMAC_MD5
] != NULL
)
459 dst_t_func
[KEY_HMAC_MD5
] = malloc(sizeof(struct dst_func
));
460 if (dst_t_func
[KEY_HMAC_MD5
] == NULL
)
462 memset(dst_t_func
[KEY_HMAC_MD5
], 0, sizeof(struct dst_func
));
463 dst_t_func
[KEY_HMAC_MD5
]->sign
= dst_hmac_md5_sign
;
464 dst_t_func
[KEY_HMAC_MD5
]->verify
= dst_hmac_md5_verify
;
465 dst_t_func
[KEY_HMAC_MD5
]->compare
= dst_hmac_md5_compare_keys
;
466 dst_t_func
[KEY_HMAC_MD5
]->generate
= dst_hmac_md5_generate_key
;
467 dst_t_func
[KEY_HMAC_MD5
]->destroy
= dst_hmac_md5_free_key_structure
;
468 dst_t_func
[KEY_HMAC_MD5
]->to_dns_key
= dst_hmac_md5_to_dns_key
;
469 dst_t_func
[KEY_HMAC_MD5
]->from_dns_key
= dst_buffer_to_hmac_md5
;
470 dst_t_func
[KEY_HMAC_MD5
]->to_file_fmt
= dst_hmac_md5_key_to_file_format
;
471 dst_t_func
[KEY_HMAC_MD5
]->from_file_fmt
= dst_hmac_md5_key_from_file_format
;
476 #define dst_hmac_md5_init __dst_hmac_md5_init