xref: /freebsd/lib/libmd/mdX.3 (revision fd3ced15070885c818b74a44a0fbe45ed8687f44)
1c9502b53SPoul-Henning Kamp.\"
2c9502b53SPoul-Henning Kamp.\" ----------------------------------------------------------------------------
3c9502b53SPoul-Henning Kamp.\" "THE BEER-WARE LICENSE" (Revision 42):
442c159feSPoul-Henning Kamp.\" <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
5c9502b53SPoul-Henning Kamp.\" can do whatever you want with this stuff. If we meet some day, and you think
6c9502b53SPoul-Henning Kamp.\" this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
7c9502b53SPoul-Henning Kamp.\" ----------------------------------------------------------------------------
8c9502b53SPoul-Henning Kamp.\"
9*fd3ced15SKyle Evans.Dd March 8, 2022
10c9502b53SPoul-Henning Kamp.Dt MDX 3
11a307d598SRuslan Ermilov.Os
12c9502b53SPoul-Henning Kamp.Sh NAME
13c9502b53SPoul-Henning Kamp.Nm MDXInit ,
14c9502b53SPoul-Henning Kamp.Nm MDXUpdate ,
15c6bcf724SPoul-Henning Kamp.Nm MDXPad ,
16c9502b53SPoul-Henning Kamp.Nm MDXFinal ,
17c9502b53SPoul-Henning Kamp.Nm MDXEnd ,
18*fd3ced15SKyle Evans.Nm MDXFd ,
19*fd3ced15SKyle Evans.Nm MDXFdChunk ,
20c9502b53SPoul-Henning Kamp.Nm MDXFile ,
218a24546cSPoul-Henning Kamp.Nm MDXFileChunk ,
22c211e812SMike Pritchard.Nm MDXData
2304422105SGarrett Wollman.Nd calculate the RSA Data Security, Inc., ``MDX'' message digest
249452ceebSAlexey Zelkin.Sh LIBRARY
259452ceebSAlexey Zelkin.Lb libmd
26c9502b53SPoul-Henning Kamp.Sh SYNOPSIS
2732eef9aeSRuslan Ermilov.In sys/types.h
2832eef9aeSRuslan Ermilov.In mdX.h
29c9502b53SPoul-Henning Kamp.Ft void
30c9502b53SPoul-Henning Kamp.Fn MDXInit "MDX_CTX *context"
31c9502b53SPoul-Henning Kamp.Ft void
3225a14196SPoul-Henning Kamp.Fn MDXUpdate "MDX_CTX *context" "const void *data" "unsigned int len"
33c9502b53SPoul-Henning Kamp.Ft void
34c6bcf724SPoul-Henning Kamp.Fn MDXPad "MDX_CTX *context"
35c6bcf724SPoul-Henning Kamp.Ft void
36c9502b53SPoul-Henning Kamp.Fn MDXFinal "unsigned char digest[16]" "MDX_CTX *context"
37c9502b53SPoul-Henning Kamp.Ft "char *"
3805f65b29SPoul-Henning Kamp.Fn MDXEnd "MDX_CTX *context" "char *buf"
39c9502b53SPoul-Henning Kamp.Ft "char *"
40*fd3ced15SKyle Evans.Fn MDXFd "int fd" "char *buf"
41*fd3ced15SKyle Evans.Ft "char *"
42*fd3ced15SKyle Evans.Fn MDXFdChunk "int fd" "char *buf" "off_t offset" "off_t length"
43*fd3ced15SKyle Evans.Ft "char *"
44f7787644SJoerg Wunsch.Fn MDXFile "const char *filename" "char *buf"
45c9502b53SPoul-Henning Kamp.Ft "char *"
468a24546cSPoul-Henning Kamp.Fn MDXFileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
478a24546cSPoul-Henning Kamp.Ft "char *"
4825a14196SPoul-Henning Kamp.Fn MDXData "const void *data" "unsigned int len" "char *buf"
49c9502b53SPoul-Henning Kamp.Sh DESCRIPTION
50c9502b53SPoul-Henning KampThe MDX functions calculate a 128-bit cryptographic checksum (digest)
51dc2af37dSHiten Pandyafor any number of input bytes.
52dc2af37dSHiten PandyaA cryptographic checksum is a one-way
53c9502b53SPoul-Henning Kamphash-function, that is, you cannot find (except by exhaustive search)
54dc2af37dSHiten Pandyathe input corresponding to a particular output.
55dc2af37dSHiten PandyaThis net result is a
56dc2af37dSHiten Pandya.Dq fingerprint
570227791bSRuslan Ermilovof the input-data, which does not disclose the actual input.
5804422105SGarrett Wollman.Pp
599b9c3018SDavid E. O'BrienMD4 is the fastest and MD5 is somewhat slower.
6048ee93d6SGarrett WollmanMD4 has now been broken; it should only be used where necessary for
6148ee93d6SGarrett Wollmanbackward compatibility.
6248ee93d6SGarrett WollmanMD5 has not yet (1999-02-11) been broken, but sufficient attacks have been
63dc2af37dSHiten Pandyamade that its security is in some doubt.
64dc2af37dSHiten PandyaThe attacks on both MD4 and MD5
65dc2af37dSHiten Pandyaare both in the nature of finding
66dc2af37dSHiten Pandya.Dq collisions
67dc2af37dSHiten Pandya\[en]
68dc2af37dSHiten Pandyathat is, multiple
6948ee93d6SGarrett Wollmaninputs which hash to the same value; it is still unlikely for an attacker
7048ee93d6SGarrett Wollmanto be able to determine the exact original input given a hash value.
7104422105SGarrett Wollman.Pp
72c9502b53SPoul-Henning KampThe
73c9502b53SPoul-Henning Kamp.Fn MDXInit ,
7404422105SGarrett Wollman.Fn MDXUpdate ,
75c9502b53SPoul-Henning Kampand
76c9502b53SPoul-Henning Kamp.Fn MDXFinal
77dc2af37dSHiten Pandyafunctions are the core functions.
78dc2af37dSHiten PandyaAllocate an
79dc2af37dSHiten Pandya.Vt MDX_CTX ,
80dc2af37dSHiten Pandyainitialize it with
8104422105SGarrett Wollman.Fn MDXInit ,
82c9502b53SPoul-Henning Kamprun over the data with
8304422105SGarrett Wollman.Fn MDXUpdate ,
84c9502b53SPoul-Henning Kampand finally extract the result using
8566bdf50fSXin LI.Fn MDXFinal ,
8666bdf50fSXin LIwhich will also erase the
8766bdf50fSXin LI.Vt MDX_CTX .
8804422105SGarrett Wollman.Pp
890552350eSPhilippe CharnierThe
90c6bcf724SPoul-Henning Kamp.Fn MDXPad
910552350eSPhilippe Charnierfunction can be used to pad message data in same way
92c6bcf724SPoul-Henning Kampas done by
93c6bcf724SPoul-Henning Kamp.Fn MDXFinal
94c6bcf724SPoul-Henning Kampwithout terminating calculation.
95c6bcf724SPoul-Henning Kamp.Pp
960552350eSPhilippe CharnierThe
97c9502b53SPoul-Henning Kamp.Fn MDXEnd
980552350eSPhilippe Charnierfunction is a wrapper for
9904422105SGarrett Wollman.Fn MDXFinal
10004422105SGarrett Wollmanwhich converts the return value to a 33-character
10104422105SGarrett Wollman(including the terminating '\e0')
1022768d705SDag-Erling SmørgravASCII string which represents the 128 bits in hexadecimal.
10304422105SGarrett Wollman.Pp
1040552350eSPhilippe CharnierThe
105c9502b53SPoul-Henning Kamp.Fn MDXFile
1060552350eSPhilippe Charnierfunction calculates the digest of a file, and uses
107228a1015SJohn Polstra.Fn MDXEnd
10805f65b29SPoul-Henning Kampto return the result.
10904422105SGarrett WollmanIf the file cannot be opened, a null pointer is returned.
1100552350eSPhilippe CharnierThe
1118a24546cSPoul-Henning Kamp.Fn MDXFileChunk
1120552350eSPhilippe Charnierfunction is similar to
1138a24546cSPoul-Henning Kamp.Fn MDXFile ,
1148a24546cSPoul-Henning Kampbut it only calculates the digest over a byte-range of the file specified,
1158a24546cSPoul-Henning Kampstarting at
1167e5d6de0SRuslan Ermilov.Fa offset
1178a24546cSPoul-Henning Kampand spanning
1187e5d6de0SRuslan Ermilov.Fa length
1198a24546cSPoul-Henning Kampbytes.
1208a24546cSPoul-Henning KampIf the
1217e5d6de0SRuslan Ermilov.Fa length
1228a24546cSPoul-Henning Kampparameter is specified as 0, or more than the length of the remaining part
1238a24546cSPoul-Henning Kampof the file,
1248a24546cSPoul-Henning Kamp.Fn MDXFileChunk
1258a24546cSPoul-Henning Kampcalculates the digest from
1267e5d6de0SRuslan Ermilov.Fa offset
1278a24546cSPoul-Henning Kampto the end of file.
1280552350eSPhilippe CharnierThe
129c9502b53SPoul-Henning Kamp.Fn MDXData
1300552350eSPhilippe Charnierfunction calculates the digest of a chunk of data in memory, and uses
131228a1015SJohn Polstra.Fn MDXEnd
13205f65b29SPoul-Henning Kampto return the result.
13304422105SGarrett Wollman.Pp
134*fd3ced15SKyle EvansThe
135*fd3ced15SKyle Evans.Fn MDXFd
136*fd3ced15SKyle Evansand
137*fd3ced15SKyle Evans.Fn MDXFdChunk
138*fd3ced15SKyle Evansfunctions are identical to their
139*fd3ced15SKyle Evans.Fn MDXFile
140*fd3ced15SKyle Evansand
141*fd3ced15SKyle Evans.Fn MDXFileChunk
142*fd3ced15SKyle Evanscounterparts, with the exception that the first argument is an
143*fd3ced15SKyle Evans.Fa fd
144*fd3ced15SKyle Evansinstead of a
145*fd3ced15SKyle Evans.Fa filename .
146*fd3ced15SKyle Evans.Pp
147c9502b53SPoul-Henning KampWhen using
148c9502b53SPoul-Henning Kamp.Fn MDXEnd ,
14904422105SGarrett Wollman.Fn MDXFile ,
15005f65b29SPoul-Henning Kampor
151c9502b53SPoul-Henning Kamp.Fn MDXData ,
15205f65b29SPoul-Henning Kampthe
1537e5d6de0SRuslan Ermilov.Fa buf
15404422105SGarrett Wollmanargument can be a null pointer, in which case the returned string
15504422105SGarrett Wollmanis allocated with
15605f65b29SPoul-Henning Kamp.Xr malloc 3
15705f65b29SPoul-Henning Kampand subsequently must be explicitly deallocated using
158c9502b53SPoul-Henning Kamp.Xr free 3
159c9502b53SPoul-Henning Kampafter use.
16005f65b29SPoul-Henning KampIf the
1617e5d6de0SRuslan Ermilov.Fa buf
16204422105SGarrett Wollmanargument is non-null it must point to at least 33 characters of buffer space.
163f26f4cf1SAllan Jude.Sh ERRORS
164f26f4cf1SAllan JudeThe
165f26f4cf1SAllan Jude.Fn MDXEnd
166f26f4cf1SAllan Judefunction called with a null buf argument may fail and return NULL if:
167f26f4cf1SAllan Jude.Bl -tag -width Er
168f26f4cf1SAllan Jude.It Bq Er ENOMEM
169f26f4cf1SAllan JudeInsufficient storage space is available.
170f26f4cf1SAllan Jude.El
171f26f4cf1SAllan Jude.Pp
172f26f4cf1SAllan JudeThe
173f26f4cf1SAllan Jude.Fn MDXFile
174f26f4cf1SAllan Judeand
175f26f4cf1SAllan Jude.Fn MDXFileChunk
176f26f4cf1SAllan Judemay return NULL when underlying
177f26f4cf1SAllan Jude.Xr open 2 ,
178f26f4cf1SAllan Jude.Xr fstat 2 ,
179f26f4cf1SAllan Jude.Xr lseek 2 ,
180f26f4cf1SAllan Judeor
18130f62691SJens Schweikhardt.Xr MDXEnd 3
182f26f4cf1SAllan Judefail.
183c9502b53SPoul-Henning Kamp.Sh SEE ALSO
184c9502b53SPoul-Henning Kamp.Xr md4 3 ,
185c177a86bSGarrett Wollman.Xr md5 3 ,
186b468a9ffSAllan Jude.Xr ripemd 3 ,
187b468a9ffSAllan Jude.Xr sha 3 ,
188b468a9ffSAllan Jude.Xr sha256 3 ,
189b468a9ffSAllan Jude.Xr sha512 3 ,
190b468a9ffSAllan Jude.Xr skein 3
19104422105SGarrett Wollman.Rs
19204422105SGarrett Wollman.%A R. Rivest
19304422105SGarrett Wollman.%T The MD4 Message-Digest Algorithm
19404422105SGarrett Wollman.%O RFC 1186
19504422105SGarrett Wollman.Re
19604422105SGarrett Wollman.Rs
19704422105SGarrett Wollman.%A R. Rivest
19804422105SGarrett Wollman.%T The MD5 Message-Digest Algorithm
19904422105SGarrett Wollman.%O RFC 1321
20004422105SGarrett Wollman.Re
20104422105SGarrett Wollman.Rs
20248ee93d6SGarrett Wollman.%A H. Dobbertin
20348ee93d6SGarrett Wollman.%T Alf Swindles Ann
20448ee93d6SGarrett Wollman.%J CryptoBytes
20548ee93d6SGarrett Wollman.%N 1(3):5
20648ee93d6SGarrett Wollman.%D 1995
20748ee93d6SGarrett Wollman.Re
20848ee93d6SGarrett Wollman.Rs
20948ee93d6SGarrett Wollman.%A MJ. B. Robshaw
21048ee93d6SGarrett Wollman.%T On Recent Results for MD2, MD4 and MD5
21148ee93d6SGarrett Wollman.%J RSA Laboratories Bulletin
21248ee93d6SGarrett Wollman.%N 4
21348ee93d6SGarrett Wollman.%D November 12, 1996
21404422105SGarrett Wollman.Re
21524a0682cSRuslan Ermilov.Sh HISTORY
21624a0682cSRuslan ErmilovThese functions appeared in
21724a0682cSRuslan Ermilov.Fx 2.0 .
218a5941fc2SPhilippe Charnier.Sh AUTHORS
21904422105SGarrett WollmanThe original MDX routines were developed by
2202768d705SDag-Erling SmørgravRSA Data Security, Inc., and published in the above references.
221a5941fc2SPhilippe CharnierThis code is derived directly from these implementations by
2222b7af31cSBaptiste Daroussin.An Poul-Henning Kamp Aq Mt phk@FreeBSD.org .
22304422105SGarrett Wollman.Pp
224c9502b53SPoul-Henning KampPhk ristede runen.
225c9502b53SPoul-Henning Kamp.Sh BUGS
2262768d705SDag-Erling SmørgravThe MD5 algorithm has been proven to be vulnerable to practical collision
22734841dd6SAllan Judeattacks and should not be relied upon to produce unique outputs,
22834841dd6SAllan Jude.Em nor should they be used as part of a cryptographic signature scheme.
229