mdX.3 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. .\"
  2. .\" ----------------------------------------------------------------------------
  3. .\" "THE BEER-WARE LICENSE" (Revision 42):
  4. .\" <[email protected]> wrote this file. As long as you retain this notice you
  5. .\" can do whatever you want with this stuff. If we meet some day, and you think
  6. .\" this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
  7. .\" ----------------------------------------------------------------------------
  8. .\"
  9. .\" $OpenBSD: mdX.3,v 1.11 2010/07/13 22:34:45 tedu Exp $
  10. .\"
  11. .Dd $Mdocdate: July 13 2010 $
  12. .Dt MDX 3
  13. .Os
  14. .Sh NAME
  15. .Nm MDXInit ,
  16. .Nm MDXUpdate ,
  17. .Nm MDXPad ,
  18. .Nm MDXFinal ,
  19. .Nm MDXTransform ,
  20. .Nm MDXEnd ,
  21. .Nm MDXFile ,
  22. .Nm MDXFileChunk ,
  23. .Nm MDXData
  24. .Nd calculate the RSA Data Security, Inc.,
  25. .Dq MDX
  26. message digest
  27. .Sh LIBRARY
  28. .Lb libmd
  29. .Sh SYNOPSIS
  30. .In sys/types.h
  31. .In mdX.h
  32. .Ft void
  33. .Fn MDXInit "MDX_CTX *context"
  34. .Ft void
  35. .Fn MDXUpdate "MDX_CTX *context" "const uint8_t *data" "size_t len"
  36. .Ft void
  37. .Fn MDXPad "MDX_CTX *context"
  38. .Ft void
  39. .Fn MDXFinal "uint8_t digest[MDX_DIGEST_LENGTH]" "MDX_CTX *context"
  40. .Ft void
  41. .Fn MDXTransform "uint32_t state[4]" "uint8_t block[MDX_BLOCK_LENGTH]"
  42. .Ft "char *"
  43. .Fn MDXEnd "MDX_CTX *context" "char *buf"
  44. .Ft "char *"
  45. .Fn MDXFile "const char *filename" "char *buf"
  46. .Ft "char *"
  47. .Fn MDXFileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
  48. .Ft "char *"
  49. .Fn MDXData "const uint8_t *data" "size_t len" "char *buf"
  50. .Sh DESCRIPTION
  51. The MDX functions calculate a 128-bit cryptographic checksum (digest)
  52. for any number of input bytes.
  53. A cryptographic checksum is a one-way
  54. hash-function, that is, you cannot find (except by exhaustive search)
  55. the input corresponding to a particular output.
  56. This net result is a
  57. .Dq fingerprint
  58. of the input-data, which doesn't disclose the actual input.
  59. .Pp
  60. MD2 is the slowest, MD4 is the fastest and MD5 is somewhere in the middle.
  61. MD2 can only be used for Privacy-Enhanced Mail.
  62. MD4 has been criticized for being too weak, so MD5 was developed in
  63. response as ``MD4 with safety-belts''.
  64. MD4 and MD5 have been broken; they should only be used where necessary for
  65. backward compatibility.
  66. The attacks on both MD4 and MD5
  67. are both in the nature of finding
  68. .Dq collisions
  69. \- that is, multiple
  70. inputs which hash to the same value; it is still unlikely for an attacker
  71. to be able to determine the exact original input given a hash value.
  72. .Pp
  73. The
  74. .Fn MDXInit ,
  75. .Fn MDXUpdate ,
  76. and
  77. .Fn MDXFinal
  78. functions are the core functions.
  79. Allocate an MDX_CTX, initialize it with
  80. .Fn MDXInit ,
  81. run over the data with
  82. .Fn MDXUpdate ,
  83. and finally extract the result using
  84. .Fn MDXFinal .
  85. .Pp
  86. The
  87. .Fn MDXPad
  88. function can be used to apply padding to the message digest as in
  89. .Fn MDXFinal ,
  90. but the current context can still be used with
  91. .Fn MDXUpdate .
  92. .Pp
  93. The
  94. .Fn MDXTransform
  95. function is used by
  96. .Fn MDXUpdate
  97. to hash 512-bit blocks and forms the core of the algorithm.
  98. Most programs should use the interface provided by
  99. .Fn MDXInit ,
  100. .Fn MDXUpdate
  101. and
  102. .Fn MDXFinal
  103. instead of calling
  104. .Fn MDXTransform
  105. directly.
  106. .Pp
  107. .Fn MDXEnd
  108. is a wrapper for
  109. .Fn MDXFinal
  110. which converts the return value to an MDX_DIGEST_STRING_LENGTH-character
  111. (including the terminating '\e0')
  112. .Tn ASCII
  113. string which represents the 128 bits in hexadecimal.
  114. .Pp
  115. .Fn MDXFile
  116. calculates the digest of a file, and uses
  117. .Fn MDXEnd
  118. to return the result.
  119. If the file cannot be opened, a null pointer is returned.
  120. .Pp
  121. .Fn MDXFileChunk
  122. behaves like
  123. .Fn MDXFile
  124. but calculates the digest only for that portion of the file starting at
  125. .Fa offset
  126. and continuing for
  127. .Fa length
  128. bytes or until end of file is reached, whichever comes first.
  129. A zero
  130. .Fa length
  131. can be specified to read until end of file.
  132. A negative
  133. .Fa length
  134. or
  135. .Fa offset
  136. will be ignored.
  137. .Fn MDXData
  138. calculates the digest of a chunk of data in memory, and uses
  139. .Fn MDXEnd
  140. to return the result.
  141. .Pp
  142. When using
  143. .Fn MDXEnd ,
  144. .Fn MDXFile ,
  145. .Fn MDXFileChunk ,
  146. or
  147. .Fn MDXData ,
  148. the
  149. .Ar buf
  150. argument can be a null pointer, in which case the returned string
  151. is allocated with
  152. .Xr malloc 3
  153. and subsequently must be explicitly deallocated using
  154. .Xr free 3
  155. after use.
  156. If the
  157. .Ar buf
  158. argument is non-null it must point to at least MDX_DIGEST_STRING_LENGTH
  159. characters of buffer space.
  160. .Sh SEE ALSO
  161. .Xr md2 3 ,
  162. .Xr md4 3 ,
  163. .Xr md5 3 ,
  164. .Xr rmd160 3 ,
  165. .Xr sha1 3 ,
  166. .Xr sha2 3
  167. .Rs
  168. .%A B. Kaliski
  169. .%T The MD2 Message-Digest Algorithm
  170. .%O RFC 1319
  171. .Re
  172. .Rs
  173. .%A R. Rivest
  174. .%T The MD4 Message-Digest Algorithm
  175. .%O RFC 1186
  176. .Re
  177. .Rs
  178. .%A R. Rivest
  179. .%T The MD5 Message-Digest Algorithm
  180. .%O RFC 1321
  181. .Re
  182. .Rs
  183. .%A RSA Laboratories
  184. .%T Frequently Asked Questions About today's Cryptography
  185. .%O \&<http://www.rsa.com/rsalabs/faq/>
  186. .Re
  187. .Rs
  188. .%A H. Dobbertin
  189. .%T Alf Swindles Ann
  190. .%J CryptoBytes
  191. .%N 1(3):5
  192. .%D 1995
  193. .Re
  194. .Rs
  195. .%A MJ. B. Robshaw
  196. .%T On Recent Results for MD4 and MD5
  197. .%J RSA Laboratories Bulletin
  198. .%N 4
  199. .%D November 12, 1996
  200. .Re
  201. .Rs
  202. .%A Hans Dobbertin
  203. .%T Cryptanalysis of MD5 Compress
  204. .Re
  205. .Sh HISTORY
  206. These functions appeared in
  207. .Ox 2.0
  208. and
  209. .Nx 1.3 .
  210. .Sh AUTHORS
  211. The original MDX routines were developed by
  212. .Tn RSA
  213. Data Security, Inc., and published in the above references.
  214. This code is derived from a public domain implementation written by Colin Plumb.
  215. .Pp
  216. The
  217. .Fn MDXEnd ,
  218. .Fn MDXFile ,
  219. .Fn MDXFileChunk ,
  220. and
  221. .Fn MDXData
  222. helper functions are derived from code written by Poul-Henning Kamp.
  223. .Sh BUGS
  224. Collisions have been found for the full versions of both MD4 and MD5.
  225. The use of
  226. .Xr sha2 3
  227. is recommended instead.