summaryrefslogtreecommitdiff
path: root/src/sys-crypto-md.h
blob: 3e0eccf1296f01e44809ce6bea891a34bfddca9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
/* sys-crypto-md.h - message digest (MD) wrapper
 *
 * message digest (MD) algorithms are not necessarily cryptographically secure
 * (often provided by crypto libraries, hence this file named sys-crypto-md.h)
 *
 * Copyright(c) 2020 Glenn Strauss gstrauss()gluelogic.com  All rights reserved
 * License: BSD 3-clause (same as lighttpd)
 */
#ifndef LI_SYS_CRYPTO_MD_H
#define LI_SYS_CRYPTO_MD_H
#include "first.h"

#include "sys-crypto.h" /* USE_LIB_CRYPTO */
#ifdef USE_LIB_CRYPTO

#if defined(USE_NETTLE_CRYPTO)

#include <nettle/md4.h>
#include <nettle/md5.h>
#include <nettle/sha.h>

#define USE_LIB_CRYPTO_MD4
typedef struct md4_ctx MD4_CTX;
static inline int
MD4_Init(MD4_CTX *ctx)
{
    nettle_md4_init(ctx);
    return 1;
}
static inline int
MD4_Final(unsigned char *digest, MD4_CTX *ctx)
{
    nettle_md4_digest(ctx, MD4_DIGEST_SIZE, digest);
    return 1;
}
static inline int
MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
{
    nettle_md4_update(ctx, length, data);
    return 1;
}

#define USE_LIB_CRYPTO_MD5
typedef struct md5_ctx MD5_CTX;
static inline int
MD5_Init(MD5_CTX *ctx)
{
    nettle_md5_init(ctx);
    return 1;
}
static inline int
MD5_Final(unsigned char *digest, MD5_CTX *ctx)
{
    nettle_md5_digest(ctx, MD5_DIGEST_SIZE, digest);
    return 1;
}
static inline int
MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
{
    nettle_md5_update(ctx, length, data);
    return 1;
}

#define USE_LIB_CRYPTO_SHA1
typedef struct sha1_ctx SHA_CTX;
static inline int
SHA1_Init(SHA_CTX *ctx)
{
    nettle_sha1_init(ctx);
    return 1;
}
static inline int
SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
{
    nettle_sha1_digest(ctx, SHA1_DIGEST_SIZE, digest);
    return 1;
}
static inline int
SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
{
    nettle_sha1_update(ctx, length, data);
    return 1;
}

#define USE_LIB_CRYPTO_SHA256
typedef struct sha256_ctx SHA256_CTX;
static inline int
SHA256_Init(SHA256_CTX *ctx)
{
    nettle_sha256_init(ctx);
    return 1;
}
static inline int
SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
{
    nettle_sha256_digest(ctx, SHA256_DIGEST_SIZE, digest);
    return 1;
}
static inline int
SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
{
    nettle_sha256_update(ctx, length, data);
    return 1;
}

#define USE_LIB_CRYPTO_SHA512_256
#define SHA512_256_CTX SHA512_CTX
/*(nettle/sha2.h: #define sha512_256_ctx sha512_ctx)*/
/*typedef struct sha512_256_ctx SHA512_CTX;*/    /*(yes, SHA512_CTX)*/
typedef struct sha512_ctx SHA512_CTX;
static inline int
SHA512_256_Init(SHA512_CTX *ctx)
{
    nettle_sha512_256_init(ctx);
    return 1;
}
static inline int
SHA512_256_Final(unsigned char *digest, SHA512_CTX *ctx)
{
    nettle_sha512_256_digest(ctx, SHA256_DIGEST_SIZE, digest);
    return 1;
}
static inline int
SHA512_256_Update(SHA512_CTX *ctx, const void *data, size_t length)
{
    nettle_sha512_update(ctx, length, data); /*(yes, nettle_sha512_update())*/
    return 1;
}

