Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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 | |
| 57 | #ifndef OPENSSL_HEADER_DIGEST_H |
| 58 | #define OPENSSL_HEADER_DIGEST_H |
| 59 | |
| 60 | #include <openssl/base.h> |
| 61 | |
| 62 | #if defined(__cplusplus) |
| 63 | extern "C" { |
| 64 | #endif |
| 65 | |
| 66 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 67 | // Digest functions. |
| 68 | // |
| 69 | // An EVP_MD abstracts the details of a specific hash function allowing code to |
| 70 | // deal with the concept of a "hash function" without needing to know exactly |
| 71 | // which hash function it is. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 72 | |
| 73 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 74 | // Hash algorithms. |
| 75 | // |
| 76 | // The following functions return |EVP_MD| objects that implement the named hash |
| 77 | // function. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 78 | |
Adam Langley | c9eb7ea | 2014-08-22 11:06:14 -0700 | [diff] [blame] | 79 | OPENSSL_EXPORT const EVP_MD *EVP_md4(void); |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 80 | OPENSSL_EXPORT const EVP_MD *EVP_md5(void); |
| 81 | OPENSSL_EXPORT const EVP_MD *EVP_sha1(void); |
| 82 | OPENSSL_EXPORT const EVP_MD *EVP_sha224(void); |
| 83 | OPENSSL_EXPORT const EVP_MD *EVP_sha256(void); |
| 84 | OPENSSL_EXPORT const EVP_MD *EVP_sha384(void); |
| 85 | OPENSSL_EXPORT const EVP_MD *EVP_sha512(void); |
Adam Langley | 3c11bf5 | 2020-04-15 09:10:04 -0700 | [diff] [blame] | 86 | OPENSSL_EXPORT const EVP_MD *EVP_sha512_256(void); |
Adam Langley | e133345 | 2021-01-21 11:56:52 -0800 | [diff] [blame] | 87 | OPENSSL_EXPORT const EVP_MD *EVP_blake2b256(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 88 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 89 | // EVP_md5_sha1 is a TLS-specific |EVP_MD| which computes the concatenation of |
| 90 | // MD5 and SHA-1, as used in TLS 1.1 and below. |
David Benjamin | 00505ec | 2014-11-26 16:38:00 -0500 | [diff] [blame] | 91 | OPENSSL_EXPORT const EVP_MD *EVP_md5_sha1(void); |
| 92 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 93 | // EVP_get_digestbynid returns an |EVP_MD| for the given NID, or NULL if no |
| 94 | // such digest is known. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 95 | OPENSSL_EXPORT const EVP_MD *EVP_get_digestbynid(int nid); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 96 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 97 | // EVP_get_digestbyobj returns an |EVP_MD| for the given |ASN1_OBJECT|, or NULL |
| 98 | // if no such digest is known. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 99 | OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 100 | |
| 101 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 102 | // Digest contexts. |
| 103 | // |
| 104 | // An EVP_MD_CTX represents the state of a specific digest operation in |
| 105 | // progress. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 106 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 107 | // EVP_MD_CTX_init initialises an, already allocated, |EVP_MD_CTX|. This is the |
| 108 | // same as setting the structure to zero. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 109 | OPENSSL_EXPORT void EVP_MD_CTX_init(EVP_MD_CTX *ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 111 | // EVP_MD_CTX_new allocates and initialises a fresh |EVP_MD_CTX| and returns |
| 112 | // it, or NULL on allocation failure. The caller must use |EVP_MD_CTX_free| to |
| 113 | // release the resulting object. |
| 114 | OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_new(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 115 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 116 | // EVP_MD_CTX_cleanup frees any resources owned by |ctx| and resets it to a |
| 117 | // freshly initialised state. It does not free |ctx| itself. It returns one. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 118 | OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 119 | |
Adam Langley | ec476ef | 2022-01-14 13:22:59 -0800 | [diff] [blame] | 120 | // EVP_MD_CTX_cleanse zeros the digest state in |ctx| and then performs the |
| 121 | // actions of |EVP_MD_CTX_cleanup|. Note that some |EVP_MD_CTX| objects contain |
| 122 | // more than just a digest (e.g. those resulting from |EVP_DigestSignInit|) but |
| 123 | // this function does not zero out more than just the digest state even in that |
| 124 | // case. |
| 125 | OPENSSL_EXPORT void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx); |
| 126 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 127 | // EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself. |
| 128 | OPENSSL_EXPORT void EVP_MD_CTX_free(EVP_MD_CTX *ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 129 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 130 | // EVP_MD_CTX_copy_ex sets |out|, which must already be initialised, to be a |
Joshua Liebow-Feeser | dea6d90 | 2018-08-13 15:01:04 -0700 | [diff] [blame] | 131 | // copy of |in|. It returns one on success and zero on allocation failure. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 132 | OPENSSL_EXPORT int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 133 | |
David Benjamin | e9109cb | 2021-05-22 17:20:53 -0400 | [diff] [blame] | 134 | // EVP_MD_CTX_move sets |out|, which must already be initialised, to the hash |
| 135 | // state in |in|. |in| is mutated and left in an empty state. |
| 136 | OPENSSL_EXPORT void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in); |
| 137 | |
David Benjamin | a827d18 | 2018-05-29 10:43:20 -0400 | [diff] [blame] | 138 | // EVP_MD_CTX_reset calls |EVP_MD_CTX_cleanup| followed by |EVP_MD_CTX_init|. It |
| 139 | // returns one. |
| 140 | OPENSSL_EXPORT int EVP_MD_CTX_reset(EVP_MD_CTX *ctx); |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 141 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 143 | // Digest operations. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 145 | // EVP_DigestInit_ex configures |ctx|, which must already have been |
| 146 | // initialised, for a fresh hashing operation using |type|. It returns one on |
Joshua Liebow-Feeser | dea6d90 | 2018-08-13 15:01:04 -0700 | [diff] [blame] | 147 | // success and zero on allocation failure. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 148 | OPENSSL_EXPORT int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, |
| 149 | ENGINE *engine); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 151 | // EVP_DigestInit acts like |EVP_DigestInit_ex| except that |ctx| is |
| 152 | // initialised before use. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 153 | OPENSSL_EXPORT int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 155 | // EVP_DigestUpdate hashes |len| bytes from |data| into the hashing operation |
| 156 | // in |ctx|. It returns one. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 157 | OPENSSL_EXPORT int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, |
| 158 | size_t len); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 160 | // EVP_MAX_MD_SIZE is the largest digest size supported, in bytes. |
| 161 | // Functions that output a digest generally require the buffer have |
| 162 | // at least this much space. |
| 163 | #define EVP_MAX_MD_SIZE 64 // SHA-512 is the longest so far. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 164 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 165 | // EVP_MAX_MD_BLOCK_SIZE is the largest digest block size supported, in |
| 166 | // bytes. |
| 167 | #define EVP_MAX_MD_BLOCK_SIZE 128 // SHA-512 is the longest so far. |
David Benjamin | 582d284 | 2016-04-16 17:10:01 -0400 | [diff] [blame] | 168 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 169 | // EVP_DigestFinal_ex finishes the digest in |ctx| and writes the output to |
| 170 | // |md_out|. |EVP_MD_CTX_size| bytes are written, which is at most |
| 171 | // |EVP_MAX_MD_SIZE|. If |out_size| is not NULL then |*out_size| is set to the |
| 172 | // number of bytes written. It returns one. After this call, the hash cannot be |
| 173 | // updated or finished again until |EVP_DigestInit_ex| is called to start |
| 174 | // another hashing operation. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 175 | OPENSSL_EXPORT int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, |
| 176 | unsigned int *out_size); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 177 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 178 | // EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that |
| 179 | // |EVP_MD_CTX_cleanup| is called on |ctx| before returning. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 180 | OPENSSL_EXPORT int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out, |
| 181 | unsigned int *out_size); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 182 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 183 | // EVP_Digest performs a complete hashing operation in one call. It hashes |len| |
| 184 | // bytes from |data| and writes the digest to |md_out|. |EVP_MD_CTX_size| bytes |
| 185 | // are written, which is at most |EVP_MAX_MD_SIZE|. If |out_size| is not NULL |
| 186 | // then |*out_size| is set to the number of bytes written. It returns one on |
| 187 | // success and zero otherwise. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 188 | OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out, |
| 189 | unsigned int *md_out_size, const EVP_MD *type, |
| 190 | ENGINE *impl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 191 | |
| 192 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 193 | // Digest function accessors. |
| 194 | // |
| 195 | // These functions allow code to learn details about an abstract hash |
| 196 | // function. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 198 | // EVP_MD_type returns a NID identifying |md|. (For example, |NID_sha256|.) |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 199 | OPENSSL_EXPORT int EVP_MD_type(const EVP_MD *md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 201 | // EVP_MD_flags returns the flags for |md|, which is a set of |EVP_MD_FLAG_*| |
| 202 | // values, ORed together. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 203 | OPENSSL_EXPORT uint32_t EVP_MD_flags(const EVP_MD *md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 204 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 205 | // EVP_MD_size returns the digest size of |md|, in bytes. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 206 | OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 208 | // EVP_MD_block_size returns the native block-size of |md|, in bytes. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 209 | OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 210 | |
Shelley Vohr | 98e848a | 2019-09-18 13:23:30 -0700 | [diff] [blame] | 211 | // EVP_MD_FLAG_PKEY_DIGEST indicates that the digest function is used with a |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 212 | // specific public key in order to verify signatures. (For example, |
| 213 | // EVP_dss1.) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | #define EVP_MD_FLAG_PKEY_DIGEST 1 |
| 215 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 216 | // EVP_MD_FLAG_DIGALGID_ABSENT indicates that the parameter type in an X.509 |
| 217 | // DigestAlgorithmIdentifier representing this digest function should be |
| 218 | // undefined rather than NULL. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 219 | #define EVP_MD_FLAG_DIGALGID_ABSENT 2 |
| 220 | |
Shelley Vohr | 98e848a | 2019-09-18 13:23:30 -0700 | [diff] [blame] | 221 | // EVP_MD_FLAG_XOF indicates that the digest is an extensible-output function |
| 222 | // (XOF). This flag is defined for compatibility and will never be set in any |
| 223 | // |EVP_MD| in BoringSSL. |
| 224 | #define EVP_MD_FLAG_XOF 4 |
| 225 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 226 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 227 | // Digest operation accessors. |
| 228 | |
| 229 | // EVP_MD_CTX_md returns the underlying digest function, or NULL if one has not |
| 230 | // been set. |
| 231 | OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx); |
| 232 | |
| 233 | // EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It |
| 234 | // will crash if a digest hasn't been set on |ctx|. |
| 235 | OPENSSL_EXPORT size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx); |
| 236 | |
| 237 | // EVP_MD_CTX_block_size returns the block size of the digest function used by |
| 238 | // |ctx|, in bytes. It will crash if a digest hasn't been set on |ctx|. |
| 239 | OPENSSL_EXPORT size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx); |
| 240 | |
| 241 | // EVP_MD_CTX_type returns a NID describing the digest function used by |ctx|. |
| 242 | // (For example, |NID_sha256|.) It will crash if a digest hasn't been set on |
| 243 | // |ctx|. |
| 244 | OPENSSL_EXPORT int EVP_MD_CTX_type(const EVP_MD_CTX *ctx); |
| 245 | |
| 246 | |
David Benjamin | 42e93b6 | 2017-09-25 16:30:28 -0400 | [diff] [blame] | 247 | // ASN.1 functions. |
| 248 | // |
| 249 | // These functions allow code to parse and serialize AlgorithmIdentifiers for |
| 250 | // hash functions. |
| 251 | |
| 252 | // EVP_parse_digest_algorithm parses an AlgorithmIdentifier structure containing |
| 253 | // a hash function OID (for example, 2.16.840.1.101.3.4.2.1 is SHA-256) and |
| 254 | // advances |cbs|. The parameters field may either be omitted or a NULL. It |
| 255 | // returns the digest function or NULL on error. |
| 256 | OPENSSL_EXPORT const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs); |
| 257 | |
| 258 | // EVP_marshal_digest_algorithm marshals |md| as an AlgorithmIdentifier |
| 259 | // structure and appends the result to |cbb|. It returns one on success and zero |
| 260 | // on error. |
| 261 | OPENSSL_EXPORT int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md); |
| 262 | |
| 263 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 264 | // Deprecated functions. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 266 | // EVP_MD_CTX_copy sets |out|, which must /not/ be initialised, to be a copy of |
| 267 | // |in|. It returns one on success and zero on error. |
Adam Langley | eb7d2ed | 2014-07-30 16:02:14 -0700 | [diff] [blame] | 268 | OPENSSL_EXPORT int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 269 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 270 | // EVP_add_digest does nothing and returns one. It exists only for |
| 271 | // compatibility with OpenSSL. |
Adam Langley | bed8ce7 | 2014-09-05 17:04:51 -0700 | [diff] [blame] | 272 | OPENSSL_EXPORT int EVP_add_digest(const EVP_MD *digest); |
| 273 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 274 | // EVP_get_digestbyname returns an |EVP_MD| given a human readable name in |
| 275 | // |name|, or NULL if the name is unknown. |
Matt Braithwaite | 9f8ef2d | 2015-04-28 19:38:03 -0700 | [diff] [blame] | 276 | OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *); |
| 277 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 278 | // EVP_dss1 returns the value of EVP_sha1(). This was provided by OpenSSL to |
| 279 | // specifiy the original DSA signatures, which were fixed to use SHA-1. Note, |
| 280 | // however, that attempting to sign or verify DSA signatures with the EVP |
| 281 | // interface will always fail. |
Adam Langley | 1aa03f0 | 2016-05-20 08:22:33 -0700 | [diff] [blame] | 282 | OPENSSL_EXPORT const EVP_MD *EVP_dss1(void); |
| 283 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 284 | // EVP_MD_CTX_create calls |EVP_MD_CTX_new|. |
| 285 | OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_create(void); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 287 | // EVP_MD_CTX_destroy calls |EVP_MD_CTX_free|. |
| 288 | OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 289 | |
David Benjamin | b96d470 | 2019-04-27 13:43:06 -0500 | [diff] [blame] | 290 | // EVP_DigestFinalXOF returns zero and adds an error to the error queue. |
| 291 | // BoringSSL does not support any XOF digests. |
| 292 | OPENSSL_EXPORT int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, |
| 293 | size_t len); |
| 294 | |
Shelley Vohr | 98e848a | 2019-09-18 13:23:30 -0700 | [diff] [blame] | 295 | // EVP_MD_meth_get_flags calls |EVP_MD_flags|. |
| 296 | OPENSSL_EXPORT uint32_t EVP_MD_meth_get_flags(const EVP_MD *md); |
| 297 | |
Adam Langley | 5eeaf30 | 2020-09-18 10:28:46 -0700 | [diff] [blame] | 298 | // EVP_MD_CTX_set_flags does nothing. |
| 299 | OPENSSL_EXPORT void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags); |
| 300 | |
| 301 | // EVP_MD_CTX_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS |
| 302 | // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms |
| 303 | // (it's up the the caller to use the FIPS module in a fashion compliant with |
| 304 | // their needs). Thus this exists only to allow code to compile. |
| 305 | #define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0 |
| 306 | |
David Benjamin | ae7c178 | 2021-07-01 11:27:04 -0400 | [diff] [blame] | 307 | // EVP_MD_nid calls |EVP_MD_type|. |
| 308 | OPENSSL_EXPORT int EVP_MD_nid(const EVP_MD *md); |
| 309 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | |
| 311 | struct evp_md_pctx_ops; |
| 312 | |
| 313 | struct env_md_ctx_st { |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 314 | // digest is the underlying digest function, or NULL if not set. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | const EVP_MD *digest; |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 316 | // md_data points to a block of memory that contains the hash-specific |
| 317 | // context. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 318 | void *md_data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 320 | // pctx is an opaque (at this layer) pointer to additional context that |
| 321 | // EVP_PKEY functions may store in this object. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | EVP_PKEY_CTX *pctx; |
| 323 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 324 | // pctx_ops, if not NULL, points to a vtable that contains functions to |
| 325 | // manipulate |pctx|. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 326 | const struct evp_md_pctx_ops *pctx_ops; |
| 327 | } /* EVP_MD_CTX */; |
| 328 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 329 | |
| 330 | #if defined(__cplusplus) |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 331 | } // extern C |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 332 | |
| 333 | #if !defined(BORINGSSL_NO_CXX) |
| 334 | extern "C++" { |
| 335 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 336 | BSSL_NAMESPACE_BEGIN |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 337 | |
David Benjamin | 81f030b | 2016-08-12 14:48:19 -0400 | [diff] [blame] | 338 | BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_free) |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 339 | |
| 340 | using ScopedEVP_MD_CTX = |
David Benjamin | e9109cb | 2021-05-22 17:20:53 -0400 | [diff] [blame] | 341 | internal::StackAllocatedMovable<EVP_MD_CTX, int, EVP_MD_CTX_init, |
| 342 | EVP_MD_CTX_cleanup, EVP_MD_CTX_move>; |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 343 | |
Joshua Liebow-Feeser | 8c7c635 | 2018-08-26 18:53:36 -0700 | [diff] [blame] | 344 | BSSL_NAMESPACE_END |
David Benjamin | f0e935d | 2016-09-06 18:10:19 -0400 | [diff] [blame] | 345 | |
| 346 | } // extern C++ |
| 347 | #endif |
| 348 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | #endif |
| 350 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | #define DIGEST_R_INPUT_NOT_INITIALIZED 100 |
David Benjamin | 3cb047e | 2017-03-14 00:48:11 -0400 | [diff] [blame] | 352 | #define DIGEST_R_DECODE_ERROR 101 |
| 353 | #define DIGEST_R_UNKNOWN_HASH 102 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 354 | |
David Benjamin | 4512b79 | 2017-08-18 19:21:50 -0400 | [diff] [blame] | 355 | #endif // OPENSSL_HEADER_DIGEST_H |