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_flags,
7 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 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
26 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
27 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
29 int EVP_Digest(const void *data, size_t count, unsigned char *md,
30 unsigned int *size, const EVP_MD *type, ENGINE *impl);
31 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
32 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
33 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
34 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
36 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
38 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
39 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
41 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
43 int EVP_MD_type(const EVP_MD *md);
44 int EVP_MD_pkey_type(const EVP_MD *md);
45 int EVP_MD_size(const EVP_MD *md);
46 int EVP_MD_block_size(const EVP_MD *md);
47 unsigned long EVP_MD_flags(const EVP_MD *md);
49 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
50 int EVP_MD_CTX_size(const EVP_MD *ctx);
51 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
52 int EVP_MD_CTX_type(const EVP_MD *ctx);
53 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
54 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
55 const void *data, size_t count);
56 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
57 int (*update)(EVP_MD_CTX *ctx,
58 const void *data, size_t count));
60 const EVP_MD *EVP_md_null(void);
62 const EVP_MD *EVP_get_digestbyname(const char *name);
63 const EVP_MD *EVP_get_digestbynid(int type);
64 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
66 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
67 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
71 The EVP digest routines are a high level interface to message digests,
72 and should be used instead of the cipher-specific functions.
76 =item EVP_MD_CTX_new()
78 Allocates and returns a digest context.
80 =item EVP_MD_CTX_reset()
82 Resets the digest context B<ctx>. This can be used to reuse an already
85 =item EVP_MD_CTX_free()
87 Cleans up digest context B<ctx> and frees up the space allocated to it.
89 =item EVP_MD_CTX_ctrl()
91 Performs digest-specific control actions on context B<ctx>. The control command
92 is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
93 EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
94 may apply depending on the control type and digest implementation.
95 See L</CONTROLS> below for more information.
97 =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
99 Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
103 A wrapper around the Digest Init_ex, Update and Final_ex functions.
104 Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
105 B<impl>. The digest value is placed in B<md> and its length is written at B<size>
106 if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
107 If B<impl> is NULL the default implementation of digest B<type> is used.
109 =item EVP_DigestInit_ex()
111 Sets up digest context B<ctx> to use a digest B<type> from ENGINE B<impl>.
112 B<type> will typically be supplied by a function such as EVP_sha1(). If
113 B<impl> is NULL then the default implementation of digest B<type> is used.
115 =item EVP_DigestUpdate()
117 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
118 function can be called several times on the same B<ctx> to hash additional
121 =item EVP_DigestFinal_ex()
123 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
124 parameter is not NULL then the number of bytes of data written (i.e. the
125 length of the digest) will be written to the integer at B<s>, at most
126 B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
127 no additional calls to EVP_DigestUpdate() can be made, but
128 EVP_DigestInit_ex() can be called to initialize a new digest operation.
130 =item EVP_DigestFinalXOF()
132 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
133 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
134 After calling this function no additional calls to EVP_DigestUpdate() can be
135 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
137 =item EVP_MD_CTX_copy_ex()
139 Can be used to copy the message digest state from B<in> to B<out>. This is
140 useful if large amounts of data are to be hashed which only differ in the last
143 =item EVP_DigestInit()
145 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
146 default digest implementation.
148 =item EVP_DigestFinal()
150 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
151 automatically cleaned up.
153 =item EVP_MD_CTX_copy()
155 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
161 Return the size of the message digest when passed an B<EVP_MD> or an
162 B<EVP_MD_CTX> structure, i.e. the size of the hash.
164 =item EVP_MD_block_size(),
165 EVP_MD_CTX_block_size()
167 Return the block size of the message digest when passed an B<EVP_MD> or an
168 B<EVP_MD_CTX> structure.
173 Return the NID of the OBJECT IDENTIFIER representing the given message digest
174 when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
175 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
177 =item EVP_MD_CTX_md_data()
179 Return the digest method private data for the passed B<EVP_MD_CTX>.
180 The space is allocated by OpenSSL and has the size originally set with
181 EVP_MD_meth_set_app_datasize().
183 =item EVP_MD_CTX_md()
185 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
186 will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex() (or
187 other similar function) when the EVP_MD_CTX was first initialised. Note that
188 where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
189 this function will not have its reference count incremented and therefore it
190 should not be used after the EVP_MD_CTX is freed.
192 =item EVP_MD_CTX_set_update_fn()
194 Sets the update function for B<ctx> to B<update>.
195 This is the function that is called by EVP_DigestUpdate. If not set, the
196 update function from the B<EVP_MD> type specified at initialization is used.
198 =item EVP_MD_CTX_update_fn()
200 Returns the update function for B<ctx>.
204 Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
205 ones. See L<EVP_MD_meth_set_flags(3)> for more information.
207 =item EVP_MD_pkey_type()
209 Returns the NID of the public key signing algorithm associated with this
210 digest. For example EVP_sha1() is associated with RSA so this will return
211 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
212 longer linked this function is only retained for compatibility reasons.
216 A "null" message digest that does nothing: i.e. the hash it returns is of zero
219 =item EVP_get_digestbyname(),
220 EVP_get_digestbynid(),
221 EVP_get_digestbyobj()
223 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
224 B<ASN1_OBJECT> structure respectively.
226 =item EVP_MD_CTX_pkey_ctx()
228 Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
229 be freed by the caller.
231 =item EVP_MD_CTX_set_pkey_ctx()
233 Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
234 a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
235 L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
236 by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
237 assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
238 depends on how the B<EVP_PKEY_CTX> is created.
244 EVP_MD_CTX_ctrl() can be used to send the following standard controls:
248 =item EVP_MD_CTRL_MICALG
250 Gets the digest Message Integrity Check algorithm string. This is used when
251 creating S/MIME multipart/signed messages, as specified in RFC 3851.
252 The string value is written to B<p2>.
254 =item EVP_MD_CTRL_XOF_LEN
256 This control sets the digest length for extendable output functions to B<p1>.
257 Sending this control directly should not be necessary, the use of
258 C<EVP_DigestFinalXOF()> is preferred.
259 Currently used by SHAKE.
265 EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
266 can be used the manipulate and test these B<EVP_MD_CTX> flags:
270 =item EVP_MD_CTX_FLAG_ONESHOT
272 This flag instructs the digest to optimize for one update only, if possible.
274 =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
276 =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
278 =for comment We currently avoid documenting flags that are only bit holder:
279 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
281 =item EVP_MD_CTX_FLAG_NO_INIT
283 This flag instructs EVP_DigestInit() and similar not to initialise the
284 implementation specific data.
286 =item EVP_MD_CTX_FLAG_FINALISE
288 Some functions such as EVP_DigestSign only finalise copies of internal
289 contexts so additional data can be included after the finalisation call.
290 This is inefficient if this functionality is not required, and can be
291 disabled with this flag.
299 =item EVP_DigestInit_ex(),
304 success and 0 for failure.
306 =item EVP_MD_CTX_ctrl()
308 Returns 1 if successful or 0 for failure.
310 =item EVP_MD_CTX_copy_ex()
312 Returns 1 if successful or 0 for failure.
317 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
323 EVP_MD_CTX_block_size()
325 Returns the digest or block size in bytes.
329 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
331 =item EVP_get_digestbyname(),
332 EVP_get_digestbynid(),
333 EVP_get_digestbyobj()
335 Returns either an B<EVP_MD> structure or NULL if an error occurs.
337 =item EVP_MD_CTX_set_pkey_ctx()
339 This function has no return value.
345 The B<EVP> interface to message digests should almost always be used in
346 preference to the low level interfaces. This is because the code then becomes
347 transparent to the digest used and much more flexible.
349 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
350 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
351 are still in common use.
353 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
354 set to NULL to use the default digest implementation.
356 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
357 obsolete but are retained to maintain compatibility with existing code. New
358 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
359 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
360 instead of initializing and cleaning it up on each call and allow non default
361 implementations of digests to be specified.
363 If digest contexts are not cleaned up after use,
364 memory leaks will occur.
366 EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
367 EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
370 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
375 This example digests the data "Test Message\n" and "Hello World\n", using the
376 digest name passed on the command line.
380 #include <openssl/evp.h>
382 int main(int argc, char *argv[])
386 char mess1[] = "Test Message\n";
387 char mess2[] = "Hello World\n";
388 unsigned char md_value[EVP_MAX_MD_SIZE];
389 unsigned int md_len, i;
391 if (argv[1] == NULL) {
392 printf("Usage: mdtest digestname\n");
396 md = EVP_get_digestbyname(argv[1]);
398 printf("Unknown message digest %s\n", argv[1]);
402 mdctx = EVP_MD_CTX_new();
403 EVP_DigestInit_ex(mdctx, md, NULL);
404 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
405 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
406 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
407 EVP_MD_CTX_free(mdctx);
409 printf("Digest is: ");
410 for (i = 0; i < md_len; i++)
411 printf("%02x", md_value[i]);
419 L<EVP_MD_meth_new(3)>,
423 The full list of digest algorithms are provided below.
425 L<EVP_blake2b512(3)>,
439 The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
440 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
442 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
443 later, so now EVP_sha1() can be used with RSA and DSA.
445 The EVP_dss1() function was removed in OpenSSL 1.1.0.
447 The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
451 Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
453 Licensed under the Apache License 2.0 (the "License"). You may not use
454 this file except in compliance with the License. You can obtain a copy
455 in the file LICENSE in the source distribution or at
456 L<https://www.openssl.org/source/license.html>.