#define USE_LIB_CRYPTO_SHA512
/*typedef struct sha512_ctx SHA512_CTX;*//*(defined above)*/
static inline int
SHA512_Init(SHA512_CTX *ctx)
{
    nettle_sha512_init(ctx);
    return 1;
}
static inline int
SHA512_Final(unsigned char *digest, SHA512_CTX *ctx)
{
    nettle_sha512_digest(ctx, SHA512_DIGEST_SIZE, digest);
    return 1;
}
static inline int
SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length)
{
    nettle_sha512_update(ctx, length, data);
    return 1;
}

#elif defined(USE_MBEDTLS_CRYPTO)

#include <mbedtls/version.h>
/*#include <mbedtls/compat-2.x.h>*//*(func renames ifdef'd below)*/

#ifdef MBEDTLS_MD4_C
#define USE_LIB_CRYPTO_MD4
#include <mbedtls/md4.h>
typedef struct mbedtls_md4_context MD4_CTX;
static inline int
MD4_Init(MD4_CTX *ctx)
{
    mbedtls_md4_init(ctx);
    return (0 == mbedtls_md4_starts_ret(ctx));
}
static inline int
MD4_Final(unsigned char *digest, MD4_CTX *ctx)
{
    int rc = mbedtls_md4_finish_ret(ctx, digest);
    mbedtls_md4_free(ctx);
    return (0 == rc);
}
static inline int
MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
{
    return (0 == mbedtls_md4_update_ret(ctx, data, length));
}
#endif

#ifdef MBEDTLS_MD5_C
#define USE_LIB_CRYPTO_MD5
#include <mbedtls/md5.h>
typedef struct mbedtls_md5_context MD5_CTX;
static inline int
MD5_Init(MD5_CTX *ctx)
{
    mbedtls_md5_init(ctx);
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_md5_starts(ctx));
  #else
    return (0 == mbedtls_md5_starts_ret(ctx));
  #endif
}
static inline int
MD5_Final(unsigned char *digest, MD5_CTX *ctx)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    int rc = mbedtls_md5_finish(ctx, digest);
  #else
    int rc = mbedtls_md5_finish_ret(ctx, digest);
  #endif
    mbedtls_md5_free(ctx);
    return (0 == rc);
}
static inline int
MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_md5_update(ctx, data, length));
  #else
    return (0 == mbedtls_md5_update_ret(ctx, data, length));
  #endif
}
#endif

#ifdef MBEDTLS_SHA1_C
#define USE_LIB_CRYPTO_SHA1
#include <mbedtls/sha1.h>
typedef struct mbedtls_sha1_context SHA_CTX;
static inline int
SHA1_Init(SHA_CTX *ctx)
{
    mbedtls_sha1_init(ctx);
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_sha1_starts(ctx));
  #else
    return (0 == mbedtls_sha1_starts_ret(ctx));
  #endif
}
static inline int
SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    int rc = mbedtls_sha1_finish(ctx, digest);
  #else
    int rc = mbedtls_sha1_finish_ret(ctx, digest);
  #endif
    mbedtls_sha1_free(ctx);
    return (0 == rc);
}
static inline int
SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_sha1_update(ctx, data, length));
  #else
    return (0 == mbedtls_sha1_update_ret(ctx, data, length));
  #endif
}
#endif

#ifdef MBEDTLS_SHA256_C
#define USE_LIB_CRYPTO_SHA256
#include <mbedtls/sha256.h>
typedef struct mbedtls_sha256_context SHA256_CTX;
static inline int
SHA256_Init(SHA256_CTX *ctx)
{
    mbedtls_sha256_init(ctx);
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_sha256_starts(ctx, 0));
  #else
    return (0 == mbedtls_sha256_starts_ret(ctx, 0));
  #endif
}
static inline int
SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    int rc = mbedtls_sha256_finish(ctx, digest);
  #else
    int rc = mbedtls_sha256_finish_ret(ctx, digest);
  #endif
    mbedtls_sha256_free(ctx);
    return (0 == rc);
}
static inline int
SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_sha256_update(ctx, data, length));
  #else
    return (0 == mbedtls_sha256_update_ret(ctx, data, length));
  #endif
}
#endif

