6 EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy,
7 EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl, EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
8 EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
9 EVP_Digest, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
10 EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
11 EVP_MD_name, EVP_MD_provider,
12 EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_flags,
14 EVP_MD_CTX_md, EVP_MD_CTX_type, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
15 EVP_MD_CTX_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
17 EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
18 EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_set_pkey_ctx - EVP digest routines
22 #include <openssl/evp.h>
24 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
25 const char *properties);
26 EVP_MD_CTX *EVP_MD_CTX_new(void);
27 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
28 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
29 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
30 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
31 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
32 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
33 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
34 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
36 int EVP_Digest(const void *data, size_t count, unsigned char *md,
37 unsigned int *size, const EVP_MD *type, ENGINE *impl);
38 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
39 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
40 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
41 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
43 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
45 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
46 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
48 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
50 const char *EVP_MD_name(const EVP_MD *md);
51 const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md);
52 int EVP_MD_type(const EVP_MD *md);
53 int EVP_MD_pkey_type(const EVP_MD *md);
54 int EVP_MD_size(const EVP_MD *md);
55 int EVP_MD_block_size(const EVP_MD *md);
56 unsigned long EVP_MD_flags(const EVP_MD *md);
58 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
59 const char *EVP_MD_CTX_name(const EVP_MD_CTX *ctx);
60 int EVP_MD_CTX_size(const EVP_MD *ctx);
61 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
62 int EVP_MD_CTX_type(const EVP_MD *ctx);
63 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
64 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
65 const void *data, size_t count);
66 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
67 int (*update)(EVP_MD_CTX *ctx,
68 const void *data, size_t count));
70 const EVP_MD *EVP_md_null(void);
72 const EVP_MD *EVP_get_digestbyname(const char *name);
73 const EVP_MD *EVP_get_digestbynid(int type);
74 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
76 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
77 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
81 The EVP digest routines are a high level interface to message digests,
82 and should be used instead of the cipher-specific functions.
88 Fetches the digest implementation for the given B<algorithm> from any
89 provider offering it, within the criteria given by the B<properties>.
90 See L<provider(7)/Fetching algorithms> for further information.
92 The returned value must eventually be freed with L<EVP_MD_meth_free(3)>.
94 =item EVP_MD_CTX_new()
96 Allocates and returns a digest context.
98 =item EVP_MD_CTX_reset()
100 Resets the digest context B<ctx>. This can be used to reuse an already
103 =item EVP_MD_CTX_free()
105 Cleans up digest context B<ctx> and frees up the space allocated to it.
107 =item EVP_MD_CTX_ctrl()
109 This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
110 is the mechanism that should be used to set and get parameters that are used by
112 Performs digest-specific control actions on context B<ctx>. The control command
113 is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
114 EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
115 may apply depending on the control type and digest implementation.
116 See L</CONTROLS> below for more information.
118 =item EVP_MD_CTX_get_params
120 Retrieves the requested list of B<params> from a MD context B<ctx>.
121 See L</PARAMS> below for more information.
123 =item EVP_MD_CTX_set_params
125 Sets the list of <params> into a MD context B<ctx>.
126 See L</PARAMS> below for more information.
128 =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
130 Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
134 A wrapper around the Digest Init_ex, Update and Final_ex functions.
135 Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
136 B<impl>. The digest value is placed in B<md> and its length is written at B<size>
137 if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
138 If B<impl> is NULL the default implementation of digest B<type> is used.
140 =item EVP_DigestInit_ex()
142 Sets up digest context B<ctx> to use a digest B<type>.
143 B<type> is typically supplied by a function such as EVP_sha1(), or a
144 value explicitly fetched with EVP_MD_fetch().
146 If B<impl> is non-NULL, its implementation of the digest B<type> is used if
147 there is one, and if not, the default implementation is used.
149 =item EVP_DigestUpdate()
151 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
152 function can be called several times on the same B<ctx> to hash additional
155 =item EVP_DigestFinal_ex()
157 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
158 parameter is not NULL then the number of bytes of data written (i.e. the
159 length of the digest) will be written to the integer at B<s>, at most
160 B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
161 no additional calls to EVP_DigestUpdate() can be made, but
162 EVP_DigestInit_ex() can be called to initialize a new digest operation.
164 =item EVP_DigestFinalXOF()
166 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
167 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
168 After calling this function no additional calls to EVP_DigestUpdate() can be
169 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
171 =item EVP_MD_CTX_copy_ex()
173 Can be used to copy the message digest state from B<in> to B<out>. This is
174 useful if large amounts of data are to be hashed which only differ in the last
177 =item EVP_DigestInit()
179 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
180 default digest implementation.
182 =item EVP_DigestFinal()
184 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
185 automatically cleaned up.
187 =item EVP_MD_CTX_copy()
189 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
195 Return the name of the given message digest.
197 =item EVP_MD_provider()
199 Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
205 Return the size of the message digest when passed an B<EVP_MD> or an
206 B<EVP_MD_CTX> structure, i.e. the size of the hash.
208 =item EVP_MD_block_size(),
209 EVP_MD_CTX_block_size()
211 Return the block size of the message digest when passed an B<EVP_MD> or an
212 B<EVP_MD_CTX> structure.
217 Return the NID of the OBJECT IDENTIFIER representing the given message digest
218 when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
219 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
221 =item EVP_MD_CTX_md_data()
223 Return the digest method private data for the passed B<EVP_MD_CTX>.
224 The space is allocated by OpenSSL and has the size originally set with
225 EVP_MD_meth_set_app_datasize().
227 =item EVP_MD_CTX_md()
229 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
230 will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex() (or
231 other similar function) when the EVP_MD_CTX was first initialised. Note that
232 where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
233 this function will not have its reference count incremented and therefore it
234 should not be used after the EVP_MD_CTX is freed.
236 =item EVP_MD_CTX_set_update_fn()
238 Sets the update function for B<ctx> to B<update>.
239 This is the function that is called by EVP_DigestUpdate. If not set, the
240 update function from the B<EVP_MD> type specified at initialization is used.
242 =item EVP_MD_CTX_update_fn()
244 Returns the update function for B<ctx>.
248 Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
249 ones. See L<EVP_MD_meth_set_flags(3)> for more information.
251 =item EVP_MD_pkey_type()
253 Returns the NID of the public key signing algorithm associated with this
254 digest. For example EVP_sha1() is associated with RSA so this will return
255 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
256 longer linked this function is only retained for compatibility reasons.
260 A "null" message digest that does nothing: i.e. the hash it returns is of zero
263 =item EVP_get_digestbyname(),
264 EVP_get_digestbynid(),
265 EVP_get_digestbyobj()
267 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
268 B<ASN1_OBJECT> structure respectively.
270 =item EVP_MD_CTX_pkey_ctx()
272 Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
273 be freed by the caller.
275 =item EVP_MD_CTX_set_pkey_ctx()
277 Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
278 a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
279 L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
280 by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
281 assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
282 depends on how the B<EVP_PKEY_CTX> is created.
288 See L<OSSL_PARAM(3)> for information about passing parameters.
290 EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
294 =item OSSL_PARAM_DIGEST_KEY_XOFLEN <size_t>
296 Sets the digest length for extendable output functions.
297 It is used by the SHAKE algorithm.
299 =item OSSL_PARAM_DIGEST_KEY_PAD_TYPE <int>
302 It is used by the MDC2 algorithm.
306 EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
310 =item OSSL_PARAM_DIGEST_KEY_MICALG <utf8string>.
312 Gets the digest Message Integrity Check algorithm string. This is used when
313 creating S/MIME multipart/signed messages, as specified in RFC 3851.
314 It may be used by external engines or providers.
320 EVP_MD_CTX_ctrl() can be used to send the following standard controls:
324 =item EVP_MD_CTRL_MICALG
326 Gets the digest Message Integrity Check algorithm string. This is used when
327 creating S/MIME multipart/signed messages, as specified in RFC 3851.
328 The string value is written to B<p2>.
330 =item EVP_MD_CTRL_XOF_LEN
332 This control sets the digest length for extendable output functions to B<p1>.
333 Sending this control directly should not be necessary, the use of
334 C<EVP_DigestFinalXOF()> is preferred.
335 Currently used by SHAKE.
341 EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
342 can be used the manipulate and test these B<EVP_MD_CTX> flags:
346 =item EVP_MD_CTX_FLAG_ONESHOT
348 This flag instructs the digest to optimize for one update only, if possible.
350 =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
352 =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
354 =for comment We currently avoid documenting flags that are only bit holder:
355 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
357 =item EVP_MD_CTX_FLAG_NO_INIT
359 This flag instructs EVP_DigestInit() and similar not to initialise the
360 implementation specific data.
362 =item EVP_MD_CTX_FLAG_FINALISE
364 Some functions such as EVP_DigestSign only finalise copies of internal
365 contexts so additional data can be included after the finalisation call.
366 This is inefficient if this functionality is not required, and can be
367 disabled with this flag.
377 Returns a pointer to a B<EVP_MD> for success or NULL for failure.
379 =item EVP_DigestInit_ex(),
384 success and 0 for failure.
386 =item EVP_MD_CTX_ctrl()
388 Returns 1 if successful or 0 for failure.
390 =item EVP_MD_CTX_set_params(),
391 EVP_MD_CTX_get_params()
393 Returns 1 if successful or 0 for failure.
395 =item EVP_MD_CTX_copy_ex()
397 Returns 1 if successful or 0 for failure.
402 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
408 EVP_MD_CTX_block_size()
410 Returns the digest or block size in bytes.
414 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
416 =item EVP_get_digestbyname(),
417 EVP_get_digestbynid(),
418 EVP_get_digestbyobj()
420 Returns either an B<EVP_MD> structure or NULL if an error occurs.
422 =item EVP_MD_CTX_set_pkey_ctx()
424 This function has no return value.
430 The B<EVP> interface to message digests should almost always be used in
431 preference to the low level interfaces. This is because the code then becomes
432 transparent to the digest used and much more flexible.
434 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
435 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
436 are still in common use.
438 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
439 set to NULL to use the default digest implementation.
441 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
442 obsolete but are retained to maintain compatibility with existing code. New
443 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
444 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
445 instead of initializing and cleaning it up on each call and allow non default
446 implementations of digests to be specified.
448 If digest contexts are not cleaned up after use,
449 memory leaks will occur.
451 EVP_MD_CTX_name(), EVP_MD_CTX_size(), EVP_MD_CTX_block_size(),
452 EVP_MD_CTX_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are defined
455 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
460 This example digests the data "Test Message\n" and "Hello World\n", using the
461 digest name passed on the command line.
465 #include <openssl/evp.h>
467 int main(int argc, char *argv[])
471 char mess1[] = "Test Message\n";
472 char mess2[] = "Hello World\n";
473 unsigned char md_value[EVP_MAX_MD_SIZE];
474 unsigned int md_len, i;
476 if (argv[1] == NULL) {
477 printf("Usage: mdtest digestname\n");
481 md = EVP_get_digestbyname(argv[1]);
483 printf("Unknown message digest %s\n", argv[1]);
487 mdctx = EVP_MD_CTX_new();
488 EVP_DigestInit_ex(mdctx, md, NULL);
489 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
490 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
491 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
492 EVP_MD_CTX_free(mdctx);
494 printf("Digest is: ");
495 for (i = 0; i < md_len; i++)
496 printf("%02x", md_value[i]);
504 L<EVP_MD_meth_new(3)>,
510 The full list of digest algorithms are provided below.
512 L<EVP_blake2b512(3)>,
523 L<provider(7)/Fetching algorithms>
527 The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
528 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
530 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
531 later, so now EVP_sha1() can be used with RSA and DSA.
533 The EVP_dss1() function was removed in OpenSSL 1.1.0.
535 The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
537 The EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params() functions were
542 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
544 Licensed under the Apache License 2.0 (the "License"). You may not use
545 this file except in compliance with the License. You can obtain a copy
546 in the file LICENSE in the source distribution or at
547 L<https://www.openssl.org/source/license.html>.