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_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_flags,
12 EVP_MD_CTX_md, EVP_MD_CTX_type, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
13 EVP_MD_CTX_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
15 EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
16 EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_set_pkey_ctx - EVP digest routines
20 #include <openssl/evp.h>
22 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
23 const char *properties);
24 EVP_MD_CTX *EVP_MD_CTX_new(void);
25 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
26 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
27 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
28 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
29 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
30 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
31 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
32 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
34 int EVP_Digest(const void *data, size_t count, unsigned char *md,
35 unsigned int *size, const EVP_MD *type, ENGINE *impl);
36 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
37 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
38 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
39 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
41 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
43 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
44 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
46 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
48 int EVP_MD_type(const EVP_MD *md);
49 int EVP_MD_pkey_type(const EVP_MD *md);
50 int EVP_MD_size(const EVP_MD *md);
51 int EVP_MD_block_size(const EVP_MD *md);
52 unsigned long EVP_MD_flags(const EVP_MD *md);
54 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
55 int EVP_MD_CTX_size(const EVP_MD *ctx);
56 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
57 int EVP_MD_CTX_type(const EVP_MD *ctx);
58 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
59 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
60 const void *data, size_t count);
61 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
62 int (*update)(EVP_MD_CTX *ctx,
63 const void *data, size_t count));
65 const EVP_MD *EVP_md_null(void);
67 const EVP_MD *EVP_get_digestbyname(const char *name);
68 const EVP_MD *EVP_get_digestbynid(int type);
69 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
71 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
72 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
76 The EVP digest routines are a high level interface to message digests,
77 and should be used instead of the cipher-specific functions.
83 Fetches the digest implementation for the given B<algorithm> from any
84 provider offering it, within the criteria given by the B<properties>.
85 See L<provider(7)/Fetching algorithms> for further information.
87 The returned value must eventually be freed with L<EVP_MD_meth_free(3)>.
89 =item EVP_MD_CTX_new()
91 Allocates and returns a digest context.
93 =item EVP_MD_CTX_reset()
95 Resets the digest context B<ctx>. This can be used to reuse an already
98 =item EVP_MD_CTX_free()
100 Cleans up digest context B<ctx> and frees up the space allocated to it.
102 =item EVP_MD_CTX_ctrl()
104 This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
105 is the mechanism that should be used to set and get parameters that are used by
107 Performs digest-specific control actions on context B<ctx>. The control command
108 is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
109 EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
110 may apply depending on the control type and digest implementation.
111 See L</CONTROLS> below for more information.
113 =item EVP_MD_CTX_get_params
115 Retrieves the requested list of B<params> from a MD context B<ctx>.
116 See L</PARAMS> below for more information.
118 =item EVP_MD_CTX_set_params
120 Sets the list of <params> into a MD context B<ctx>.
121 See L</PARAMS> below for more information.
123 =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
125 Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
129 A wrapper around the Digest Init_ex, Update and Final_ex functions.
130 Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
131 B<impl>. The digest value is placed in B<md> and its length is written at B<size>
132 if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
133 If B<impl> is NULL the default implementation of digest B<type> is used.
135 =item EVP_DigestInit_ex()
137 Sets up digest context B<ctx> to use a digest B<type>.
138 B<type> is typically supplied by a function such as EVP_sha1(), or a
139 value explicitly fetched with EVP_MD_fetch().
141 If B<impl> is non-NULL, its implementation of the digest B<type> is used if
142 there is one, and if not, the default implementation is used.
144 =item EVP_DigestUpdate()
146 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
147 function can be called several times on the same B<ctx> to hash additional
150 =item EVP_DigestFinal_ex()
152 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
153 parameter is not NULL then the number of bytes of data written (i.e. the
154 length of the digest) will be written to the integer at B<s>, at most
155 B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
156 no additional calls to EVP_DigestUpdate() can be made, but
157 EVP_DigestInit_ex() can be called to initialize a new digest operation.
159 =item EVP_DigestFinalXOF()
161 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
162 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
163 After calling this function no additional calls to EVP_DigestUpdate() can be
164 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
166 =item EVP_MD_CTX_copy_ex()
168 Can be used to copy the message digest state from B<in> to B<out>. This is
169 useful if large amounts of data are to be hashed which only differ in the last
172 =item EVP_DigestInit()
174 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
175 default digest implementation.
177 =item EVP_DigestFinal()
179 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
180 automatically cleaned up.
182 =item EVP_MD_CTX_copy()
184 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
190 Return the size of the message digest when passed an B<EVP_MD> or an
191 B<EVP_MD_CTX> structure, i.e. the size of the hash.
193 =item EVP_MD_block_size(),
194 EVP_MD_CTX_block_size()
196 Return the block size of the message digest when passed an B<EVP_MD> or an
197 B<EVP_MD_CTX> structure.
202 Return the NID of the OBJECT IDENTIFIER representing the given message digest
203 when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
204 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
206 =item EVP_MD_CTX_md_data()
208 Return the digest method private data for the passed B<EVP_MD_CTX>.
209 The space is allocated by OpenSSL and has the size originally set with
210 EVP_MD_meth_set_app_datasize().
212 =item EVP_MD_CTX_md()
214 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
215 will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex() (or
216 other similar function) when the EVP_MD_CTX was first initialised. Note that
217 where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
218 this function will not have its reference count incremented and therefore it
219 should not be used after the EVP_MD_CTX is freed.
221 =item EVP_MD_CTX_set_update_fn()
223 Sets the update function for B<ctx> to B<update>.
224 This is the function that is called by EVP_DigestUpdate. If not set, the
225 update function from the B<EVP_MD> type specified at initialization is used.
227 =item EVP_MD_CTX_update_fn()
229 Returns the update function for B<ctx>.
233 Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
234 ones. See L<EVP_MD_meth_set_flags(3)> for more information.
236 =item EVP_MD_pkey_type()
238 Returns the NID of the public key signing algorithm associated with this
239 digest. For example EVP_sha1() is associated with RSA so this will return
240 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
241 longer linked this function is only retained for compatibility reasons.
245 A "null" message digest that does nothing: i.e. the hash it returns is of zero
248 =item EVP_get_digestbyname(),
249 EVP_get_digestbynid(),
250 EVP_get_digestbyobj()
252 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
253 B<ASN1_OBJECT> structure respectively.
255 =item EVP_MD_CTX_pkey_ctx()
257 Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
258 be freed by the caller.
260 =item EVP_MD_CTX_set_pkey_ctx()
262 Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
263 a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
264 L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
265 by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
266 assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
267 depends on how the B<EVP_PKEY_CTX> is created.
273 See L<OSSL_PARAM(3)> for information about passing parameters.
275 EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
279 =item OSSL_PARAM_DIGEST_KEY_XOFLEN <size_t>
281 Sets the digest length for extendable output functions.
282 It is used by the SHAKE algorithm.
284 =item OSSL_PARAM_DIGEST_KEY_PAD_TYPE <int>
287 It is used by the MDC2 algorithm.
291 EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
295 =item OSSL_PARAM_DIGEST_KEY_MICALG <utf8string>.
297 Gets the digest Message Integrity Check algorithm string. This is used when
298 creating S/MIME multipart/signed messages, as specified in RFC 3851.
299 It may be used by external engines or providers.
305 EVP_MD_CTX_ctrl() can be used to send the following standard controls:
309 =item EVP_MD_CTRL_MICALG
311 Gets the digest Message Integrity Check algorithm string. This is used when
312 creating S/MIME multipart/signed messages, as specified in RFC 3851.
313 The string value is written to B<p2>.
315 =item EVP_MD_CTRL_XOF_LEN
317 This control sets the digest length for extendable output functions to B<p1>.
318 Sending this control directly should not be necessary, the use of
319 C<EVP_DigestFinalXOF()> is preferred.
320 Currently used by SHAKE.
326 EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
327 can be used the manipulate and test these B<EVP_MD_CTX> flags:
331 =item EVP_MD_CTX_FLAG_ONESHOT
333 This flag instructs the digest to optimize for one update only, if possible.
335 =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
337 =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
339 =for comment We currently avoid documenting flags that are only bit holder:
340 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
342 =item EVP_MD_CTX_FLAG_NO_INIT
344 This flag instructs EVP_DigestInit() and similar not to initialise the
345 implementation specific data.
347 =item EVP_MD_CTX_FLAG_FINALISE
349 Some functions such as EVP_DigestSign only finalise copies of internal
350 contexts so additional data can be included after the finalisation call.
351 This is inefficient if this functionality is not required, and can be
352 disabled with this flag.
362 Returns a pointer to a B<EVP_MD> for success or NULL for failure.
364 =item EVP_DigestInit_ex(),
369 success and 0 for failure.
371 =item EVP_MD_CTX_ctrl()
373 Returns 1 if successful or 0 for failure.
375 =item EVP_MD_CTX_set_params(),
376 EVP_MD_CTX_get_params()
378 Returns 1 if successful or 0 for failure.
380 =item EVP_MD_CTX_copy_ex()
382 Returns 1 if successful or 0 for failure.
387 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
393 EVP_MD_CTX_block_size()
395 Returns the digest or block size in bytes.
399 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
401 =item EVP_get_digestbyname(),
402 EVP_get_digestbynid(),
403 EVP_get_digestbyobj()
405 Returns either an B<EVP_MD> structure or NULL if an error occurs.
407 =item EVP_MD_CTX_set_pkey_ctx()
409 This function has no return value.
415 The B<EVP> interface to message digests should almost always be used in
416 preference to the low level interfaces. This is because the code then becomes
417 transparent to the digest used and much more flexible.
419 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
420 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
421 are still in common use.
423 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
424 set to NULL to use the default digest implementation.
426 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
427 obsolete but are retained to maintain compatibility with existing code. New
428 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
429 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
430 instead of initializing and cleaning it up on each call and allow non default
431 implementations of digests to be specified.
433 If digest contexts are not cleaned up after use,
434 memory leaks will occur.
436 EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
437 EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
440 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
445 This example digests the data "Test Message\n" and "Hello World\n", using the
446 digest name passed on the command line.
450 #include <openssl/evp.h>
452 int main(int argc, char *argv[])
456 char mess1[] = "Test Message\n";
457 char mess2[] = "Hello World\n";
458 unsigned char md_value[EVP_MAX_MD_SIZE];
459 unsigned int md_len, i;
461 if (argv[1] == NULL) {
462 printf("Usage: mdtest digestname\n");
466 md = EVP_get_digestbyname(argv[1]);
468 printf("Unknown message digest %s\n", argv[1]);
472 mdctx = EVP_MD_CTX_new();
473 EVP_DigestInit_ex(mdctx, md, NULL);
474 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
475 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
476 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
477 EVP_MD_CTX_free(mdctx);
479 printf("Digest is: ");
480 for (i = 0; i < md_len; i++)
481 printf("%02x", md_value[i]);
489 L<EVP_MD_meth_new(3)>,
495 The full list of digest algorithms are provided below.
497 L<EVP_blake2b512(3)>,
508 L<provider(7)/Fetching algorithms>
512 The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
513 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
515 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
516 later, so now EVP_sha1() can be used with RSA and DSA.
518 The EVP_dss1() function was removed in OpenSSL 1.1.0.
520 The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
522 The EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params() functions were
527 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
529 Licensed under the Apache License 2.0 (the "License"). You may not use
530 this file except in compliance with the License. You can obtain a copy
531 in the file LICENSE in the source distribution or at
532 L<https://www.openssl.org/source/license.html>.