#ifdef MBEDTLS_SHA512_C
#define USE_LIB_CRYPTO_SHA512
#include <mbedtls/sha512.h>
typedef struct mbedtls_sha512_context SHA512_CTX;
static inline int
SHA512_Init(SHA512_CTX *ctx)
{
    mbedtls_sha512_init(ctx);
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_sha512_starts(ctx, 0));
  #else
    return (0 == mbedtls_sha512_starts_ret(ctx, 0));
  #endif
}
static inline int
SHA512_Final(unsigned char *digest, SHA512_CTX *ctx)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    int rc = mbedtls_sha512_finish(ctx, digest);
  #else
    int rc = mbedtls_sha512_finish_ret(ctx, digest);
  #endif
    mbedtls_sha512_free(ctx);
    return (0 == rc);
}
static inline int
SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length)
{
  #if MBEDTLS_VERSION_MAJOR >= 3
    return (0 == mbedtls_sha512_update(ctx, data, length));
  #else
    return (0 == mbedtls_sha512_update_ret(ctx, data, length));
  #endif
}
#endif

#elif defined(USE_WOLFSSL_CRYPTO)

/* WolfSSL compatibility API for OpenSSL unnecessarily bounces through an extra
 * layer of indirection.  However, to avoid conflicting typedefs when includers
 * also include headers from the WolfSSL compatibility API for OpenSSL, we
 * include those headers here, as well, and use the compatibility API typedefs.
 * (undef of OPENSSL_EXTRA and NO_OLD_WC_NAMES not sufficient, and not friendly
 *  to do in a header when others might rely on them) */

/* workaround fragile code in wolfssl/wolfcrypto/types.h */
#if !defined(SIZEOF_LONG) || !defined(SIZEOF_LONG_LONG)
#undef SIZEOF_LONG
#undef SIZEOF_LONG_LONG
#endif

#include <wolfssl/options.h> /* wolfssl NO_* macros */

#ifndef NO_MD4
#include <wolfssl/wolfcrypt/md4.h>
#include <wolfssl/openssl/md4.h>
#undef MD4_Init
#undef MD4_Final
#undef MD4_Update
#define USE_LIB_CRYPTO_MD4
/*typedef Md4 MD4_CTX;*/
static inline int
MD4_Init(MD4_CTX *ctx)
{
    wc_InitMd4((Md4 *)ctx);
    return 1;
}
static inline int
MD4_Final(unsigned char *digest, MD4_CTX *ctx)
{
    wc_Md4Final((Md4 *)ctx, digest);
    return 1;
}
static inline int
MD4_Update(MD4_CTX *ctx, const void *data, size_t length)
{
    wc_Md4Update((Md4 *)ctx, data, length);
    return 1;
}
#endif

#ifndef NO_MD5
#include <wolfssl/wolfcrypt/md5.h>
#include <wolfssl/openssl/md5.h>
#undef MD5_Init
#undef MD5_Final
#undef MD5_Update
#define USE_LIB_CRYPTO_MD5
/*typedef wc_Md5 MD5_CTX;*/
static inline int
MD5_Init(MD5_CTX *ctx)
{
    return (0 == wc_InitMd5((wc_Md5 *)ctx));
}
static inline int
MD5_Final(unsigned char *digest, MD5_CTX *ctx)
{
    return (0 == wc_Md5Final((wc_Md5 *)ctx, digest));
}
static inline int
MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
{
    wc_Md5Update((wc_Md5 *)ctx, data, length);
    return 1;
}
#endif

#ifndef NO_SHA
#include <wolfssl/wolfcrypt/sha.h>
#include <wolfssl/openssl/sha.h>
#undef SHA1_Init
#undef SHA1_Final
#undef SHA1_Update
#define USE_LIB_CRYPTO_SHA1
/*typedef wc_Sha SHA_CTX;*/
static inline int
SHA1_Init(SHA_CTX *ctx)
{
    return (0 == wc_InitSha((wc_Sha *)ctx));
}
static inline int
SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
{
    return (0 == wc_ShaFinal((wc_Sha *)ctx, digest));
}
static inline int
SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
{
    wc_ShaUpdate((wc_Sha *)ctx, data, length);
    return 1;
}
#endif

