lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

670 lines
17 KiB

  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. #include "sys-crypto.h" /* USE_LIB_CRYPTO */
  13. #ifdef USE_LIB_CRYPTO
  14. #if defined(USE_NETTLE_CRYPTO)
  15. #include <nettle/md4.h>
  16. #include <nettle/md5.h>
  17. #include <nettle/sha.h>
  18. #define USE_LIB_CRYPTO_MD4
  19. typedef struct md4_ctx MD4_CTX;
  20. static inline int
  21. MD4_Init(MD4_CTX *ctx)
  22. {
  23. nettle_md4_init(ctx);
  24. return 1;
  25. }
  26. static inline int
  27. MD4_Final(unsigned char *digest, MD4_CTX *ctx)
  28. {
  29. nettle_md4_digest(ctx, MD4_DIGEST_SIZE, digest);
  30. return 1;
  31. }
  32. static inline int
  33. MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
  34. {
  35. nettle_md4_update(ctx, length, data);
  36. return 1;
  37. }
  38. #define USE_LIB_CRYPTO_MD5
  39. typedef struct md5_ctx MD5_CTX;
  40. static inline int
  41. MD5_Init(MD5_CTX *ctx)
  42. {
  43. nettle_md5_init(ctx);
  44. return 1;
  45. }
  46. static inline int
  47. MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  48. {
  49. nettle_md5_digest(ctx, MD5_DIGEST_SIZE, digest);
  50. return 1;
  51. }
  52. static inline int
  53. MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
  54. {
  55. nettle_md5_update(ctx, length, data);
  56. return 1;
  57. }
  58. #define USE_LIB_CRYPTO_SHA1
  59. typedef struct sha1_ctx SHA_CTX;
  60. static inline int
  61. SHA1_Init(SHA_CTX *ctx)
  62. {
  63. nettle_sha1_init(ctx);
  64. return 1;
  65. }
  66. static inline int
  67. SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
  68. {
  69. nettle_sha1_digest(ctx, SHA1_DIGEST_SIZE, digest);
  70. return 1;
  71. }
  72. static inline int
  73. SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
  74. {
  75. nettle_sha1_update(ctx, length, data);
  76. return 1;
  77. }
  78. #define USE_LIB_CRYPTO_SHA256
  79. typedef struct sha256_ctx SHA256_CTX;
  80. static inline int
  81. SHA256_Init(SHA256_CTX *ctx)
  82. {
  83. nettle_sha256_init(ctx);
  84. return 1;
  85. }
  86. static inline int
  87. SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
  88. {
  89. nettle_sha256_digest(ctx, SHA256_DIGEST_SIZE, digest);
  90. return 1;
  91. }
  92. static inline int
  93. SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
  94. {
  95. nettle_sha256_update(ctx, length, data);
  96. return 1;
  97. }
  98. #define USE_LIB_CRYPTO_SHA512_256
  99. #ifndef SHA512_256_DIGEST_LENGTH
  100. #define SHA512_256_DIGEST_LENGTH 32
  101. #endif
  102. typedef struct sha512_256_ctx SHA512_CTX; /*(yes, SHA512_CTX)*/
  103. static inline int
  104. SHA512_256_Init(SHA512_CTX *ctx)
  105. {
  106. nettle_sha512_256_init(ctx);
  107. return 1;
  108. }
  109. static inline int
  110. SHA512_256_Final(unsigned char *digest, SHA512_CTX *ctx)
  111. {
  112. nettle_sha512_256_digest(ctx, SHA256_DIGEST_SIZE, digest);
  113. return 1;
  114. }
  115. static inline int
  116. SHA512_256_Update(SHA512_CTX *ctx, const void *data, size_t length)
  117. {
  118. nettle_sha512_update(ctx, length, data); /*(yes, nettle_sha512_update())*/
  119. return 1;
  120. }
  121. #elif defined(USE_MBEDTLS_CRYPTO)
  122. #include <mbedtls/config.h>
  123. #ifdef MBEDTLS_MD4_C
  124. #define USE_LIB_CRYPTO_MD4
  125. #include <mbedtls/md4.h>
  126. typedef struct mbedtls_md4_context MD4_CTX;
  127. static inline int
  128. MD4_Init(MD4_CTX *ctx)
  129. {
  130. mbedtls_md4_init(ctx);
  131. return (0 == mbedtls_md4_starts_ret(ctx));
  132. }
  133. static inline int
  134. MD4_Final(unsigned char *digest, MD4_CTX *ctx)
  135. {
  136. int rc = mbedtls_md4_finish_ret(ctx, digest);
  137. mbedtls_md4_free(ctx);
  138. return (0 == rc);
  139. }
  140. static inline int
  141. MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
  142. {
  143. return (0 == mbedtls_md4_update_ret(ctx, data, length));
  144. }
  145. #endif
  146. #ifdef MBEDTLS_MD5_C
  147. #define USE_LIB_CRYPTO_MD5
  148. #include <mbedtls/md5.h>
  149. typedef struct mbedtls_md5_context MD5_CTX;
  150. static inline int
  151. MD5_Init(MD5_CTX *ctx)
  152. {
  153. mbedtls_md5_init(ctx);
  154. return (0 == mbedtls_md5_starts_ret(ctx));
  155. }
  156. static inline int
  157. MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  158. {
  159. int rc = mbedtls_md5_finish_ret(ctx, digest);
  160. mbedtls_md5_free(ctx);
  161. return (0 == rc);
  162. }
  163. static inline int
  164. MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
  165. {
  166. return (0 == mbedtls_md5_update_ret(ctx, data, length));
  167. }
  168. #endif
  169. #ifdef MBEDTLS_SHA1_C
  170. #define USE_LIB_CRYPTO_SHA1
  171. #include <mbedtls/sha1.h>
  172. typedef struct mbedtls_sha1_context SHA_CTX;
  173. static inline int
  174. SHA1_Init(SHA_CTX *ctx)
  175. {
  176. mbedtls_sha1_init(ctx);
  177. return (0 == mbedtls_sha1_starts_ret(ctx));
  178. }
  179. static inline int
  180. SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
  181. {
  182. int rc = mbedtls_sha1_finish_ret(ctx, digest);
  183. mbedtls_sha1_free(ctx);
  184. return (0 == rc);
  185. }
  186. static inline int
  187. SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
  188. {
  189. return (0 == mbedtls_sha1_update_ret(ctx, data, length));
  190. }
  191. #endif
  192. #ifdef MBEDTLS_SHA256_C
  193. #define USE_LIB_CRYPTO_SHA256
  194. #include <mbedtls/sha256.h>
  195. typedef struct mbedtls_sha256_context SHA256_CTX;
  196. static inline int
  197. SHA256_Init(SHA256_CTX *ctx)
  198. {
  199. mbedtls_sha256_init(ctx);
  200. return (0 == mbedtls_sha256_starts_ret(ctx, 0));
  201. }
  202. static inline int
  203. SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
  204. {
  205. int rc = mbedtls_sha256_finish_ret(ctx, digest);
  206. mbedtls_sha256_free(ctx);
  207. return (0 == rc);
  208. }
  209. static inline int
  210. SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
  211. {
  212. return (0 == mbedtls_sha256_update_ret(ctx, data, length));
  213. }
  214. #endif
  215. #elif defined(USE_WOLFSSL_CRYPTO) && !defined(USE_OPENSSL_CRYPTO)
  216. /* WolfSSL compatibility API for OpenSSL unnecessarily bounces through an extra
  217. * layer of indirection. However, to avoid conflicting typedefs when includers
  218. * also include headers from the WolfSSL compatibility API for OpenSSL, we
  219. * include those headers here, as well, and use the compatibility API typedefs.
  220. * (undef of OPENSSL_EXTRA and NO_OLD_WC_NAMES not sufficient, and not friendly
  221. * to do in a header when others might rely on them) */
  222. #ifndef NO_MD4
  223. #include <wolfssl/wolfcrypt/md4.h>
  224. #include <wolfssl/openssl/md4.h>
  225. #undef MD4_Init
  226. #undef MD4_Final
  227. #undef MD4_Update
  228. #define USE_LIB_CRYPTO_MD4
  229. /*typedef Md4 MD4_CTX;*/
  230. static inline int
  231. MD4_Init(MD4_CTX *ctx)
  232. {
  233. wc_InitMd4((Md4 *)ctx);
  234. return 1;
  235. }
  236. static inline int
  237. MD4_Final(unsigned char *digest, MD4_CTX *ctx)
  238. {
  239. wc_Md4Final((Md4 *)ctx, digest);
  240. return 1;
  241. }
  242. static inline int
  243. MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
  244. {
  245. wc_Md4Update((Md4 *)ctx, data, length);
  246. return 1;
  247. }
  248. #endif
  249. #ifndef NO_MD5
  250. #include <wolfssl/wolfcrypt/md5.h>
  251. #include <wolfssl/openssl/md5.h>
  252. #undef MD5_Init
  253. #undef MD5_Final
  254. #undef MD5_Update
  255. #define USE_LIB_CRYPTO_MD5
  256. /*typedef wc_Md5 MD5_CTX;*/
  257. static inline int
  258. MD5_Init(MD5_CTX *ctx)
  259. {
  260. return (0 == wc_InitMd5((wc_Md5 *)ctx));
  261. }
  262. static inline int
  263. MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  264. {
  265. return (0 == wc_Md5Final((wc_Md5 *)ctx, digest));
  266. }
  267. static inline int
  268. MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
  269. {
  270. wc_Md5Update((wc_Md5 *)ctx, data, length);
  271. return 1;
  272. }
  273. #endif
  274. #ifndef NO_SHA
  275. #include <wolfssl/wolfcrypt/sha.h>
  276. #include <wolfssl/openssl/sha.h>
  277. #undef SHA1_Init
  278. #undef SHA1_Final
  279. #undef SHA1_Update
  280. #define USE_LIB_CRYPTO_SHA1
  281. /*typedef wc_Sha SHA_CTX;*/
  282. static inline int
  283. SHA1_Init(SHA_CTX *ctx)
  284. {
  285. return (0 == wc_InitSha((wc_Sha *)ctx));
  286. }
  287. static inline int
  288. SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
  289. {
  290. return (0 == wc_ShaFinal((wc_Sha *)ctx, digest));
  291. }
  292. static inline int
  293. SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
  294. {
  295. wc_ShaUpdate((wc_Sha *)ctx, data, length);
  296. return 1;
  297. }
  298. #endif
  299. #ifndef NO_SHA256
  300. #include <wolfssl/wolfcrypt/sha256.h>
  301. #include <wolfssl/openssl/sha.h>
  302. #undef SHA256_Init
  303. #undef SHA256_Final
  304. #undef SHA256_Update
  305. #define USE_LIB_CRYPTO_SHA256
  306. /*typedef wc_Sha256 SHA256_CTX;*/
  307. static inline int
  308. SHA256_Init(SHA256_CTX *ctx)
  309. {
  310. return (0 == wc_InitSha256((wc_Sha256 *)ctx));
  311. }
  312. static inline int
  313. SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
  314. {
  315. return (0 == wc_Sha256Final((wc_Sha256 *)ctx, digest));
  316. }
  317. static inline int
  318. SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
  319. {
  320. wc_Sha256Update((wc_Sha256 *)ctx, data, length);
  321. return 1;
  322. }
  323. #endif
  324. #elif defined(USE_OPENSSL_CRYPTO)
  325. #include <openssl/md4.h>
  326. #include <openssl/md5.h>
  327. #include <openssl/sha.h>
  328. #ifndef OPENSSL_NO_MD4
  329. #define USE_LIB_CRYPTO_MD4
  330. #endif
  331. #ifndef OPENSSL_NO_MD5
  332. #define USE_LIB_CRYPTO_MD5
  333. #endif
  334. #define USE_LIB_CRYPTO_SHA1
  335. #define USE_LIB_CRYPTO_SHA256
  336. #ifdef SHA512_256_DIGEST_LENGTH
  337. #define USE_LIB_CRYPTO_SHA512_256
  338. #endif
  339. #include <openssl/opensslv.h>
  340. #if OPENSSL_VERSION_NUMBER >= 0x30000000L
  341. #include <openssl/evp.h>
  342. #ifdef USE_LIB_CRYPTO_MD4
  343. #define MD4_CTX EVP_MD4_CTX
  344. #define MD4_Init EVP_MD4_Init
  345. #define MD4_Final EVP_MD4_Final
  346. #define MD4_Update EVP_MD4_Update
  347. typedef EVP_MD_CTX * MD4_CTX;
  348. static inline int
  349. EVP_MD4_Init(EVP_MD4_CTX *ctx)
  350. {
  351. return ((*ctx = EVP_MD_CTX_new()) != NULL
  352. && 1 == EVP_DigestInit_ex(*ctx, EVP_md4(), NULL));
  353. }
  354. static inline int
  355. EVP_MD4_Final(unsigned char *digest, EVP_MD4_CTX *ctx)
  356. {
  357. /* MD4_DIGEST_LENGTH; EVP_MD_size(EVP_md4()) */
  358. int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
  359. EVP_MD_CTX_free(*ctx);
  360. return (1 == rc);
  361. }
  362. static inline int
  363. EVP_MD4_Update(EVP_MD4_CTX *ctx, const void *data, size_t length)
  364. {
  365. return (1 == EVP_DigestUpdate(*ctx, data, length));
  366. }
  367. #endif
  368. #ifdef USE_LIB_CRYPTO_MD5
  369. #define MD5_CTX EVP_MD5_CTX
  370. #define MD5_Init EVP_MD5_Init
  371. #define MD5_Final EVP_MD5_Final
  372. #define MD5_Update EVP_MD5_Update
  373. typedef EVP_MD_CTX * EVP_MD5_CTX;
  374. static inline int
  375. EVP_MD5_Init(EVP_MD5_CTX *ctx)
  376. {
  377. return ((*ctx = EVP_MD_CTX_new()) != NULL
  378. && 1 == EVP_DigestInit_ex(*ctx, EVP_md5(), NULL));
  379. }
  380. static inline int
  381. EVP_MD5_Final(unsigned char *digest, EVP_MD5_CTX *ctx)
  382. {
  383. /* MD5_DIGEST_LENGTH; EVP_MD_size(EVP_md5()) */
  384. int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
  385. EVP_MD_CTX_free(*ctx);
  386. return (1 == rc);
  387. }
  388. static inline int
  389. EVP_MD5_Update(EVP_MD5_CTX *ctx, const void *data, size_t length)
  390. {
  391. return (1 == EVP_DigestUpdate(*ctx, data, length));
  392. }
  393. #endif
  394. #ifdef USE_LIB_CRYPTO_SHA1
  395. #define SHA_CTX EVP_SHA1_CTX
  396. #define SHA1_Init EVP_SHA1_Init
  397. #define SHA1_Final EVP_SHA1_Final
  398. #define SHA1_Update EVP_SHA1_Update
  399. typedef EVP_MD_CTX * EVP_SHA1_CTX;
  400. static inline int
  401. EVP_SHA1_Init(EVP_SHA1_CTX *ctx)
  402. {
  403. return ((*ctx = EVP_MD_CTX_new()) != NULL
  404. && 1 == EVP_DigestInit_ex(*ctx, EVP_sha1(), NULL));
  405. }
  406. static inline int
  407. EVP_SHA1_Final(unsigned char *digest, EVP_SHA1_CTX *ctx)
  408. {
  409. /* SHA_DIGEST_LENGTH; EVP_MD_size(EVP_sha1()) */
  410. int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
  411. EVP_MD_CTX_free(*ctx);
  412. return (1 == rc);
  413. }
  414. static inline int
  415. EVP_SHA1_Update(EVP_SHA1_CTX *ctx, const void *data, size_t length)
  416. {
  417. return (1 == EVP_DigestUpdate(*ctx, data, length));
  418. }
  419. #endif
  420. #ifdef USE_LIB_CRYPTO_SHA256
  421. #define SHA256_CTX EVP_SHA256_CTX
  422. #define SHA256_Init EVP_SHA256_Init
  423. #define SHA256_Final EVP_SHA256_Final
  424. #define SHA256_Update EVP_SHA256_Update
  425. typedef EVP_MD_CTX * EVP_SHA256_CTX;
  426. static inline int
  427. EVP_SHA256_Init(EVP_SHA256_CTX *ctx)
  428. {
  429. return ((*ctx = EVP_MD_CTX_new()) != NULL
  430. && 1 == EVP_DigestInit_ex(*ctx, EVP_sha256(), NULL));
  431. }
  432. static inline int
  433. EVP_SHA256_Final(unsigned char *digest, EVP_SHA256_CTX *ctx)
  434. {
  435. /* SHA256_DIGEST_LENGTH; EVP_MD_size(EVP_sha256()) */
  436. int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
  437. EVP_MD_CTX_free(*ctx);
  438. return (1 == rc);
  439. }
  440. static inline int
  441. EVP_SHA256_Update(EVP_SHA256_CTX *ctx, const void *data, size_t length)
  442. {
  443. return (1 == EVP_DigestUpdate(*ctx, data, length));
  444. }
  445. #endif
  446. #ifdef USE_LIB_CRYPTO_SHA512_256
  447. #define SHA512_256_CTX EVP_SHA512_256_CTX
  448. #define SHA512_256_Init EVP_SHA512_256_Init
  449. #define SHA512_256_Final EVP_SHA512_256_Final
  450. #define SHA512_256_Update EVP_SHA512_256_Update
  451. typedef EVP_MD_CTX * EVP_SHA512_256_CTX;
  452. static inline int
  453. EVP_SHA512_256_Init(EVP_SHA512_256_CTX *ctx)
  454. {
  455. return ((*ctx = EVP_MD_CTX_new()) != NULL
  456. && 1 == EVP_DigestInit_ex(*ctx, EVP_sha512_256(), NULL));
  457. }
  458. static inline int
  459. EVP_SHA512_256_Final(unsigned char *digest, EVP_SHA512_256_CTX *ctx)
  460. {
  461. /* SHA256_DIGEST_LENGTH; EVP_MD_size(EVP_sha512_256()) */
  462. int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
  463. EVP_MD_CTX_free(*ctx);
  464. return (1 == rc);
  465. }
  466. static inline int
  467. EVP_SHA512_256_Update(EVP_SHA512_256_CTX *ctx, const void *data, size_t length)
  468. {
  469. return (1 == EVP_DigestUpdate(*ctx, data, length));
  470. }
  471. #endif
  472. #endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
  473. #elif defined(USE_GNUTLS_CRYPTO)
  474. #include <gnutls/crypto.h>
  475. #include "buffer.h" /* SEGFAULT() */
  476. #define USE_LIB_CRYPTO_MD5
  477. typedef gnutls_hash_hd_t MD5_CTX;
  478. static inline int
  479. MD5_Init(MD5_CTX *ctx)
  480. {
  481. if (gnutls_hash_init(ctx, GNUTLS_DIG_MD5) < 0)
  482. SEGFAULT();
  483. return 1;
  484. }
  485. static inline int
  486. MD5_Final(unsigned char *digest, MD5_CTX *ctx)
  487. {
  488. gnutls_hash_deinit(*ctx, digest);
  489. return 1;
  490. }
  491. static inline int
  492. MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
  493. {
  494. gnutls_hash(*ctx, data, length);
  495. return 1;
  496. }
  497. #define USE_LIB_CRYPTO_SHA1
  498. typedef gnutls_hash_hd_t SHA_CTX;
  499. static inline int
  500. SHA1_Init(SHA_CTX *ctx)
  501. {
  502. if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA1) < 0)
  503. SEGFAULT();
  504. return 1;
  505. }
  506. static inline int
  507. SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
  508. {
  509. gnutls_hash_deinit(*ctx, digest);
  510. return 1;
  511. }
  512. static inline int
  513. SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
  514. {
  515. gnutls_hash(*ctx, data, length);
  516. return 1;
  517. }
  518. #define USE_LIB_CRYPTO_SHA256
  519. typedef gnutls_hash_hd_t SHA256_CTX;
  520. static inline int
  521. SHA256_Init(SHA256_CTX *ctx)
  522. {
  523. if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA256) < 0)
  524. SEGFAULT();
  525. return 1;
  526. }
  527. static inline int
  528. SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
  529. {
  530. gnutls_hash_deinit(*ctx, digest);
  531. return 1;
  532. }
  533. static inline int
  534. SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
  535. {
  536. gnutls_hash(*ctx, data, length);
  537. return 1;
  538. }
  539. #elif defined(USE_NSS_CRYPTO)
  540. /* basic algorithms fail if NSS library has not been init'd (WTH).
  541. * lighttpd defers initialization of rand and crypto until first use
  542. * to attempt to avoid long, blocking init at startup while waiting
  543. * for sufficient system entropy to become available */
  544. #ifdef NSS_VER_INCLUDE
  545. #include <nss3/nss.h> /* NSS_IsInitialized() NSS_NoDB_Init() */
  546. #else
  547. #include <nss/nss.h> /* NSS_IsInitialized() NSS_NoDB_Init() */
  548. #endif
  549. #include <stdlib.h> /* abort() */
  550. __attribute_cold__
  551. static inline void
  552. nss_requires_explicit_init_for_basic_crypto_wth(void)
  553. {
  554. if (NSS_NoDB_Init(NULL) < 0)
  555. abort();
  556. }
  557. #ifdef NSS_VER_INCLUDE
  558. #include <nss3/sechash.h>
  559. #else
  560. #include <nss/sechash.h>
  561. #endif
  562. #define NSS_gen_hashfuncs(name, typ) \
  563. static inline int \
  564. name##_Init(void **ctx) \
  565. { \
  566. if (!NSS_IsInitialized()) \
  567. nss_requires_explicit_init_for_basic_crypto_wth(); \
  568. const SECHashObject * const hashObj = HASH_GetHashObject(typ); \
  569. return ((*ctx=hashObj->create()) != NULL) ? (hashObj->begin(*ctx),1) : 0; \
  570. } \
  571. static inline int \
  572. name##_Final(unsigned char *dest, void **ctx) \
  573. { \
  574. const SECHashObject * const hashObj = HASH_GetHashObject(typ); \
  575. unsigned int retLen; \
  576. hashObj->end(*ctx, dest, &retLen, hashObj->length); \
  577. hashObj->destroy(*ctx, PR_TRUE); \
  578. return 1; \
  579. } \
  580. static inline int \
  581. name##_Update(void **ctx, const void *src, size_t len) \
  582. { \
  583. const SECHashObject * const hashObj = HASH_GetHashObject(typ); \
  584. hashObj->update(*ctx, src, (int)len); \
  585. return 1; \
  586. } \
  587. typedef void * name##_CTX
  588. typedef void * SHA_CTX;
  589. #define USE_LIB_CRYPTO_MD5
  590. /* MD5_Init()
  591. * MD5_Update()
  592. * MD5_Final() */
  593. NSS_gen_hashfuncs(MD5, HASH_AlgMD5);
  594. #define USE_LIB_CRYPTO_SHA1
  595. /* SHA1_Init()
  596. * SHA1_Update()
  597. * SHA1_Final() */
  598. NSS_gen_hashfuncs(SHA1, HASH_AlgSHA1);
  599. #define USE_LIB_CRYPTO_SHA256
  600. /* SHA256_Init()
  601. * SHA256_Update()
  602. * SHA256_Final() */
  603. NSS_gen_hashfuncs(SHA256, HASH_AlgSHA256);
  604. #endif
  605. #endif /* USE_LIB_CRYPTO */
  606. #ifdef USE_LIB_CRYPTO_MD5
  607. #ifndef MD5_DIGEST_LENGTH
  608. #define MD5_DIGEST_LENGTH 16
  609. #endif
  610. #include "algo_md5.h" /*(for legacy li_MD5_*() name mangling)*/
  611. #else
  612. #include "algo_md5.h"
  613. #endif
  614. #ifdef USE_LIB_CRYPTO_SHA1
  615. #ifndef SHA_DIGEST_LENGTH
  616. #define SHA_DIGEST_LENGTH 20
  617. #endif
  618. #else
  619. #include "algo_sha1.h"
  620. #endif
  621. #endif /* LI_SYS_CRYPTO_MD_H */