5 static const char rcsid
[] = "Header: /proj/cvs/prod/libbind/dst/hmac_link.c,v 1.8 2007/09/24 17:18:25 each Exp";
8 * Portions Copyright (c) 1995-1998 by Trusted Information Systems, Inc.
10 * Permission to use, copy modify, and distribute this software for any
11 * purpose with or without fee is hereby granted, provided that the above
12 * copyright notice and this permission notice appear in all copies.
14 * THE SOFTWARE IS PROVIDED "AS IS" AND TRUSTED INFORMATION SYSTEMS
15 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
17 * TRUSTED INFORMATION SYSTEMS BE LIABLE FOR ANY SPECIAL, DIRECT,
18 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
19 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
20 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
21 * WITH THE USE OR PERFORMANCE OF THE SOFTWARE.
25 * This file contains an implementation of the HMAC-MD5 algorithm.
27 #include "port_before.h"
34 #include <sys/param.h>
36 #include <netinet/in.h>
37 #include <arpa/nameser.h>
40 #include "dst_internal.h"
51 # define _MD5_H_ 1 /*%< make sure we do not include rsaref md5.h file */
55 #include "port_after.h"
59 #define HMAC_IPAD 0x36
60 #define HMAC_OPAD 0x5c
64 typedef struct hmackey
{
65 u_char hk_ipad
[64], hk_opad
[64];
69 /**************************************************************************
71 * Call HMAC signing functions to sign a block of data.
72 * There are three steps to signing, INIT (initialize structures),
73 * UPDATE (hash (more) data), FINAL (generate a signature). This
74 * routine performs one or more of these steps.
76 * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
77 * priv_key key to use for signing.
78 * context the context to be used in this digest
79 * data data to be signed.
80 * len length in bytes of data.
81 * signature location to store signature.
82 * sig_len size of the signature location
84 * N Success on SIG_MODE_FINAL = returns signature length in bytes
85 * 0 Success on SIG_MODE_INIT and UPDATE
90 dst_hmac_md5_sign(const int mode
, DST_KEY
*d_key
, void **context
,
91 const u_char
*data
, const int len
,
92 u_char
*signature
, const int sig_len
)
98 if (d_key
== NULL
|| d_key
->dk_KEY_struct
== NULL
)
101 if (mode
& SIG_MODE_INIT
)
102 ctx
= (MD5_CTX
*) malloc(sizeof(*ctx
));
104 ctx
= (MD5_CTX
*) *context
;
108 key
= (HMAC_Key
*) d_key
->dk_KEY_struct
;
110 if (mode
& SIG_MODE_INIT
) {
112 MD5Update(ctx
, key
->hk_ipad
, HMAC_LEN
);
115 if ((mode
& SIG_MODE_UPDATE
) && (data
&& len
> 0))
116 MD5Update(ctx
, data
, len
);
118 if (mode
& SIG_MODE_FINAL
) {
119 if (signature
== NULL
|| sig_len
< MD5_LEN
)
120 return (SIGN_FINAL_FAILURE
);
121 MD5Final(signature
, ctx
);
123 /* perform outer MD5 */
125 MD5Update(ctx
, key
->hk_opad
, HMAC_LEN
);
126 MD5Update(ctx
, signature
, MD5_LEN
);
127 MD5Final(signature
, ctx
);
134 *context
= (void *) ctx
;
140 /**************************************************************************
141 * dst_hmac_md5_verify()
142 * Calls HMAC verification routines. There are three steps to
143 * verification, INIT (initialize structures), UPDATE (hash (more) data),
144 * FINAL (generate a signature). This routine performs one or more of
147 * mode SIG_MODE_INIT, SIG_MODE_UPDATE and/or SIG_MODE_FINAL.
148 * dkey key to use for verify.
150 * len length in bytes of data.
151 * signature signature.
152 * sig_len length in bytes of signature.
159 dst_hmac_md5_verify(const int mode
, DST_KEY
*d_key
, void **context
,
160 const u_char
*data
, const int len
,
161 const u_char
*signature
, const int sig_len
)
166 if (d_key
== NULL
|| d_key
->dk_KEY_struct
== NULL
)
169 if (mode
& SIG_MODE_INIT
)
170 ctx
= (MD5_CTX
*) malloc(sizeof(*ctx
));
172 ctx
= (MD5_CTX
*) *context
;
176 key
= (HMAC_Key
*) d_key
->dk_KEY_struct
;
177 if (mode
& SIG_MODE_INIT
) {
179 MD5Update(ctx
, key
->hk_ipad
, HMAC_LEN
);
181 if ((mode
& SIG_MODE_UPDATE
) && (data
&& len
> 0))
182 MD5Update(ctx
, data
, len
);
184 if (mode
& SIG_MODE_FINAL
) {
185 u_char digest
[MD5_LEN
];
186 if (signature
== NULL
|| key
== NULL
|| sig_len
!= MD5_LEN
)
187 return (VERIFY_FINAL_FAILURE
);
188 MD5Final(digest
, ctx
);
190 /* perform outer MD5 */
192 MD5Update(ctx
, key
->hk_opad
, HMAC_LEN
);
193 MD5Update(ctx
, digest
, MD5_LEN
);
194 MD5Final(digest
, ctx
);
197 if (memcmp(digest
, signature
, MD5_LEN
) != 0)
198 return (VERIFY_FINAL_FAILURE
);
203 *context
= (void *) ctx
;
209 /**************************************************************************
210 * dst_buffer_to_hmac_md5
211 * Converts key from raw data to an HMAC Key
212 * This function gets in a pointer to the data
214 * hkey the HMAC key to be filled in
215 * key the key in raw format
216 * keylen the length of the key
222 dst_buffer_to_hmac_md5(DST_KEY
*dkey
, const u_char
*key
, const int keylen
)
225 HMAC_Key
*hkey
= NULL
;
227 int local_keylen
= keylen
;
230 if (dkey
== NULL
|| key
== NULL
|| keylen
< 0)
233 if ((hkey
= (HMAC_Key
*) malloc(sizeof(HMAC_Key
))) == NULL
)
236 memset(hkey
->hk_ipad
, 0, sizeof(hkey
->hk_ipad
));
237 memset(hkey
->hk_opad
, 0, sizeof(hkey
->hk_opad
));
239 /* if key is longer than HMAC_LEN bytes reset it to key=MD5(key) */
240 if (keylen
> HMAC_LEN
) {
242 MD5Update(&ctx
, key
, keylen
);
244 memset((void *) &ctx
, 0, sizeof(ctx
));
246 local_keylen
= MD5_LEN
;
248 /* start out by storing key in pads */
249 memcpy(hkey
->hk_ipad
, key
, local_keylen
);
250 memcpy(hkey
->hk_opad
, key
, local_keylen
);
252 /* XOR key with hk_ipad and opad values */
253 for (i
= 0; i
< HMAC_LEN
; i
++) {
254 hkey
->hk_ipad
[i
] ^= HMAC_IPAD
;
255 hkey
->hk_opad
[i
] ^= HMAC_OPAD
;
257 dkey
->dk_key_size
= local_keylen
;
258 dkey
->dk_KEY_struct
= (void *) hkey
;
263 /**************************************************************************
264 * dst_hmac_md5_key_to_file_format
265 * Encodes an HMAC Key into the portable file format.
267 * hkey HMAC KEY structure
269 * buff_len size of output buffer
271 * 0 Failure - null input hkey
272 * -1 Failure - not enough space in output area
273 * N Success - Length of data returned in buff
277 dst_hmac_md5_key_to_file_format(const DST_KEY
*dkey
, char *buff
,
282 u_char key
[HMAC_LEN
];
285 if (dkey
== NULL
|| dkey
->dk_KEY_struct
== NULL
)
288 * Using snprintf() would be so much simpler here.
291 buff_len
<= (int)(strlen(key_file_fmt_str
) +
292 strlen(KEY_FILE_FORMAT
) + 4))
293 return (-1); /*%< no OR not enough space in output area */
294 hkey
= (HMAC_Key
*) dkey
->dk_KEY_struct
;
295 memset(buff
, 0, buff_len
); /*%< just in case */
296 /* write file header */
297 sprintf(buff
, key_file_fmt_str
, KEY_FILE_FORMAT
, KEY_HMAC_MD5
, "HMAC");
299 bp
= buff
+ strlen(buff
);
301 memset(key
, 0, HMAC_LEN
);
302 for (i
= 0; i
< HMAC_LEN
; i
++)
303 key
[i
] = hkey
->hk_ipad
[i
] ^ HMAC_IPAD
;
304 for (i
= HMAC_LEN
- 1; i
>= 0; i
--)
309 if (buff_len
- (bp
- buff
) < 6)
312 bp
+= strlen("Key: ");
314 len
= b64_ntop(key
, key_len
, bp
, buff_len
- (bp
- buff
));
318 if (buff_len
- (bp
- buff
) < 2)
327 /**************************************************************************
328 * dst_hmac_md5_key_from_file_format
329 * Converts contents of a key file into an HMAC key.
331 * hkey structure to put key into
332 * buff buffer containing the encoded key
333 * buff_len the length of the buffer
335 * n >= 0 Foot print of the key converted
336 * n < 0 Error in conversion
340 dst_hmac_md5_key_from_file_format(DST_KEY
*dkey
, const char *buff
,
343 const char *p
= buff
, *eol
;
344 u_char key
[HMAC_LEN
+1]; /* b64_pton needs more than 64 bytes do decode
345 * it should probably be fixed rather than doing
353 if (buff
== NULL
|| buff_len
< 0)
356 memset(key
, 0, sizeof(key
));
358 if (!dst_s_verify_str(&p
, "Key: "))
361 eol
= strchr(p
, '\n');
365 tmp
= malloc(len
+ 2);
370 key_len
= b64_pton((char *)tmp
, key
, HMAC_LEN
+1); /*%< see above */
371 SAFE_FREE2(tmp
, len
+ 2);
373 if (dst_buffer_to_hmac_md5(dkey
, key
, key_len
) < 0) {
380 * dst_hmac_md5_to_dns_key()
381 * function to extract hmac key from DST_KEY structure
383 * in_key: HMAC-MD5 key
385 * out_str: buffer to write ot
386 * out_len: size of output buffer
388 * number of bytes written to output buffer
391 dst_hmac_md5_to_dns_key(const DST_KEY
*in_key
, u_char
*out_str
,
398 if (in_key
== NULL
|| in_key
->dk_KEY_struct
== NULL
||
399 out_len
<= in_key
->dk_key_size
|| out_str
== NULL
)
402 hkey
= (HMAC_Key
*) in_key
->dk_KEY_struct
;
403 for (i
= 0; i
< in_key
->dk_key_size
; i
++)
404 out_str
[i
] = hkey
->hk_ipad
[i
] ^ HMAC_IPAD
;
408 /**************************************************************************
409 * dst_hmac_md5_compare_keys
410 * Compare two keys for equality.
412 * 0 The keys are equal
413 * NON-ZERO The keys are not equal
417 dst_hmac_md5_compare_keys(const DST_KEY
*key1
, const DST_KEY
*key2
)
419 HMAC_Key
*hkey1
= (HMAC_Key
*) key1
->dk_KEY_struct
;
420 HMAC_Key
*hkey2
= (HMAC_Key
*) key2
->dk_KEY_struct
;
421 return memcmp(hkey1
->hk_ipad
, hkey2
->hk_ipad
, HMAC_LEN
);
424 /**************************************************************************
425 * dst_hmac_md5_free_key_structure
426 * Frees all (none) dynamically allocated structures in hkey
430 dst_hmac_md5_free_key_structure(void *key
)
432 HMAC_Key
*hkey
= key
;
438 /***************************************************************************
439 * dst_hmac_md5_generate_key
440 * Creates a HMAC key of size size with a maximum size of 63 bytes
441 * generating a HMAC key larger than 63 bytes makes no sense as that key
442 * is digested before use.
446 dst_hmac_md5_generate_key(DST_KEY
*key
, const int nothing
)
454 * dst_hmac_md5_init() Function to answer set up function pointers for HMAC
464 if (dst_t_func
[KEY_HMAC_MD5
] != NULL
)
466 dst_t_func
[KEY_HMAC_MD5
] = malloc(sizeof(struct dst_func
));
467 if (dst_t_func
[KEY_HMAC_MD5
] == NULL
)
469 memset(dst_t_func
[KEY_HMAC_MD5
], 0, sizeof(struct dst_func
));
470 dst_t_func
[KEY_HMAC_MD5
]->sign
= dst_hmac_md5_sign
;
471 dst_t_func
[KEY_HMAC_MD5
]->verify
= dst_hmac_md5_verify
;
472 dst_t_func
[KEY_HMAC_MD5
]->compare
= dst_hmac_md5_compare_keys
;
473 dst_t_func
[KEY_HMAC_MD5
]->generate
= dst_hmac_md5_generate_key
;
474 dst_t_func
[KEY_HMAC_MD5
]->destroy
= dst_hmac_md5_free_key_structure
;
475 dst_t_func
[KEY_HMAC_MD5
]->to_dns_key
= dst_hmac_md5_to_dns_key
;
476 dst_t_func
[KEY_HMAC_MD5
]->from_dns_key
= dst_buffer_to_hmac_md5
;
477 dst_t_func
[KEY_HMAC_MD5
]->to_file_fmt
= dst_hmac_md5_key_to_file_format
;
478 dst_t_func
[KEY_HMAC_MD5
]->from_file_fmt
= dst_hmac_md5_key_from_file_format
;
483 #define dst_hmac_md5_init __dst_hmac_md5_init