#ifndef NO_SHA256
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/openssl/sha.h>
#undef SHA256_Init
#undef SHA256_Final
#undef SHA256_Update
#define USE_LIB_CRYPTO_SHA256
/*typedef wc_Sha256 SHA256_CTX;*/
static inline int
SHA256_Init(SHA256_CTX *ctx)
{
    return (0 == wc_InitSha256((wc_Sha256 *)ctx));
}
static inline int
SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
{
    return (0 == wc_Sha256Final((wc_Sha256 *)ctx, digest));
}
static inline int
SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
{
    wc_Sha256Update((wc_Sha256 *)ctx, data, length);
    return 1;
}
#endif

#ifndef NO_SHA512
#ifdef WOLFSSL_SHA512
#include <wolfssl/wolfcrypt/sha512.h>
#include <wolfssl/openssl/sha.h>
#undef SHA512_Init
#undef SHA512_Final
#undef SHA512_Update
#define USE_LIB_CRYPTO_SHA512
/*typedef wc_Sha512 SHA512_CTX;*/
static inline int
SHA512_Init(SHA512_CTX *ctx)
{
    return (0 == wc_InitSha512((wc_Sha512 *)ctx));
}
static inline int
SHA512_Final(unsigned char *digest, SHA512_CTX *ctx)
{
    return (0 == wc_Sha512Final((wc_Sha512 *)ctx, digest));
}
static inline int
SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length)
{
    wc_Sha512Update((wc_Sha512 *)ctx, data, length);
    return 1;
}
#endif
#endif

#elif defined(USE_OPENSSL_CRYPTO)

#include <openssl/md4.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#ifndef OPENSSL_NO_MD4
#define USE_LIB_CRYPTO_MD4
#endif
#ifndef OPENSSL_NO_MD5
#define USE_LIB_CRYPTO_MD5
#endif
#define USE_LIB_CRYPTO_SHA1
#define USE_LIB_CRYPTO_SHA256
#ifdef SHA512_256_DIGEST_LENGTH
#define USE_LIB_CRYPTO_SHA512_256
#endif
#ifdef SHA512_DIGEST_LENGTH
#define USE_LIB_CRYPTO_SHA512
#endif

#include <openssl/opensslv.h>
#ifdef BORINGSSL_API_VERSION
typedef SHA512_CTX SHA512_256_CTX;
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/evp.h>

#ifdef USE_LIB_CRYPTO_MD4
#define MD4_CTX EVP_MD4_CTX
#define MD4_Init EVP_MD4_Init
#define MD4_Final EVP_MD4_Final
#define MD4_Update EVP_MD4_Update
typedef EVP_MD_CTX * MD4_CTX;
static inline int
EVP_MD4_Init(EVP_MD4_CTX *ctx)
{
    return ((*ctx = EVP_MD_CTX_new()) != NULL
            && 1 == EVP_DigestInit_ex(*ctx, EVP_md4(), NULL));
}
static inline int
EVP_MD4_Final(unsigned char *digest, EVP_MD4_CTX *ctx)
{
    /* MD4_DIGEST_LENGTH; EVP_MD_size(EVP_md4()) */
    int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
    EVP_MD_CTX_free(*ctx);
    return (1 == rc);
}
static inline int
EVP_MD4_Update(EVP_MD4_CTX *ctx, const void *data, size_t length)
{
    return (1 == EVP_DigestUpdate(*ctx, data, length));
}
#endif

#ifdef USE_LIB_CRYPTO_MD5
#define MD5_CTX EVP_MD5_CTX
#define MD5_Init EVP_MD5_Init
#define MD5_Final EVP_MD5_Final
#define MD5_Update EVP_MD5_Update
typedef EVP_MD_CTX * EVP_MD5_CTX;
static inline int
EVP_MD5_Init(EVP_MD5_CTX *ctx)
{
    return ((*ctx = EVP_MD_CTX_new()) != NULL
            && 1 == EVP_DigestInit_ex(*ctx, EVP_md5(), NULL));
}
static inline int
EVP_MD5_Final(unsigned char *digest, EVP_MD5_CTX *ctx)
{
    /* MD5_DIGEST_LENGTH; EVP_MD_size(EVP_md5()) */
    int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
    EVP_MD_CTX_free(*ctx);
    return (1 == rc);
}
static inline int
EVP_MD5_Update(EVP_MD5_CTX *ctx, const void *data, size_t length)
{
    return (1 == EVP_DigestUpdate(*ctx, data, length));
}
#endif

