5 EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy,
6 EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl, EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
7 EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
8 EVP_Digest, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
9 EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
10 EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_flags,
11 EVP_MD_CTX_md, EVP_MD_CTX_type, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
12 EVP_MD_CTX_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
14 EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
15 EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_set_pkey_ctx - EVP digest routines
19 #include <openssl/evp.h>
21 EVP_MD_CTX *EVP_MD_CTX_new(void);
22 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
23 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
24 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
25 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
26 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
27 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
28 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
29 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
31 int EVP_Digest(const void *data, size_t count, unsigned char *md,
32 unsigned int *size, const EVP_MD *type, ENGINE *impl);
33 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
34 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
35 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
36 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
38 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
40 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
41 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
43 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
45 int EVP_MD_type(const EVP_MD *md);
46 int EVP_MD_pkey_type(const EVP_MD *md);
47 int EVP_MD_size(const EVP_MD *md);
48 int EVP_MD_block_size(const EVP_MD *md);
49 unsigned long EVP_MD_flags(const EVP_MD *md);
51 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
52 int EVP_MD_CTX_size(const EVP_MD *ctx);
53 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
54 int EVP_MD_CTX_type(const EVP_MD *ctx);
55 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
56 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
57 const void *data, size_t count);
58 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
59 int (*update)(EVP_MD_CTX *ctx,
60 const void *data, size_t count));
62 const EVP_MD *EVP_md_null(void);
64 const EVP_MD *EVP_get_digestbyname(const char *name);
65 const EVP_MD *EVP_get_digestbynid(int type);
66 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
68 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
69 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
73 The EVP digest routines are a high level interface to message digests,
74 and should be used instead of the cipher-specific functions.
78 =item EVP_MD_CTX_new()
80 Allocates and returns a digest context.
82 =item EVP_MD_CTX_reset()
84 Resets the digest context B<ctx>. This can be used to reuse an already
87 =item EVP_MD_CTX_free()
89 Cleans up digest context B<ctx> and frees up the space allocated to it.
91 =item EVP_MD_CTX_ctrl()
93 This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
94 is the mechanism that should be used to set and get parameters that are used by
96 Performs digest-specific control actions on context B<ctx>. The control command
97 is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
98 EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
99 may apply depending on the control type and digest implementation.
100 See L</CONTROLS> below for more information.
102 =item EVP_MD_CTX_get_params
104 Retrieves the requested list of B<params> from a MD context B<ctx>.
105 See L</PARAMS> below for more information.
107 =item EVP_MD_CTX_set_params
109 Sets the list of <params> into a MD context B<ctx>.
110 See L</PARAMS> below for more information.
112 =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
114 Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
118 A wrapper around the Digest Init_ex, Update and Final_ex functions.
119 Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
120 B<impl>. The digest value is placed in B<md> and its length is written at B<size>
121 if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
122 If B<impl> is NULL the default implementation of digest B<type> is used.
124 =item EVP_DigestInit_ex()
126 Sets up digest context B<ctx> to use a digest B<type> from ENGINE B<impl>.
127 B<type> will typically be supplied by a function such as EVP_sha1(). If
128 B<impl> is NULL then the default implementation of digest B<type> is used.
130 =item EVP_DigestUpdate()
132 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
133 function can be called several times on the same B<ctx> to hash additional
136 =item EVP_DigestFinal_ex()
138 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
139 parameter is not NULL then the number of bytes of data written (i.e. the
140 length of the digest) will be written to the integer at B<s>, at most
141 B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
142 no additional calls to EVP_DigestUpdate() can be made, but
143 EVP_DigestInit_ex() can be called to initialize a new digest operation.
145 =item EVP_DigestFinalXOF()
147 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
148 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
149 After calling this function no additional calls to EVP_DigestUpdate() can be
150 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
152 =item EVP_MD_CTX_copy_ex()
154 Can be used to copy the message digest state from B<in> to B<out>. This is
155 useful if large amounts of data are to be hashed which only differ in the last
158 =item EVP_DigestInit()
160 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
161 default digest implementation.
163 =item EVP_DigestFinal()
165 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
166 automatically cleaned up.
168 =item EVP_MD_CTX_copy()
170 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
176 Return the size of the message digest when passed an B<EVP_MD> or an
177 B<EVP_MD_CTX> structure, i.e. the size of the hash.
179 =item EVP_MD_block_size(),
180 EVP_MD_CTX_block_size()
182 Return the block size of the message digest when passed an B<EVP_MD> or an
183 B<EVP_MD_CTX> structure.
188 Return the NID of the OBJECT IDENTIFIER representing the given message digest
189 when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
190 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
192 =item EVP_MD_CTX_md_data()
194 Return the digest method private data for the passed B<EVP_MD_CTX>.
195 The space is allocated by OpenSSL and has the size originally set with
196 EVP_MD_meth_set_app_datasize().
198 =item EVP_MD_CTX_md()
200 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
201 will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex() (or
202 other similar function) when the EVP_MD_CTX was first initialised. Note that
203 where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
204 this function will not have its reference count incremented and therefore it
205 should not be used after the EVP_MD_CTX is freed.
207 =item EVP_MD_CTX_set_update_fn()
209 Sets the update function for B<ctx> to B<update>.
210 This is the function that is called by EVP_DigestUpdate. If not set, the
211 update function from the B<EVP_MD> type specified at initialization is used.
213 =item EVP_MD_CTX_update_fn()
215 Returns the update function for B<ctx>.
219 Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
220 ones. See L<EVP_MD_meth_set_flags(3)> for more information.
222 =item EVP_MD_pkey_type()
224 Returns the NID of the public key signing algorithm associated with this
225 digest. For example EVP_sha1() is associated with RSA so this will return
226 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
227 longer linked this function is only retained for compatibility reasons.
231 A "null" message digest that does nothing: i.e. the hash it returns is of zero
234 =item EVP_get_digestbyname(),
235 EVP_get_digestbynid(),
236 EVP_get_digestbyobj()
238 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
239 B<ASN1_OBJECT> structure respectively.
241 =item EVP_MD_CTX_pkey_ctx()
243 Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
244 be freed by the caller.
246 =item EVP_MD_CTX_set_pkey_ctx()
248 Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
249 a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
250 L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
251 by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
252 assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
253 depends on how the B<EVP_PKEY_CTX> is created.
259 See L<OSSL_PARAM(3)> for information about passing parameters.
261 EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
265 =item OSSL_PARAM_DIGEST_KEY_XOFLEN <size_t>
267 Sets the digest length for extendable output functions.
268 It is used by the SHAKE algorithm.
270 =item OSSL_PARAM_DIGEST_KEY_PAD_TYPE <int>
273 It is used by the MDC2 algorithm.
277 EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
281 =item OSSL_PARAM_DIGEST_KEY_MICALG <utf8string>.
283 Gets the digest Message Integrity Check algorithm string. This is used when
284 creating S/MIME multipart/signed messages, as specified in RFC 3851.
285 It may be used by external engines or providers.
291 EVP_MD_CTX_ctrl() can be used to send the following standard controls:
295 =item EVP_MD_CTRL_MICALG
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 The string value is written to B<p2>.
301 =item EVP_MD_CTRL_XOF_LEN
303 This control sets the digest length for extendable output functions to B<p1>.
304 Sending this control directly should not be necessary, the use of
305 C<EVP_DigestFinalXOF()> is preferred.
306 Currently used by SHAKE.
312 EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
313 can be used the manipulate and test these B<EVP_MD_CTX> flags:
317 =item EVP_MD_CTX_FLAG_ONESHOT
319 This flag instructs the digest to optimize for one update only, if possible.
321 =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
323 =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
325 =for comment We currently avoid documenting flags that are only bit holder:
326 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
328 =item EVP_MD_CTX_FLAG_NO_INIT
330 This flag instructs EVP_DigestInit() and similar not to initialise the
331 implementation specific data.
333 =item EVP_MD_CTX_FLAG_FINALISE
335 Some functions such as EVP_DigestSign only finalise copies of internal
336 contexts so additional data can be included after the finalisation call.
337 This is inefficient if this functionality is not required, and can be
338 disabled with this flag.
346 =item EVP_DigestInit_ex(),
351 success and 0 for failure.
353 =item EVP_MD_CTX_ctrl()
355 Returns 1 if successful or 0 for failure.
357 =item EVP_MD_CTX_set_params(),
358 EVP_MD_CTX_get_params()
360 Returns 1 if successful or 0 for failure.
362 =item EVP_MD_CTX_copy_ex()
364 Returns 1 if successful or 0 for failure.
369 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
375 EVP_MD_CTX_block_size()
377 Returns the digest or block size in bytes.
381 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
383 =item EVP_get_digestbyname(),
384 EVP_get_digestbynid(),
385 EVP_get_digestbyobj()
387 Returns either an B<EVP_MD> structure or NULL if an error occurs.
389 =item EVP_MD_CTX_set_pkey_ctx()
391 This function has no return value.
397 The B<EVP> interface to message digests should almost always be used in
398 preference to the low level interfaces. This is because the code then becomes
399 transparent to the digest used and much more flexible.
401 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
402 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
403 are still in common use.
405 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
406 set to NULL to use the default digest implementation.
408 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
409 obsolete but are retained to maintain compatibility with existing code. New
410 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
411 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
412 instead of initializing and cleaning it up on each call and allow non default
413 implementations of digests to be specified.
415 If digest contexts are not cleaned up after use,
416 memory leaks will occur.
418 EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
419 EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
422 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
427 This example digests the data "Test Message\n" and "Hello World\n", using the
428 digest name passed on the command line.
432 #include <openssl/evp.h>
434 int main(int argc, char *argv[])
438 char mess1[] = "Test Message\n";
439 char mess2[] = "Hello World\n";
440 unsigned char md_value[EVP_MAX_MD_SIZE];
441 unsigned int md_len, i;
443 if (argv[1] == NULL) {
444 printf("Usage: mdtest digestname\n");
448 md = EVP_get_digestbyname(argv[1]);
450 printf("Unknown message digest %s\n", argv[1]);
454 mdctx = EVP_MD_CTX_new();
455 EVP_DigestInit_ex(mdctx, md, NULL);
456 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
457 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
458 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
459 EVP_MD_CTX_free(mdctx);
461 printf("Digest is: ");
462 for (i = 0; i < md_len; i++)
463 printf("%02x", md_value[i]);
471 L<EVP_MD_meth_new(3)>,
477 The full list of digest algorithms are provided below.
479 L<EVP_blake2b512(3)>,
493 The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
494 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
496 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
497 later, so now EVP_sha1() can be used with RSA and DSA.
499 The EVP_dss1() function was removed in OpenSSL 1.1.0.
501 The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
503 The EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params() functions were
508 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
510 Licensed under the Apache License 2.0 (the "License"). You may not use
511 this file except in compliance with the License. You can obtain a copy
512 in the file LICENSE in the source distribution or at
513 L<https://www.openssl.org/source/license.html>.