Commit 847e682f8de452d5f0038458f06ae68ae3959b3a

Guillem Jover 2021-02-07T00:09:30

Use libmd hashing function implementations instead of embedding our own This splits the implementation responsibilities, and reduces embedded code copies, which was one of the driving points with this project to start with, so it's nice to give a good example.

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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
diff --git a/COPYING b/COPYING
index 34ec432..b51db0e 100644
--- a/COPYING
+++ b/COPYING
@@ -278,8 +278,6 @@ Files:
  man/getpeereid.3bsd
  man/pidfile.3bsd
  src/expand_number.c
- src/hash/sha512.h
- src/hash/sha512c.c
  src/pidfile.c
  src/reallocf.c
  src/timeconv.c
@@ -459,17 +457,6 @@ License: Expat
  USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 Files:
- include/bsd/md5.h
- src/hash/md5.c
-Copyright:
- None
-License: public-domain-Colin-Plumb
- This code implements the MD5 message-digest algorithm.
- The algorithm is due to Ron Rivest. This code was
- written by Colin Plumb in 1993, no copyright is claimed.
- This code is in the public domain; do with it what you wish.
-
-Files:
  src/explicit_bzero.c
  src/chacha_private.h
 Copyright:
@@ -479,8 +466,6 @@ License: public-domain
 
 Files:
  man/mdX.3bsd
- src/hash/md5hl.c
- src/hash/helper.c
 Copyright:
  None
 License: Beerware
diff --git a/README b/README
index d516db0..9b2bbdb 100644
--- a/README
+++ b/README
@@ -5,6 +5,9 @@ and lacking on others like GNU systems, thus making it easier to port
 projects with strong BSD origins, without needing to embed the same
 code over and over again on each project.
 
+A BSD compatible message-digest library is required, on systems where
+this is not provided by its libc or libmd libraries, the canonical
+implementation to use is <https://www.hadrons.org/software/libmd/>.
 
 Releases
 --------
diff --git a/configure.ac b/configure.ac
index cf064bb..df05b0e 100644
--- a/configure.ac
+++ b/configure.ac
@@ -63,6 +63,17 @@ AC_SUBST([TESTU01_LIBS])
 AM_CONDITIONAL([HAVE_LIBTESTU01],
                [test "x$ac_cv_lib_testu01_unif01_CreateExternGenBits" = "xyes"])
 
