5 EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy_ex,
6 EVP_MD_CTX_ctrl, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
7 EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
8 EVP_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size,
9 EVP_MD_block_size, EVP_MD_CTX_md, EVP_MD_CTX_size,
10 EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_md_null,
11 EVP_get_digestbyname, EVP_get_digestbynid,
12 EVP_get_digestbyobj - EVP digest routines
16 #include <openssl/evp.h>
18 EVP_MD_CTX *EVP_MD_CTX_new(void);
19 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
20 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
21 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
23 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
24 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
25 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
26 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
28 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
30 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
31 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
33 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
35 int EVP_MD_type(const EVP_MD *md);
36 int EVP_MD_pkey_type(const EVP_MD *md);
37 int EVP_MD_size(const EVP_MD *md);
38 int EVP_MD_block_size(const EVP_MD *md);
40 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
41 int EVP_MD_CTX_size(const EVP_MD *ctx);
42 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
43 int EVP_MD_CTX_type(const EVP_MD *ctx);
45 const EVP_MD *EVP_md_null(void);
47 const EVP_MD *EVP_get_digestbyname(const char *name);
48 const EVP_MD *EVP_get_digestbynid(int type);
49 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
53 The EVP digest routines are a high level interface to message digests,
54 and should be used instead of the cipher-specific functions.
58 =item EVP_MD_CTX_new()
60 Allocates and returns a digest context.
62 =item EVP_MD_CTX_reset()
64 Resets the digest context B<ctx>. This can be used to reuse an already
67 =item EVP_MD_CTX_free()
69 Cleans up digest context B<ctx> and frees up the space allocated to it.
71 =item EVP_MD_CTX_ctrl()
73 Performs digest-specific control actions on context B<ctx>.
75 =item EVP_DigestInit_ex()
77 Sets up digest context B<ctx> to use a digest B<type> from ENGINE B<impl>.
78 B<type> will typically be supplied by a function such as EVP_sha1(). If
79 B<impl> is NULL then the default implementation of digest B<type> is used.
81 =item EVP_DigestUpdate()
83 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
84 function can be called several times on the same B<ctx> to hash additional
87 =item EVP_DigestFinal_ex()
89 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
90 parameter is not NULL then the number of bytes of data written (i.e. the
91 length of the digest) will be written to the integer at B<s>, at most
92 B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
93 no additional calls to EVP_DigestUpdate() can be made, but
94 EVP_DigestInit_ex() can be called to initialize a new digest operation.
96 =item EVP_DigestFinalXOF()
98 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
99 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
100 After calling this function no additional calls to EVP_DigestUpdate() can be
101 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
103 =item EVP_MD_CTX_copy_ex()
105 Can be used to copy the message digest state from B<in> to B<out>. This is
106 useful if large amounts of data are to be hashed which only differ in the last
109 =item EVP_DigestInit()
111 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
112 default digest implementation.
114 =item EVP_DigestFinal()
116 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
117 automatically cleaned up.
119 =item EVP_MD_CTX_copy()
121 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
127 Return the size of the message digest when passed an B<EVP_MD> or an
128 B<EVP_MD_CTX> structure, i.e. the size of the hash.
130 =item EVP_MD_block_size(),
131 EVP_MD_CTX_block_size()
133 Return the block size of the message digest when passed an B<EVP_MD> or an
134 B<EVP_MD_CTX> structure.
139 Return the NID of the OBJECT IDENTIFIER representing the given message digest
140 when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
141 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
143 =item EVP_MD_CTX_md()
145 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>.
147 =item EVP_MD_pkey_type()
149 Returns the NID of the public key signing algorithm associated with this
150 digest. For example EVP_sha1() is associated with RSA so this will return
151 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
152 longer linked this function is only retained for compatibility reasons.
156 A "null" message digest that does nothing: i.e. the hash it returns is of zero
159 =item EVP_get_digestbyname(),
160 EVP_get_digestbynid(),
161 EVP_get_digestbyobj()
163 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
164 B<ASN1_OBJECT> structure respectively.
172 =item EVP_DigestInit_ex(),
177 success and 0 for failure.
179 =item EVP_MD_CTX_ctrl()
181 Returns 1 if successful or 0 for failure.
183 =item EVP_MD_CTX_copy_ex()
185 Returns 1 if successful or 0 for failure.
191 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
197 EVP_MD_CTX_block_size()
199 Returns the digest or block size in bytes.
203 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
205 =item EVP_get_digestbyname(),
206 EVP_get_digestbynid(),
207 EVP_get_digestbyobj()
209 Returns either an B<EVP_MD> structure or NULL if an error occurs.
215 The B<EVP> interface to message digests should almost always be used in
216 preference to the low level interfaces. This is because the code then becomes
217 transparent to the digest used and much more flexible.
219 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
220 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
221 are still in common use.
223 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
224 set to NULL to use the default digest implementation.
226 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
227 obsolete but are retained to maintain compatibility with existing code. New
228 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
229 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
230 instead of initializing and cleaning it up on each call and allow non default
231 implementations of digests to be specified.
233 If digest contexts are not cleaned up after use
234 memory leaks will occur.
236 EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
237 EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
240 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
245 This example digests the data "Test Message\n" and "Hello World\n", using the
246 digest name passed on the command line.
249 #include <openssl/evp.h>
251 main(int argc, char *argv[])
255 char mess1[] = "Test Message\n";
256 char mess2[] = "Hello World\n";
257 unsigned char md_value[EVP_MAX_MD_SIZE];
260 if (argv[1] == NULL) {
261 printf("Usage: mdtest digestname\n");
265 md = EVP_get_digestbyname(argv[1]);
267 printf("Unknown message digest %s\n", argv[1]);
271 mdctx = EVP_MD_CTX_new();
272 EVP_DigestInit_ex(mdctx, md, NULL);
273 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
274 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
275 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
276 EVP_MD_CTX_free(mdctx);
278 printf("Digest is: ");
279 for (i = 0; i < md_len; i++)
280 printf("%02x", md_value[i]);
291 The full list of digest algorithms are provided below.
293 L<EVP_blake2b512(3)>,
307 EVP_MD_CTX_create() and EVP_MD_CTX_destroy() were renamed to
308 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0.
310 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
311 later, so now EVP_sha1() can be used with RSA and DSA.
313 EVP_dss1() was removed in OpenSSL 1.1.0.
317 Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
319 Licensed under the OpenSSL license (the "License"). You may not use
320 this file except in compliance with the License. You can obtain a copy
321 in the file LICENSE in the source distribution or at
322 L<https://www.openssl.org/source/license.html>.