VirtualBox

source: vbox/trunk/src/libs/openssl-3.3.2/doc/man3/EVP_DigestInit.pod

Last change on this file was 108206, checked in by vboxsync, 3 months ago

openssl-3.3.2: Exported all files to OSE and removed .scm-settings ​bugref:10757

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette