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