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_copy,
8 EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl,
9 EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
10 EVP_MD_CTX_settable_params, EVP_MD_CTX_gettable_params,
11 EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
12 EVP_Digest, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
13 EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
14 EVP_MD_name, EVP_MD_provider,
15 EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_flags,
17 EVP_MD_CTX_md, EVP_MD_CTX_type, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
18 EVP_MD_CTX_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
20 EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
21 EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_set_pkey_ctx,
27 #include <openssl/evp.h>
29 EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
30 const char *properties);
31 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]);
32 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest);
33 EVP_MD_CTX *EVP_MD_CTX_new(void);
34 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
35 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
36 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
37 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
38 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
39 const OSSL_PARAM *EVP_MD_CTX_settable_params(const EVP_MD *digest);
40 const OSSL_PARAM *EVP_MD_CTX_gettable_params(const EVP_MD *digest);
41 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
42 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
43 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
45 int EVP_Digest(const void *data, size_t count, unsigned char *md,
46 unsigned int *size, const EVP_MD *type, ENGINE *impl);
47 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
48 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
49 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
50 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
52 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
54 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
55 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
57 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
59 const char *EVP_MD_name(const EVP_MD *md);
60 const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md);
61 int EVP_MD_type(const EVP_MD *md);
62 int EVP_MD_pkey_type(const EVP_MD *md);
63 int EVP_MD_size(const EVP_MD *md);
64 int EVP_MD_block_size(const EVP_MD *md);
65 unsigned long EVP_MD_flags(const EVP_MD *md);
67 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
68 const char *EVP_MD_CTX_name(const EVP_MD_CTX *ctx);
69 int EVP_MD_CTX_size(const EVP_MD *ctx);
70 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
71 int EVP_MD_CTX_type(const EVP_MD *ctx);
72 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
73 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
74 const void *data, size_t count);
75 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
76 int (*update)(EVP_MD_CTX *ctx,
77 const void *data, size_t count));
79 const EVP_MD *EVP_md_null(void);
81 const EVP_MD *EVP_get_digestbyname(const char *name);
82 const EVP_MD *EVP_get_digestbynid(int type);
83 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
85 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
86 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
88 void EVP_MD_do_all_ex(OPENSSL_CTX *libctx,
89 void (*fn)(EVP_MD *mac, void *arg),
94 The EVP digest routines are a high level interface to message digests,
95 and should be used instead of the cipher-specific functions.
101 Fetches the digest implementation for the given B<algorithm> from any
102 provider offering it, within the criteria given by the B<properties>.
103 See L<provider(7)/Fetching algorithms> for further information.
105 The returned value must eventually be freed with L<EVP_MD_meth_free(3)>.
107 =item EVP_MD_CTX_new()
109 Allocates and returns a digest context.
111 =item EVP_MD_CTX_reset()
113 Resets the digest context B<ctx>. This can be used to reuse an already
116 =item EVP_MD_CTX_free()
118 Cleans up digest context B<ctx> and frees up the space allocated to it.
120 =item EVP_MD_CTX_ctrl()
122 This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
123 is the mechanism that should be used to set and get parameters that are used by
125 Performs digest-specific control actions on context B<ctx>. The control command
126 is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
127 EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
128 may apply depending on the control type and digest implementation.
129 See L</CONTROLS> below for more information.
131 =item EVP_MD_get_params()
133 Retrieves the requested list of B<params> from a MD B<md>.
134 See L</PARAMS> below for more information.
136 =item EVP_MD_CTX_get_params()
138 Retrieves the requested list of B<params> from a MD context B<ctx>.
139 See L</PARAMS> below for more information.
141 =item EVP_MD_CTX_set_params()
143 Sets the list of B<params> into a MD context B<ctx>.
144 See L</PARAMS> below for more information.
146 =item EVP_MD_gettable_params(), EVP_MD_CTX_gettable_params(),
147 EVP_MD_CTX_settable_params()
149 Get a B<OSSL_PARAM> array that describes the retrievable and settable
150 parameters, i.e. parameters that can be used with EVP_MD_get_params(),
151 EVP_MD_CTX_get_params() and EVP_MD_CTX_set_params(), respectively.
152 See L<OSSL_PARAM(3)> for the use of B<OSSL_PARAM> as parameter descriptor.
154 =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
156 Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
160 A wrapper around the Digest Init_ex, Update and Final_ex functions.
161 Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
162 B<impl>. The digest value is placed in B<md> and its length is written at B<size>
163 if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
164 If B<impl> is NULL the default implementation of digest B<type> is used.
166 =item EVP_DigestInit_ex()
168 Sets up digest context B<ctx> to use a digest B<type>.
169 B<type> is typically supplied by a function such as EVP_sha1(), or a
170 value explicitly fetched with EVP_MD_fetch().
172 If B<impl> is non-NULL, its implementation of the digest B<type> is used if
173 there is one, and if not, the default implementation is used.
175 =item EVP_DigestUpdate()
177 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
178 function can be called several times on the same B<ctx> to hash additional
181 =item EVP_DigestFinal_ex()
183 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
184 parameter is not NULL then the number of bytes of data written (i.e. the
185 length of the digest) will be written to the integer at B<s>, at most
186 B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
187 no additional calls to EVP_DigestUpdate() can be made, but
188 EVP_DigestInit_ex() can be called to initialize a new digest operation.
190 =item EVP_DigestFinalXOF()
192 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
193 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
194 After calling this function no additional calls to EVP_DigestUpdate() can be
195 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
197 =item EVP_MD_CTX_copy_ex()
199 Can be used to copy the message digest state from B<in> to B<out>. This is
200 useful if large amounts of data are to be hashed which only differ in the last
203 =item EVP_DigestInit()
205 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
206 default digest implementation.
208 =item EVP_DigestFinal()
210 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
211 automatically cleaned up.
213 =item EVP_MD_CTX_copy()
215 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
221 Return the name of the given message digest.
223 =item EVP_MD_provider()
225 Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
231 Return the size of the message digest when passed an B<EVP_MD> or an
232 B<EVP_MD_CTX> structure, i.e. the size of the hash.
234 =item EVP_MD_block_size(),
235 EVP_MD_CTX_block_size()
237 Return the block size of the message digest when passed an B<EVP_MD> or an
238 B<EVP_MD_CTX> structure.
243 Return the NID of the OBJECT IDENTIFIER representing the given message digest
244 when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
245 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
247 =item EVP_MD_CTX_md_data()
249 Return the digest method private data for the passed B<EVP_MD_CTX>.
250 The space is allocated by OpenSSL and has the size originally set with
251 EVP_MD_meth_set_app_datasize().
253 =item EVP_MD_CTX_md()
255 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
256 will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex() (or
257 other similar function) when the EVP_MD_CTX was first initialised. Note that
258 where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
259 this function will not have its reference count incremented and therefore it
260 should not be used after the EVP_MD_CTX is freed.
262 =item EVP_MD_CTX_set_update_fn()
264 Sets the update function for B<ctx> to B<update>.
265 This is the function that is called by EVP_DigestUpdate. If not set, the
266 update function from the B<EVP_MD> type specified at initialization is used.
268 =item EVP_MD_CTX_update_fn()
270 Returns the update function for B<ctx>.
274 Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
275 ones. See L<EVP_MD_meth_set_flags(3)> for more information.
277 =item EVP_MD_pkey_type()
279 Returns the NID of the public key signing algorithm associated with this
280 digest. For example EVP_sha1() is associated with RSA so this will return
281 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
282 longer linked this function is only retained for compatibility reasons.
286 A "null" message digest that does nothing: i.e. the hash it returns is of zero
289 =item EVP_get_digestbyname(),
290 EVP_get_digestbynid(),
291 EVP_get_digestbyobj()
293 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
294 B<ASN1_OBJECT> structure respectively.
296 =item EVP_MD_CTX_pkey_ctx()
298 Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
299 be freed by the caller.
301 =item EVP_MD_CTX_set_pkey_ctx()
303 Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
304 a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
305 L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
306 by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
307 assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
308 depends on how the B<EVP_PKEY_CTX> is created.
310 =item EVP_MD_do_all_ex()
312 Traverses all messages digests implemented by all activated providers
313 in the given library context I<libctx>, and for each of the implementations,
314 calls the given function I<fn> with the implementation method and the given
321 See L<OSSL_PARAM(3)> for information about passing parameters.
323 EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
327 =item OSSL_PARAM_DIGEST_KEY_XOFLEN <size_t>
329 Sets the digest length for extendable output functions.
330 It is used by the SHAKE algorithm.
332 =item OSSL_PARAM_DIGEST_KEY_PAD_TYPE <int>
335 It is used by the MDC2 algorithm.
339 EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
343 =item OSSL_PARAM_DIGEST_KEY_MICALG <utf8string>.
345 Gets the digest Message Integrity Check algorithm string. This is used when
346 creating S/MIME multipart/signed messages, as specified in RFC 3851.
347 It may be used by external engines or providers.
353 EVP_MD_CTX_ctrl() can be used to send the following standard controls:
357 =item EVP_MD_CTRL_MICALG
359 Gets the digest Message Integrity Check algorithm string. This is used when
360 creating S/MIME multipart/signed messages, as specified in RFC 3851.
361 The string value is written to B<p2>.
363 =item EVP_MD_CTRL_XOF_LEN
365 This control sets the digest length for extendable output functions to B<p1>.
366 Sending this control directly should not be necessary, the use of
367 C<EVP_DigestFinalXOF()> is preferred.
368 Currently used by SHAKE.
374 EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
375 can be used the manipulate and test these B<EVP_MD_CTX> flags:
379 =item EVP_MD_CTX_FLAG_ONESHOT
381 This flag instructs the digest to optimize for one update only, if possible.
383 =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
385 =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
387 =for comment We currently avoid documenting flags that are only bit holder:
388 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
390 =item EVP_MD_CTX_FLAG_NO_INIT
392 This flag instructs EVP_DigestInit() and similar not to initialise the
393 implementation specific data.
395 =item EVP_MD_CTX_FLAG_FINALISE
397 Some functions such as EVP_DigestSign only finalise copies of internal
398 contexts so additional data can be included after the finalisation call.
399 This is inefficient if this functionality is not required, and can be
400 disabled with this flag.
410 Returns a pointer to a B<EVP_MD> for success or NULL for failure.
412 =item EVP_DigestInit_ex(),
417 success and 0 for failure.
419 =item EVP_MD_CTX_ctrl()
421 Returns 1 if successful or 0 for failure.
423 =item EVP_MD_CTX_set_params(),
424 EVP_MD_CTX_get_params()
426 Returns 1 if successful or 0 for failure.
428 =item EVP_MD_CTX_settable_params(),
429 EVP_MD_CTX_gettable_params()
431 Return an array of constant B<OSSL_PARAM>s, or NULL if there is none
434 =item EVP_MD_CTX_copy_ex()
436 Returns 1 if successful or 0 for failure.
441 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
447 EVP_MD_CTX_block_size()
449 Returns the digest or block size in bytes.
453 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
455 =item EVP_get_digestbyname(),
456 EVP_get_digestbynid(),
457 EVP_get_digestbyobj()
459 Returns either an B<EVP_MD> structure or NULL if an error occurs.
461 =item EVP_MD_CTX_set_pkey_ctx()
463 This function has no return value.
469 The B<EVP> interface to message digests should almost always be used in
470 preference to the low level interfaces. This is because the code then becomes
471 transparent to the digest used and much more flexible.
473 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
474 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
475 are still in common use.
477 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
478 set to NULL to use the default digest implementation.
480 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
481 obsolete but are retained to maintain compatibility with existing code. New
482 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
483 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
484 instead of initializing and cleaning it up on each call and allow non default
485 implementations of digests to be specified.
487 If digest contexts are not cleaned up after use,
488 memory leaks will occur.
490 EVP_MD_CTX_name(), EVP_MD_CTX_size(), EVP_MD_CTX_block_size(),
491 EVP_MD_CTX_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are defined
494 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
499 This example digests the data "Test Message\n" and "Hello World\n", using the
500 digest name passed on the command line.
504 #include <openssl/evp.h>
506 int main(int argc, char *argv[])
510 char mess1[] = "Test Message\n";
511 char mess2[] = "Hello World\n";
512 unsigned char md_value[EVP_MAX_MD_SIZE];
513 unsigned int md_len, i;
515 if (argv[1] == NULL) {
516 printf("Usage: mdtest digestname\n");
520 md = EVP_get_digestbyname(argv[1]);
522 printf("Unknown message digest %s\n", argv[1]);
526 mdctx = EVP_MD_CTX_new();
527 EVP_DigestInit_ex(mdctx, md, NULL);
528 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
529 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
530 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
531 EVP_MD_CTX_free(mdctx);
533 printf("Digest is: ");
534 for (i = 0; i < md_len; i++)
535 printf("%02x", md_value[i]);
543 L<EVP_MD_meth_new(3)>,
549 The full list of digest algorithms are provided below.
551 L<EVP_blake2b512(3)>,
562 L<provider(7)/Fetching algorithms>
566 The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
567 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
569 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
570 later, so now EVP_sha1() can be used with RSA and DSA.
572 The EVP_dss1() function was removed in OpenSSL 1.1.0.
574 The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
576 The EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params() functions were
581 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
583 Licensed under the Apache License 2.0 (the "License"). You may not use
584 this file except in compliance with the License. You can obtain a copy
585 in the file LICENSE in the source distribution or at
586 L<https://www.openssl.org/source/license.html>.