xref: /freebsd/crypto/openssl/doc/man3/EVP_DigestInit.pod (revision 02e9120893770924227138ba49df1edb3896112a)
1=pod
2
3=head1 NAME
4
5EVP_MD_fetch, EVP_MD_up_ref, EVP_MD_free,
6EVP_MD_get_params, EVP_MD_gettable_params,
7EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy,
8EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl,
9EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
10EVP_MD_settable_ctx_params, EVP_MD_gettable_ctx_params,
11EVP_MD_CTX_settable_params, EVP_MD_CTX_gettable_params,
12EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
13EVP_Q_digest, EVP_Digest, EVP_DigestInit_ex2, EVP_DigestInit_ex, EVP_DigestInit,
14EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
15EVP_MD_is_a, EVP_MD_get0_name, EVP_MD_get0_description,
16EVP_MD_names_do_all, EVP_MD_get0_provider, EVP_MD_get_type,
17EVP_MD_get_pkey_type, EVP_MD_get_size, EVP_MD_get_block_size, EVP_MD_get_flags,
18EVP_MD_CTX_get0_name, EVP_MD_CTX_md, EVP_MD_CTX_get0_md, EVP_MD_CTX_get1_md,
19EVP_MD_CTX_get_type, EVP_MD_CTX_get_size, EVP_MD_CTX_get_block_size,
20EVP_MD_CTX_get0_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
21EVP_md_null,
22EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
23EVP_MD_CTX_get_pkey_ctx, EVP_MD_CTX_set_pkey_ctx,
24EVP_MD_do_all_provided,
25EVP_MD_type, EVP_MD_nid, EVP_MD_name, EVP_MD_pkey_type, EVP_MD_size,
26EVP_MD_block_size, EVP_MD_flags, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
27EVP_MD_CTX_type, EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_md_data
28- EVP digest routines
29
30=head1 SYNOPSIS
31
32 #include <openssl/evp.h>
33
34 EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
35                      const char *properties);
36 int EVP_MD_up_ref(EVP_MD *md);
37 void EVP_MD_free(EVP_MD *md);
38 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]);
39 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest);
40 EVP_MD_CTX *EVP_MD_CTX_new(void);
41 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
42 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
43 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
44 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
45 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
46 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md);
47 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md);
48 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx);
49 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx);
50 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
51 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
52 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
53
54 int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
55                  const void *data, size_t datalen,
56                  unsigned char *md, size_t *mdlen);
57 int EVP_Digest(const void *data, size_t count, unsigned char *md,
58                unsigned int *size, const EVP_MD *type, ENGINE *impl);
59 int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
60                        const OSSL_PARAM params[]);
61 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
62 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
63 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
64 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
65
66 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
67
68 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
69 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
70
71 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
72
73 const char *EVP_MD_get0_name(const EVP_MD *md);
74 const char *EVP_MD_get0_description(const EVP_MD *md);
75 int EVP_MD_is_a(const EVP_MD *md, const char *name);
76 int EVP_MD_names_do_all(const EVP_MD *md,
77                         void (*fn)(const char *name, void *data),
78                         void *data);
79 const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md);
80 int EVP_MD_get_type(const EVP_MD *md);
81 int EVP_MD_get_pkey_type(const EVP_MD *md);
82 int EVP_MD_get_size(const EVP_MD *md);
83 int EVP_MD_get_block_size(const EVP_MD *md);
84 unsigned long EVP_MD_get_flags(const EVP_MD *md);
85
86 const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx);
87 EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx);
88 const char *EVP_MD_CTX_get0_name(const EVP_MD_CTX *ctx);
89 int EVP_MD_CTX_get_size(const EVP_MD_CTX *ctx);
90 int EVP_MD_CTX_get_block_size(const EVP_MD_CTX *ctx);
91 int EVP_MD_CTX_get_type(const EVP_MD_CTX *ctx);
92 void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx);
93
94 const EVP_MD *EVP_md_null(void);
95
96 const EVP_MD *EVP_get_digestbyname(const char *name);
97 const EVP_MD *EVP_get_digestbynid(int type);
98 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
99
100 EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx);
101 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
102
103 void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
104                             void (*fn)(EVP_MD *mac, void *arg),
105                             void *arg);
106
107 #define EVP_MD_type EVP_MD_get_type
108 #define EVP_MD_nid EVP_MD_get_type
109 #define EVP_MD_name EVP_MD_get0_name
110 #define EVP_MD_pkey_type EVP_MD_get_pkey_type
111 #define EVP_MD_size EVP_MD_get_size
112 #define EVP_MD_block_size EVP_MD_get_block_size
113 #define EVP_MD_flags EVP_MD_get_flags
114 #define EVP_MD_CTX_size EVP_MD_CTX_get_size
115 #define EVP_MD_CTX_block_size EVP_MD_CTX_get_block_size
116 #define EVP_MD_CTX_type EVP_MD_CTX_get_type
117 #define EVP_MD_CTX_pkey_ctx EVP_MD_CTX_get_pkey_ctx
118 #define EVP_MD_CTX_md_data EVP_MD_CTX_get0_md_data
119
120The following functions have been deprecated since OpenSSL 3.0, and can be
121hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
122see L<openssl_user_macros(7)>:
123
124 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
125
126 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
127                                              const void *data, size_t count);
128
129 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
130                               int (*update)(EVP_MD_CTX *ctx,
131                                             const void *data, size_t count));
132
133=head1 DESCRIPTION
134
135The EVP digest routines are a high-level interface to message digests,
136and should be used instead of the digest-specific functions.
137
138The B<EVP_MD> type is a structure for digest method implementation.
139
140=over 4
141
142=item EVP_MD_fetch()
143
144Fetches the digest implementation for the given I<algorithm> from any
145provider offering it, within the criteria given by the I<properties>.
146See L<crypto(7)/ALGORITHM FETCHING> for further information.
147
148The returned value must eventually be freed with EVP_MD_free().
149
150Fetched B<EVP_MD> structures are reference counted.
151
152=item EVP_MD_up_ref()
153
154Increments the reference count for an B<EVP_MD> structure.
155
156=item EVP_MD_free()
157
158Decrements the reference count for the fetched B<EVP_MD> structure.
159If the reference count drops to 0 then the structure is freed.
160
161=item EVP_MD_CTX_new()
162
163Allocates and returns a digest context.
164
165=item EVP_MD_CTX_reset()
166
167Resets the digest context I<ctx>.  This can be used to reuse an already
168existing context.
169
170=item EVP_MD_CTX_free()
171
172Cleans up digest context I<ctx> and frees up the space allocated to it.
173
174=item EVP_MD_CTX_ctrl()
175
176I<This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
177is the mechanism that should be used to set and get parameters that are used by
178providers.>
179
180Performs digest-specific control actions on context I<ctx>. The control command
181is indicated in I<cmd> and any additional arguments in I<p1> and I<p2>.
182EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex2(). Other restrictions
183may apply depending on the control type and digest implementation.
184
185If this function happens to be used with a fetched B<EVP_MD>, it will
186translate the controls that are known to OpenSSL into L<OSSL_PARAM(3)>
187parameters with keys defined by OpenSSL and call EVP_MD_CTX_get_params() or
188EVP_MD_CTX_set_params() as is appropriate for each control command.
189
190See L</CONTROLS> below for more information, including what translations are
191being done.
192
193=item EVP_MD_get_params()
194
195Retrieves the requested list of I<params> from a MD I<md>.
196See L</PARAMETERS> below for more information.
197
198=item EVP_MD_CTX_get_params()
199
200Retrieves the requested list of I<params> from a MD context I<ctx>.
201See L</PARAMETERS> below for more information.
202
203=item EVP_MD_CTX_set_params()
204
205Sets the list of I<params> into a MD context I<ctx>.
206See L</PARAMETERS> below for more information.
207
208=item EVP_MD_gettable_params()
209
210Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
211that can be used with EVP_MD_get_params().
212
213=item EVP_MD_gettable_ctx_params(), EVP_MD_CTX_gettable_params()
214
215Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
216that can be used with EVP_MD_CTX_get_params().  EVP_MD_gettable_ctx_params()
217returns the parameters that can be retrieved from the algorithm, whereas
218EVP_MD_CTX_gettable_params() returns the parameters that can be retrieved
219in the context's current state.
220
221=item EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params()
222
223Get a constant L<OSSL_PARAM(3)> array that describes the settable parameters
224that can be used with EVP_MD_CTX_set_params().  EVP_MD_settable_ctx_params()
225returns the parameters that can be set from the algorithm, whereas
226EVP_MD_CTX_settable_params() returns the parameters that can be set in the
227context's current state.
228
229=item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
230
231Sets, clears and tests I<ctx> flags.  See L</FLAGS> below for more information.
232
233=item EVP_Q_digest() is a quick one-shot digest function.
234
235It hashes I<datalen> bytes of data at I<data> using the digest algorithm
236I<name>, which is fetched using the optional I<libctx> and I<propq> parameters.
237The digest value is placed in I<md> and its length is written at I<mdlen>
238if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
239
240=item EVP_Digest()
241
242A wrapper around the Digest Init_ex, Update and Final_ex functions.
243Hashes I<count> bytes of data at I<data> using a digest I<type> from ENGINE
244I<impl>. The digest value is placed in I<md> and its length is written at I<size>
245if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
246If I<impl> is NULL the default implementation of digest I<type> is used.
247
248=item EVP_DigestInit_ex2()
249
250Sets up digest context I<ctx> to use a digest I<type>.
251I<type> is typically supplied by a function such as EVP_sha1(), or a
252value explicitly fetched with EVP_MD_fetch().
253
254The parameters B<params> are set on the context after initialisation.
255
256The I<type> parameter can be NULL if I<ctx> has been already initialized
257with another EVP_DigestInit_ex() call and has not been reset with
258EVP_MD_CTX_reset().
259
260=item EVP_DigestInit_ex()
261
262Sets up digest context I<ctx> to use a digest I<type>.
263I<type> is typically supplied by a function such as EVP_sha1(), or a
264value explicitly fetched with EVP_MD_fetch().
265
266If I<impl> is non-NULL, its implementation of the digest I<type> is used if
267there is one, and if not, the default implementation is used.
268
269The I<type> parameter can be NULL if I<ctx> has been already initialized
270with another EVP_DigestInit_ex() call and has not been reset with
271EVP_MD_CTX_reset().
272
273=item EVP_DigestUpdate()
274
275Hashes I<cnt> bytes of data at I<d> into the digest context I<ctx>. This
276function can be called several times on the same I<ctx> to hash additional
277data.
278
279=item EVP_DigestFinal_ex()
280
281Retrieves the digest value from I<ctx> and places it in I<md>. If the I<s>
282parameter is not NULL then the number of bytes of data written (i.e. the
283length of the digest) will be written to the integer at I<s>, at most
284B<EVP_MAX_MD_SIZE> bytes will be written.  After calling EVP_DigestFinal_ex()
285no additional calls to EVP_DigestUpdate() can be made, but
286EVP_DigestInit_ex2() can be called to initialize a new digest operation.
287
288=item EVP_DigestFinalXOF()
289
290Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
291It retrieves the digest value from I<ctx> and places it in I<len>-sized I<md>.
292After calling this function no additional calls to EVP_DigestUpdate() can be
293made, but EVP_DigestInit_ex2() can be called to initialize a new operation.
294
295=item EVP_MD_CTX_copy_ex()
296
297Can be used to copy the message digest state from I<in> to I<out>. This is
298useful if large amounts of data are to be hashed which only differ in the last
299few bytes.
300
301=item EVP_DigestInit()
302
303Behaves in the same way as EVP_DigestInit_ex2() except it doesn't set any
304parameters and calls EVP_MD_CTX_reset() so it cannot be used with an I<type>
305of NULL.
306
307=item EVP_DigestFinal()
308
309Similar to EVP_DigestFinal_ex() except after computing the digest
310the digest context I<ctx> is automatically cleaned up with EVP_MD_CTX_reset().
311
312=item EVP_MD_CTX_copy()
313
314Similar to EVP_MD_CTX_copy_ex() except the destination I<out> does not have to
315be initialized.
316
317=item EVP_MD_is_a()
318
319Returns 1 if I<md> is an implementation of an algorithm that's
320identifiable with I<name>, otherwise 0.
321
322If I<md> is a legacy digest (it's the return value from the likes of
323EVP_sha256() rather than the result of an EVP_MD_fetch()), only cipher
324names registered with the default library context (see
325L<OSSL_LIB_CTX(3)>) will be considered.
326
327=item EVP_MD_get0_name(),
328EVP_MD_CTX_get0_name()
329
330Return the name of the given message digest.  For fetched message
331digests with multiple names, only one of them is returned; it's
332recommended to use EVP_MD_names_do_all() instead.
333
334=item EVP_MD_names_do_all()
335
336Traverses all names for the I<md>, and calls I<fn> with each name and
337I<data>.  This is only useful with fetched B<EVP_MD>s.
338
339=item EVP_MD_get0_description()
340
341Returns a description of the digest, meant for display and human consumption.
342The description is at the discretion of the digest implementation.
343
344=item EVP_MD_get0_provider()
345
346Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
347B<EVP_MD>.
348
349=item EVP_MD_get_size(),
350EVP_MD_CTX_get_size()
351
352Return the size of the message digest when passed an B<EVP_MD> or an
353B<EVP_MD_CTX> structure, i.e. the size of the hash.
354
355=item EVP_MD_get_block_size(),
356EVP_MD_CTX_get_block_size()
357
358Return the block size of the message digest when passed an B<EVP_MD> or an
359B<EVP_MD_CTX> structure.
360
361=item EVP_MD_get_type(),
362EVP_MD_CTX_get_type()
363
364Return the NID of the OBJECT IDENTIFIER representing the given message digest
365when passed an B<EVP_MD> structure.  For example, C<EVP_MD_get_type(EVP_sha1())>
366returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
367
368=item EVP_MD_CTX_get0_md_data()
369
370Return the digest method private data for the passed B<EVP_MD_CTX>.
371The space is allocated by OpenSSL and has the size originally set with
372EVP_MD_meth_set_app_datasize().
373
374=item EVP_MD_CTX_get0_md(), EVP_MD_CTX_get1_md()
375
376EVP_MD_CTX_get0_md() returns
377the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
378will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex2() (or
379other similar function) when the EVP_MD_CTX was first initialised. Note that
380where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
381this function will not have its reference count incremented and therefore it
382should not be used after the EVP_MD_CTX is freed.
383EVP_MD_CTX_get1_md() is the same except the ownership is passed to the
384caller and is from the passed B<EVP_MD_CTX>.
385
386=item EVP_MD_CTX_set_update_fn()
387
388Sets the update function for I<ctx> to I<update>.
389This is the function that is called by EVP_DigestUpdate(). If not set, the
390update function from the B<EVP_MD> type specified at initialization is used.
391
392=item EVP_MD_CTX_update_fn()
393
394Returns the update function for I<ctx>.
395
396=item EVP_MD_get_flags()
397
398Returns the I<md> flags. Note that these are different from the B<EVP_MD_CTX>
399ones. See L<EVP_MD_meth_set_flags(3)> for more information.
400
401=item EVP_MD_get_pkey_type()
402
403Returns the NID of the public key signing algorithm associated with this
404digest. For example EVP_sha1() is associated with RSA so this will return
405B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
406longer linked this function is only retained for compatibility reasons.
407
408=item EVP_md_null()
409
410A "null" message digest that does nothing: i.e. the hash it returns is of zero
411length.
412
413=item EVP_get_digestbyname(),
414EVP_get_digestbynid(),
415EVP_get_digestbyobj()
416
417Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
418B<ASN1_OBJECT> structure respectively.
419
420The EVP_get_digestbyname() function is present for backwards compatibility with
421OpenSSL prior to version 3 and is different to the EVP_MD_fetch() function
422since it does not attempt to "fetch" an implementation of the cipher.
423Additionally, it only knows about digests that are built-in to OpenSSL and have
424an associated NID. Similarly EVP_get_digestbynid() and EVP_get_digestbyobj()
425also return objects without an associated implementation.
426
427When the digest objects returned by these functions are used (such as in a call
428to EVP_DigestInit_ex()) an implementation of the digest will be implicitly
429fetched from the loaded providers. This fetch could fail if no suitable
430implementation is available. Use EVP_MD_fetch() instead to explicitly fetch
431the algorithm and an associated implementation from a provider.
432
433See L<crypto(7)/ALGORITHM FETCHING> for more information about fetching.
434
435The digest objects returned from these functions do not need to be freed with
436EVP_MD_free().
437
438=item EVP_MD_CTX_get_pkey_ctx()
439
440Returns the B<EVP_PKEY_CTX> assigned to I<ctx>. The returned pointer should not
441be freed by the caller.
442
443=item EVP_MD_CTX_set_pkey_ctx()
444
445Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
446a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
447L<EVP_DigestVerifyInit(3)>. The I<pctx> passed to this function should be freed
448by the caller. A NULL I<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
449assigned to I<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
450depends on how the B<EVP_PKEY_CTX> is created.
451
452=item EVP_MD_do_all_provided()
453
454Traverses all messages digests implemented by all activated providers
455in the given library context I<libctx>, and for each of the implementations,
456calls the given function I<fn> with the implementation method and the given
457I<arg> as argument.
458
459=back
460
461=head1 PARAMETERS
462
463See L<OSSL_PARAM(3)> for information about passing parameters.
464
465EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
466
467=over 4
468
469=item "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>) <unsigned integer>
470
471Sets the digest length for extendable output functions.
472It is used by the SHAKE algorithm and should not exceed what can be given
473using a B<size_t>.
474
475=item "pad-type" (B<OSSL_DIGEST_PARAM_PAD_TYPE>) <unsigned integer>
476
477Sets the padding type.
478It is used by the MDC2 algorithm.
479
480=back
481
482EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
483
484=over 4
485
486=item "micalg" (B<OSSL_PARAM_DIGEST_KEY_MICALG>) <UTF8 string>.
487
488Gets the digest Message Integrity Check algorithm string. This is used when
489creating S/MIME multipart/signed messages, as specified in RFC 3851.
490It may be used by external engines or providers.
491
492=back
493
494=head1 CONTROLS
495
496EVP_MD_CTX_ctrl() can be used to send the following standard controls:
497
498=over 4
499
500=item EVP_MD_CTRL_MICALG
501
502Gets the digest Message Integrity Check algorithm string. This is used when
503creating S/MIME multipart/signed messages, as specified in RFC 3851.
504The string value is written to I<p2>.
505
506When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
507an L<OSSL_PARAM(3)> item with the key "micalg" (B<OSSL_DIGEST_PARAM_MICALG>).
508
509=item EVP_MD_CTRL_XOF_LEN
510
511This control sets the digest length for extendable output functions to I<p1>.
512Sending this control directly should not be necessary, the use of
513EVP_DigestFinalXOF() is preferred.
514Currently used by SHAKE.
515
516When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
517an L<OSSL_PARAM(3)> item with the key "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>).
518
519=back
520
521=head1 FLAGS
522
523EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
524can be used the manipulate and test these B<EVP_MD_CTX> flags:
525
526=over 4
527
528=item EVP_MD_CTX_FLAG_ONESHOT
529
530This flag instructs the digest to optimize for one update only, if possible.
531
532=for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
533
534=for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
535
536=for comment We currently avoid documenting flags that are only bit holder:
537EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
538
539=item EVP_MD_CTX_FLAG_NO_INIT
540
541This flag instructs EVP_DigestInit() and similar not to initialise the
542implementation specific data.
543
544=item EVP_MD_CTX_FLAG_FINALISE
545
546Some functions such as EVP_DigestSign only finalise copies of internal
547contexts so additional data can be included after the finalisation call.
548This is inefficient if this functionality is not required, and can be
549disabled with this flag.
550
551=back
552
553=head1 RETURN VALUES
554
555=over 4
556
557=item EVP_MD_fetch()
558
559Returns a pointer to a B<EVP_MD> for success or NULL for failure.
560
561=item EVP_MD_up_ref()
562
563Returns 1 for success or 0 for failure.
564
565=item EVP_Q_digest(),
566EVP_Digest(),
567EVP_DigestInit_ex2(),
568EVP_DigestInit_ex(),
569EVP_DigestInit(),
570EVP_DigestUpdate(),
571EVP_DigestFinal_ex(),
572EVP_DigestFinalXOF(), and
573EVP_DigestFinal()
574
575return 1 for
576success and 0 for failure.
577
578=item EVP_MD_CTX_ctrl()
579
580Returns 1 if successful or 0 for failure.
581
582=item EVP_MD_CTX_set_params(),
583EVP_MD_CTX_get_params()
584
585Returns 1 if successful or 0 for failure.
586
587=item EVP_MD_CTX_settable_params(),
588EVP_MD_CTX_gettable_params()
589
590Return an array of constant L<OSSL_PARAM(3)>s, or NULL if there is none
591to get.
592
593=item EVP_MD_CTX_copy_ex()
594
595Returns 1 if successful or 0 for failure.
596
597=item EVP_MD_get_type(),
598EVP_MD_get_pkey_type()
599
600Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
601exists.
602
603=item EVP_MD_get_size(),
604EVP_MD_get_block_size(),
605EVP_MD_CTX_get_size(),
606EVP_MD_CTX_get_block_size()
607
608Returns the digest or block size in bytes or -1 for failure.
609
610=item EVP_md_null()
611
612Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
613
614=item EVP_get_digestbyname(),
615EVP_get_digestbynid(),
616EVP_get_digestbyobj()
617
618Returns either an B<EVP_MD> structure or NULL if an error occurs.
619
620=item EVP_MD_CTX_set_pkey_ctx()
621
622This function has no return value.
623
624=item EVP_MD_names_do_all()
625
626Returns 1 if the callback was called for all names. A return value of 0 means
627that the callback was not called for any names.
628
629=back
630
631=head1 NOTES
632
633The B<EVP> interface to message digests should almost always be used in
634preference to the low-level interfaces. This is because the code then becomes
635transparent to the digest used and much more flexible.
636
637New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
638digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
639are still in common use.
640
641For most applications the I<impl> parameter to EVP_DigestInit_ex() will be
642set to NULL to use the default digest implementation.
643
644Ignoring failure returns of EVP_DigestInit_ex(), EVP_DigestInit_ex2(), or
645EVP_DigestInit() can lead to undefined behavior on subsequent calls
646updating or finalizing the B<EVP_MD_CTX> such as the EVP_DigestUpdate() or
647EVP_DigestFinal() functions. The only valid calls on the B<EVP_MD_CTX>
648when initialization fails are calls that attempt another initialization of
649the context or release the context.
650
651The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
652obsolete but are retained to maintain compatibility with existing code. New
653applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
654EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
655instead of initializing and cleaning it up on each call and allow non default
656implementations of digests to be specified.
657
658If digest contexts are not cleaned up after use,
659memory leaks will occur.
660
661EVP_MD_CTX_get0_name(), EVP_MD_CTX_get_size(), EVP_MD_CTX_get_block_size(),
662EVP_MD_CTX_get_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are
663defined as macros.
664
665EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
666or control.
667
668=head1 EXAMPLES
669
670This example digests the data "Test Message\n" and "Hello World\n", using the
671digest name passed on the command line.
672
673 #include <stdio.h>
674 #include <string.h>
675 #include <openssl/evp.h>
676
677 int main(int argc, char *argv[])
678 {
679     EVP_MD_CTX *mdctx;
680     const EVP_MD *md;
681     char mess1[] = "Test Message\n";
682     char mess2[] = "Hello World\n";
683     unsigned char md_value[EVP_MAX_MD_SIZE];
684     unsigned int md_len, i;
685
686     if (argv[1] == NULL) {
687         printf("Usage: mdtest digestname\n");
688         exit(1);
689     }
690
691     md = EVP_get_digestbyname(argv[1]);
692     if (md == NULL) {
693         printf("Unknown message digest %s\n", argv[1]);
694         exit(1);
695     }
696
697     mdctx = EVP_MD_CTX_new();
698     if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
699         printf("Message digest initialization failed.\n");
700         EVP_MD_CTX_free(mdctx);
701         exit(1);
702     }
703     if (!EVP_DigestUpdate(mdctx, mess1, strlen(mess1))) {
704         printf("Message digest update failed.\n");
705         EVP_MD_CTX_free(mdctx);
706         exit(1);
707     }
708     if (!EVP_DigestUpdate(mdctx, mess2, strlen(mess2))) {
709         printf("Message digest update failed.\n");
710         EVP_MD_CTX_free(mdctx);
711         exit(1);
712     }
713     if (!EVP_DigestFinal_ex(mdctx, md_value, &md_len)) {
714         printf("Message digest finalization failed.\n");
715         EVP_MD_CTX_free(mdctx);
716         exit(1);
717     }
718     EVP_MD_CTX_free(mdctx);
719
720     printf("Digest is: ");
721     for (i = 0; i < md_len; i++)
722         printf("%02x", md_value[i]);
723     printf("\n");
724
725     exit(0);
726 }
727
728=head1 SEE ALSO
729
730L<EVP_MD_meth_new(3)>,
731L<openssl-dgst(1)>,
732L<evp(7)>,
733L<OSSL_PROVIDER(3)>,
734L<OSSL_PARAM(3)>,
735L<property(7)>,
736L<crypto(7)/ALGORITHM FETCHING>,
737L<provider-digest(7)>,
738L<life_cycle-digest(7)>
739
740The full list of digest algorithms are provided below.
741
742L<EVP_blake2b512(3)>,
743L<EVP_md2(3)>,
744L<EVP_md4(3)>,
745L<EVP_md5(3)>,
746L<EVP_mdc2(3)>,
747L<EVP_ripemd160(3)>,
748L<EVP_sha1(3)>,
749L<EVP_sha224(3)>,
750L<EVP_sha3_224(3)>,
751L<EVP_sm3(3)>,
752L<EVP_whirlpool(3)>
753
754=head1 HISTORY
755
756The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
757EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
758
759The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
760later, so now EVP_sha1() can be used with RSA and DSA.
761
762The EVP_dss1() function was removed in OpenSSL 1.1.0.
763
764The EVP_MD_CTX_set_pkey_ctx() function was added in OpenSSL 1.1.1.
765
766The EVP_Q_digest(), EVP_DigestInit_ex2(),
767EVP_MD_fetch(), EVP_MD_free(), EVP_MD_up_ref(),
768EVP_MD_get_params(), EVP_MD_CTX_set_params(), EVP_MD_CTX_get_params(),
769EVP_MD_gettable_params(), EVP_MD_gettable_ctx_params(),
770EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params() and
771EVP_MD_CTX_gettable_params() functions were added in OpenSSL 3.0.
772
773The EVP_MD_type(), EVP_MD_nid(), EVP_MD_name(), EVP_MD_pkey_type(),
774EVP_MD_size(), EVP_MD_block_size(), EVP_MD_flags(), EVP_MD_CTX_size(),
775EVP_MD_CTX_block_size(), EVP_MD_CTX_type(), and EVP_MD_CTX_md_data()
776functions were renamed to include C<get> or C<get0> in their names in
777OpenSSL 3.0, respectively. The old names are kept as non-deprecated
778alias macros.
779
780The EVP_MD_CTX_md() function was deprecated in OpenSSL 3.0; use
781EVP_MD_CTX_get0_md() instead.
782EVP_MD_CTX_update_fn() and EVP_MD_CTX_set_update_fn() were deprecated
783in OpenSSL 3.0.
784
785=head1 COPYRIGHT
786
787Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
788
789Licensed under the Apache License 2.0 (the "License").  You may not use
790this file except in compliance with the License.  You can obtain a copy
791in the file LICENSE in the source distribution or at
792L<https://www.openssl.org/source/license.html>.
793
794=cut
795