digest.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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_DIGEST_H
  57. #define OPENSSL_HEADER_DIGEST_H
  58. #include <openssl/base.h>
  59. #if defined(__cplusplus)
  60. extern "C" {
  61. #endif
  62. // Digest functions.
  63. //
  64. // An EVP_MD abstracts the details of a specific hash function allowing code to
  65. // deal with the concept of a "hash function" without needing to know exactly
  66. // which hash function it is.
  67. // Hash algorithms.
  68. //
  69. // The following functions return |EVP_MD| objects that implement the named hash
  70. // function.
  71. OPENSSL_EXPORT const EVP_MD *EVP_md4(void);
  72. OPENSSL_EXPORT const EVP_MD *EVP_md5(void);
  73. OPENSSL_EXPORT const EVP_MD *EVP_sha1(void);
  74. OPENSSL_EXPORT const EVP_MD *EVP_sha224(void);
  75. OPENSSL_EXPORT const EVP_MD *EVP_sha256(void);
  76. OPENSSL_EXPORT const EVP_MD *EVP_sha384(void);
  77. OPENSSL_EXPORT const EVP_MD *EVP_sha512(void);
  78. OPENSSL_EXPORT const EVP_MD *EVP_sha512_256(void);
  79. OPENSSL_EXPORT const EVP_MD *EVP_blake2b256(void);
  80. // EVP_md5_sha1 is a TLS-specific |EVP_MD| which computes the concatenation of
  81. // MD5 and SHA-1, as used in TLS 1.1 and below.
  82. OPENSSL_EXPORT const EVP_MD *EVP_md5_sha1(void);
  83. // EVP_get_digestbynid returns an |EVP_MD| for the given NID, or NULL if no
  84. // such digest is known.
  85. OPENSSL_EXPORT const EVP_MD *EVP_get_digestbynid(int nid);
  86. // EVP_get_digestbyobj returns an |EVP_MD| for the given |ASN1_OBJECT|, or NULL
  87. // if no such digest is known.
  88. OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj);
  89. // Digest contexts.
  90. //
  91. // An EVP_MD_CTX represents the state of a specific digest operation in
  92. // progress.
  93. // EVP_MD_CTX_init initialises an, already allocated, |EVP_MD_CTX|. This is the
  94. // same as setting the structure to zero.
  95. OPENSSL_EXPORT void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
  96. // EVP_MD_CTX_new allocates and initialises a fresh |EVP_MD_CTX| and returns
  97. // it, or NULL on allocation failure. The caller must use |EVP_MD_CTX_free| to
  98. // release the resulting object.
  99. OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_new(void);
  100. // EVP_MD_CTX_cleanup frees any resources owned by |ctx| and resets it to a
  101. // freshly initialised state. It does not free |ctx| itself. It returns one.
  102. OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
  103. // EVP_MD_CTX_cleanse zeros the digest state in |ctx| and then performs the
  104. // actions of |EVP_MD_CTX_cleanup|. Note that some |EVP_MD_CTX| objects contain
  105. // more than just a digest (e.g. those resulting from |EVP_DigestSignInit|) but
  106. // this function does not zero out more than just the digest state even in that
  107. // case.
  108. OPENSSL_EXPORT void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx);
  109. // EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself.
  110. OPENSSL_EXPORT void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
  111. // EVP_MD_CTX_copy_ex sets |out|, which must already be initialised, to be a
  112. // copy of |in|. It returns one on success and zero on allocation failure.
  113. OPENSSL_EXPORT int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
  114. // EVP_MD_CTX_move sets |out|, which must already be initialised, to the hash
  115. // state in |in|. |in| is mutated and left in an empty state.
  116. OPENSSL_EXPORT void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in);
  117. // EVP_MD_CTX_reset calls |EVP_MD_CTX_cleanup| followed by |EVP_MD_CTX_init|. It
  118. // returns one.
  119. OPENSSL_EXPORT int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
  120. // Digest operations.
  121. // EVP_DigestInit_ex configures |ctx|, which must already have been
  122. // initialised, for a fresh hashing operation using |type|. It returns one on
  123. // success and zero on allocation failure.
  124. OPENSSL_EXPORT int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type,
  125. ENGINE *engine);
  126. // EVP_DigestInit acts like |EVP_DigestInit_ex| except that |ctx| is
  127. // initialised before use.
  128. OPENSSL_EXPORT int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
  129. // EVP_DigestUpdate hashes |len| bytes from |data| into the hashing operation
  130. // in |ctx|. It returns one.
  131. OPENSSL_EXPORT int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data,
  132. size_t len);
  133. // EVP_MAX_MD_SIZE is the largest digest size supported, in bytes.
  134. // Functions that output a digest generally require the buffer have
  135. // at least this much space.
  136. #define EVP_MAX_MD_SIZE 64 // SHA-512 is the longest so far.
  137. // EVP_MAX_MD_BLOCK_SIZE is the largest digest block size supported, in
  138. // bytes.
  139. #define EVP_MAX_MD_BLOCK_SIZE 128 // SHA-512 is the longest so far.
  140. // EVP_DigestFinal_ex finishes the digest in |ctx| and writes the output to
  141. // |md_out|. |EVP_MD_CTX_size| bytes are written, which is at most
  142. // |EVP_MAX_MD_SIZE|. If |out_size| is not NULL then |*out_size| is set to the
  143. // number of bytes written. It returns one. After this call, the hash cannot be
  144. // updated or finished again until |EVP_DigestInit_ex| is called to start
  145. // another hashing operation.
  146. OPENSSL_EXPORT int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out,
  147. unsigned int *out_size);
  148. // EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that
  149. // |EVP_MD_CTX_cleanup| is called on |ctx| before returning.
  150. OPENSSL_EXPORT int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out,
  151. unsigned int *out_size);
  152. // EVP_Digest performs a complete hashing operation in one call. It hashes |len|
  153. // bytes from |data| and writes the digest to |md_out|. |EVP_MD_CTX_size| bytes
  154. // are written, which is at most |EVP_MAX_MD_SIZE|. If |out_size| is not NULL
  155. // then |*out_size| is set to the number of bytes written. It returns one on
  156. // success and zero otherwise.
  157. OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out,
  158. unsigned int *md_out_size, const EVP_MD *type,
  159. ENGINE *impl);
  160. // Digest function accessors.
  161. //
  162. // These functions allow code to learn details about an abstract hash
  163. // function.
  164. // EVP_MD_type returns a NID identifying |md|. (For example, |NID_sha256|.)
  165. OPENSSL_EXPORT int EVP_MD_type(const EVP_MD *md);
  166. // EVP_MD_flags returns the flags for |md|, which is a set of |EVP_MD_FLAG_*|
  167. // values, ORed together.
  168. OPENSSL_EXPORT uint32_t EVP_MD_flags(const EVP_MD *md);
  169. // EVP_MD_size returns the digest size of |md|, in bytes.
  170. OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md);
  171. // EVP_MD_block_size returns the native block-size of |md|, in bytes.
  172. OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md);
  173. // EVP_MD_FLAG_PKEY_DIGEST indicates that the digest function is used with a
  174. // specific public key in order to verify signatures. (For example,
  175. // EVP_dss1.)
  176. #define EVP_MD_FLAG_PKEY_DIGEST 1
  177. // EVP_MD_FLAG_DIGALGID_ABSENT indicates that the parameter type in an X.509
  178. // DigestAlgorithmIdentifier representing this digest function should be
  179. // undefined rather than NULL.
  180. #define EVP_MD_FLAG_DIGALGID_ABSENT 2
  181. // EVP_MD_FLAG_XOF indicates that the digest is an extensible-output function
  182. // (XOF). This flag is defined for compatibility and will never be set in any
  183. // |EVP_MD| in BoringSSL.
  184. #define EVP_MD_FLAG_XOF 4
  185. // Digest operation accessors.
  186. // EVP_MD_CTX_md returns the underlying digest function, or NULL if one has not
  187. // been set.
  188. OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
  189. // EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It
  190. // will crash if a digest hasn't been set on |ctx|.
  191. OPENSSL_EXPORT size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx);
  192. // EVP_MD_CTX_block_size returns the block size of the digest function used by
  193. // |ctx|, in bytes. It will crash if a digest hasn't been set on |ctx|.
  194. OPENSSL_EXPORT size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx);
  195. // EVP_MD_CTX_type returns a NID describing the digest function used by |ctx|.
  196. // (For example, |NID_sha256|.) It will crash if a digest hasn't been set on
  197. // |ctx|.
  198. OPENSSL_EXPORT int EVP_MD_CTX_type(const EVP_MD_CTX *ctx);
  199. // ASN.1 functions.
  200. //
  201. // These functions allow code to parse and serialize AlgorithmIdentifiers for
  202. // hash functions.
  203. // EVP_parse_digest_algorithm parses an AlgorithmIdentifier structure containing
  204. // a hash function OID (for example, 2.16.840.1.101.3.4.2.1 is SHA-256) and
  205. // advances |cbs|. The parameters field may either be omitted or a NULL. It
  206. // returns the digest function or NULL on error.
  207. OPENSSL_EXPORT const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs);
  208. // EVP_marshal_digest_algorithm marshals |md| as an AlgorithmIdentifier
  209. // structure and appends the result to |cbb|. It returns one on success and zero
  210. // on error.
  211. OPENSSL_EXPORT int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md);
  212. // Deprecated functions.
  213. // EVP_MD_CTX_copy sets |out|, which must /not/ be initialised, to be a copy of
  214. // |in|. It returns one on success and zero on error.
  215. OPENSSL_EXPORT int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in);
  216. // EVP_add_digest does nothing and returns one. It exists only for
  217. // compatibility with OpenSSL.
  218. OPENSSL_EXPORT int EVP_add_digest(const EVP_MD *digest);
  219. // EVP_get_digestbyname returns an |EVP_MD| given a human readable name in
  220. // |name|, or NULL if the name is unknown.
  221. OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *);
  222. // EVP_dss1 returns the value of EVP_sha1(). This was provided by OpenSSL to
  223. // specifiy the original DSA signatures, which were fixed to use SHA-1. Note,
  224. // however, that attempting to sign or verify DSA signatures with the EVP
  225. // interface will always fail.
  226. OPENSSL_EXPORT const EVP_MD *EVP_dss1(void);
  227. // EVP_MD_CTX_create calls |EVP_MD_CTX_new|.
  228. OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_create(void);
  229. // EVP_MD_CTX_destroy calls |EVP_MD_CTX_free|.
  230. OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
  231. // EVP_DigestFinalXOF returns zero and adds an error to the error queue.
  232. // BoringSSL does not support any XOF digests.
  233. OPENSSL_EXPORT int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out,
  234. size_t len);
  235. // EVP_MD_meth_get_flags calls |EVP_MD_flags|.
  236. OPENSSL_EXPORT uint32_t EVP_MD_meth_get_flags(const EVP_MD *md);
  237. // EVP_MD_CTX_set_flags does nothing.
  238. OPENSSL_EXPORT void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
  239. // EVP_MD_CTX_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS
  240. // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms
  241. // (it's up the the caller to use the FIPS module in a fashion compliant with
  242. // their needs). Thus this exists only to allow code to compile.
  243. #define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0
  244. // EVP_MD_nid calls |EVP_MD_type|.
  245. OPENSSL_EXPORT int EVP_MD_nid(const EVP_MD *md);
  246. struct evp_md_pctx_ops;
  247. struct env_md_ctx_st {
  248. // digest is the underlying digest function, or NULL if not set.
  249. const EVP_MD *digest;
  250. // md_data points to a block of memory that contains the hash-specific
  251. // context.
  252. void *md_data;
  253. // pctx is an opaque (at this layer) pointer to additional context that
  254. // EVP_PKEY functions may store in this object.
  255. EVP_PKEY_CTX *pctx;
  256. // pctx_ops, if not NULL, points to a vtable that contains functions to
  257. // manipulate |pctx|.
  258. const struct evp_md_pctx_ops *pctx_ops;
  259. } /* EVP_MD_CTX */;
  260. #if defined(__cplusplus)
  261. } // extern C
  262. #if !defined(BORINGSSL_NO_CXX)
  263. extern "C++" {
  264. BSSL_NAMESPACE_BEGIN
  265. BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_free)
  266. using ScopedEVP_MD_CTX =
  267. internal::StackAllocatedMovable<EVP_MD_CTX, int, EVP_MD_CTX_init,
  268. EVP_MD_CTX_cleanup, EVP_MD_CTX_move>;
  269. BSSL_NAMESPACE_END
  270. } // extern C++
  271. #endif
  272. #endif
  273. #define DIGEST_R_INPUT_NOT_INITIALIZED 100
  274. #define DIGEST_R_DECODE_ERROR 101
  275. #define DIGEST_R_UNKNOWN_HASH 102
  276. #endif // OPENSSL_HEADER_DIGEST_H