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