#ifdef USE_LIB_CRYPTO_SHA1
#define SHA_CTX EVP_SHA1_CTX
#define SHA1_Init EVP_SHA1_Init
#define SHA1_Final EVP_SHA1_Final
#define SHA1_Update EVP_SHA1_Update
typedef EVP_MD_CTX * EVP_SHA1_CTX;
static inline int
EVP_SHA1_Init(EVP_SHA1_CTX *ctx)
{
    return ((*ctx = EVP_MD_CTX_new()) != NULL
            && 1 == EVP_DigestInit_ex(*ctx, EVP_sha1(), NULL));
}
static inline int
EVP_SHA1_Final(unsigned char *digest, EVP_SHA1_CTX *ctx)
{
    /* SHA_DIGEST_LENGTH; EVP_MD_size(EVP_sha1()) */
    int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
    EVP_MD_CTX_free(*ctx);
    return (1 == rc);
}
static inline int
EVP_SHA1_Update(EVP_SHA1_CTX *ctx, const void *data, size_t length)
{
    return (1 == EVP_DigestUpdate(*ctx, data, length));
}
#endif

#ifdef USE_LIB_CRYPTO_SHA256
#define SHA256_CTX EVP_SHA256_CTX
#define SHA256_Init EVP_SHA256_Init
#define SHA256_Final EVP_SHA256_Final
#define SHA256_Update EVP_SHA256_Update
typedef EVP_MD_CTX * EVP_SHA256_CTX;
static inline int
EVP_SHA256_Init(EVP_SHA256_CTX *ctx)
{
    return ((*ctx = EVP_MD_CTX_new()) != NULL
            && 1 == EVP_DigestInit_ex(*ctx, EVP_sha256(), NULL));
}
static inline int
EVP_SHA256_Final(unsigned char *digest, EVP_SHA256_CTX *ctx)
{
    /* SHA256_DIGEST_LENGTH; EVP_MD_size(EVP_sha256()) */
    int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
    EVP_MD_CTX_free(*ctx);
    return (1 == rc);
}
static inline int
EVP_SHA256_Update(EVP_SHA256_CTX *ctx, const void *data, size_t length)
{
    return (1 == EVP_DigestUpdate(*ctx, data, length));
}
#endif

#ifdef USE_LIB_CRYPTO_SHA512_256
#define SHA512_256_CTX EVP_SHA512_256_CTX
#define SHA512_256_Init EVP_SHA512_256_Init
#define SHA512_256_Final EVP_SHA512_256_Final
#define SHA512_256_Update EVP_SHA512_256_Update
typedef EVP_MD_CTX * EVP_SHA512_256_CTX;
static inline int
EVP_SHA512_256_Init(EVP_SHA512_256_CTX *ctx)
{
    return ((*ctx = EVP_MD_CTX_new()) != NULL
            && 1 == EVP_DigestInit_ex(*ctx, EVP_sha512_256(), NULL));
}
static inline int
EVP_SHA512_256_Final(unsigned char *digest, EVP_SHA512_256_CTX *ctx)
{
    /* SHA256_DIGEST_LENGTH; EVP_MD_size(EVP_sha512_256()) */
    int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
    EVP_MD_CTX_free(*ctx);
    return (1 == rc);
}
static inline int
EVP_SHA512_256_Update(EVP_SHA512_256_CTX *ctx, const void *data, size_t length)
{
    return (1 == EVP_DigestUpdate(*ctx, data, length));
}
#endif

#ifdef USE_LIB_CRYPTO_SHA512
#define SHA512_CTX EVP_SHA512_CTX
#define SHA512_Init EVP_SHA512_Init
#define SHA512_Final EVP_SHA512_Final
#define SHA512_Update EVP_SHA512_Update
typedef EVP_MD_CTX * EVP_SHA512_CTX;
static inline int
EVP_SHA512_Init(EVP_SHA512_CTX *ctx)
{
    return ((*ctx = EVP_MD_CTX_new()) != NULL
            && 1 == EVP_DigestInit_ex(*ctx, EVP_sha512(), NULL));
}
static inline int
EVP_SHA512_Final(unsigned char *digest, EVP_SHA512_CTX *ctx)
{
    /* SHA512_DIGEST_LENGTH; EVP_MD_size(EVP_sha512()) */
    int rc = EVP_DigestFinal_ex(*ctx, digest, NULL);
    EVP_MD_CTX_free(*ctx);
    return (1 == rc);
}
static inline int
EVP_SHA512_Update(EVP_SHA512_CTX *ctx, const void *data, size_t length)
{
    return (1 == EVP_DigestUpdate(*ctx, data, length));
}
#endif

#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */

#elif defined(USE_GNUTLS_CRYPTO)

#include <gnutls/crypto.h>
#include "ck.h"

#define USE_LIB_CRYPTO_MD5
typedef gnutls_hash_hd_t MD5_CTX;
static inline int
MD5_Init(MD5_CTX *ctx)
{
    if (gnutls_hash_init(ctx, GNUTLS_DIG_MD5) < 0)
        ck_bt_abort(__FILE__, __LINE__, "aborted");
    return 1;
}
static inline int
MD5_Final(unsigned char *digest, MD5_CTX *ctx)
{
    gnutls_hash_deinit(*ctx, digest);
    return 1;
}
static inline int
MD5_Update(MD5_CTX *ctx, const void *data, size_t length)
{
    gnutls_hash(*ctx, data, length);
    return 1;
}

#define USE_LIB_CRYPTO_SHA1
typedef gnutls_hash_hd_t SHA_CTX;
static inline int
SHA1_Init(SHA_CTX *ctx)
{
    if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA1) < 0)
        ck_bt_abort(__FILE__, __LINE__, "aborted");
    return 1;
}
static inline int
SHA1_Final(unsigned char *digest, SHA_CTX *ctx)
{
    gnutls_hash_deinit(*ctx, digest);
    return 1;
}
static inline int
SHA1_Update(SHA_CTX *ctx, const void *data, size_t length)
{
    gnutls_hash(*ctx, data, length);
    return 1;
}

#define USE_LIB_CRYPTO_SHA256
typedef gnutls_hash_hd_t SHA256_CTX;
static inline int
SHA256_Init(SHA256_CTX *ctx)
{
    if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA256) < 0)
        ck_bt_abort(__FILE__, __LINE__, "aborted");
    return 1;
}
static inline int
SHA256_Final(unsigned char *digest, SHA256_CTX *ctx)
{
    gnutls_hash_deinit(*ctx, digest);
    return 1;
}
static inline int
SHA256_Update(SHA256_CTX *ctx, const void *data, size_t length)
{
    gnutls_hash(*ctx, data, length);
    return 1;
}

#define USE_LIB_CRYPTO_SHA512
typedef gnutls_hash_hd_t SHA512_CTX;
static inline int
SHA512_Init(SHA512_CTX *ctx)
{
    if (gnutls_hash_init(ctx, GNUTLS_DIG_SHA512) < 0)
        ck_bt_abort(__FILE__, __LINE__, "aborted");
    return 1;
}
static inline int
SHA512_Final(unsigned char *digest, SHA512_CTX *ctx)
{
    gnutls_hash_deinit(*ctx, digest);
    return 1;
}
static inline int
SHA512_Update(SHA512_CTX *ctx, const void *data, size_t length)
{
    gnutls_hash(*ctx, data, length);
    return 1;
}

#elif defined(USE_NSS_CRYPTO)

#ifdef __has_include
#if __has_include(<nss3/nss.h>)
#define NSS_VER_INCLUDE
#endif
#endif

/* basic algorithms fail if NSS library has not been init'd (WTH).
 * lighttpd defers initialization of rand and crypto until first use
 * to attempt to avoid long, blocking init at startup while waiting
 * for sufficient system entropy to become available */
