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