+saved_LIBS="$LIBS"
+AC_SEARCH_LIBS([MD5Update], [md], [
+  AC_SEARCH_LIBS([SHA512Update], [md], [
+    MD_LIBS="-lmd"
+  ])
+], [
+  AC_MSG_ERROR([cannot find required message digest functions in libc or libmd])
+])
+AC_SUBST([MD_LIBS])
+LIBS="$saved_LIBS"
+
 is_windows=no
 AS_CASE([$host_os],
   [*-gnu*], [
diff --git a/include/bsd/md5.h b/include/bsd/md5.h
index bf36a30..5bd58d8 100644
--- a/include/bsd/md5.h
+++ b/include/bsd/md5.h
@@ -1,58 +1,31 @@
-/*	$OpenBSD: md5.h,v 1.16 2004/06/22 01:57:30 jfb Exp $	*/
-
 /*
- * This code implements the MD5 message-digest algorithm.
- * The algorithm is due to Ron Rivest.  This code was
- * written by Colin Plumb in 1993, no copyright is claimed.
- * This code is in the public domain; do with it what you wish.
+ * Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
  *
- * Equivalent code is available from RSA Data Security, Inc.
- * This code has been tested against that, and is equivalent,
- * except that you don't need to include two pages of legalese
- * with every copy.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef LIBBSD_MD5_H
-#define LIBBSD_MD5_H
-
-#include <stdint.h>
-
-#define	MD5_BLOCK_LENGTH		64
-#define	MD5_DIGEST_LENGTH		16
-#define	MD5_DIGEST_STRING_LENGTH	(MD5_DIGEST_LENGTH * 2 + 1)
-
-typedef struct MD5Context {
-	uint32_t state[4];			/* state */
-	uint64_t count;				/* number of bits, mod 2^64 */
-	uint8_t buffer[MD5_BLOCK_LENGTH];	/* input buffer */
-} MD5_CTX;
-
 #ifdef LIBBSD_OVERLAY
-#include <sys/cdefs.h>
+#include_next <md5.h>
 #else
-#include <bsd/sys/cdefs.h>
+#include <md5.h>
 #endif
-#include <sys/types.h>
-
-__BEGIN_DECLS
-void	 MD5Init(MD5_CTX *);
-void	 MD5Update(MD5_CTX *, const uint8_t *, size_t)
-		__attribute__((__bounded__(__string__,2,3)));
-void	 MD5Pad(MD5_CTX *);
-void	 MD5Final(uint8_t [MD5_DIGEST_LENGTH], MD5_CTX *)
-		__attribute__((__bounded__(__minbytes__,1,MD5_DIGEST_LENGTH)));
-void	 MD5Transform(uint32_t [4], const uint8_t [MD5_BLOCK_LENGTH])
-		__attribute__((__bounded__(__minbytes__,1,4)))
-		__attribute__((__bounded__(__minbytes__,2,MD5_BLOCK_LENGTH)));
-char	*MD5End(MD5_CTX *, char *)
-		__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
-char	*MD5File(const char *, char *)
-		__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
-char	*MD5FileChunk(const char *, char *, off_t, off_t)
-		__attribute__((__bounded__(__minbytes__,2,MD5_DIGEST_STRING_LENGTH)));
-char	*MD5Data(const uint8_t *, size_t, char *)
-		__attribute__((__bounded__(__string__,1,2)))
-		__attribute__((__bounded__(__minbytes__,3,MD5_DIGEST_STRING_LENGTH)));
-__END_DECLS
-
-#endif /* LIBBSD_MD5_H */
diff --git a/man/libbsd.7 b/man/libbsd.7
index 407b3e4..bfaa3ce 100644
--- a/man/libbsd.7
+++ b/man/libbsd.7
@@ -180,9 +180,10 @@ for example.
 .It Fn MD5File 3
 .It Fn MD5FileChunk 3
 .It Fn MD5Data 3
-The set of MD5 digest functions are now provided by the
+The set of MD5 digest functions are now proxies for the implementations
+provided by the
 .Nm libmd
-companion library, so it is advised to use that instead.
+companion library, so it is advised to switch to use that directly instead.
 .It Fn explicit_bzero 3
 This function is provided by
 .Nm glibc
diff --git a/src/Makefile.am b/src/Makefile.am
index c13d385..48fb1fa 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -15,7 +15,6 @@ AM_CPPFLAGS += \
 endif
 
 libbsd_la_included_sources = \
-	hash/helper.c \
 	getentropy_aix.c \
 	getentropy_bsd.c \
 	getentropy_hpux.c \
@@ -34,10 +33,6 @@ EXTRA_DIST = \
 	$(libbsd_la_included_sources) \
 	$(nil)
 
-CLEANFILES = \
-	hash/md5hl.c \
-	$(nil)
-
 pkgconfigdir = $(libdir)/pkgconfig
 pkgconfig_DATA = \
 	libbsd.pc \
@@ -53,14 +48,11 @@ pkgconfig_DATA += libbsd-ctor.pc
 lib_LIBRARIES += libbsd-ctor.a
 endif
 
-hash/md5hl.c: $(srcdir)/hash/helper.c
-	$(AM_V_at) $(MKDIR_P) hash
-	$(AM_V_GEN) sed -e 's:hashinc:md5.h:g' -e 's:HASH:MD5:g' $< > $@
-
 libbsd_la_DEPENDENCIES = \
 	$(libbsd_la_included_sources) \
 	libbsd.map
 libbsd_la_LIBADD = \
+	$(MD_LIBS) \
 	$(CLOCK_GETTIME_LIBS) \
 	$(ARC4RANDOM_ATFORK_LIBS) \
 	$(nil)
@@ -95,15 +87,12 @@ libbsd_la_SOURCES = \
 	funopen.c \
 	getbsize.c \
 	getpeereid.c \
-	hash/md5.c \
-	hash/md5hl.c \
-	hash/sha512.h \
-	hash/sha512c.c \
 	heapsort.c \
 	humanize_number.c \
 	inet_net_pton.c \
 	local-elf.h \
 	local-link.h \
+	md5.c \
 	merge.c \
 	nlist.c \
 	pidfile.c \
diff --git a/src/getentropy_aix.c b/src/getentropy_aix.c
index d759fe0..a963071 100644
--- a/src/getentropy_aix.c
+++ b/src/getentropy_aix.c
@@ -39,7 +39,7 @@
 #include <string.h>
 #include <termios.h>
 
-#include "hash/sha512.h"
+#include <sha512.h>
 
 #include <libperfstat.h>
 
@@ -54,9 +54,9 @@
 			HD(b); \
 	} while (0)
 
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x)    (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x)	 (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x)    (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
 
 int	getentropy(void *buf, size_t len);
 
diff --git a/src/getentropy_hpux.c b/src/getentropy_hpux.c
index 5be096a..b21ff4f 100644
--- a/src/getentropy_hpux.c
+++ b/src/getentropy_hpux.c
@@ -42,7 +42,7 @@
 #include <unistd.h>
 #include <time.h>
 
-#include "hash/sha512.h"
+#include <sha512.h>
 
 #include <sys/vfs.h>
 
@@ -59,9 +59,9 @@
 			HD(b); \
 	} while (0)
 
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x)    (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x)	 (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x)    (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
 
 int	getentropy(void *buf, size_t len);
 
diff --git a/src/getentropy_hurd.c b/src/getentropy_hurd.c
index 738dc3b..2f76baa 100644
--- a/src/getentropy_hurd.c
+++ b/src/getentropy_hurd.c
@@ -44,7 +44,7 @@
 #include <unistd.h>
 #include <time.h>
 
-#include "hash/sha512.h"
+#include <sha512.h>
 
 #ifdef HAVE_GETAUXVAL
 #include <sys/auxv.h>
@@ -62,9 +62,9 @@
 			HD(b); \
 	} while (0)
 
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x)    (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x)	 (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x)    (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
 
 int	getentropy(void *buf, size_t len);
 
@@ -228,7 +228,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
 {
 	SHA512_CTX *ctx = data;
 
-	SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
+	SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
 	return (0);
 }
 
diff --git a/src/getentropy_linux.c b/src/getentropy_linux.c
index 74d965e..25c986e 100644
--- a/src/getentropy_linux.c
+++ b/src/getentropy_linux.c
@@ -48,7 +48,7 @@
 #include <unistd.h>
 #include <time.h>
 
-#include "hash/sha512.h"
+#include <sha512.h>
 
 #include <linux/types.h>
 #include <linux/random.h>
@@ -68,9 +68,9 @@
 			HD(b); \
 	} while (0)
 
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x)    (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x)	 (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x)    (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
 
 int	getentropy(void *buf, size_t len);
 
@@ -333,7 +333,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
 {
 	SHA512_CTX *ctx = data;
 
-	SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
+	SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
 	return (0);
 }
 
diff --git a/src/getentropy_osx.c b/src/getentropy_osx.c
index bcdbce5..d67f33d 100644
--- a/src/getentropy_osx.c
+++ b/src/getentropy_osx.c
@@ -76,9 +76,9 @@
 			HD(b); \
 	} while (0)
 
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x)    (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x)	 (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x)    (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
 
 int	getentropy(void *buf, size_t len);
 
diff --git a/src/getentropy_solaris.c b/src/getentropy_solaris.c
index f0fcdcf..05d3e23 100644
--- a/src/getentropy_solaris.c
+++ b/src/getentropy_solaris.c
@@ -62,9 +62,9 @@
 			HD(b); \
 	} while (0)
 
