5 EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy_ex,
6 EVP_MD_CTX_ctrl, EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags,
7 EVP_MD_CTX_test_flags, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
8 EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
9 EVP_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size,
10 EVP_MD_block_size, EVP_MD_CTX_md, EVP_MD_CTX_size,
11 EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_MD_CTX_md_data,
13 EVP_get_digestbyname, EVP_get_digestbynid,
15 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_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
30 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
31 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
32 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
34 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
36 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
37 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
39 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
41 int EVP_MD_type(const EVP_MD *md);
42 int EVP_MD_pkey_type(const EVP_MD *md);
43 int EVP_MD_size(const EVP_MD *md);
44 int EVP_MD_block_size(const EVP_MD *md);
46 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
47 int EVP_MD_CTX_size(const EVP_MD *ctx);
48 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
49 int EVP_MD_CTX_type(const EVP_MD *ctx);
50 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
52 const EVP_MD *EVP_md_null(void);
54 const EVP_MD *EVP_get_digestbyname(const char *name);
55 const EVP_MD *EVP_get_digestbynid(int type);
56 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
58 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
62 The EVP digest routines are a high level interface to message digests,
63 and should be used instead of the cipher-specific functions.
67 =item EVP_MD_CTX_new()
69 Allocates and returns a digest context.
71 =item EVP_MD_CTX_reset()
73 Resets the digest context B<ctx>. This can be used to reuse an already
76 =item EVP_MD_CTX_free()
78 Cleans up digest context B<ctx> and frees up the space allocated to it.
80 =item EVP_MD_CTX_ctrl()
82 Performs digest-specific control actions on context B<ctx>.
84 =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
86 Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
88 =item EVP_DigestInit_ex()
90 Sets up digest context B<ctx> to use a digest B<type> from ENGINE B<impl>.
91 B<type> will typically be supplied by a function such as EVP_sha1(). If
92 B<impl> is NULL then the default implementation of digest B<type> is used.
94 =item EVP_DigestUpdate()
96 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
97 function can be called several times on the same B<ctx> to hash additional
100 =item EVP_DigestFinal_ex()
102 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
103 parameter is not NULL then the number of bytes of data written (i.e. the
104 length of the digest) will be written to the integer at B<s>, at most
105 B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
106 no additional calls to EVP_DigestUpdate() can be made, but
107 EVP_DigestInit_ex() can be called to initialize a new digest operation.
109 =item EVP_DigestFinalXOF()
111 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
112 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
113 After calling this function no additional calls to EVP_DigestUpdate() can be
114 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
116 =item EVP_MD_CTX_copy_ex()
118 Can be used to copy the message digest state from B<in> to B<out>. This is
119 useful if large amounts of data are to be hashed which only differ in the last
122 =item EVP_DigestInit()
124 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
125 default digest implementation.
127 =item EVP_DigestFinal()
129 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
130 automatically cleaned up.
132 =item EVP_MD_CTX_copy()
134 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
140 Return the size of the message digest when passed an B<EVP_MD> or an
141 B<EVP_MD_CTX> structure, i.e. the size of the hash.
143 =item EVP_MD_block_size(),
144 EVP_MD_CTX_block_size()
146 Return the block size of the message digest when passed an B<EVP_MD> or an
147 B<EVP_MD_CTX> structure.
152 Return the NID of the OBJECT IDENTIFIER representing the given message digest
153 when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
154 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
156 =item EVP_MD_CTX_md_data()
158 Return the digest method private data for the passed B<EVP_MD_CTX>.
159 The space is allocated by OpenSSL and has the size originally set with
160 EVP_MD_meth_set_app_datasize().
162 =item EVP_MD_CTX_md()
164 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>.
166 =item EVP_MD_pkey_type()
168 Returns the NID of the public key signing algorithm associated with this
169 digest. For example EVP_sha1() is associated with RSA so this will return
170 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
171 longer linked this function is only retained for compatibility reasons.
175 A "null" message digest that does nothing: i.e. the hash it returns is of zero
178 =item EVP_get_digestbyname(),
179 EVP_get_digestbynid(),
180 EVP_get_digestbyobj()
182 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
183 B<ASN1_OBJECT> structure respectively.
185 =item EVP_MD_CTX_set_pkey_ctx()
187 Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
188 a customzied B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
189 L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
190 by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
191 assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
192 depends on how the B<EVP_PKEY_CTX> is created.
198 EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
199 can be used the manipulate and test these B<EVP_MD_CTX> flags:
203 =item EVP_MD_CTX_FLAG_ONESHOT
205 This flag instructs the digest to optimize for one update only, if possible.
207 =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
209 =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
211 =for comment We currently avoid documenting flags that are only bit holder:
212 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
214 =item EVP_MD_CTX_FLAG_NO_INIT
216 This flag instructs EVP_DigestInit() and similar not to initialise the
217 implementation specific data.
219 =item EVP_MD_CTX_FLAG_FINALISE
221 Some functions such as EVP_DigestSign only finalise copies of internal
222 contexts so additional data can be included after the finalisation call.
223 This is inefficient if this functionality is not required, and can be
224 disabled with this flag.
232 =item EVP_DigestInit_ex(),
237 success and 0 for failure.
239 =item EVP_MD_CTX_ctrl()
241 Returns 1 if successful or 0 for failure.
243 =item EVP_MD_CTX_copy_ex()
245 Returns 1 if successful or 0 for failure.
251 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
257 EVP_MD_CTX_block_size()
259 Returns the digest or block size in bytes.
263 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
265 =item EVP_get_digestbyname(),
266 EVP_get_digestbynid(),
267 EVP_get_digestbyobj()
269 Returns either an B<EVP_MD> structure or NULL if an error occurs.
271 =item EVP_MD_CTX_set_pkey_ctx()
273 This function has no return value.
279 The B<EVP> interface to message digests should almost always be used in
280 preference to the low level interfaces. This is because the code then becomes
281 transparent to the digest used and much more flexible.
283 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
284 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
285 are still in common use.
287 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
288 set to NULL to use the default digest implementation.
290 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
291 obsolete but are retained to maintain compatibility with existing code. New
292 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
293 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
294 instead of initializing and cleaning it up on each call and allow non default
295 implementations of digests to be specified.
297 If digest contexts are not cleaned up after use,
298 memory leaks will occur.
300 EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
301 EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
304 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
309 This example digests the data "Test Message\n" and "Hello World\n", using the
310 digest name passed on the command line.
313 #include <openssl/evp.h>
315 main(int argc, char *argv[])
319 char mess1[] = "Test Message\n";
320 char mess2[] = "Hello World\n";
321 unsigned char md_value[EVP_MAX_MD_SIZE];
324 if (argv[1] == NULL) {
325 printf("Usage: mdtest digestname\n");
329 md = EVP_get_digestbyname(argv[1]);
331 printf("Unknown message digest %s\n", argv[1]);
335 mdctx = EVP_MD_CTX_new();
336 EVP_DigestInit_ex(mdctx, md, NULL);
337 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
338 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
339 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
340 EVP_MD_CTX_free(mdctx);
342 printf("Digest is: ");
343 for (i = 0; i < md_len; i++)
344 printf("%02x", md_value[i]);
355 The full list of digest algorithms are provided below.
357 L<EVP_blake2b512(3)>,
371 EVP_MD_CTX_create() and EVP_MD_CTX_destroy() were renamed to
372 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0.
374 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
375 later, so now EVP_sha1() can be used with RSA and DSA.
377 EVP_dss1() was removed in OpenSSL 1.1.0.
379 EVP_MD_CTX_set_pkey_ctx() was added in 1.1.1.
383 Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
385 Licensed under the OpenSSL license (the "License"). You may not use
386 this file except in compliance with the License. You can obtain a copy
387 in the file LICENSE in the source distribution or at
388 L<https://www.openssl.org/source/license.html>.