digest.h (13379B)
1 // Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef OPENSSL_HEADER_DIGEST_H 16 #define OPENSSL_HEADER_DIGEST_H 17 18 #include <openssl/base.h> // IWYU pragma: export 19 20 #if defined(__cplusplus) 21 extern "C" { 22 #endif 23 24 25 // Digest functions. 26 // 27 // An EVP_MD abstracts the details of a specific hash function allowing code to 28 // deal with the concept of a "hash function" without needing to know exactly 29 // which hash function it is. 30 31 32 // Hash algorithms. 33 // 34 // The following functions return |EVP_MD| objects that implement the named hash 35 // function. 36 37 OPENSSL_EXPORT const EVP_MD *EVP_md4(void); 38 OPENSSL_EXPORT const EVP_MD *EVP_md5(void); 39 OPENSSL_EXPORT const EVP_MD *EVP_sha1(void); 40 OPENSSL_EXPORT const EVP_MD *EVP_sha224(void); 41 OPENSSL_EXPORT const EVP_MD *EVP_sha256(void); 42 OPENSSL_EXPORT const EVP_MD *EVP_sha384(void); 43 OPENSSL_EXPORT const EVP_MD *EVP_sha512(void); 44 OPENSSL_EXPORT const EVP_MD *EVP_sha512_256(void); 45 OPENSSL_EXPORT const EVP_MD *EVP_blake2b256(void); 46 47 // EVP_md5_sha1 is a TLS-specific |EVP_MD| which computes the concatenation of 48 // MD5 and SHA-1, as used in TLS 1.1 and below. 49 OPENSSL_EXPORT const EVP_MD *EVP_md5_sha1(void); 50 51 // EVP_get_digestbynid returns an |EVP_MD| for the given NID, or NULL if no 52 // such digest is known. 53 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbynid(int nid); 54 55 // EVP_get_digestbyobj returns an |EVP_MD| for the given |ASN1_OBJECT|, or NULL 56 // if no such digest is known. 57 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj); 58 59 60 // Digest contexts. 61 // 62 // An EVP_MD_CTX represents the state of a specific digest operation in 63 // progress. 64 65 // EVP_MD_CTX_init initialises an, already allocated, |EVP_MD_CTX|. This is the 66 // same as setting the structure to zero. 67 OPENSSL_EXPORT void EVP_MD_CTX_init(EVP_MD_CTX *ctx); 68 69 // EVP_MD_CTX_new allocates and initialises a fresh |EVP_MD_CTX| and returns 70 // it, or NULL on allocation failure. The caller must use |EVP_MD_CTX_free| to 71 // release the resulting object. 72 OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_new(void); 73 74 // EVP_MD_CTX_cleanup frees any resources owned by |ctx| and resets it to a 75 // freshly initialised state. It does not free |ctx| itself. It returns one. 76 OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx); 77 78 // EVP_MD_CTX_cleanse zeros the digest state in |ctx| and then performs the 79 // actions of |EVP_MD_CTX_cleanup|. Note that some |EVP_MD_CTX| objects contain 80 // more than just a digest (e.g. those resulting from |EVP_DigestSignInit|) but 81 // this function does not zero out more than just the digest state even in that 82 // case. 83 OPENSSL_EXPORT void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx); 84 85 // EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself. 86 OPENSSL_EXPORT void EVP_MD_CTX_free(EVP_MD_CTX *ctx); 87 88 // EVP_MD_CTX_copy_ex sets |out|, which must already be initialised, to be a 89 // copy of |in|. It returns one on success and zero on allocation failure. 90 OPENSSL_EXPORT int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in); 91 92 // EVP_MD_CTX_move sets |out|, which must already be initialised, to the hash 93 // state in |in|. |in| is mutated and left in an empty state. 94 OPENSSL_EXPORT void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in); 95 96 // EVP_MD_CTX_reset calls |EVP_MD_CTX_cleanup| followed by |EVP_MD_CTX_init|. It 97 // returns one. 98 OPENSSL_EXPORT int EVP_MD_CTX_reset(EVP_MD_CTX *ctx); 99 100 101 // Digest operations. 102 103 // EVP_DigestInit_ex configures |ctx|, which must already have been 104 // initialised, for a fresh hashing operation using |type|. It returns one on 105 // success and zero on allocation failure. 106 OPENSSL_EXPORT int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, 107 ENGINE *engine); 108 109 // EVP_DigestInit acts like |EVP_DigestInit_ex| except that |ctx| is 110 // initialised before use. 111 OPENSSL_EXPORT int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type); 112 113 // EVP_DigestUpdate hashes |len| bytes from |data| into the hashing operation 114 // in |ctx|. It returns one. 115 OPENSSL_EXPORT int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, 116 size_t len); 117 118 // EVP_MAX_MD_SIZE is the largest digest size supported, in bytes. 119 // Functions that output a digest generally require the buffer have 120 // at least this much space. 121 #define EVP_MAX_MD_SIZE 64 // SHA-512 is the longest so far. 122 123 // EVP_MAX_MD_BLOCK_SIZE is the largest digest block size supported, in 124 // bytes. 125 #define EVP_MAX_MD_BLOCK_SIZE 128 // SHA-512 is the longest so far. 126 127 // EVP_DigestFinal_ex finishes the digest in |ctx| and writes the output to 128 // |md_out|. |EVP_MD_CTX_size| bytes are written, which is at most 129 // |EVP_MAX_MD_SIZE|. If |out_size| is not NULL then |*out_size| is set to the 130 // number of bytes written. It returns one. After this call, the hash cannot be 131 // updated or finished again until |EVP_DigestInit_ex| is called to start 132 // another hashing operation. 133 OPENSSL_EXPORT int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out, 134 unsigned int *out_size); 135 136 // EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that 137 // |EVP_MD_CTX_cleanup| is called on |ctx| before returning. 138 OPENSSL_EXPORT int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out, 139 unsigned int *out_size); 140 141 // EVP_Digest performs a complete hashing operation in one call. It hashes |len| 142 // bytes from |data| and writes the digest to |md_out|. |EVP_MD_CTX_size| bytes 143 // are written, which is at most |EVP_MAX_MD_SIZE|. If |out_size| is not NULL 144 // then |*out_size| is set to the number of bytes written. It returns one on 145 // success and zero otherwise. 146 OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out, 147 unsigned int *md_out_size, const EVP_MD *type, 148 ENGINE *impl); 149 150 151 // Digest function accessors. 152 // 153 // These functions allow code to learn details about an abstract hash 154 // function. 155 156 // EVP_MD_type returns a NID identifying |md|. (For example, |NID_sha256|.) 157 OPENSSL_EXPORT int EVP_MD_type(const EVP_MD *md); 158 159 // EVP_MD_flags returns the flags for |md|, which is a set of |EVP_MD_FLAG_*| 160 // values, ORed together. 161 OPENSSL_EXPORT uint32_t EVP_MD_flags(const EVP_MD *md); 162 163 // EVP_MD_size returns the digest size of |md|, in bytes. 164 OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md); 165 166 // EVP_MD_block_size returns the native block-size of |md|, in bytes. 167 OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md); 168 169 // EVP_MD_FLAG_DIGALGID_ABSENT indicates that the parameter type in an X.509 170 // DigestAlgorithmIdentifier representing this digest function should be 171 // undefined rather than NULL. 172 #define EVP_MD_FLAG_DIGALGID_ABSENT 2 173 174 // EVP_MD_FLAG_XOF indicates that the digest is an extensible-output function 175 // (XOF). This flag is defined for compatibility and will never be set in any 176 // |EVP_MD| in BoringSSL. 177 #define EVP_MD_FLAG_XOF 4 178 179 180 // Digest operation accessors. 181 182 // EVP_MD_CTX_get0_md returns the underlying digest function, or NULL if one has 183 // not been set. 184 OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx); 185 186 // EVP_MD_CTX_md returns the underlying digest function, or NULL if one has not 187 // been set. (This is the same as |EVP_MD_CTX_get0_md| but OpenSSL has 188 // deprecated this spelling.) 189 OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx); 190 191 // EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It 192 // will crash if a digest hasn't been set on |ctx|. 193 OPENSSL_EXPORT size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx); 194 195 // EVP_MD_CTX_block_size returns the block size of the digest function used by 196 // |ctx|, in bytes. It will crash if a digest hasn't been set on |ctx|. 197 OPENSSL_EXPORT size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx); 198 199 // EVP_MD_CTX_type returns a NID describing the digest function used by |ctx|. 200 // (For example, |NID_sha256|.) It will crash if a digest hasn't been set on 201 // |ctx|. 202 OPENSSL_EXPORT int EVP_MD_CTX_type(const EVP_MD_CTX *ctx); 203 204 205 // ASN.1 functions. 206 // 207 // These functions allow code to parse and serialize AlgorithmIdentifiers for 208 // hash functions. 209 210 // EVP_parse_digest_algorithm parses an AlgorithmIdentifier structure containing 211 // a hash function OID (for example, 2.16.840.1.101.3.4.2.1 is SHA-256) and 212 // advances |cbs|. The parameters field may either be omitted or a NULL. It 213 // returns the digest function or NULL on error. 214 OPENSSL_EXPORT const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs); 215 216 // EVP_marshal_digest_algorithm marshals |md| as an AlgorithmIdentifier 217 // structure and appends the result to |cbb|. It returns one on success and zero 218 // on error. It sets the parameters field to NULL. Use 219 // |EVP_marshal_digest_algorithm_no_params| to omit the parameters instead. 220 // 221 // In general, the parameters should be omitted for digest algorithms, but the 222 // following specifications require a NULL parameter instead. 223 // 224 // - Hash algorithms and MGF-1 hash algorithms used in RSASSA-PSS and RSAES-OAEP 225 // (see RFC 4055, Section 2.1) 226 // - The hash algorithm in the DigestInfo structure of RSASSA-PKCS1-v1_5 (see 227 // RFC 8017, Appendix A.2.4) 228 // 229 // Some existing software also uses NULL parameters in other contexts. In 230 // practice, digest algorithms are encoded wildly inconsistently. 231 OPENSSL_EXPORT int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md); 232 233 // EVP_marshal_digest_algorithm_no_params behaves like 234 // |EVP_marshal_digest_algorithm| but omits the parameters field. 235 OPENSSL_EXPORT int EVP_marshal_digest_algorithm_no_params(CBB *cbb, 236 const EVP_MD *md); 237 238 239 // Deprecated functions. 240 241 // EVP_MD_CTX_copy sets |out|, which must /not/ be initialised, to be a copy of 242 // |in|. It returns one on success and zero on error. 243 OPENSSL_EXPORT int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in); 244 245 // EVP_add_digest does nothing and returns one. It exists only for 246 // compatibility with OpenSSL. 247 OPENSSL_EXPORT int EVP_add_digest(const EVP_MD *digest); 248 249 // EVP_get_digestbyname returns an |EVP_MD| given a human readable name in 250 // |name|, or NULL if the name is unknown. 251 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *); 252 253 // EVP_dss1 returns the value of EVP_sha1(). This was provided by OpenSSL to 254 // specifiy the original DSA signatures, which were fixed to use SHA-1. Note, 255 // however, that attempting to sign or verify DSA signatures with the EVP 256 // interface will always fail. 257 OPENSSL_EXPORT const EVP_MD *EVP_dss1(void); 258 259 // EVP_MD_CTX_create calls |EVP_MD_CTX_new|. 260 OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_create(void); 261 262 // EVP_MD_CTX_destroy calls |EVP_MD_CTX_free|. 263 OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx); 264 265 // EVP_DigestFinalXOF returns zero and adds an error to the error queue. 266 // BoringSSL does not support any XOF digests. 267 OPENSSL_EXPORT int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out, 268 size_t len); 269 270 // EVP_MD_meth_get_flags calls |EVP_MD_flags|. 271 OPENSSL_EXPORT uint32_t EVP_MD_meth_get_flags(const EVP_MD *md); 272 273 // EVP_MD_CTX_set_flags does nothing. 274 OPENSSL_EXPORT void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags); 275 276 // EVP_MD_CTX_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS 277 // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms 278 // (it's up the the caller to use the FIPS module in a fashion compliant with 279 // their needs). Thus this exists only to allow code to compile. 280 #define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0 281 282 // EVP_MD_nid calls |EVP_MD_type|. 283 OPENSSL_EXPORT int EVP_MD_nid(const EVP_MD *md); 284 285 286 struct evp_md_pctx_ops; 287 288 // env_md_ctx_st is typoed ("evp" -> "env"), but the typo comes from OpenSSL 289 // and some consumers forward-declare these structures so we're leaving it 290 // alone. 291 struct env_md_ctx_st { 292 // digest is the underlying digest function, or NULL if not set. 293 const EVP_MD *digest; 294 // md_data points to a block of memory that contains the hash-specific 295 // context. 296 void *md_data; 297 298 // pctx is an opaque (at this layer) pointer to additional context that 299 // EVP_PKEY functions may store in this object. 300 EVP_PKEY_CTX *pctx; 301 302 // pctx_ops, if not NULL, points to a vtable that contains functions to 303 // manipulate |pctx|. 304 const struct evp_md_pctx_ops *pctx_ops; 305 } /* EVP_MD_CTX */; 306 307 308 #if defined(__cplusplus) 309 } // extern C 310 311 #if !defined(BORINGSSL_NO_CXX) 312 extern "C++" { 313 314 BSSL_NAMESPACE_BEGIN 315 316 BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_free) 317 318 using ScopedEVP_MD_CTX = 319 internal::StackAllocatedMovable<EVP_MD_CTX, int, EVP_MD_CTX_init, 320 EVP_MD_CTX_cleanup, EVP_MD_CTX_move>; 321 322 BSSL_NAMESPACE_END 323 324 } // extern C++ 325 #endif 326 327 #endif 328 329 #define DIGEST_R_INPUT_NOT_INITIALIZED 100 330 #define DIGEST_R_DECODE_ERROR 101 331 #define DIGEST_R_UNKNOWN_HASH 102 332 333 #endif // OPENSSL_HEADER_DIGEST_H