3 static const char rcsid
[] = "$Header: /proj/cvs/prod/bind9/lib/bind/dst/Attic/hmac_link.c,v 1.8 2007/09/24 17:18:25 each 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
;
228 if (dkey
== NULL
|| key
== NULL
|| keylen
< 0)
231 if ((hkey
= (HMAC_Key
*) malloc(sizeof(HMAC_Key
))) == NULL
)
234 memset(hkey
->hk_ipad
, 0, sizeof(hkey
->hk_ipad
));
235 memset(hkey
->hk_opad
, 0, sizeof(hkey
->hk_opad
));
237 /* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
238 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
,
280 u_char key
[HMAC_LEN
];
283 if (dkey
== NULL
|| dkey
->dk_KEY_struct
== NULL
)
286 * Using snprintf() would be so much simpler here.
289 buff_len
<= (int)(strlen(key_file_fmt_str
) +
290 strlen(KEY_FILE_FORMAT
) + 4))
291 return (-1); /*%< no OR not enough space in output area */
292 hkey
= (HMAC_Key
*) dkey
->dk_KEY_struct
;
293 memset(buff
, 0, buff_len
); /*%< just in case */
294 /* write file header */
295 sprintf(buff
, key_file_fmt_str
, KEY_FILE_FORMAT
, KEY_HMAC_MD5
, "HMAC");
297 bp
= buff
+ strlen(buff
);
299 memset(key
, 0, HMAC_LEN
);
300 for (i
= 0; i
< HMAC_LEN
; i
++)
301 key
[i
] = hkey
->hk_ipad
[i
] ^ HMAC_IPAD
;
302 for (i
= HMAC_LEN
- 1; i
>= 0; i
--)
307 if (buff_len
- (bp
- buff
) < 6)
310 bp
+= strlen("Key: ");
312 len
= b64_ntop(key
, key_len
, bp
, buff_len
- (bp
- buff
));
316 if (buff_len
- (bp
- buff
) < 2)
325 /**************************************************************************
326 * dst_hmac_md5_key_from_file_format
327 * Converts contents of a key file into an HMAC key.
329 * hkey structure to put key into
330 * buff buffer containing the encoded key
331 * buff_len the length of the buffer
333 * n >= 0 Foot print of the key converted
334 * n < 0 Error in conversion
338 dst_hmac_md5_key_from_file_format(DST_KEY
*dkey
, const char *buff
,
341 const char *p
= buff
, *eol
;
342 u_char key
[HMAC_LEN
+1]; /* b64_pton needs more than 64 bytes do decode
343 * it should probably be fixed rather than doing
351 if (buff
== NULL
|| buff_len
< 0)
354 memset(key
, 0, sizeof(key
));
356 if (!dst_s_verify_str(&p
, "Key: "))
359 eol
= strchr(p
, '\n');
363 tmp
= malloc(len
+ 2);
368 key_len
= b64_pton((char *)tmp
, key
, HMAC_LEN
+1); /*%< see above */
369 SAFE_FREE2(tmp
, len
+ 2);
371 if (dst_buffer_to_hmac_md5(dkey
, key
, key_len
) < 0) {
378 * dst_hmac_md5_to_dns_key()
379 * function to extract hmac key from DST_KEY structure
381 * in_key: HMAC-MD5 key
383 * out_str: buffer to write ot
384 * out_len: size of output buffer
386 * number of bytes written to output buffer
389 dst_hmac_md5_to_dns_key(const DST_KEY
*in_key
, u_char
*out_str
,
396 if (in_key
== NULL
|| in_key
->dk_KEY_struct
== NULL
||
397 out_len
<= in_key
->dk_key_size
|| out_str
== NULL
)
400 hkey
= (HMAC_Key
*) in_key
->dk_KEY_struct
;
401 for (i
= 0; i
< in_key
->dk_key_size
; i
++)
402 out_str
[i
] = hkey
->hk_ipad
[i
] ^ HMAC_IPAD
;
406 /**************************************************************************
407 * dst_hmac_md5_compare_keys
408 * Compare two keys for equality.
410 * 0 The keys are equal
411 * NON-ZERO The keys are not equal
415 dst_hmac_md5_compare_keys(const DST_KEY
*key1
, const DST_KEY
*key2
)
417 HMAC_Key
*hkey1
= (HMAC_Key
*) key1
->dk_KEY_struct
;
418 HMAC_Key
*hkey2
= (HMAC_Key
*) key2
->dk_KEY_struct
;
419 return memcmp(hkey1
->hk_ipad
, hkey2
->hk_ipad
, HMAC_LEN
);
422 /**************************************************************************
423 * dst_hmac_md5_free_key_structure
424 * Frees all (none) dynamically allocated structures in hkey
428 dst_hmac_md5_free_key_structure(void *key
)
430 HMAC_Key
*hkey
= key
;
436 /***************************************************************************
437 * dst_hmac_md5_generate_key
438 * Creates a HMAC key of size size with a maximum size of 63 bytes
439 * generating a HMAC key larger than 63 bytes makes no sense as that key
440 * is digested before use.
444 dst_hmac_md5_generate_key(DST_KEY
*key
, const int nothing
)
452 * dst_hmac_md5_init() Function to answer set up function pointers for HMAC
462 if (dst_t_func
[KEY_HMAC_MD5
] != NULL
)
464 dst_t_func
[KEY_HMAC_MD5
] = malloc(sizeof(struct dst_func
));
465 if (dst_t_func
[KEY_HMAC_MD5
] == NULL
)
467 memset(dst_t_func
[KEY_HMAC_MD5
], 0, sizeof(struct dst_func
));
468 dst_t_func
[KEY_HMAC_MD5
]->sign
= dst_hmac_md5_sign
;
469 dst_t_func
[KEY_HMAC_MD5
]->verify
= dst_hmac_md5_verify
;
470 dst_t_func
[KEY_HMAC_MD5
]->compare
= dst_hmac_md5_compare_keys
;
471 dst_t_func
[KEY_HMAC_MD5
]->generate
= dst_hmac_md5_generate_key
;
472 dst_t_func
[KEY_HMAC_MD5
]->destroy
= dst_hmac_md5_free_key_structure
;
473 dst_t_func
[KEY_HMAC_MD5
]->to_dns_key
= dst_hmac_md5_to_dns_key
;
474 dst_t_func
[KEY_HMAC_MD5
]->from_dns_key
= dst_buffer_to_hmac_md5
;
475 dst_t_func
[KEY_HMAC_MD5
]->to_file_fmt
= dst_hmac_md5_key_to_file_format
;
476 dst_t_func
[KEY_HMAC_MD5
]->from_file_fmt
= dst_hmac_md5_key_from_file_format
;
481 #define dst_hmac_md5_init __dst_hmac_md5_init