sha1.3 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. .\" $OpenBSD: sha1.3,v 1.36 2007/05/31 19:19:29 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://csrc.nist.gov/publications/fips/fips180-1/fip180-1.txt
  18. .\" for the detailed standard
  19. .\"
  20. .Dd $Mdocdate: February 13 2008 $
  21. .Dt SHA1 3
  22. .Os
  23. .Sh NAME
  24. .Nm SHA1Init ,
  25. .Nm SHA1Update ,
  26. .Nm SHA1Pad ,
  27. .Nm SHA1Final ,
  28. .Nm SHA1Transform ,
  29. .Nm SHA1End ,
  30. .Nm SHA1File ,
  31. .Nm SHA1FileChunk ,
  32. .Nm SHA1Data
  33. .Nd calculate the NIST Secure Hash Algorithm
  34. .Sh LIBRARY
  35. .Lb libmd
  36. .Sh SYNOPSIS
  37. .Fd #include <sys/types.h>
  38. .Fd #include <sha1.h>
  39. .Ft void
  40. .Fn SHA1Init "SHA1_CTX *context"
  41. .Ft void
  42. .Fn SHA1Update "SHA1_CTX *context" "const uint8_t *data" "size_t len"
  43. .Ft void
  44. .Fn SHA1Pad "SHA1_CTX *context"
  45. .Ft void
  46. .Fn SHA1Final "uint8_t digest[SHA1_DIGEST_LENGTH]" "SHA1_CTX *context"
  47. .Ft void
  48. .Fn SHA1Transform "uint32_t state[5]" "const uint8_t buffer[SHA1_BLOCK_LENGTH]"
  49. .Ft "char *"
  50. .Fn SHA1End "SHA1_CTX *context" "char *buf"
  51. .Ft "char *"
  52. .Fn SHA1File "const char *filename" "char *buf"
  53. .Ft "char *"
  54. .Fn SHA1FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
  55. .Ft "char *"
  56. .Fn SHA1Data "const uint8_t *data" "size_t len" "char *buf"
  57. .Sh DESCRIPTION
  58. The SHA1 functions implement the NIST Secure Hash Algorithm (SHA-1),
  59. FIPS PUB 180-1.
  60. SHA-1 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. While the SHA1 functions are considered to be more secure than the
  67. .Xr md4 3
  68. and
  69. .Xr md5 3
  70. functions with which they share a similar interface, they are considered
  71. insecure as of 2005, and as of 2020 chosen-prefix attacks have become
  72. practical, thus these must not be used in cryptographic contexts.
  73. .Pp
  74. The
  75. .Fn SHA1Init
  76. function initializes a SHA1_CTX
  77. .Ar context
  78. for use with
  79. .Fn SHA1Update ,
  80. and
  81. .Fn SHA1Final .
  82. The
  83. .Fn SHA1Update
  84. function adds
  85. .Ar data
  86. of length
  87. .Ar len
  88. to the SHA1_CTX specified by
  89. .Ar context .
  90. .Fn SHA1Final
  91. is called when all data has been added via
  92. .Fn SHA1Update
  93. and stores a message digest in the
  94. .Ar digest
  95. parameter.
  96. .Pp
  97. The
  98. .Fn SHA1Pad
  99. function can be used to apply padding to the message digest as in
  100. .Fn SHA1Final ,
  101. but the current context can still be used with
  102. .Fn SHA1Update .
  103. .Pp
  104. The
  105. .Fn SHA1Transform
  106. function is used by
  107. .Fn SHA1Update
  108. to hash 512-bit blocks and forms the core of the algorithm.
  109. Most programs should use the interface provided by
  110. .Fn SHA1Init ,
  111. .Fn SHA1Update
  112. and
  113. .Fn SHA1Final
  114. instead of calling
  115. .Fn SHA1Transform
  116. directly.
  117. .Pp
  118. The
  119. .Fn SHA1End
  120. function is a front end for
  121. .Fn SHA1Final
  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 SHA1File
  128. function calculates the digest for a file and returns the result via
  129. .Fn SHA1End .
  130. If
  131. .Fn SHA1File
  132. is unable to open the file a NULL pointer is returned.
  133. .Pp
  134. .Fn SHA1FileChunk
  135. behaves like
  136. .Fn SHA1File
  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 SHA1Data
  153. function
  154. calculates the digest of an arbitrary string and returns the result via
  155. .Fn SHA1End .
  156. .Pp
  157. For each of the
  158. .Fn SHA1End ,
  159. .Fn SHA1File ,
  160. and
  161. .Fn SHA1Data
  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 ``0xa9993e364706816aba3e25717850c26c9cd0d89d''.
  174. .Bd -literal -offset indent
  175. SHA1_CTX sha;
  176. uint8_t results[SHA1_DIGEST_LENGTH];
  177. char *buf;
  178. int n;
  179. buf = "abc";
  180. n = strlen(buf);
  181. SHA1Init(&sha);
  182. SHA1Update(&sha, (uint8_t *)buf, n);
  183. SHA1Final(results, &sha);
  184. /* Print the digest as one long hex value */
  185. printf("0x");
  186. for (n = 0; n < SHA1_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. uint8_t output[SHA1_DIGEST_STRING_LENGTH];
  194. char *buf = "abc";
  195. printf("0x%s\en", SHA1Data(buf, strlen(buf), output));
  196. .Ed
  197. .Sh SEE ALSO
  198. .Xr cksum 1 ,
  199. .Xr sha1 1 ,
  200. .Xr md4 3 ,
  201. .Xr md5 3 ,
  202. .Xr rmd160 3 ,
  203. .Xr sha2 3
  204. .Rs
  205. .%A J. Burrows
  206. .%T The Secure Hash Standard
  207. .%O FIPS PUB 180-1
  208. .Re
  209. .Rs
  210. .%A D. Eastlake and P. Jones
  211. .%T US Secure Hash Algorithm 1
  212. .%O RFC 3174
  213. .Re
  214. .Sh HISTORY
  215. The SHA-1 functions appeared in
  216. .Ox 2.0 .
  217. .Sh AUTHORS
  218. This implementation of SHA-1 was written by Steve Reid.
  219. .Pp
  220. The
  221. .Fn SHA1End ,
  222. .Fn SHA1File ,
  223. .Fn SHA1FileChunk ,
  224. and
  225. .Fn SHA1Data
  226. helper functions are derived from code written by Poul-Henning Kamp.
  227. .Sh CAVEATS
  228. This implementation of SHA-1 has not been validated by NIST
  229. and as such is not in official compliance with the standard.
  230. .Pp
  231. If a message digest is to be copied to a multi-byte type (ie:
  232. an array of five 32-bit integers) it will be necessary to
  233. perform byte swapping on little endian machines such as the i386, alpha,
  234. and vax.