-#define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
-#define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
-#define HF(x)    (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
+#define HR(x, l) (SHA512_Update(&ctx, (uint8_t *)(x), (l)))
+#define HD(x)	 (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (x)))
+#define HF(x)    (SHA512_Update(&ctx, (uint8_t *)&(x), sizeof (void*)))
 
 int	getentropy(void *buf, size_t len);
 
@@ -249,7 +249,7 @@ getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
 {
 	SHA512_CTX *ctx = data;
 
-	SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
+	SHA512_Update(ctx, (uint8_t *)&info->dlpi_addr, sizeof (info->dlpi_addr));
 	return (0);
 }
 
diff --git a/src/hash/.gitignore b/src/hash/.gitignore
deleted file mode 100644
index bfdcb6a..0000000
--- a/src/hash/.gitignore
+++ /dev/null
@@ -1 +0,0 @@
-md5hl.c
diff --git a/src/hash/helper.c b/src/hash/helper.c
deleted file mode 100644
index 352f978..0000000
--- a/src/hash/helper.c
+++ /dev/null
@@ -1,105 +0,0 @@
-/**	$MirOS: src/lib/libc/hash/helper.c,v 1.5 2007/05/07 15:21:18 tg Exp $ */
-/*	$OpenBSD: helper.c,v 1.8 2005/08/08 08:05:35 espie Exp $	*/
-
-/*
- * ----------------------------------------------------------------------------
- * "THE BEER-WARE LICENSE" (Revision 42):
- * <phk@login.dkuug.dk> wrote this file.  As long as you retain this notice you
- * can do whatever you want with this stuff. If we meet some day, and you think
- * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
- * ----------------------------------------------------------------------------
- */
-
-#include <sys/param.h>
-#include <sys/stat.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <unistd.h>
-#ifdef _WIN32
-#include <io.h>
-#endif
-
-#include <hashinc>
-
-/* ARGSUSED */
-char *
-HASHEnd(HASH_CTX *ctx, char *buf)
-{
-	int i;
-	uint8_t digest[HASH_DIGEST_LENGTH];
-#ifdef HASH_DIGEST_UPPERCASE
-	static const char hex[] = "0123456789ABCDEF";
-#else
-	static const char hex[] = "0123456789abcdef";
-#endif
-
-	if (buf == NULL && (buf = malloc(HASH_DIGEST_STRING_LENGTH)) == NULL)
-		return (NULL);
-
-	HASHFinal(digest, ctx);
-	for (i = 0; i < HASH_DIGEST_LENGTH; i++) {
-		buf[i + i] = hex[digest[i] >> 4];
-		buf[i + i + 1] = hex[digest[i] & 0x0f];
-	}
-	buf[i + i] = '\0';
-	memset(digest, 0, sizeof(digest));
-	return (buf);
-}
-
-char *
-HASHFileChunk(const char *filename, char *buf, off_t off, off_t len)
-{
-	struct stat sb;
-	unsigned char buffer[BUFSIZ];
-	HASH_CTX ctx;
-	int fd, save_errno;
-	ssize_t nr;
-
-	HASHInit(&ctx);
-
-	if ((fd = open(filename, O_RDONLY)) < 0)
-		return (NULL);
-	if (len == 0) {
-		if (fstat(fd, &sb) == -1) {
-			close(fd);
-			return (NULL);
-		}
-		len = sb.st_size;
-	}
-	if ((len < 0) || (off > 0 && lseek(fd, off, SEEK_SET) < 0)) {
-		close(fd);
-		return (NULL);
-	}
-
-	while ((nr = read(fd, buffer,
-	    (size_t)(len ? MIN(BUFSIZ, len) : BUFSIZ))) > 0) {
-		HASHUpdate(&ctx, buffer, (size_t)nr);
-		if (len > 0 && (len -= nr) == 0)
-			break;
-	}
-
-	save_errno = errno;
-	close(fd);
-	errno = save_errno;
-	return (nr < 0 ? NULL : HASHEnd(&ctx, buf));
-}
-
-char *
-HASHFile(const char *filename, char *buf)
-{
-	return (HASHFileChunk(filename, buf, (off_t)0, (off_t)0));
-}
-
-char *
-HASHData(const unsigned char *data, size_t len, char *buf)
-{
-	HASH_CTX ctx;
-
-	HASHInit(&ctx);
-	HASHUpdate(&ctx, data, len);
-	return (HASHEnd(&ctx, buf));
-}
diff --git a/src/hash/md5.c b/src/hash/md5.c
deleted file mode 100644
index 7e9672e..0000000
--- a/src/hash/md5.c
+++ /dev/null
@@ -1,248 +0,0 @@
-/*	$OpenBSD: md5.c,v 1.8 2005/08/08 08:05:35 espie Exp $	*/
-
-/*
- * This code implements the MD5 message-digest algorithm.
- * The algorithm is due to Ron Rivest.	This code was
- * written by Colin Plumb in 1993, no copyright is claimed.
- * This code is in the public domain; do with it what you wish.
- *
- * Equivalent code is available from RSA Data Security, Inc.
- * This code has been tested against that, and is equivalent,
- * except that you don't need to include two pages of legalese
- * with every copy.
- *
- * To compute the message digest of a chunk of bytes, declare an
- * MD5Context structure, pass it to MD5Init, call MD5Update as
- * needed on buffers full of bytes, and then call MD5Final, which
- * will fill a supplied 16-byte array with the digest.
- */
-
-#include <sys/types.h>
-#include <string.h>
-#include <md5.h>
-
-#define PUT_64BIT_LE(cp, value) do {					\
-	(cp)[7] = (value) >> 56;					\
-	(cp)[6] = (value) >> 48;					\
-	(cp)[5] = (value) >> 40;					\
-	(cp)[4] = (value) >> 32;					\
-	(cp)[3] = (value) >> 24;					\
-	(cp)[2] = (value) >> 16;					\
-	(cp)[1] = (value) >> 8;						\
-	(cp)[0] = (value); } while (0)
-
-#define PUT_32BIT_LE(cp, value) do {					\
-	(cp)[3] = (value) >> 24;					\
-	(cp)[2] = (value) >> 16;					\
-	(cp)[1] = (value) >> 8;						\
-	(cp)[0] = (value); } while (0)
-
-static uint8_t PADDING[MD5_BLOCK_LENGTH] = {
-	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-
-/*
- * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
- * initialization constants.
- */
-void
-MD5Init(MD5_CTX *ctx)
-{
-	ctx->count = 0;
-	ctx->state[0] = 0x67452301;
-	ctx->state[1] = 0xefcdab89;
-	ctx->state[2] = 0x98badcfe;
-	ctx->state[3] = 0x10325476;
-}
-
-/*
- * Update context to reflect the concatenation of another buffer full
- * of bytes.
- */
-void
-MD5Update(MD5_CTX *ctx, const unsigned char *input, size_t len)
-{
-	size_t have, need;
-
-	/* Check how many bytes we already have and how many more we need. */
-	have = (size_t)((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
-	need = MD5_BLOCK_LENGTH - have;
-
-	/* Update bitcount */
-	ctx->count += (uint64_t)len << 3;
-
-	if (len >= need) {
-		if (have != 0) {
-			memcpy(ctx->buffer + have, input, need);
-			MD5Transform(ctx->state, ctx->buffer);
-			input += need;
-			len -= need;
-			have = 0;
-		}
-
-		/* Process data in MD5_BLOCK_LENGTH-byte chunks. */
-		while (len >= MD5_BLOCK_LENGTH) {
-			MD5Transform(ctx->state, input);
-			input += MD5_BLOCK_LENGTH;
-			len -= MD5_BLOCK_LENGTH;
-		}
-	}
-
-	/* Handle any remaining bytes of data. */
-	if (len != 0)
-		memcpy(ctx->buffer + have, input, len);
-}
-
-/*
- * Pad pad to 64-byte boundary with the bit pattern
- * 1 0* (64-bit count of bits processed, MSB-first)
- */
-void
-MD5Pad(MD5_CTX *ctx)
-{
-	uint8_t count[8];
-	size_t padlen;
-
-	/* Convert count to 8 bytes in little endian order. */
-	PUT_64BIT_LE(count, ctx->count);
-
-	/* Pad out to 56 mod 64. */
-	padlen = MD5_BLOCK_LENGTH -
-	    ((ctx->count >> 3) & (MD5_BLOCK_LENGTH - 1));
-	if (padlen < 1 + 8)
-		padlen += MD5_BLOCK_LENGTH;
-	MD5Update(ctx, PADDING, padlen - 8);		/* padlen - 8 <= 64 */
-	MD5Update(ctx, count, 8);
-}
-
-/*
- * Final wrapup--call MD5Pad, fill in digest and zero out ctx.
- */
-void
-MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx)
-{
-	int i;
-
-	MD5Pad(ctx);
-	if (digest != NULL) {
-		for (i = 0; i < 4; i++)
-			PUT_32BIT_LE(digest + i * 4, ctx->state[i]);
-		memset(ctx, 0, sizeof(*ctx));
-	}
-}
-
-
-/* The four core functions - F1 is optimized somewhat */
-
-/* #define F1(x, y, z) (x & y | ~x & z) */
-#define F1(x, y, z) (z ^ (x & (y ^ z)))
-#define F2(x, y, z) F1(z, x, y)
-#define F3(x, y, z) (x ^ y ^ z)
-#define F4(x, y, z) (y ^ (x | ~z))
-
-/* This is the central step in the MD5 algorithm. */
-#define MD5STEP(f, w, x, y, z, data, s) \
-	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
-
-/*
- * The core of the MD5 algorithm, this alters an existing MD5 hash to
- * reflect the addition of 16 longwords of new data.  MD5Update blocks
- * the data and converts bytes into longwords for this routine.
- */
-void
-MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
-{
-	uint32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4];
-
-#if BYTE_ORDER == LITTLE_ENDIAN
-	memcpy(in, block, sizeof(in));
-#else
-	for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) {
-		in[a] = (uint32_t)(
-		    (uint32_t)(block[a * 4 + 0]) |
-		    (uint32_t)(block[a * 4 + 1]) <<  8 |
-		    (uint32_t)(block[a * 4 + 2]) << 16 |
-		    (uint32_t)(block[a * 4 + 3]) << 24);
-	}
-#endif
-
-	a = state[0];
-	b = state[1];
-	c = state[2];
-	d = state[3];
-
-	MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478,  7);
-	MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
-	MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
-	MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
-	MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf,  7);
-	MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
-	MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
-	MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
-	MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8,  7);
-	MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
-	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
-	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
-	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122,  7);
-	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
-	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
-	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
-
-	MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562,  5);
-	MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340,  9);
-	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
-	MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
-	MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d,  5);
-	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453,  9);
-	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
-	MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
-	MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6,  5);
-	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6,  9);
-	MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
-	MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
-	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905,  5);
-	MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8,  9);
-	MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
-	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
-
-	MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942,  4);
-	MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
-	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
-	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
-	MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44,  4);
-	MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
-	MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
-	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
-	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6,  4);
-	MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
-	MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
-	MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
-	MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039,  4);
-	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
-	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
-	MD5STEP(F3, b, c, d, a, in[2 ] + 0xc4ac5665, 23);
-
-	MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244,  6);
-	MD5STEP(F4, d, a, b, c, in[7 ] + 0x432aff97, 10);
-	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
-	MD5STEP(F4, b, c, d, a, in[5 ] + 0xfc93a039, 21);
-	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3,  6);
-	MD5STEP(F4, d, a, b, c, in[3 ] + 0x8f0ccc92, 10);
-	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
-	MD5STEP(F4, b, c, d, a, in[1 ] + 0x85845dd1, 21);
-	MD5STEP(F4, a, b, c, d, in[8 ] + 0x6fa87e4f,  6);
-	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
-	MD5STEP(F4, c, d, a, b, in[6 ] + 0xa3014314, 15);
-	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
-	MD5STEP(F4, a, b, c, d, in[4 ] + 0xf7537e82,  6);
-	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
-	MD5STEP(F4, c, d, a, b, in[2 ] + 0x2ad7d2bb, 15);
-	MD5STEP(F4, b, c, d, a, in[9 ] + 0xeb86d391, 21);
-
-	state[0] += a;
-	state[1] += b;
-	state[2] += c;
-	state[3] += d;
-}
diff --git a/src/hash/sha512.h b/src/hash/sha512.h
deleted file mode 100644
index 4f368a1..0000000
--- a/src/hash/sha512.h
+++ /dev/null
@@ -1,53 +0,0 @@
-/*-
- * Copyright 2005 Colin Percival
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * $FreeBSD$
- */
-
-#ifndef _SHA512_H_
-#define _SHA512_H_
-
-#include <sys/types.h>
-
-#define SHA512_DIGEST_LENGTH		64
-
-typedef struct SHA512Context {
-	uint64_t state[8];
-	uint64_t count[2];
-	unsigned char buf[128];
-} SHA512_CTX;
-
-__BEGIN_DECLS
-
-void	SHA512_Init(SHA512_CTX *);
-void	SHA512_Update(SHA512_CTX *, const void *, size_t);
-void	SHA512_Final(unsigned char [64], SHA512_CTX *);
-char   *SHA512_End(SHA512_CTX *, char *);
-char   *SHA512_File(const char *, char *);
-char   *SHA512_FileChunk(const char *, char *, off_t, off_t);
-char   *SHA512_Data(const void *, unsigned int, char *);
-__END_DECLS
-
-#endif /* !_SHA512_H_ */
diff --git a/src/hash/sha512c.c b/src/hash/sha512c.c
deleted file mode 100644
index b3c8d5e..0000000
--- a/src/hash/sha512c.c
+++ /dev/null
@@ -1,318 +0,0 @@
-/*-
- * Copyright 2005 Colin Percival
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#include <sys/endian.h>
-#include <sys/types.h>
-
-#include <string.h>
-
-#include "sha512.h"
-
-#if BYTE_ORDER == BIG_ENDIAN
-
-/* Copy a vector of big-endian uint64_t into a vector of bytes */
-#define be64enc_vect(dst, src, len)	\
-	memcpy((void *)dst, (const void *)src, (size_t)len)
-
-/* Copy a vector of bytes into a vector of big-endian uint64_t */
-#define be64dec_vect(dst, src, len)	\
-	memcpy((void *)dst, (const void *)src, (size_t)len)
-
-#else /* BYTE_ORDER != BIG_ENDIAN */
-
-/*
- * Encode a length len/4 vector of (uint64_t) into a length len vector of
- * (unsigned char) in big-endian form.  Assumes len is a multiple of 8.
- */
-static void
-be64enc_vect(unsigned char *dst, const uint64_t *src, size_t len)
-{
-	size_t i;
-
-	for (i = 0; i < len / 8; i++)
-		be64enc(dst + i * 8, src[i]);
-}
-
-/*
- * Decode a big-endian length len vector of (unsigned char) into a length
- * len/4 vector of (uint64_t).  Assumes len is a multiple of 8.
- */
-static void
-be64dec_vect(uint64_t *dst, const unsigned char *src, size_t len)
-{
-	size_t i;
-
-	for (i = 0; i < len / 8; i++)
-		dst[i] = be64dec(src + i * 8);
-}
-
-#endif /* BYTE_ORDER != BIG_ENDIAN */
-
-/* Elementary functions used by SHA512 */
-#define Ch(x, y, z)	((x & (y ^ z)) ^ z)
-#define Maj(x, y, z)	((x & (y | z)) | (y & z))
-#define SHR(x, n)	(x >> n)
-#define ROTR(x, n)	((x >> n) | (x << (64 - n)))
-#define S0(x)		(ROTR(x, 28) ^ ROTR(x, 34) ^ ROTR(x, 39))
-#define S1(x)		(ROTR(x, 14) ^ ROTR(x, 18) ^ ROTR(x, 41))
-#define s0(x)		(ROTR(x, 1) ^ ROTR(x, 8) ^ SHR(x, 7))
-#define s1(x)		(ROTR(x, 19) ^ ROTR(x, 61) ^ SHR(x, 6))
-
-/* SHA512 round function */
-#define RND(a, b, c, d, e, f, g, h, k)			\
-	t0 = h + S1(e) + Ch(e, f, g) + k;		\
-	t1 = S0(a) + Maj(a, b, c);			\
-	d += t0;					\
-	h  = t0 + t1;
-
-/* Adjusted round function for rotating state */
-#define RNDr(S, W, i, k)			\
-	RND(S[(80 - i) % 8], S[(81 - i) % 8],	\
-	    S[(82 - i) % 8], S[(83 - i) % 8],	\
-	    S[(84 - i) % 8], S[(85 - i) % 8],	\
-	    S[(86 - i) % 8], S[(87 - i) % 8],	\
-	    W[i] + k)
-
-/*
- * SHA512 block compression function.  The 512-bit state is transformed via
- * the 512-bit input block to produce a new state.
- */
-static void
-SHA512_Transform(uint64_t * state, const unsigned char block[128])
-{
-	uint64_t W[80];
-	uint64_t S[8];
-	uint64_t t0, t1;
-	int i;
-
-	/* 1. Prepare message schedule W. */
-	be64dec_vect(W, block, 128);
-	for (i = 16; i < 80; i++)
-		W[i] = s1(W[i - 2]) + W[i - 7] + s0(W[i - 15]) + W[i - 16];
-
-	/* 2. Initialize working variables. */
-	memcpy(S, state, 64);
-
-	/* 3. Mix. */
-	RNDr(S, W, 0, 0x428a2f98d728ae22ULL);
-	RNDr(S, W, 1, 0x7137449123ef65cdULL);
-	RNDr(S, W, 2, 0xb5c0fbcfec4d3b2fULL);
-	RNDr(S, W, 3, 0xe9b5dba58189dbbcULL);
-	RNDr(S, W, 4, 0x3956c25bf348b538ULL);
-	RNDr(S, W, 5, 0x59f111f1b605d019ULL);
-	RNDr(S, W, 6, 0x923f82a4af194f9bULL);
-	RNDr(S, W, 7, 0xab1c5ed5da6d8118ULL);
-	RNDr(S, W, 8, 0xd807aa98a3030242ULL);
-	RNDr(S, W, 9, 0x12835b0145706fbeULL);
-	RNDr(S, W, 10, 0x243185be4ee4b28cULL);
-	RNDr(S, W, 11, 0x550c7dc3d5ffb4e2ULL);
-	RNDr(S, W, 12, 0x72be5d74f27b896fULL);
-	RNDr(S, W, 13, 0x80deb1fe3b1696b1ULL);
-	RNDr(S, W, 14, 0x9bdc06a725c71235ULL);
-	RNDr(S, W, 15, 0xc19bf174cf692694ULL);
-	RNDr(S, W, 16, 0xe49b69c19ef14ad2ULL);
-	RNDr(S, W, 17, 0xefbe4786384f25e3ULL);
-	RNDr(S, W, 18, 0x0fc19dc68b8cd5b5ULL);
-	RNDr(S, W, 19, 0x240ca1cc77ac9c65ULL);
-	RNDr(S, W, 20, 0x2de92c6f592b0275ULL);
-	RNDr(S, W, 21, 0x4a7484aa6ea6e483ULL);
-	RNDr(S, W, 22, 0x5cb0a9dcbd41fbd4ULL);
-	RNDr(S, W, 23, 0x76f988da831153b5ULL);
-	RNDr(S, W, 24, 0x983e5152ee66dfabULL);
-	RNDr(S, W, 25, 0xa831c66d2db43210ULL);
-	RNDr(S, W, 26, 0xb00327c898fb213fULL);
-	RNDr(S, W, 27, 0xbf597fc7beef0ee4ULL);
-	RNDr(S, W, 28, 0xc6e00bf33da88fc2ULL);
-	RNDr(S, W, 29, 0xd5a79147930aa725ULL);
-	RNDr(S, W, 30, 0x06ca6351e003826fULL);
-	RNDr(S, W, 31, 0x142929670a0e6e70ULL);
-	RNDr(S, W, 32, 0x27b70a8546d22ffcULL);
-	RNDr(S, W, 33, 0x2e1b21385c26c926ULL);
-	RNDr(S, W, 34, 0x4d2c6dfc5ac42aedULL);
-	RNDr(S, W, 35, 0x53380d139d95b3dfULL);
-	RNDr(S, W, 36, 0x650a73548baf63deULL);
-	RNDr(S, W, 37, 0x766a0abb3c77b2a8ULL);
-	RNDr(S, W, 38, 0x81c2c92e47edaee6ULL);
-	RNDr(S, W, 39, 0x92722c851482353bULL);
-	RNDr(S, W, 40, 0xa2bfe8a14cf10364ULL);
-	RNDr(S, W, 41, 0xa81a664bbc423001ULL);
-	RNDr(S, W, 42, 0xc24b8b70d0f89791ULL);
-	RNDr(S, W, 43, 0xc76c51a30654be30ULL);
-	RNDr(S, W, 44, 0xd192e819d6ef5218ULL);
-	RNDr(S, W, 45, 0xd69906245565a910ULL);
-	RNDr(S, W, 46, 0xf40e35855771202aULL);
-	RNDr(S, W, 47, 0x106aa07032bbd1b8ULL);
-	RNDr(S, W, 48, 0x19a4c116b8d2d0c8ULL);
-	RNDr(S, W, 49, 0x1e376c085141ab53ULL);
-	RNDr(S, W, 50, 0x2748774cdf8eeb99ULL);
-	RNDr(S, W, 51, 0x34b0bcb5e19b48a8ULL);
-	RNDr(S, W, 52, 0x391c0cb3c5c95a63ULL);
-	RNDr(S, W, 53, 0x4ed8aa4ae3418acbULL);
-	RNDr(S, W, 54, 0x5b9cca4f7763e373ULL);
-	RNDr(S, W, 55, 0x682e6ff3d6b2b8a3ULL);
-	RNDr(S, W, 56, 0x748f82ee5defb2fcULL);
-	RNDr(S, W, 57, 0x78a5636f43172f60ULL);
-	RNDr(S, W, 58, 0x84c87814a1f0ab72ULL);
-	RNDr(S, W, 59, 0x8cc702081a6439ecULL);
-	RNDr(S, W, 60, 0x90befffa23631e28ULL);
-	RNDr(S, W, 61, 0xa4506cebde82bde9ULL);
-	RNDr(S, W, 62, 0xbef9a3f7b2c67915ULL);
-	RNDr(S, W, 63, 0xc67178f2e372532bULL);
-	RNDr(S, W, 64, 0xca273eceea26619cULL);
-	RNDr(S, W, 65, 0xd186b8c721c0c207ULL);
-	RNDr(S, W, 66, 0xeada7dd6cde0eb1eULL);
-	RNDr(S, W, 67, 0xf57d4f7fee6ed178ULL);
-	RNDr(S, W, 68, 0x06f067aa72176fbaULL);
-	RNDr(S, W, 69, 0x0a637dc5a2c898a6ULL);
-	RNDr(S, W, 70, 0x113f9804bef90daeULL);
-	RNDr(S, W, 71, 0x1b710b35131c471bULL);
-	RNDr(S, W, 72, 0x28db77f523047d84ULL);
-	RNDr(S, W, 73, 0x32caab7b40c72493ULL);
-	RNDr(S, W, 74, 0x3c9ebe0a15c9bebcULL);
-	RNDr(S, W, 75, 0x431d67c49c100d4cULL);
-	RNDr(S, W, 76, 0x4cc5d4becb3e42b6ULL);
-	RNDr(S, W, 77, 0x597f299cfc657e2aULL);
-	RNDr(S, W, 78, 0x5fcb6fab3ad6faecULL);
-	RNDr(S, W, 79, 0x6c44198c4a475817ULL);
-
-	/* 4. Mix local working variables into global state */
-	for (i = 0; i < 8; i++)
-		state[i] += S[i];
-}
-
-static unsigned char PAD[128] = {
-	0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
-};
-
-/* Add padding and terminating bit-count. */
-static void
-SHA512_Pad(SHA512_CTX * ctx)
-{
-	unsigned char len[16];
-	uint64_t r, plen;
-
-	/*
-	 * Convert length to a vector of bytes -- we do this now rather
-	 * than later because the length will change after we pad.
-	 */
-	be64enc_vect(len, ctx->count, 16);
-
-	/* Add 1--128 bytes so that the resulting length is 112 mod 128 */
-	r = (ctx->count[1] >> 3) & 0x7f;
-	plen = (r < 112) ? (112 - r) : (240 - r);
-	SHA512_Update(ctx, PAD, (size_t)plen);
-
-	/* Add the terminating bit-count */
-	SHA512_Update(ctx, len, 16);
-}
-
-/* SHA-512 initialization.  Begins a SHA-512 operation. */
-void
-SHA512_Init(SHA512_CTX * ctx)
-{
-
-	/* Zero bits processed so far */
-	ctx->count[0] = ctx->count[1] = 0;
-
-	/* Magic initialization constants */
-	ctx->state[0] = 0x6a09e667f3bcc908ULL;
-	ctx->state[1] = 0xbb67ae8584caa73bULL;
-	ctx->state[2] = 0x3c6ef372fe94f82bULL;
-	ctx->state[3] = 0xa54ff53a5f1d36f1ULL;
-	ctx->state[4] = 0x510e527fade682d1ULL;
-	ctx->state[5] = 0x9b05688c2b3e6c1fULL;
-	ctx->state[6] = 0x1f83d9abfb41bd6bULL;
-	ctx->state[7] = 0x5be0cd19137e2179ULL;
-}
-
-/* Add bytes into the hash */
-void
-SHA512_Update(SHA512_CTX * ctx, const void *in, size_t len)
-{
-	uint64_t bitlen[2];
-	uint64_t r;
-	const unsigned char *src = in;
-
-	/* Number of bytes left in the buffer from previous updates */
-	r = (ctx->count[1] >> 3) & 0x7f;
-
-	/* Convert the length into a number of bits */
-	bitlen[1] = ((uint64_t)len) << 3;
-	bitlen[0] = ((uint64_t)len) >> 61;
-
-	/* Update number of bits */
-	if ((ctx->count[1] += bitlen[1]) < bitlen[1])
-		ctx->count[0]++;
-	ctx->count[0] += bitlen[0];
-
-	/* Handle the case where we don't need to perform any transforms */
-	if (len < 128 - r) {
-		memcpy(&ctx->buf[r], src, len);
-		return;
-	}
-
-	/* Finish the current block */
-	memcpy(&ctx->buf[r], src, 128 - r);
-	SHA512_Transform(ctx->state, ctx->buf);
-	src += 128 - r;
-	len -= 128 - r;
-
-	/* Perform complete blocks */
-	while (len >= 128) {
-		SHA512_Transform(ctx->state, src);
-		src += 128;
-		len -= 128;
-	}
-
-	/* Copy left over data into buffer */
-	memcpy(ctx->buf, src, len);
-}
-
-/*
- * SHA-512 finalization.  Pads the input data, exports the hash value,
- * and clears the context state.
- */
-void
-SHA512_Final(unsigned char digest[64], SHA512_CTX * ctx)
-{
-
-	/* Add padding */
-	SHA512_Pad(ctx);
-
-	/* Write the hash */
-	be64enc_vect(digest, ctx->state, 64);
-
-	/* Clear the context state */
-	memset((void *)ctx, 0, sizeof(*ctx));
-}
diff --git a/src/libbsd.map b/src/libbsd.map
index 34e709b..fb2888f 100644
--- a/src/libbsd.map
+++ b/src/libbsd.map
@@ -35,6 +35,8 @@ LIBBSD_0.0 {
     strunvis;
     strunvisx;
 
+    /* The following functions are provided via the system libc or libmd
+     * implementations. */
     MD5Init;
     MD5Update;
     MD5Pad;
diff --git a/src/md5.c b/src/md5.c
new file mode 100644
index 0000000..b74ce7d
--- /dev/null
+++ b/src/md5.c
@@ -0,0 +1,91 @@
+/*
+ * Copyright © 2021 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <md5.h>
+#include "local-link.h"
+
+void
+bsd_MD5Init(MD5_CTX *context)
+{
+	MD5Init(context);
+}
+libbsd_symver_variant(MD5Init, bsd_MD5Init, LIBBSD_0.0);
+
+void
+bsd_MD5Update(MD5_CTX *context, const uint8_t *data, size_t len)
+{
+	MD5Update(context, data, len);
+}
+libbsd_symver_variant(MD5Update, bsd_MD5Update, LIBBSD_0.0);
+
+void
+bsd_MD5Pad(MD5_CTX *context)
+{
+	MD5Pad(context);
+}
+libbsd_symver_variant(MD5Pad, bsd_MD5Pad, LIBBSD_0.0);
+
+void
+bsd_MD5Final(uint8_t digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
+{
+	MD5Final(digest, context);
+}
+libbsd_symver_variant(MD5Final, bsd_MD5Final, LIBBSD_0.0);
+
+void
+bsd_MD5Transform(uint32_t state[4], const uint8_t block[MD5_BLOCK_LENGTH])
+{
+	MD5Transform(state, block);
+}
+libbsd_symver_variant(MD5Transform, bsd_MD5Transform, LIBBSD_0.0);
+
+char *
+bsd_MD5End(MD5_CTX *context, char *buf)
+{
+	return MD5End(context, buf);
+}
+libbsd_symver_variant(MD5End, bsd_MD5End, LIBBSD_0.0);
+
+char *
+bsd_MD5File(const char *filename, char *buf)
+{
+	return MD5File(filename, buf);
+}
+libbsd_symver_variant(MD5File, bsd_MD5File, LIBBSD_0.0);
+
+char *
+bsd_MD5FileChunk(const char *filename, char *buf, off_t offset, off_t length)
+{
+	return MD5FileChunk(filename, buf, offset, length);
+}
+libbsd_symver_variant(MD5FileChunk, bsd_MD5FileChunk, LIBBSD_0.0);
+
+char *
+bsd_MD5Data(const uint8_t *data, size_t len, char *buf)
+{
+	return MD5Data(data, len, buf);
+}
+libbsd_symver_variant(MD5Data, bsd_MD5Data, LIBBSD_0.0);
diff --git a/test/Makefile.am b/test/Makefile.am
index 90fe384..a1c8e68 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -52,6 +52,8 @@ check_PROGRAMS = \
 	vis-openbsd \
 	$(nil)
 
+md5_LDADD = $(LDADD) $(MD_LIBS)
+
 if HAVE_LIBTESTU01
 arc4random_LDADD = $(LDADD) $(TESTU01_LIBS)