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