Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/include/openssl/digest.h

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #ifndef OPENSSL_HEADER_DIGEST_H
5 #define OPENSSL_HEADER_DIGEST_H
6
7 #include <stdbool.h>
8
9 #include <openssl/base.h>
10
11 #if defined(__cplusplus)
12 extern "C" {
13 #endif
14
15 // Digest functions.
16 //
17 // An EVP_MD abstracts the details of a specific hash function allowing code to
18 // deal with the concept of a "hash function" without needing to know exactly
19 // which hash function it is.
20
21
22 // Hash algorithms.
23 //
24 // The following functions return |EVP_MD| objects that implement the named hash
25 // function.
26
27 OPENSSL_EXPORT const EVP_MD *EVP_md4(void);
28 OPENSSL_EXPORT const EVP_MD *EVP_md5(void);
29 OPENSSL_EXPORT const EVP_MD *EVP_ripemd160(void);
30 OPENSSL_EXPORT const EVP_MD *EVP_sha1(void);
31 OPENSSL_EXPORT const EVP_MD *EVP_sha224(void);
32 OPENSSL_EXPORT const EVP_MD *EVP_sha256(void);
33 OPENSSL_EXPORT const EVP_MD *EVP_sha384(void);
34 OPENSSL_EXPORT const EVP_MD *EVP_sha512(void);
35 OPENSSL_EXPORT const EVP_MD *EVP_sha512_224(void);
36 OPENSSL_EXPORT const EVP_MD *EVP_sha512_256(void);
37 OPENSSL_EXPORT const EVP_MD *EVP_sha3_224(void);
38 OPENSSL_EXPORT const EVP_MD *EVP_sha3_256(void);
39 OPENSSL_EXPORT const EVP_MD *EVP_sha3_384(void);
40 OPENSSL_EXPORT const EVP_MD *EVP_sha3_512(void);
41 OPENSSL_EXPORT const EVP_MD *EVP_shake128(void);
42 OPENSSL_EXPORT const EVP_MD *EVP_shake256(void);
43 OPENSSL_EXPORT const EVP_MD *EVP_blake2b256(void);
44
45 // EVP_md5_sha1 is a TLS-specific |EVP_MD| which computes the concatenation of
46 // MD5 and SHA-1, as used in TLS 1.1 and below.
47 OPENSSL_EXPORT const EVP_MD *EVP_md5_sha1(void);
48
49 // EVP_get_digestbynid returns an |EVP_MD| for the given NID, or NULL if no
50 // such digest is known.
51 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbynid(int nid);
52
53 // EVP_get_digestbyobj returns an |EVP_MD| for the given |ASN1_OBJECT|, or NULL
54 // if no such digest is known.
55 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *obj);
56
57
58 // Digest contexts.
59 //
60 // An EVP_MD_CTX represents the state of a specific digest operation in
61 // progress.
62
63 // EVP_MD_CTX_init initialises an, already allocated, |EVP_MD_CTX|. This is the
64 // same as setting the structure to zero.
65 OPENSSL_EXPORT void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
66
67 // EVP_MD_CTX_new allocates and initialises a fresh |EVP_MD_CTX| and returns
68 // it, or NULL on allocation failure. The caller must use |EVP_MD_CTX_free| to
69 // release the resulting object.
70 OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_new(void);
71
72 // EVP_MD_CTX_cleanup frees any resources owned by |ctx| and resets it to a
73 // freshly initialised state. It does not free |ctx| itself. It returns one.
74 OPENSSL_EXPORT int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
75
76 // EVP_MD_CTX_cleanse zeros the digest state in |ctx| and then performs the
77 // actions of |EVP_MD_CTX_cleanup|. Note that some |EVP_MD_CTX| objects contain
78 // more than just a digest (e.g. those resulting from |EVP_DigestSignInit|) but
79 // this function does not zero out more than just the digest state even in that
80 // case.
81 OPENSSL_EXPORT void EVP_MD_CTX_cleanse(EVP_MD_CTX *ctx);
82
83 // EVP_MD_CTX_free calls |EVP_MD_CTX_cleanup| and then frees |ctx| itself.
84 OPENSSL_EXPORT void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
85
86 // EVP_MD_CTX_copy_ex sets |out|, which must already be initialised, to be a
87 // copy of |in|. It returns one on success and zero on allocation failure.
88 OPENSSL_EXPORT int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
89
90 // EVP_MD_CTX_move sets |out|, which must already be initialised, to the hash
91 // state in |in|. |in| is mutated and left in an empty state.
92 OPENSSL_EXPORT void EVP_MD_CTX_move(EVP_MD_CTX *out, EVP_MD_CTX *in);
93
94 // EVP_MD_CTX_reset calls |EVP_MD_CTX_cleanup| followed by |EVP_MD_CTX_init|. It
95 // returns one.
96 OPENSSL_EXPORT int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
97
98
99 // Digest operations.
100
101 // EVP_DigestInit_ex configures |ctx|, which must already have been
102 // initialised, for a fresh hashing operation using |type|. It returns one on
103 // success and zero on allocation failure.
104 //
105 // On failure, |ctx| must be released with |EVP_MD_CTX_[cleanup,free]|.
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_CHAINING_LENGTH is the largest chaining length supported, in
124 // bytes. This constant is only for Merkle-Damgard-based hashed functions
125 // like SHA-1, SHA-2, and MD5. The chaining length is defined as the output
126 // length of the hash in bytes, before any truncation (e.g., 32 for SHA-224 and
127 // SHA-256, 64 for SHA-384 and SHA-512).
128 // This constant is only used internally by HMAC.
129 #define EVP_MAX_MD_CHAINING_LENGTH 64 // SHA-512 has the longest chaining length so far
130
131 // EVP_MAX_MD_BLOCK_SIZE is the largest digest block size supported, in
132 // bytes.
133 #define EVP_MAX_MD_BLOCK_SIZE 144 // SHA3-224 has the largest block size so far
134
135 // EVP_DigestFinal_ex finishes the digest in |ctx| and writes the output to
136 // |md_out|. |EVP_MD_CTX_size| bytes are written, which is at most
137 // |EVP_MAX_MD_SIZE|. If |out_size| is not NULL then |*out_size| is set to the
138 // number of bytes written. It returns one. After this call, the hash cannot be
139 // updated or finished again until |EVP_DigestInit_ex| is called to start
140 // another hashing operation.
141 OPENSSL_EXPORT int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, uint8_t *md_out,
142 unsigned int *out_size);
143
144 // EVP_DigestFinal acts like |EVP_DigestFinal_ex| except that
145 // |EVP_MD_CTX_cleanup| is called on |ctx| before returning.
146 OPENSSL_EXPORT int EVP_DigestFinal(EVP_MD_CTX *ctx, uint8_t *md_out,
147 unsigned int *out_size);
148
149 // EVP_Digest performs a complete hashing operation in one call. It hashes |len|
150 // bytes from |data| and writes the digest to |md_out|. |EVP_MD_CTX_size| bytes
151 // are written, which is at most |EVP_MAX_MD_SIZE|. If |out_size| is not NULL
152 // then |*out_size| is set to the number of bytes written. It returns one on
153 // success and zero otherwise. If |type| is an XOF, |out_size| must be set to
154 // the desired output length.
155 OPENSSL_EXPORT int EVP_Digest(const void *data, size_t len, uint8_t *md_out,
156 unsigned int *out_size, const EVP_MD *type,
157 ENGINE *impl);
158
159
160 // Digest function accessors.
161 //
162 // These functions allow code to learn details about an abstract hash
163 // function.
164
165 // EVP_MD_type returns a NID identifying |md|. (For example, |NID_sha256|.)
166 OPENSSL_EXPORT int EVP_MD_type(const EVP_MD *md);
167
168 // EVP_MD_flags returns the flags for |md|, which is a set of |EVP_MD_FLAG_*|
169 // values, ORed together.
170 OPENSSL_EXPORT uint32_t EVP_MD_flags(const EVP_MD *md);
171
172 // EVP_MD_size returns the digest size of |md|, in bytes.
173 OPENSSL_EXPORT size_t EVP_MD_size(const EVP_MD *md);
174
175 // EVP_MD_block_size returns the native block-size of |md|, in bytes.
176 OPENSSL_EXPORT size_t EVP_MD_block_size(const EVP_MD *md);
177
178 // EVP_MD_FLAG_DIGALGID_ABSENT indicates that the parameter type in an X.509
179 // DigestAlgorithmIdentifier representing this digest function should be
180 // undefined rather than NULL.
181 #define EVP_MD_FLAG_DIGALGID_ABSENT 2
182
183 // EVP_MD_FLAG_XOF indicates that the digest is an extensible-output function
184 // (XOF).
185 #define EVP_MD_FLAG_XOF 4
186
187
188 // Digest operation accessors.
189
190 // EVP_MD_CTX_md returns the underlying digest function, or NULL if one has not
191 // been set.
192 OPENSSL_EXPORT const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
193
194 // EVP_MD_CTX_size returns the digest size of |ctx|, in bytes. It
195 // will crash if a digest hasn't been set on |ctx|.
196 OPENSSL_EXPORT size_t EVP_MD_CTX_size(const EVP_MD_CTX *ctx);
197
198 // EVP_MD_CTX_block_size returns the block size of the digest function used by
199 // |ctx|, in bytes. It will crash if a digest hasn't been set on |ctx|.
200 OPENSSL_EXPORT size_t EVP_MD_CTX_block_size(const EVP_MD_CTX *ctx);
201
202 // EVP_MD_CTX_type returns a NID describing the digest function used by |ctx|.
203 // (For example, |NID_sha256|.) It will crash if a digest hasn't been set on
204 // |ctx|.
205 OPENSSL_EXPORT int EVP_MD_CTX_type(const EVP_MD_CTX *ctx);
206
207
208 // ASN.1 functions.
209 //
210 // These functions allow code to parse and serialize AlgorithmIdentifiers for
211 // hash functions.
212
213 // EVP_parse_digest_algorithm parses an AlgorithmIdentifier structure containing
214 // a hash function OID (for example, 2.16.840.1.101.3.4.2.1 is SHA-256) and
215 // advances |cbs|. The parameters field may either be omitted or a NULL. It
216 // returns the digest function or NULL on error.
217 OPENSSL_EXPORT const EVP_MD *EVP_parse_digest_algorithm(CBS *cbs);
218
219 // EVP_marshal_digest_algorithm marshals |md| as an AlgorithmIdentifier
220 // structure and appends the result to |cbb|. It returns one on success and zero
221 // on error.
222 OPENSSL_EXPORT int EVP_marshal_digest_algorithm(CBB *cbb, const EVP_MD *md);
223
224
225 // Deprecated functions.
226
227 // EVP_MD_CTX_copy sets |out|, which must /not/ be initialised, to be a copy of
228 // |in|. It returns one on success and zero on error.
229 OPENSSL_EXPORT int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in);
230
231 // EVP_get_digestbyname returns an |EVP_MD| given a human readable name in
232 // |name|, or NULL if the name is unknown.
233 OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *);
234
235 // EVP_MD_CTX_create calls |EVP_MD_CTX_new|.
236 OPENSSL_EXPORT EVP_MD_CTX *EVP_MD_CTX_create(void);
237
238 // EVP_MD_CTX_destroy calls |EVP_MD_CTX_free|.
239 OPENSSL_EXPORT void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
240
241 // EVP_DigestFinalXOF behaves like |EVP_DigestFinal| for XOF digests, writing
242 // |len| bytes of extended output to |out|.
243 OPENSSL_EXPORT int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, uint8_t *out,
244 size_t len);
245
246 // EVP_DigestSqueeze provides byte-wise streaming XOF output generation for
247 // XOF digests, writing |len| bytes of extended output to |out|. It can be
248 // called multiple times with arbitrary length |len| output requests.
249 // It returns one on success and zero on error.
250 OPENSSL_EXPORT int EVP_DigestSqueeze(EVP_MD_CTX *ctx, uint8_t *out,
251 size_t len);
252
253 // EVP_MD_meth_get_flags calls |EVP_MD_flags|.
254 OPENSSL_EXPORT uint32_t EVP_MD_meth_get_flags(const EVP_MD *md);
255
256 // EVP_MD_nid calls |EVP_MD_type|.
257 OPENSSL_EXPORT int EVP_MD_nid(const EVP_MD *md);
258
259 // EVP_MD_CTX_set_pkey_ctx sets |ctx|'s |EVP_PKEY_CTX| reference to |pctx|.
260 // The |EVP_PKEY_CTX| object |pctx| needs to have been initialised before
261 // associating it with |ctx|. The hash functions associated to |ctx| and |pctx|
262 // must be equal. Once |EVP_MD_CTX_set_pkey_ctx| is called, the caller is
263 // responsible for freeing |pctx|. Calling |EVP_MD_CTX_cleanup| will not free
264 // |pctx|.
265 //
266 // A NULL |pctx| pointer is also allowed to set the |EVP_PKEY_CTX| reference
267 // inside |ctx| to NULL. However, even when doing so, the caller is still
268 // responsible for freeing the |pctx| pointer that had originally been
269 // associated.
270 //
271 // |EVP_MD_CTX_set_pkey_ctx| will overwrite any |EVP_PKEY_CTX| object associated
272 // to |ctx|. If it was not associated through a previous
273 // |EVP_MD_CTX_set_pkey_ctx| call, it will be freed first.
274 OPENSSL_EXPORT void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx,
275 EVP_PKEY_CTX *pctx);
276
277 // EVP_MD_CTX_get_pkey_ctx returns the pointer of |ctx|'s |EVP_PKEY_CTX|.
278 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx);
279
280 // EVP_MD_CTX_pkey_ctx is a legacy alias of |EVP_MD_CTX_get_pkey_ctx|.
281 OPENSSL_EXPORT EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
282
283 struct evp_md_pctx_ops;
284
285 // env_md_ctx_st is typoed ("evp" -> "env"), but the typo comes from OpenSSL
286 // and some consumers forward-declare these structures so we're leaving it
287 // alone.
288 struct env_md_ctx_st {
289 // digest is the underlying digest function, or NULL if not set.
290 const EVP_MD *digest;
291 // md_data points to a block of memory that contains the hash-specific
292 // context.
293 void *md_data;
294
295 // update is usually copied from |digest->update|. This is only different
296 // when consumed through |EVP_PKEY_HMAC|.
297 // TODO: Look into untangling this, so that |EVP_PKEY_HMAC| can directly call
298 // |digest->update|. |digest->update| operates against |md_data| above, but
299 // |HMAC_CTX| maintains its own data state in |HMAC_CTX->md_ctx|.
300 // |HMAC_Update| also has an additional state transition to handle.
301 int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
302
303 // pctx is an opaque (at this layer) pointer to additional context that
304 // EVP_PKEY functions may store in this object.
305 EVP_PKEY_CTX *pctx;
306
307 // pctx_ops, if not NULL, points to a vtable that contains functions to
308 // manipulate |pctx|.
309 const struct evp_md_pctx_ops *pctx_ops;
310
311 // flags is only used for two cases.
312 // 1. Set flag |EVP_MD_CTX_FLAG_KEEP_PKEY_CTX|, so as to let |*pctx| refrain
313 // from being freed when |*pctx| was set externally with
314 // |EVP_MD_CTX_set_pkey_ctx|.
315 // 2. Set flag |EVP_MD_CTX_HMAC| for |EVP_PKEY_HMAC|.
316 unsigned long flags;
317 } /* EVP_MD_CTX */;
318
319
320 // General No-op Functions [Deprecated].
321
322 // EVP_MD_unstable_sha3_enable is a no-op as SHA3 is always enabled.
323 OPENSSL_EXPORT OPENSSL_DEPRECATED void EVP_MD_unstable_sha3_enable(bool enable);
324
325 // EVP_MD_unstable_sha3_is_enabled always returns true as SHA3 is always
326 // enabled.
327 OPENSSL_EXPORT OPENSSL_DEPRECATED bool EVP_MD_unstable_sha3_is_enabled(void);
328
329 // EVP_MD_CTX_set_flags does nothing. We strongly discourage doing any
330 // additional configurations when consuming |EVP_MD_CTX|.
331 OPENSSL_EXPORT OPENSSL_DEPRECATED void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx,
332 int flags);
333
334 // EVP_MD_CTX_FLAG_NON_FIPS_ALLOW is meaningless. In OpenSSL it permits non-FIPS
335 // algorithms in FIPS mode. But BoringSSL FIPS mode doesn't prohibit algorithms
336 // (it's up the the caller to use the FIPS module in a fashion compliant with
337 // their needs). Thus this exists only to allow code to compile.
338 #define EVP_MD_CTX_FLAG_NON_FIPS_ALLOW 0
339
340 // EVP_add_digest does nothing and returns one. It exists only for
341 // compatibility with OpenSSL, which requires manually loading supported digests
342 // when certain options are turned on.
343 OPENSSL_EXPORT OPENSSL_DEPRECATED int EVP_add_digest(const EVP_MD *digest);
344
345 // EVP_md_null is a "null" message digest that does nothing: i.e. the hash it
346 // returns is of zero length. Included for OpenSSL compatibility
347 OPENSSL_EXPORT OPENSSL_DEPRECATED const EVP_MD *EVP_md_null(void);
348
349
350 #if defined(__cplusplus)
351 } // extern C
352
353 #if !defined(BORINGSSL_NO_CXX)
354 extern "C++" {
355
356 BSSL_NAMESPACE_BEGIN
357
358 BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_free)
359
360 using ScopedEVP_MD_CTX =
361 internal::StackAllocatedMovable<EVP_MD_CTX, int, EVP_MD_CTX_init,
362 EVP_MD_CTX_cleanup, EVP_MD_CTX_move>;
363
364 BSSL_NAMESPACE_END
365
366 } // extern C++
367 #endif
368
369 #endif
370
371 #define DIGEST_R_INPUT_NOT_INITIALIZED 100
372 #define DIGEST_R_DECODE_ERROR 101
373 #define DIGEST_R_UNKNOWN_HASH 102
374
375 #endif // OPENSSL_HEADER_DIGEST_H