1 /* sys-crypto-md.h - message digest (MD) wrapper 2 * 3 * message digest (MD) algorithms are not necessarily cryptographically secure 4 * (often provided by crypto libraries, hence this file named sys-crypto-md.h) 5 * 6 * Copyright(c) 2020 Glenn Strauss gstrauss()gluelogic.com All rights reserved 7 * License: BSD 3-clause (same as lighttpd) 8 */ 9 #ifndef LI_SYS_CRYPTO_MD_H 10 #define LI_SYS_CRYPTO_MD_H 11 #include "first.h" 12 13 #include "sys-crypto.h" /* USE_LIB_CRYPTO */ 14 #ifdef USE_LIB_CRYPTO 15 16 #if defined(USE_NETTLE_CRYPTO) 17 18 #include <nettle/md4.h> 19 #include <nettle/md5.h> 20 #include <nettle/sha.h> 21 22 #define USE_LIB_CRYPTO_MD4 23 typedef struct md4_ctx MD4_CTX; 24 static inline int 25 MD4_Init(MD4_CTX *ctx) 26 { 27 nettle_md4_init(ctx); 28 return 1; 29 } 30 static inline int 31 MD4_Final(unsigned char *digest, MD4_CTX *ctx) 32 { 33 nettle_md4_digest(ctx, MD4_DIGEST_SIZE, digest); 34 return 1; 35 } 36 static inline int 37 MD4_Update(MD4_CTX *ctx, const void *data, size_t length) 38 { 39 nettle_md4_update(ctx, length, data); 40 return 1; 41 } 42 43 #define USE_LIB_CRYPTO_MD5 44 typedef struct md5_ctx MD5_CTX; 45 static inline int 46 MD5_Init(MD5_CTX *ctx) 47 { 48 nettle_md5_init(ctx); 49 return 1; 50 } 51 static inline int 52 MD5_Final(unsigned char *digest, MD5_CTX *ctx) 53 { 54 nettle_md5_digest(ctx, MD5_DIGEST_SIZE, digest); 55 return 1; 56 } 57 static inline int 58 MD5_Update(MD5_CTX *ctx, const void *data, size_t length) 59 { 60 nettle_md5_update(ctx, length, data); 61 return 1; 62 } 63 64 #define USE_LIB_CRYPTO_SHA1 65 typedef struct sha1_ctx SHA_CTX; 66 static inline int 67 SHA1_Init(SHA_CTX *ctx) 68 { 69 nettle_sha1_init(ctx); 70 return 1; 71 } 72 static inline int 73 SHA1_Final(unsigned char *digest, SHA_CTX *ctx) 74 { 75 nettle_sha1_digest(ctx, SHA1_DIGEST_SIZE, digest); 76 return 1; 77 } 78 static inline int 79 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length) 80 { 81 nettle_sha1_update(ctx, length, data); 82 return 1; 83 } 84 85 #define USE_LIB_CRYPTO_SHA256 86 typedef struct sha256_ctx SHA256_CTX; 87 static inline int 88 SHA256_Init(SHA256_CTX *ctx) 89 { 90 nettle_sha256_init(ctx); 91 return 1; 92 } 93 static inline int 94 SHA256_Final(unsigned char *digest, SHA256_CTX *ctx) 95 { 96 nettle_sha256_digest(ctx, SHA256_DIGEST_SIZE, digest); 97 return 1; 98 } 99 static inline int 100 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length) 101 { 102 nettle_sha256_update(ctx, length, data); 103 return 1; 104 } 105 106 #define USE_LIB_CRYPTO_SHA512_256 107 /*(nettle/sha2.h: #define sha512_256_ctx sha512_ctx)*/ 108 /*typedef struct sha512_256_ctx SHA512_CTX;*/ /*(yes, SHA512_CTX)*/ 109 typedef struct sha512_ctx SHA512_CTX; 110 static inline int 111 SHA512_256_Init(SHA512_CTX *ctx) 112 { 113 nettle_sha512_256_init(ctx); 114 return 1; 115 } 116 static inline int 117 SHA512_256_Final(unsigned char *digest, SHA512_CTX *ctx) 118 { 119 nettle_sha512_256_digest(ctx, SHA256_DIGEST_SIZE, digest); 120 return 1; 121 } 122 static inline int 123 SHA512_256_Update(SHA512_CTX *ctx, const void *data, size_t length) 124 { 125 nettle_sha512_update(ctx, length, data); /*(yes, nettle_sha512_update())*/ 126 return 1; 127 } 128 129 #define USE_LIB_CRYPTO_SHA512 130 /*typedef struct sha512_ctx SHA512_CTX;*//*(defined above)*/ 131 static inline int 132 SHA512_Init(SHA512_CTX *ctx) 133 { 134 nettle_sha512_init(ctx); 135 return 1; 136 } 137 static inline int 138 SHA512_Final(unsigned char *digest, SHA512_CTX *ctx) 139 { 140 nettle_sha512_digest(ctx, SHA512_DIGEST_SIZE, digest); 141 return 1; 142 } 143 static inline int 144 SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length) 145 { 146 nettle_sha512_update(ctx, length, data); 147 return 1; 148 } 149 150 #elif defined(USE_MBEDTLS_CRYPTO) 151 152 #include <mbedtls/config.h> 153 154 #ifdef MBEDTLS_MD4_C 155 #define USE_LIB_CRYPTO_MD4 156 #include <mbedtls/md4.h> 157 typedef struct mbedtls_md4_context MD4_CTX; 158 static inline int 159 MD4_Init(MD4_CTX *ctx) 160 { 161 mbedtls_md4_init(ctx); 162 return (0 == mbedtls_md4_starts_ret(ctx)); 163 } 164 static inline int 165 MD4_Final(unsigned char *digest, MD4_CTX *ctx) 166 { 167 int rc = mbedtls_md4_finish_ret(ctx, digest); 168 mbedtls_md4_free(ctx); 169 return (0 == rc); 170 } 171 static inline int 172 MD4_Update(MD4_CTX *ctx, const void *data, size_t length) 173 { 174 return (0 == mbedtls_md4_update_ret(ctx, data, length)); 175 } 176 #endif 177 178 #ifdef MBEDTLS_MD5_C 179 #define USE_LIB_CRYPTO_MD5 180 #include <mbedtls/md5.h> 181 typedef struct mbedtls_md5_context MD5_CTX; 182 static inline int 183 MD5_Init(MD5_CTX *ctx) 184 { 185 mbedtls_md5_init(ctx); 186 return (0 == mbedtls_md5_starts_ret(ctx)); 187 } 188 static inline int 189 MD5_Final(unsigned char *digest, MD5_CTX *ctx) 190 { 191 int rc = mbedtls_md5_finish_ret(ctx, digest); 192 mbedtls_md5_free(ctx); 193 return (0 == rc); 194 } 195 static inline int 196 MD5_Update(MD5_CTX *ctx, const void *data, size_t length) 197 { 198 return (0 == mbedtls_md5_update_ret(ctx, data, length)); 199 } 200 #endif 201 202 #ifdef MBEDTLS_SHA1_C 203 #define USE_LIB_CRYPTO_SHA1 204 #include <mbedtls/sha1.h> 205 typedef struct mbedtls_sha1_context SHA_CTX; 206 static inline int 207 SHA1_Init(SHA_CTX *ctx) 208 { 209 mbedtls_sha1_init(ctx); 210 return (0 == mbedtls_sha1_starts_ret(ctx)); 211 } 212 static inline int 213 SHA1_Final(unsigned char *digest, SHA_CTX *ctx) 214 { 215 int rc = mbedtls_sha1_finish_ret(ctx, digest); 216 mbedtls_sha1_free(ctx); 217 return (0 == rc); 218 } 219 static inline int 220 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length) 221 { 222 return (0 == mbedtls_sha1_update_ret(ctx, data, length)); 223 } 224 #endif 225 226 #ifdef MBEDTLS_SHA256_C 227 #define USE_LIB_CRYPTO_SHA256 228 #include <mbedtls/sha256.h> 229 typedef struct mbedtls_sha256_context SHA256_CTX; 230 static inline int 231 SHA256_Init(SHA256_CTX *ctx) 232 { 233 mbedtls_sha256_init(ctx); 234 return (0 == mbedtls_sha256_starts_ret(ctx, 0)); 235 } 236 static inline int 237 SHA256_Final(unsigned char *digest, SHA256_CTX *ctx) 238 { 239 int rc = mbedtls_sha256_finish_ret(ctx, digest); 240 mbedtls_sha256_free(ctx); 241 return (0 == rc); 242 } 243 static inline int 244 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length) 245 { 246 return (0 == mbedtls_sha256_update_ret(ctx, data, length)); 247 } 248 #endif 249 250 #ifdef MBEDTLS_SHA512_C 251 #define USE_LIB_CRYPTO_SHA512 252 #include <mbedtls/sha512.h> 253 typedef struct mbedtls_sha512_context SHA512_CTX; 254 static inline int 255 SHA512_Init(SHA512_CTX *ctx) 256 { 257 mbedtls_sha512_init(ctx); 258 return (0 == mbedtls_sha512_starts_ret(ctx, 0)); 259 } 260 static inline int 261 SHA512_Final(unsigned char *digest, SHA512_CTX *ctx) 262 { 263 int rc = mbedtls_sha512_finish_ret(ctx, digest); 264 mbedtls_sha512_free(ctx); 265 return (0 == rc); 266 } 267 static inline int 268 SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length) 269 { 270 return (0 == mbedtls_sha512_update_ret(ctx, data, length)); 271 } 272 #endif 273 274 #elif defined(USE_WOLFSSL_CRYPTO) 275 276 /* WolfSSL compatibility API for OpenSSL unnecessarily bounces through an extra 277 * layer of indirection. However, to avoid conflicting typedefs when includers 278 * also include headers from the WolfSSL compatibility API for OpenSSL, we 279 * include those headers here, as well, and use the compatibility API typedefs. 280 * (undef of OPENSSL_EXTRA and NO_OLD_WC_NAMES not sufficient, and not friendly 281 * to do in a header when others might rely on them) */ 282 283 /* workaround fragile code in wolfssl/wolfcrypto/types.h */ 284 #if !defined(SIZEOF_LONG) || !defined(SIZEOF_LONG_LONG) 285 #undef SIZEOF_LONG 286 #undef SIZEOF_LONG_LONG 287 #endif 288 289 #ifndef NO_MD4 290 #include <wolfssl/wolfcrypt/md4.h> 291 #include <wolfssl/openssl/md4.h> 292 #undef MD4_Init 293 #undef MD4_Final 294 #undef MD4_Update 295 #define USE_LIB_CRYPTO_MD4 296 /*typedef Md4 MD4_CTX;*/ 297 static inline int 298 MD4_Init(MD4_CTX *ctx) 299 { 300 wc_InitMd4((Md4 *)ctx); 301 return 1; 302 } 303 static inline int 304 MD4_Final(unsigned char *digest, MD4_CTX *ctx) 305 { 306 wc_Md4Final((Md4 *)ctx, digest); 307 return 1; 308 } 309 static inline int 310 MD4_Update(MD4_CTX *ctx, const void *data, size_t length) 311 { 312 wc_Md4Update((Md4 *)ctx, data, length); 313 return 1; 314 } 315 #endif 316 317 #ifndef NO_MD5 318 #include <wolfssl/wolfcrypt/md5.h> 319 #include <wolfssl/openssl/md5.h> 320 #undef MD5_Init 321 #undef MD5_Final 322 #undef MD5_Update 323 #define USE_LIB_CRYPTO_MD5 324 /*typedef wc_Md5 MD5_CTX;*/ 325 static inline int 326 MD5_Init(MD5_CTX *ctx) 327 { 328 return (0 == wc_InitMd5((wc_Md5 *)ctx)); 329 } 330 static inline int 331 MD5_Final(unsigned char *digest, MD5_CTX *ctx) 332 { 333 return (0 == wc_Md5Final((wc_Md5 *)ctx, digest)); 334 } 335 static inline int 336 MD5_Update(MD5_CTX *ctx, const void *data, size_t length) 337 { 338 wc_Md5Update((wc_Md5 *)ctx, data, length); 339 return 1; 340 } 341 #endif 342 343 #ifndef NO_SHA 344 #include <wolfssl/wolfcrypt/sha.h> 345 #include <wolfssl/openssl/sha.h> 346 #undef SHA1_Init 347 #undef SHA1_Final 348 #undef SHA1_Update 349 #define USE_LIB_CRYPTO_SHA1 350 /*typedef wc_Sha SHA_CTX;*/ 351 static inline int 352 SHA1_Init(SHA_CTX *ctx) 353 { 354 return (0 == wc_InitSha((wc_Sha *)ctx)); 355 } 356 static inline int 357 SHA1_Final(unsigned char *digest, SHA_CTX *ctx) 358 { 359 return (0 == wc_ShaFinal((wc_Sha *)ctx, digest)); 360 } 361 static inline int 362 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length) 363 { 364 wc_ShaUpdate((wc_Sha *)ctx, data, length); 365 return 1; 366 } 367 #endif 368 369 #ifndef NO_SHA256 370 #include <wolfssl/wolfcrypt/sha256.h> 371 #include <wolfssl/openssl/sha.h> 372 #undef SHA256_Init 373 #undef SHA256_Final 374 #undef SHA256_Update 375 #define USE_LIB_CRYPTO_SHA256 376 /*typedef wc_Sha256 SHA256_CTX;*/ 377 static inline int 378 SHA256_Init(SHA256_CTX *ctx) 379 { 380 return (0 == wc_InitSha256((wc_Sha256 *)ctx)); 381 } 382 static inline int 383 SHA256_Final(unsigned char *digest, SHA256_CTX *ctx) 384 { 385 return (0 == wc_Sha256Final((wc_Sha256 *)ctx, digest)); 386 } 387 static inline int 388 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length) 389 { 390 wc_Sha256Update((wc_Sha256 *)ctx, data, length); 391 return 1; 392 } 393 #endif 394 395 #ifndef NO_SHA512 396 #ifdef WOLFSSL_SHA512 397 #include <wolfssl/wolfcrypt/sha512.h> 398 #include <wolfssl/openssl/sha.h> 399 #undef SHA512_Init 400 #undef SHA512_Final 401 #undef SHA512_Update 402 #define USE_LIB_CRYPTO_SHA512 403 /*typedef wc_Sha512 SHA512_CTX;*/ 404 static inline int 405 SHA512_Init(SHA512_CTX *ctx) 406 { 407 return (0 == wc_InitSha512((wc_Sha512 *)ctx)); 408 } 409 static inline int 410 SHA512_Final(unsigned char *digest, SHA512_CTX *ctx) 411 { 412 return (0 == wc_Sha512Final((wc_Sha512 *)ctx, digest)); 413 } 414 static inline int 415 SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length) 416 { 417 wc_Sha512Update((wc_Sha512 *)ctx, data, length); 418 return 1; 419 } 420 #endif 421 #endif 422 423 #elif defined(USE_OPENSSL_CRYPTO) 424 425 #include <openssl/md4.h> 426 #include <openssl/md5.h> 427 #include <openssl/sha.h> 428 #ifndef OPENSSL_NO_MD4 429 #define USE_LIB_CRYPTO_MD4 430 #endif 431 #ifndef OPENSSL_NO_MD5 432 #define USE_LIB_CRYPTO_MD5 433 #endif 434 #define USE_LIB_CRYPTO_SHA1 435 #define USE_LIB_CRYPTO_SHA256 436 #ifdef SHA512_256_DIGEST_LENGTH 437 #define USE_LIB_CRYPTO_SHA512_256 438 #endif 439 #ifdef SHA512_DIGEST_LENGTH 440 #define USE_LIB_CRYPTO_SHA512 441 #endif 442 443 #include <openssl/opensslv.h> 444 #if OPENSSL_VERSION_NUMBER >= 0x30000000L 445 #include <openssl/evp.h> 446 447 #ifdef USE_LIB_CRYPTO_MD4 448 #define MD4_CTX EVP_MD4_CTX 449 #define MD4_Init EVP_MD4_Init 450 #define MD4_Final EVP_MD4_Final 451 #define MD4_Update EVP_MD4_Update 452 typedef EVP_MD_CTX * MD4_CTX; 453 static inline int 454 EVP_MD4_Init(EVP_MD4_CTX *ctx) 455 { 456 return ((*ctx = EVP_MD_CTX_new()) != NULL 457 && 1 == EVP_DigestInit_ex(*ctx, EVP_md4(), NULL)); 458 } 459 static inline int 460 EVP_MD4_Final(unsigned char *digest, EVP_MD4_CTX *ctx) 461 { 462 /* MD4_DIGEST_LENGTH; EVP_MD_size(EVP_md4()) */ 463 int rc = EVP_DigestFinal_ex(*ctx, digest, NULL); 464 EVP_MD_CTX_free(*ctx); 465 return (1 == rc); 466 } 467 static inline int 468 EVP_MD4_Update(EVP_MD4_CTX *ctx, const void *data, size_t length) 469 { 470 return (1 == EVP_DigestUpdate(*ctx, data, length)); 471 } 472 #endif 473 474 #ifdef USE_LIB_CRYPTO_MD5 475 #define MD5_CTX EVP_MD5_CTX 476 #define MD5_Init EVP_MD5_Init 477 #define MD5_Final EVP_MD5_Final 478 #define MD5_Update EVP_MD5_Update 479 typedef EVP_MD_CTX * EVP_MD5_CTX; 480 static inline int 481 EVP_MD5_Init(EVP_MD5_CTX *ctx) 482 { 483 return ((*ctx = EVP_MD_CTX_new()) != NULL 484 && 1 == EVP_DigestInit_ex(*ctx, EVP_md5(), NULL)); 485 } 486 static inline int 487 EVP_MD5_Final(unsigned char *digest, EVP_MD5_CTX *ctx) 488 { 489 /* MD5_DIGEST_LENGTH; EVP_MD_size(EVP_md5()) */ 490 int rc = EVP_DigestFinal_ex(*ctx, digest, NULL); 491 EVP_MD_CTX_free(*ctx); 492 return (1 == rc); 493 } 494 static inline int 495 EVP_MD5_Update(EVP_MD5_CTX *ctx, const void *data, size_t length) 496 { 497 return (1 == EVP_DigestUpdate(*ctx, data, length)); 498 } 499 #endif 500 501 #ifdef USE_LIB_CRYPTO_SHA1 502 #define SHA_CTX EVP_SHA1_CTX 503 #define SHA1_Init EVP_SHA1_Init 504 #define SHA1_Final EVP_SHA1_Final 505 #define SHA1_Update EVP_SHA1_Update 506 typedef EVP_MD_CTX * EVP_SHA1_CTX; 507 static inline int 508 EVP_SHA1_Init(EVP_SHA1_CTX *ctx) 509 { 510 return ((*ctx = EVP_MD_CTX_new()) != NULL 511 && 1 == EVP_DigestInit_ex(*ctx, EVP_sha1(), NULL)); 512 } 513 static inline int 514 EVP_SHA1_Final(unsigned char *digest, EVP_SHA1_CTX *ctx) 515 { 516 /* SHA_DIGEST_LENGTH; EVP_MD_size(EVP_sha1()) */ 517 int rc = EVP_DigestFinal_ex(*ctx, digest, NULL); 518 EVP_MD_CTX_free(*ctx); 519 return (1 == rc); 520 } 521 static inline int 522 EVP_SHA1_Update(EVP_SHA1_CTX *ctx, const void *data, size_t length) 523 { 524 return (1 == EVP_DigestUpdate(*ctx, data, length)); 525 } 526 #endif 527 528 #ifdef USE_LIB_CRYPTO_SHA256 529 #define SHA256_CTX EVP_SHA256_CTX 530 #define SHA256_Init EVP_SHA256_Init 531 #define SHA256_Final EVP_SHA256_Final 532 #define SHA256_Update EVP_SHA256_Update 533 typedef EVP_MD_CTX * EVP_SHA256_CTX; 534 static inline int 535 EVP_SHA256_Init(EVP_SHA256_CTX *ctx) 536 { 537 return ((*ctx = EVP_MD_CTX_new()) != NULL 538 && 1 == EVP_DigestInit_ex(*ctx, EVP_sha256(), NULL)); 539 } 540 static inline int 541 EVP_SHA256_Final(unsigned char *digest, EVP_SHA256_CTX *ctx) 542 { 543 /* SHA256_DIGEST_LENGTH; EVP_MD_size(EVP_sha256()) */ 544 int rc = EVP_DigestFinal_ex(*ctx, digest, NULL); 545 EVP_MD_CTX_free(*ctx); 546 return (1 == rc); 547 } 548 static inline int 549 EVP_SHA256_Update(EVP_SHA256_CTX *ctx, const void *data, size_t length) 550 { 551 return (1 == EVP_DigestUpdate(*ctx, data, length)); 552 } 553 #endif 554 555 #ifdef USE_LIB_CRYPTO_SHA512_256 556 #define SHA512_256_CTX EVP_SHA512_256_CTX 557 #define SHA512_256_Init EVP_SHA512_256_Init 558 #define SHA512_256_Final EVP_SHA512_256_Final 559 #define SHA512_256_Update EVP_SHA512_256_Update 560 typedef EVP_MD_CTX * EVP_SHA512_256_CTX; 561 static inline int 562 EVP_SHA512_256_Init(EVP_SHA512_256_CTX *ctx) 563 { 564 return ((*ctx = EVP_MD_CTX_new()) != NULL 565 && 1 == EVP_DigestInit_ex(*ctx, EVP_sha512_256(), NULL)); 566 } 567 static inline int 568 EVP_SHA512_256_Final(unsigned char *digest, EVP_SHA512_256_CTX *ctx) 569 { 570 /* SHA256_DIGEST_LENGTH; EVP_MD_size(EVP_sha512_256()) */ 571 int rc = EVP_DigestFinal_ex(*ctx, digest, NULL); 572 EVP_MD_CTX_free(*ctx); 573 return (1 == rc); 574 } 575 static inline int 576 EVP_SHA512_256_Update(EVP_SHA512_256_CTX *ctx, const void *data, size_t length) 577 { 578 return (1 == EVP_DigestUpdate(*ctx, data, length)); 579 } 580 #endif 581 582 #ifdef USE_LIB_CRYPTO_SHA512 583 #define SHA512_CTX EVP_SHA512_CTX 584 #define SHA512_Init EVP_SHA512_Init 585 #define SHA512_Final EVP_SHA512_Final 586 #define SHA512_Update EVP_SHA512_Update 587 typedef EVP_MD_CTX * EVP_SHA512_CTX; 588 static inline int 589 EVP_SHA512_Init(EVP_SHA512_CTX *ctx) 590 { 591 return ((*ctx = EVP_MD_CTX_new()) != NULL 592 && 1 == EVP_DigestInit_ex(*ctx, EVP_sha512(), NULL)); 593 } 594 static inline int 595 EVP_SHA512_Final(unsigned char *digest, EVP_SHA512_CTX *ctx) 596 { 597 /* SHA512_DIGEST_LENGTH; EVP_MD_size(EVP_sha512()) */ 598 int rc = EVP_DigestFinal_ex(*ctx, digest, NULL); 599 EVP_MD_CTX_free(*ctx); 600 return (1 == rc); 601 } 602 static inline int 603 EVP_SHA512_Update(EVP_SHA512_CTX *ctx, const void *data, size_t length) 604 { 605 return (1 == EVP_DigestUpdate(*ctx, data, length)); 606 } 607 #endif 608 609 #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */ 610 611 #elif defined(USE_GNUTLS_CRYPTO) 612 613 #include <gnutls/crypto.h> 614 #include "buffer.h" /* SEGFAULT() */ 615 616 #define USE_LIB_CRYPTO_MD5 617 typedef gnutls_hash_hd_t MD5_CTX; 618 static inline int 619 MD5_Init(MD5_CTX *ctx) 620 { 621 if (gnutls_hash_init(ctx, GNUTLS_DIG_MD5) < 0) 622 SEGFAULT(); 623 return 1; 624 } 625 static inline int 626 MD5_Final(unsigned char *digest, MD5_CTX *ctx) 627 { 628 gnutls_hash_deinit(*ctx, digest); 629 return 1; 630 } 631 static inline int 632 MD5_Update(MD5_CTX *ctx, const void *data, size_t length) 633 { 634 gnutls_hash(*ctx, data, length); 635 return 1; 636 } 637 638 #define USE_LIB_CRYPTO_SHA1 639 typedef gnutls_hash_hd_t SHA_CTX; 640 static inline int 641 SHA1_Init(SHA_CTX *ctx) 642 { 643 if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA1) < 0) 644 SEGFAULT(); 645 return 1; 646 } 647 static inline int 648 SHA1_Final(unsigned char *digest, SHA_CTX *ctx) 649 { 650 gnutls_hash_deinit(*ctx, digest); 651 return 1; 652 } 653 static inline int 654 SHA1_Update(SHA_CTX *ctx, const void *data, size_t length) 655 { 656 gnutls_hash(*ctx, data, length); 657 return 1; 658 } 659 660 #define USE_LIB_CRYPTO_SHA256 661 typedef gnutls_hash_hd_t SHA256_CTX; 662 static inline int 663 SHA256_Init(SHA256_CTX *ctx) 664 { 665 if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA256) < 0) 666 SEGFAULT(); 667 return 1; 668 } 669 static inline int 670 SHA256_Final(unsigned char *digest, SHA256_CTX *ctx) 671 { 672 gnutls_hash_deinit(*ctx, digest); 673 return 1; 674 } 675 static inline int 676 SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length) 677 { 678 gnutls_hash(*ctx, data, length); 679 return 1; 680 } 681 682 #define USE_LIB_CRYPTO_SHA512 683 typedef gnutls_hash_hd_t SHA512_CTX; 684 static inline int 685 SHA512_Init(SHA512_CTX *ctx) 686 { 687 if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA512) < 0) 688 SEGFAULT(); 689 return 1; 690 } 691 static inline int 692 SHA512_Final(unsigned char *digest, SHA512_CTX *ctx) 693 { 694 gnutls_hash_deinit(*ctx, digest); 695 return 1; 696 } 697 static inline int 698 SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length) 699 { 700 gnutls_hash(*ctx, data, length); 701 return 1; 702 } 703 704 #elif defined(USE_NSS_CRYPTO) 705 706 #ifdef __has_include 707 #if __has_include(<nss3/nss.h>) 708 #define NSS_VER_INCLUDE 709 #endif 710 #endif 711 712 /* basic algorithms fail if NSS library has not been init'd (WTH). 713 * lighttpd defers initialization of rand and crypto until first use 714 * to attempt to avoid long, blocking init at startup while waiting 715 * for sufficient system entropy to become available */ 716 #ifdef NSS_VER_INCLUDE 717 #include <nss3/nss.h> /* NSS_IsInitialized() NSS_NoDB_Init() */ 718 #else 719 #include <nss/nss.h> /* NSS_IsInitialized() NSS_NoDB_Init() */ 720 #endif 721 #include <stdlib.h> /* abort() */ 722 __attribute_cold__ 723 static inline void 724 nss_requires_explicit_init_for_basic_crypto_wth(void) 725 { 726 if (NSS_NoDB_Init(NULL) < 0) 727 abort(); 728 } 729 730 #ifdef NSS_VER_INCLUDE 731 #include <nss3/sechash.h> 732 #else 733 #include <nss/sechash.h> 734 #endif 735 736 #define NSS_gen_hashfuncs(name, typ) \ 737 static inline int \ 738 name##_Init(void **ctx) \ 739 { \ 740 if (!NSS_IsInitialized()) \ 741 nss_requires_explicit_init_for_basic_crypto_wth(); \ 742 const SECHashObject * const hashObj = HASH_GetHashObject(typ); \ 743 return ((*ctx=hashObj->create()) != NULL) ? (hashObj->begin(*ctx),1) : 0; \ 744 } \ 745 static inline int \ 746 name##_Final(unsigned char *dest, void **ctx) \ 747 { \ 748 const SECHashObject * const hashObj = HASH_GetHashObject(typ); \ 749 unsigned int retLen; \ 750 hashObj->end(*ctx, dest, &retLen, hashObj->length); \ 751 hashObj->destroy(*ctx, PR_TRUE); \ 752 return 1; \ 753 } \ 754 static inline int \ 755 name##_Update(void **ctx, const void *src, size_t len) \ 756 { \ 757 const SECHashObject * const hashObj = HASH_GetHashObject(typ); \ 758 hashObj->update(*ctx, src, (int)len); \ 759 return 1; \ 760 } \ 761 typedef void * name##_CTX 762 typedef void * SHA_CTX; 763 764 #define USE_LIB_CRYPTO_MD5 765 /* MD5_Init() 766 * MD5_Update() 767 * MD5_Final() */ 768 NSS_gen_hashfuncs(MD5, HASH_AlgMD5); 769 770 #define USE_LIB_CRYPTO_SHA1 771 /* SHA1_Init() 772 * SHA1_Update() 773 * SHA1_Final() */ 774 NSS_gen_hashfuncs(SHA1, HASH_AlgSHA1); 775 776 #define USE_LIB_CRYPTO_SHA256 777 /* SHA256_Init() 778 * SHA256_Update() 779 * SHA256_Final() */ 780 NSS_gen_hashfuncs(SHA256, HASH_AlgSHA256); 781 782 #define USE_LIB_CRYPTO_SHA512 783 /* SHA512_Init() 784 * SHA512_Update() 785 * SHA512_Final() */ 786 NSS_gen_hashfuncs(SHA512, HASH_AlgSHA512); 787 788 #endif 789 790 #endif /* USE_LIB_CRYPTO */ 791 792 793 #ifdef USE_LIB_CRYPTO_MD5 794 #ifndef MD5_DIGEST_LENGTH 795 #define MD5_DIGEST_LENGTH 16 796 #endif 797 #include "algo_md5.h" /*(for legacy li_MD5_*() name mangling)*/ 798 #else 799 #include "algo_md5.h" 800 #endif 801 802 803 #ifdef USE_LIB_CRYPTO_SHA1 804 #ifndef SHA_DIGEST_LENGTH 805 #define SHA_DIGEST_LENGTH 20 806 #endif 807 #else 808 #include "algo_sha1.h" 809 #endif 810 811 812 #ifdef USE_LIB_CRYPTO_SHA256 813 #ifndef SHA256_DIGEST_LENGTH 814 #define SHA256_DIGEST_LENGTH 32 815 #endif 816 #endif 817 818 819 #ifdef USE_LIB_CRYPTO_SHA512_256 820 #ifndef SHA512_256_DIGEST_LENGTH 821 #define SHA512_256_DIGEST_LENGTH 32 822 #endif 823 #endif 824 825 826 #ifdef USE_LIB_CRYPTO_SHA512 827 #ifndef SHA512_DIGEST_LENGTH 828 #define SHA512_DIGEST_LENGTH 64 829 #endif 830 #endif 831 832 833 #endif /* LI_SYS_CRYPTO_MD_H */ 834