#ifdef NSS_VER_INCLUDE
#include <nss3/nss.h>   /* NSS_IsInitialized() NSS_NoDB_Init() */
#else
#include <nss/nss.h>    /* NSS_IsInitialized() NSS_NoDB_Init() */
#endif
#include <stdlib.h>     /* abort() */
__attribute_cold__
static inline void
nss_requires_explicit_init_for_basic_crypto_wth(void)
{
    if (NSS_NoDB_Init(NULL) < 0)
        abort();
}

#ifdef NSS_VER_INCLUDE
#include <nss3/sechash.h>
#else
#include <nss/sechash.h>
#endif

#define NSS_gen_hashfuncs(name, typ)                                          \
static inline int                                                             \
name##_Init(void **ctx)                                                       \
{                                                                             \
    if (!NSS_IsInitialized())                                                 \
        nss_requires_explicit_init_for_basic_crypto_wth();                    \
    const SECHashObject * const hashObj = HASH_GetHashObject(typ);            \
    return ((*ctx=hashObj->create()) != NULL) ? (hashObj->begin(*ctx),1) : 0; \
}                                                                             \
static inline int                                                             \
name##_Final(unsigned char *dest, void **ctx)                                 \
{                                                                             \
    const SECHashObject * const hashObj = HASH_GetHashObject(typ);            \
    unsigned int retLen;                                                      \
    hashObj->end(*ctx, dest, &retLen, hashObj->length);                       \
    hashObj->destroy(*ctx, PR_TRUE);                                          \
    return 1;                                                                 \
}                                                                             \
static inline int                                                             \
name##_Update(void **ctx, const void *src, size_t len)                        \
{                                                                             \
    const SECHashObject * const hashObj = HASH_GetHashObject(typ);            \
    hashObj->update(*ctx, src, (int)len);                                     \
    return 1;                                                                 \
}                                                                             \
typedef void * name##_CTX
typedef void * SHA_CTX;

#define USE_LIB_CRYPTO_MD5
/* MD5_Init()
 * MD5_Update()
 * MD5_Final() */
NSS_gen_hashfuncs(MD5, HASH_AlgMD5);

#define USE_LIB_CRYPTO_SHA1
/* SHA1_Init()
 * SHA1_Update()
 * SHA1_Final() */
NSS_gen_hashfuncs(SHA1, HASH_AlgSHA1);

#define USE_LIB_CRYPTO_SHA256
/* SHA256_Init()
 * SHA256_Update()
 * SHA256_Final() */
NSS_gen_hashfuncs(SHA256, HASH_AlgSHA256);

#define USE_LIB_CRYPTO_SHA512
/* SHA512_Init()
 * SHA512_Update()
 * SHA512_Final() */
NSS_gen_hashfuncs(SHA512, HASH_AlgSHA512);

#endif

#endif /* USE_LIB_CRYPTO */


#ifdef USE_LIB_CRYPTO_MD4
#ifndef MD4_DIGEST_LENGTH
#define MD4_DIGEST_LENGTH 16
#endif
#undef  MD_DIGEST_LENGTH_MAX
#define MD_DIGEST_LENGTH_MAX MD4_DIGEST_LENGTH
#endif


#ifdef USE_LIB_CRYPTO_MD5
#ifndef MD5_DIGEST_LENGTH
#define MD5_DIGEST_LENGTH 16
#endif
#include "algo_md5.h" /*(for legacy li_MD5_*() name mangling)*/
#else
#include "algo_md5.h" /* MD5 implementation included with lighttpd */
#endif
#undef  MD_DIGEST_LENGTH_MAX
#define MD_DIGEST_LENGTH_MAX MD5_DIGEST_LENGTH


