sha2.3 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. .\" $OpenBSD: sha2.3,v 1.15 2008/09/06 12:00:19 djm Exp $
  2. .\"
  3. .\" Copyright (c) 2003, 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. .\" Sponsored in part by the Defense Advanced Research Projects
  18. .\" Agency (DARPA) and Air Force Research Laboratory, Air Force
  19. .\" Materiel Command, USAF, under agreement number F39502-99-1-0512.
  20. .\"
  21. .\" See http://www.nist.gov/sha/ for the detailed standard
  22. .\"
  23. .Dd $Mdocdate: September 12 2008 $
  24. .Dt SHA2 3
  25. .Os
  26. .Sh NAME
  27. .Nm SHA256Init ,
  28. .Nm SHA256Update ,
  29. .Nm SHA256Pad ,
  30. .Nm SHA256Final ,
  31. .Nm SHA256Transform ,
  32. .Nm SHA256End ,
  33. .Nm SHA256File ,
  34. .Nm SHA256FileChunk ,
  35. .Nm SHA256Data
  36. .Nd calculate the NIST Secure Hash Standard (version 2)
  37. .Sh LIBRARY
  38. .Lb libmd
  39. .Sh SYNOPSIS
  40. .Fd #include <sys/types.h>
  41. .Fd #include <sha2.h>
  42. .Ft void
  43. .Fn SHA256Init "SHA2_CTX *context"
  44. .Ft void
  45. .Fn SHA256Update "SHA2_CTX *context" "const uint8_t *data" "size_t len"
  46. .Ft void
  47. .Fn SHA256Pad "SHA2_CTX *context"
  48. .Ft void
  49. .Fn SHA256Final "uint8_t digest[SHA256_DIGEST_LENGTH]" "SHA2_CTX *context"
  50. .Ft void
  51. .Fn SHA256Transform "uint32_t state[8]" "const uint8_t buffer[SHA256_BLOCK_LENGTH]"
  52. .Ft "char *"
  53. .Fn SHA256End "SHA2_CTX *context" "char *buf"
  54. .Ft "char *"
  55. .Fn SHA256File "const char *filename" "char *buf"
  56. .Ft "char *"
  57. .Fn SHA256FileChunk "const char *filename" "char *buf" "off_t offset" "off_t length"
  58. .Ft "char *"
  59. .Fn SHA256Data "uint8_t *data" "size_t len" "char *buf"
  60. .Ft void
  61. .Fn SHA384Init "SHA2_CTX *context"
  62. .Ft void
  63. .Fn SHA384Update "SHA2_CTX *context" "const uint8_t *data" "size_t len"
  64. .Ft void
  65. .Fn SHA384Pad "SHA2_CTX *context"
  66. .Ft void
  67. .Fn SHA384Final "uint8_t digest[SHA384_DIGEST_LENGTH]" "SHA2_CTX *context"
  68. .Ft void
  69. .Fn SHA384Transform "uint64_t state[8]" "const uint8_t buffer[SHA384_BLOCK_LENGTH]"
  70. .Ft "char *"
  71. .Fn SHA384End "SHA2_CTX *context" "char *buf"
  72. .Ft "char *"
  73. .Fn SHA384File "char *filename" "char *buf"
  74. .Ft "char *"
  75. .Fn SHA384FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
  76. .Ft "char *"
  77. .Fn SHA384Data "uint8_t *data" "size_t len" "char *buf"
  78. .Ft void
  79. .Fn SHA512Init "SHA2_CTX *context"
  80. .Ft void
  81. .Fn SHA512Update "SHA2_CTX *context" "const uint8_t *data" "size_t len"
  82. .Ft void
  83. .Fn SHA512Pad "SHA2_CTX *context"
  84. .Ft void
  85. .Fn SHA512Final "uint8_t digest[SHA512_DIGEST_LENGTH]" "SHA2_CTX *context"
  86. .Ft void
  87. .Fn SHA512Transform "uint64_t state[8]" "const uint8_t buffer[SHA512_BLOCK_LENGTH]"
  88. .Ft "char *"
  89. .Fn SHA512End "SHA2_CTX *context" "char *buf"
  90. .Ft "char *"
  91. .Fn SHA512File "char *filename" "char *buf"
  92. .Ft "char *"
  93. .Fn SHA512FileChunk "char *filename" "char *buf" "off_t offset" "off_t length"
  94. .Ft "char *"
  95. .Fn SHA512Data "uint8_t *data" "size_t len" "char *buf"
  96. .Sh DESCRIPTION
  97. The SHA2 functions implement the NIST Secure Hash Standard,
  98. FIPS PUB 180-2.
  99. The SHA2 functions are used to generate a condensed representation of a
  100. message called a message digest, suitable for use as a digital signature.
  101. There are three families of functions, with names corresponding to
  102. the number of bits in the resulting message digest.
  103. The SHA-256 functions are limited to processing a message of less
  104. than 2^64 bits as input.
  105. The SHA-384 and SHA-512 functions can process a message of at most 2^128 - 1
  106. bits as input.
  107. .Pp
  108. The SHA2 functions are considered to be more secure than the
  109. .Xr sha1 3
  110. functions with which they share a similar interface.
  111. The 256, 384, and 512-bit versions of SHA2 share the same interface.
  112. For brevity, only the 256-bit variants are described below.
  113. .Pp
  114. The
  115. .Fn SHA256Init
  116. function initializes a SHA2_CTX
  117. .Ar context
  118. for use with
  119. .Fn SHA256Update
  120. and
  121. .Fn SHA256Final .
  122. The
  123. .Fn SHA256Update
  124. function adds
  125. .Ar data
  126. of length
  127. .Ar len
  128. to the SHA2_CTX specified by
  129. .Ar context .
  130. .Fn SHA256Final
  131. is called when all data has been added via
  132. .Fn SHA256Update
  133. and stores a message digest in the
  134. .Ar digest
  135. parameter.
  136. .Pp
  137. The
  138. .Fn SHA256Pad
  139. function can be used to apply padding to the message digest as in
  140. .Fn SHA256Final ,
  141. but the current context can still be used with
  142. .Fn SHA256Update .
  143. .Pp
  144. The
  145. .Fn SHA256Transform
  146. function is used by
  147. .Fn SHA256Update
  148. to hash 512-bit blocks and forms the core of the algorithm.
  149. Most programs should use the interface provided by
  150. .Fn SHA256Init ,
  151. .Fn SHA256Update ,
  152. and
  153. .Fn SHA256Final
  154. instead of calling
  155. .Fn SHA256Transform
  156. directly.
  157. .Pp
  158. The
  159. .Fn SHA256End
  160. function is a front end for
  161. .Fn SHA256Final
  162. which converts the digest into an
  163. .Tn ASCII
  164. representation of the digest in hexadecimal.
  165. .Pp
  166. The
  167. .Fn SHA256File
  168. function calculates the digest for a file and returns the result via
  169. .Fn SHA256End .
  170. If
  171. .Fn SHA256File
  172. is unable to open the file, a
  173. .Dv NULL
  174. pointer is returned.
  175. .Pp
  176. .Fn SHA256FileChunk
  177. behaves like
  178. .Fn SHA256File
  179. but calculates the digest only for that portion of the file starting at
  180. .Fa offset
  181. and continuing for
  182. .Fa length
  183. bytes or until end of file is reached, whichever comes first.
  184. A zero
  185. .Fa length
  186. can be specified to read until end of file.
  187. A negative
  188. .Fa length
  189. or
  190. .Fa offset
  191. will be ignored.
  192. .Pp
  193. The
  194. .Fn SHA256Data
  195. function
  196. calculates the digest of an arbitrary string and returns the result via
  197. .Fn SHA256End .
  198. .Pp
  199. For each of the
  200. .Fn SHA256End ,
  201. .Fn SHA256File ,
  202. .Fn SHA256FileChunk ,
  203. and
  204. .Fn SHA256Data
  205. functions the
  206. .Ar buf
  207. parameter should either be a string large enough to hold the resulting digest
  208. (e.g.\&
  209. .Ev SHA256_DIGEST_STRING_LENGTH ,
  210. .Ev SHA384_DIGEST_STRING_LENGTH ,
  211. or
  212. .Ev SHA512_DIGEST_STRING_LENGTH ,
  213. depending on the function being used)
  214. or a
  215. .Dv NULL
  216. pointer.
  217. In the latter case, space will be dynamically allocated via
  218. .Xr malloc 3
  219. and should be freed using
  220. .Xr free 3
  221. when it is no longer needed.
  222. .Sh EXAMPLES
  223. The following code fragment will calculate the SHA-256 digest for the string
  224. .Qq abc ,
  225. which is
  226. .Dq 0xba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad .
  227. .Bd -literal -offset indent
  228. SHA2_CTX ctx;
  229. uint8_t results[SHA256_DIGEST_LENGTH];
  230. char *buf;
  231. int n;
  232. buf = "abc";
  233. n = strlen(buf);
  234. SHA256Init(&ctx);
  235. SHA256Update(&ctx, (uint8_t *)buf, n);
  236. SHA256Final(results, &ctx);
  237. /* Print the digest as one long hex value */
  238. printf("0x");
  239. for (n = 0; n \*(Lt SHA256_DIGEST_LENGTH; n++)
  240. printf("%02x", results[n]);
  241. putchar('\en');
  242. .Ed
  243. .Pp
  244. Alternately, the helper functions could be used in the following way:
  245. .Bd -literal -offset indent
  246. uint8_t output[SHA256_DIGEST_STRING_LENGTH];
  247. char *buf = "abc";
  248. printf("0x%s\en", SHA256Data(buf, strlen(buf), output));
  249. .Ed
  250. .Sh SEE ALSO
  251. .Xr cksum 1 ,
  252. .Xr md4 3 ,
  253. .Xr md5 3 ,
  254. .Xr rmd160 3 ,
  255. .Xr sha1 3
  256. .Rs
  257. .%T Secure Hash Standard
  258. .%O FIPS PUB 180-2
  259. .Re
  260. .Sh HISTORY
  261. The SHA2 functions appeared in
  262. .Ox 3.4 .
  263. .Sh AUTHORS
  264. This implementation of the SHA functions was written by Aaron D. Gifford.
  265. .Pp
  266. The
  267. .Fn SHA256End ,
  268. .Fn SHA256File ,
  269. .Fn SHA256FileChunk ,
  270. and
  271. .Fn SHA256Data
  272. helper functions are derived from code written by Poul-Henning Kamp.
  273. .Sh CAVEATS
  274. This implementation of the Secure Hash Standard has not been validated by
  275. NIST and as such is not in official compliance with the standard.
  276. .Pp
  277. If a message digest is to be copied to a multi-byte type (i.e.\&
  278. an array of 32-bit integers) it will be necessary to
  279. perform byte swapping on little endian machines such as the i386, alpha,
  280. and vax.