rmd160.3 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. .\" $OpenBSD: rmd160.3,v 1.30 2010/07/13 07:01:23 jmc Exp $
  2. .\"
  3. .\" Copyright (c) 1997, 2004 Todd C. Miller <[email protected]>
  4. .\"
  5. .\" Permission to use, copy, modify, and distribute this software for any
  6. .\" purpose with or without fee is hereby granted, provided that the above
  7. .\" copyright notice and this permission notice appear in all copies.
  8. .\"
  9. .\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. .\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. .\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. .\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. .\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. .\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. .\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. .\"
  17. .\" See http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html
  18. .\" for detailed information about RIPEMD-160.
  19. .\"
  20. .Dd $Mdocdate: July 13 2010 $
  21. .Dt RMD160 3
  22. .Os
  23. .Sh NAME
  24. .Nm RMD160Init ,
  25. .Nm RMD160Update ,
  26. .Nm RMD160Pad ,
  27. .Nm RMD160Final ,
  28. .Nm RMD160Transform ,
  29. .Nm RMD160End ,
  30. .Nm RMD160File ,
  31. .Nm RMD160FileChunk ,
  32. .Nm RMD160Data
  33. .Nd calculate the ``RIPEMD-160'' message digest
  34. .Sh LIBRARY
  35. .Lb libmd
  36. .Sh SYNOPSIS
  37. .Fd #include <sys/types.h>
  38. .Fd #include <rmd160.h>
  39. .Ft void
  40. .Fn RMD160Init "RMD160_CTX *context"
  41. .Ft void
  42. .Fn RMD160Update "RMD160_CTX *context" "const uint8_t *data" "uint32_t nbytes"
  43. .Ft void
  44. .Fn RMD160Pad "RMD160_CTX *context"
  45. .Ft void
  46. .Fn RMD160Final "uint8_t digest[RMD160_DIGEST_LENGTH]" "RMD160_CTX *context"
  47. .Ft void
  48. .Fn RMD160Transform "uint32_t state[5]" "const uint8_t block[RMD160_BLOCK_LENGTH]"
  49. .Ft "char *"
  50. .Fn RMD160End "RMD160_CTX *context" "char *buf"
  51. .Ft "char *"
  52. .Fn RMD160File "const char *filename" "char *buf"
  53. .Ft "char *"
  54. .Fn RMD160FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
  55. .Ft "char *"
  56. .Fn RMD160Data "const uint8_t *data" "size_t len" "char *buf"
  57. .Sh DESCRIPTION
  58. The RMD160 functions implement the 160-bit RIPE message digest hash algorithm
  59. (RMD-160).
  60. RMD-160 is used to generate a condensed representation
  61. of a message called a message digest.
  62. The algorithm takes a
  63. message less than 2^64 bits as input and produces a 160-bit digest
  64. suitable for use as a digital signature.
  65. .Pp
  66. The RMD160 functions are considered to be more secure than the
  67. .Xr md4 3 ,
  68. .Xr md5 3
  69. and
  70. .Xr sha1 3
  71. functions.
  72. All share a similar interface.
  73. .Pp
  74. The
  75. .Fn RMD160Init
  76. function initializes a RMD160_CTX
  77. .Ar context
  78. for use with
  79. .Fn RMD160Update ,
  80. and
  81. .Fn RMD160Final .
  82. The
  83. .Fn RMD160Update
  84. function adds
  85. .Ar data
  86. of length
  87. .Ar nbytes
  88. to the RMD160_CTX specified by
  89. .Ar context .
  90. .Fn RMD160Final
  91. is called when all data has been added via
  92. .Fn RMD160Update
  93. and stores a message digest in the
  94. .Ar digest
  95. parameter.
  96. .Pp
  97. The
  98. .Fn RMD160Pad
  99. function can be used to apply padding to the message digest as in
  100. .Fn RMD160Final ,
  101. but the current context can still be used with
  102. .Fn RMD160Update .
  103. .Pp
  104. The
  105. .Fn RMD160Transform
  106. function is used by
  107. .Fn RMD160Update
  108. to hash 512-bit blocks and forms the core of the algorithm.
  109. Most programs should use the interface provided by
  110. .Fn RMD160Init ,
  111. .Fn RMD160Update
  112. and
  113. .Fn RMD160Final
  114. instead of calling
  115. .Fn RMD160Transform
  116. directly.
  117. .Pp
  118. The
  119. .Fn RMD160End
  120. function is a front end for
  121. .Fn RMD160Final
  122. which converts the digest into an
  123. .Tn ASCII
  124. representation of the 160 bit digest in hexadecimal.
  125. .Pp
  126. The
  127. .Fn RMD160File
  128. function calculates the digest for a file and returns the result via
  129. .Fn RMD160End .
  130. If
  131. .Fn RMD160File
  132. is unable to open the file a NULL pointer is returned.
  133. .Pp
  134. .Fn RMD160FileChunk
  135. behaves like
  136. .Fn RMD160File
  137. but calculates the digest only for that portion of the file starting at
  138. .Fa offset
  139. and continuing for
  140. .Fa length
  141. bytes or until end of file is reached, whichever comes first.
  142. A zero
  143. .Fa length
  144. can be specified to read until end of file.
  145. A negative
  146. .Fa length
  147. or
  148. .Fa offset
  149. will be ignored.
  150. .Pp
  151. The
  152. .Fn RMD160Data
  153. function
  154. calculates the digest of an arbitrary string and returns the result via
  155. .Fn RMD160End .
  156. .Pp
  157. For each of the
  158. .Fn RMD160End ,
  159. .Fn RMD160File ,
  160. and
  161. .Fn RMD160Data
  162. functions the
  163. .Ar buf
  164. parameter should either be a string of at least 41 characters in
  165. size or a NULL pointer.
  166. In the latter case, space will be dynamically allocated via
  167. .Xr malloc 3
  168. and should be freed using
  169. .Xr free 3
  170. when it is no longer needed.
  171. .Sh EXAMPLES
  172. The follow code fragment will calculate the digest for
  173. the string "abc" which is ``0x8eb208f7e05d987a9b044a8e98c6b087f15a0bfc''.
  174. .Bd -literal -offset indent
  175. RMD160_CTX rmd;
  176. uint8_t results[RMD160_DIGEST_LENGTH];
  177. char *buf;
  178. int n;
  179. buf = "abc";
  180. n = strlen(buf);
  181. RMD160Init(&rmd);
  182. RMD160Update(&rmd, (uint8_t *)buf, n);
  183. RMD160Final(results, &rmd);
  184. /* Print the digest as one long hex value */
  185. printf("0x");
  186. for (n = 0; n < RMD160_DIGEST_LENGTH; n++)
  187. printf("%02x", results[n]);
  188. putchar('\en');
  189. .Ed
  190. .Pp
  191. Alternately, the helper functions could be used in the following way:
  192. .Bd -literal -offset indent
  193. RMD160_CTX rmd;
  194. uint8_t output[RMD160_DIGEST_STRING_LENGTH];
  195. char *buf = "abc";
  196. printf("0x%s\en", RMD160Data(buf, strlen(buf), output));
  197. .Ed
  198. .Sh SEE ALSO
  199. .Xr cksum 1 ,
  200. .Xr md4 3 ,
  201. .Xr md5 3 ,
  202. .Xr sha1 3 ,
  203. .Xr sha2 3
  204. .Rs
  205. .%A H. Dobbertin, A. Bosselaers, B. Preneel
  206. .%T RIPEMD-160, a strengthened version of RIPEMD
  207. .Re
  208. .Rs
  209. .%T Information technology - Security techniques - Hash-functions - Part 3: Dedicated hash-functions
  210. .%O ISO/IEC 10118-3
  211. .Re
  212. .Rs
  213. .%A H. Dobbertin, A. Bosselaers, B. Preneel
  214. .%T The RIPEMD-160 cryptographic hash function
  215. .%J Dr. Dobb's Journal
  216. .%V Vol. 22, No. 1
  217. .%D January 1997
  218. .%P pp. 24-28
  219. .Re
  220. .Sh HISTORY
  221. The RMD-160 functions appeared in
  222. .Ox 2.1 .
  223. .Sh AUTHORS
  224. This implementation of RMD-160 was written by Markus Friedl.
  225. .Pp
  226. The
  227. .Fn RMD160End ,
  228. .Fn RMD160File ,
  229. .Fn RMD160FileChunk ,
  230. and
  231. .Fn RMD160Data
  232. helper functions are derived from code written by Poul-Henning Kamp.
  233. .Sh CAVEATS
  234. If a message digest is to be copied to a multi-byte type (ie:
  235. an array of five 32-bit integers) it will be necessary to
  236. perform byte swapping on little endian machines such as the i386, alpha,
  237. and vax.