#ifdef USE_LIB_CRYPTO_SHA1
typedef SHA_CTX SHA1_CTX;  /*(naming consistency with other algos)*/
#ifndef SHA_DIGEST_LENGTH
#define SHA_DIGEST_LENGTH 20
#endif
#ifndef SHA1_DIGEST_LENGTH /*(naming consistency with other algos)*/
#define SHA1_DIGEST_LENGTH SHA_DIGEST_LENGTH
#endif
#else
#include "algo_sha1.h"  /* SHA1 implementation included with lighttpd */
typedef SHA_CTX SHA1_CTX;  /*(naming consistency with other algos)*/
#endif
#undef  MD_DIGEST_LENGTH_MAX
#define MD_DIGEST_LENGTH_MAX SHA_DIGEST_LENGTH


#ifdef USE_LIB_CRYPTO_SHA256
#ifndef SHA256_DIGEST_LENGTH
#define SHA256_DIGEST_LENGTH 32
#endif
#undef  MD_DIGEST_LENGTH_MAX
#define MD_DIGEST_LENGTH_MAX SHA256_DIGEST_LENGTH
#endif


#ifdef USE_LIB_CRYPTO_SHA512_256
#ifndef SHA512_256_DIGEST_LENGTH
#define SHA512_256_DIGEST_LENGTH 32
#endif
#undef  MD_DIGEST_LENGTH_MAX
#define MD_DIGEST_LENGTH_MAX SHA512_256_DIGEST_LENGTH
#endif


#ifdef USE_LIB_CRYPTO_SHA512
#ifndef SHA512_DIGEST_LENGTH
#define SHA512_DIGEST_LENGTH 64
#endif
#undef  MD_DIGEST_LENGTH_MAX
#define MD_DIGEST_LENGTH_MAX SHA512_DIGEST_LENGTH
#endif


/* message digest wrappers operating on single ptr, and on const_iovec */


typedef void(*li_md_once_fn)(unsigned char *digest, const void *data, size_t n);

#define li_md_once(algo)                                            \
  static inline void                                                \
  algo##_once (unsigned char * const digest,                        \
               const void * const data, const size_t n)             \
  {                                                                 \
      algo##_CTX ctx;                                               \
      algo##_Init(&ctx);                                            \
      algo##_Update(&ctx, data, n);                                 \
      algo##_Final(digest, &ctx);                                   \
  }

#ifndef LI_CONST_IOVEC
#define LI_CONST_IOVEC
struct const_iovec {
  const void *iov_base;
  size_t iov_len;
};
#endif

typedef void(*li_md_iov_fn)(unsigned char *digest,
                            const struct const_iovec *iov, size_t n);

#define li_md_iov(algo)                                             \
  static inline void                                                \
  algo##_iov (unsigned char * const digest,                         \
              const struct const_iovec * const iov, const size_t n) \
  {                                                                 \
      algo##_CTX ctx;                                               \
      algo##_Init(&ctx);                                            \
      for (size_t i = 0; i < n; ++i) {                              \
          if (iov[i].iov_len)                                       \
              algo##_Update(&ctx, iov[i].iov_base, iov[i].iov_len); \
      }                                                             \
      algo##_Final(digest, &ctx);                                   \
  }

#ifdef USE_LIB_CRYPTO_MD4
li_md_once(MD4)
li_md_iov(MD4)
#endif /* MD4_once() MD4_iov() */

/*#ifdef USE_LIB_CRYPTO_MD5*/
li_md_once(MD5)
li_md_iov(MD5)
/*#endif*/ /* MD5_once() MD5_iov() */

/*#ifdef USE_LIB_CRYPTO_SHA1*/
li_md_once(SHA1)
li_md_iov(SHA1)
/*#endif*/ /* SHA1_once() SHA1_iov() */

#ifdef USE_LIB_CRYPTO_SHA256
li_md_once(SHA256)
li_md_iov(SHA256)
#endif /* SHA256_once() SHA256_iov() */

#ifdef USE_LIB_CRYPTO_SHA512_256
li_md_once(SHA512_256)
li_md_iov(SHA512_256)
#endif /* SHA512_256_once() SHA512_256_iov() */

#ifdef USE_LIB_CRYPTO_SHA512
li_md_once(SHA512)
li_md_iov(SHA512)
#endif /* SHA512_once() SHA512_iov() */


#endif /* LI_SYS_CRYPTO_MD_H */