sha.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #ifndef OPENSSL_HEADER_SHA_H
  57. #define OPENSSL_HEADER_SHA_H
  58. #include <openssl/base.h>
  59. #if defined(__cplusplus)
  60. extern "C" {
  61. #endif
  62. // The SHA family of hash functions (SHA-1 and SHA-2).
  63. // SHA_CBLOCK is the block size of SHA-1.
  64. #define SHA_CBLOCK 64
  65. // SHA_DIGEST_LENGTH is the length of a SHA-1 digest.
  66. #define SHA_DIGEST_LENGTH 20
  67. // SHA1_Init initialises |sha| and returns one.
  68. OPENSSL_EXPORT int SHA1_Init(SHA_CTX *sha);
  69. // SHA1_Update adds |len| bytes from |data| to |sha| and returns one.
  70. OPENSSL_EXPORT int SHA1_Update(SHA_CTX *sha, const void *data, size_t len);
  71. // SHA1_Final adds the final padding to |sha| and writes the resulting digest to
  72. // |out|, which must have at least |SHA_DIGEST_LENGTH| bytes of space. It
  73. // returns one.
  74. OPENSSL_EXPORT int SHA1_Final(uint8_t out[SHA_DIGEST_LENGTH], SHA_CTX *sha);
  75. // SHA1 writes the digest of |len| bytes from |data| to |out| and returns
  76. // |out|. There must be at least |SHA_DIGEST_LENGTH| bytes of space in
  77. // |out|.
  78. OPENSSL_EXPORT uint8_t *SHA1(const uint8_t *data, size_t len,
  79. uint8_t out[SHA_DIGEST_LENGTH]);
  80. // SHA1_Transform is a low-level function that performs a single, SHA-1 block
  81. // transformation using the state from |sha| and |SHA_CBLOCK| bytes from
  82. // |block|.
  83. OPENSSL_EXPORT void SHA1_Transform(SHA_CTX *sha,
  84. const uint8_t block[SHA_CBLOCK]);
  85. struct sha_state_st {
  86. #if defined(OPENSSL_WINDOWS)
  87. uint32_t h[5];
  88. #else
  89. // wpa_supplicant accesses |h0|..|h4| so we must support those names
  90. // for compatibility with it until it can be updated.
  91. union {
  92. uint32_t h[5];
  93. struct {
  94. uint32_t h0;
  95. uint32_t h1;
  96. uint32_t h2;
  97. uint32_t h3;
  98. uint32_t h4;
  99. };
  100. };
  101. #endif
  102. uint32_t Nl, Nh;
  103. uint8_t data[SHA_CBLOCK];
  104. unsigned num;
  105. };
  106. // SHA-224.
  107. // SHA224_CBLOCK is the block size of SHA-224.
  108. #define SHA224_CBLOCK 64
  109. // SHA224_DIGEST_LENGTH is the length of a SHA-224 digest.
  110. #define SHA224_DIGEST_LENGTH 28
  111. // SHA224_Init initialises |sha| and returns 1.
  112. OPENSSL_EXPORT int SHA224_Init(SHA256_CTX *sha);
  113. // SHA224_Update adds |len| bytes from |data| to |sha| and returns 1.
  114. OPENSSL_EXPORT int SHA224_Update(SHA256_CTX *sha, const void *data, size_t len);
  115. // SHA224_Final adds the final padding to |sha| and writes the resulting digest
  116. // to |out|, which must have at least |SHA224_DIGEST_LENGTH| bytes of space. It
  117. // returns one on success and zero on programmer error.
  118. OPENSSL_EXPORT int SHA224_Final(uint8_t out[SHA224_DIGEST_LENGTH],
  119. SHA256_CTX *sha);
  120. // SHA224 writes the digest of |len| bytes from |data| to |out| and returns
  121. // |out|. There must be at least |SHA224_DIGEST_LENGTH| bytes of space in
  122. // |out|.
  123. OPENSSL_EXPORT uint8_t *SHA224(const uint8_t *data, size_t len,
  124. uint8_t out[SHA224_DIGEST_LENGTH]);
  125. // SHA-256.
  126. // SHA256_CBLOCK is the block size of SHA-256.
  127. #define SHA256_CBLOCK 64
  128. // SHA256_DIGEST_LENGTH is the length of a SHA-256 digest.
  129. #define SHA256_DIGEST_LENGTH 32
  130. // SHA256_Init initialises |sha| and returns 1.
  131. OPENSSL_EXPORT int SHA256_Init(SHA256_CTX *sha);
  132. // SHA256_Update adds |len| bytes from |data| to |sha| and returns 1.
  133. OPENSSL_EXPORT int SHA256_Update(SHA256_CTX *sha, const void *data, size_t len);
  134. // SHA256_Final adds the final padding to |sha| and writes the resulting digest
  135. // to |out|, which must have at least |SHA256_DIGEST_LENGTH| bytes of space. It
  136. // returns one on success and zero on programmer error.
  137. OPENSSL_EXPORT int SHA256_Final(uint8_t out[SHA256_DIGEST_LENGTH],
  138. SHA256_CTX *sha);
  139. // SHA256 writes the digest of |len| bytes from |data| to |out| and returns
  140. // |out|. There must be at least |SHA256_DIGEST_LENGTH| bytes of space in
  141. // |out|.
  142. OPENSSL_EXPORT uint8_t *SHA256(const uint8_t *data, size_t len,
  143. uint8_t out[SHA256_DIGEST_LENGTH]);
  144. // SHA256_Transform is a low-level function that performs a single, SHA-256
  145. // block transformation using the state from |sha| and |SHA256_CBLOCK| bytes
  146. // from |block|.
  147. OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha,
  148. const uint8_t block[SHA256_CBLOCK]);
  149. // SHA256_TransformBlocks is a low-level function that takes |num_blocks| *
  150. // |SHA256_CBLOCK| bytes of data and performs SHA-256 transforms on it to update
  151. // |state|. You should not use this function unless you are implementing a
  152. // derivative of SHA-256.
  153. OPENSSL_EXPORT void SHA256_TransformBlocks(uint32_t state[8],
  154. const uint8_t *data,
  155. size_t num_blocks);
  156. struct sha256_state_st {
  157. uint32_t h[8];
  158. uint32_t Nl, Nh;
  159. uint8_t data[SHA256_CBLOCK];
  160. unsigned num, md_len;
  161. };
  162. // SHA-384.
  163. // SHA384_CBLOCK is the block size of SHA-384.
  164. #define SHA384_CBLOCK 128
  165. // SHA384_DIGEST_LENGTH is the length of a SHA-384 digest.
  166. #define SHA384_DIGEST_LENGTH 48
  167. // SHA384_Init initialises |sha| and returns 1.
  168. OPENSSL_EXPORT int SHA384_Init(SHA512_CTX *sha);
  169. // SHA384_Update adds |len| bytes from |data| to |sha| and returns 1.
  170. OPENSSL_EXPORT int SHA384_Update(SHA512_CTX *sha, const void *data, size_t len);
  171. // SHA384_Final adds the final padding to |sha| and writes the resulting digest
  172. // to |out|, which must have at least |SHA384_DIGEST_LENGTH| bytes of space. It
  173. // returns one on success and zero on programmer error.
  174. OPENSSL_EXPORT int SHA384_Final(uint8_t out[SHA384_DIGEST_LENGTH],
  175. SHA512_CTX *sha);
  176. // SHA384 writes the digest of |len| bytes from |data| to |out| and returns
  177. // |out|. There must be at least |SHA384_DIGEST_LENGTH| bytes of space in
  178. // |out|.
  179. OPENSSL_EXPORT uint8_t *SHA384(const uint8_t *data, size_t len,
  180. uint8_t out[SHA384_DIGEST_LENGTH]);
  181. // SHA-512.
  182. // SHA512_CBLOCK is the block size of SHA-512.
  183. #define SHA512_CBLOCK 128
  184. // SHA512_DIGEST_LENGTH is the length of a SHA-512 digest.
  185. #define SHA512_DIGEST_LENGTH 64
  186. // SHA512_Init initialises |sha| and returns 1.
  187. OPENSSL_EXPORT int SHA512_Init(SHA512_CTX *sha);
  188. // SHA512_Update adds |len| bytes from |data| to |sha| and returns 1.
  189. OPENSSL_EXPORT int SHA512_Update(SHA512_CTX *sha, const void *data, size_t len);
  190. // SHA512_Final adds the final padding to |sha| and writes the resulting digest
  191. // to |out|, which must have at least |SHA512_DIGEST_LENGTH| bytes of space. It
  192. // returns one on success and zero on programmer error.
  193. OPENSSL_EXPORT int SHA512_Final(uint8_t out[SHA512_DIGEST_LENGTH],
  194. SHA512_CTX *sha);
  195. // SHA512 writes the digest of |len| bytes from |data| to |out| and returns
  196. // |out|. There must be at least |SHA512_DIGEST_LENGTH| bytes of space in
  197. // |out|.
  198. OPENSSL_EXPORT uint8_t *SHA512(const uint8_t *data, size_t len,
  199. uint8_t out[SHA512_DIGEST_LENGTH]);
  200. // SHA512_Transform is a low-level function that performs a single, SHA-512
  201. // block transformation using the state from |sha| and |SHA512_CBLOCK| bytes
  202. // from |block|.
  203. OPENSSL_EXPORT void SHA512_Transform(SHA512_CTX *sha,
  204. const uint8_t block[SHA512_CBLOCK]);
  205. struct sha512_state_st {
  206. uint64_t h[8];
  207. uint64_t Nl, Nh;
  208. uint8_t p[128];
  209. unsigned num, md_len;
  210. };
  211. // SHA-512-256
  212. //
  213. // See https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf section 5.3.6
  214. #define SHA512_256_DIGEST_LENGTH 32
  215. // SHA512_256_Init initialises |sha| and returns 1.
  216. OPENSSL_EXPORT int SHA512_256_Init(SHA512_CTX *sha);
  217. // SHA512_256_Update adds |len| bytes from |data| to |sha| and returns 1.
  218. OPENSSL_EXPORT int SHA512_256_Update(SHA512_CTX *sha, const void *data,
  219. size_t len);
  220. // SHA512_256_Final adds the final padding to |sha| and writes the resulting
  221. // digest to |out|, which must have at least |SHA512_256_DIGEST_LENGTH| bytes of
  222. // space. It returns one on success and zero on programmer error.
  223. OPENSSL_EXPORT int SHA512_256_Final(uint8_t out[SHA512_256_DIGEST_LENGTH],
  224. SHA512_CTX *sha);
  225. // SHA512_256 writes the digest of |len| bytes from |data| to |out| and returns
  226. // |out|. There must be at least |SHA512_256_DIGEST_LENGTH| bytes of space in
  227. // |out|.
  228. OPENSSL_EXPORT uint8_t *SHA512_256(const uint8_t *data, size_t len,
  229. uint8_t out[SHA512_256_DIGEST_LENGTH]);
  230. #if defined(__cplusplus)
  231. } // extern C
  232. #endif
  233. #endif // OPENSSL_HEADER_SHA_H