1EVP_DIGESTINIT(3)                   OpenSSL                  EVP_DIGESTINIT(3)
2
3
4

NAME

6       EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy_ex,
7       EVP_MD_CTX_ctrl, EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags,
8       EVP_MD_CTX_test_flags, EVP_DigestInit_ex, EVP_DigestInit,
9       EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_DigestFinalXOF,
10       EVP_DigestFinal, EVP_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type,
11       EVP_MD_size, EVP_MD_block_size, EVP_MD_CTX_md, EVP_MD_CTX_size,
12       EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_MD_CTX_md_data,
13       EVP_md_null, EVP_get_digestbyname, EVP_get_digestbynid,
14       EVP_get_digestbyobj, EVP_MD_CTX_set_pkey_ctx - EVP digest routines
15

SYNOPSIS

17        #include <openssl/evp.h>
18
19        EVP_MD_CTX *EVP_MD_CTX_new(void);
20        int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
21        void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
22        void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
23        void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
24        void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
25        int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
26
27        int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
28        int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
29        int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
30        int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
31
32        int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
33
34        int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
35        int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
36
37        int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
38
39        int EVP_MD_type(const EVP_MD *md);
40        int EVP_MD_pkey_type(const EVP_MD *md);
41        int EVP_MD_size(const EVP_MD *md);
42        int EVP_MD_block_size(const EVP_MD *md);
43
44        const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
45        int EVP_MD_CTX_size(const EVP_MD *ctx);
46        int EVP_MD_CTX_block_size(const EVP_MD *ctx);
47        int EVP_MD_CTX_type(const EVP_MD *ctx);
48        void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
49
50        const EVP_MD *EVP_md_null(void);
51
52        const EVP_MD *EVP_get_digestbyname(const char *name);
53        const EVP_MD *EVP_get_digestbynid(int type);
54        const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
55
56        void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
57

DESCRIPTION

59       The EVP digest routines are a high level interface to message digests,
60       and should be used instead of the cipher-specific functions.
61
62       EVP_MD_CTX_new()
63           Allocates and returns a digest context.
64
65       EVP_MD_CTX_reset()
66           Resets the digest context ctx.  This can be used to reuse an
67           already existing context.
68
69       EVP_MD_CTX_free()
70           Cleans up digest context ctx and frees up the space allocated to
71           it.
72
73       EVP_MD_CTX_ctrl()
74           Performs digest-specific control actions on context ctx.
75
76       EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(),
77       EVP_MD_CTX_test_flags()
78           Sets, clears and tests ctx flags.  See "FLAGS" below for more
79           information.
80
81       EVP_DigestInit_ex()
82           Sets up digest context ctx to use a digest type from ENGINE impl.
83           type will typically be supplied by a function such as EVP_sha1().
84           If impl is NULL then the default implementation of digest type is
85           used.
86
87       EVP_DigestUpdate()
88           Hashes cnt bytes of data at d into the digest context ctx. This
89           function can be called several times on the same ctx to hash
90           additional data.
91
92       EVP_DigestFinal_ex()
93           Retrieves the digest value from ctx and places it in md. If the s
94           parameter is not NULL then the number of bytes of data written
95           (i.e. the length of the digest) will be written to the integer at
96           s, at most EVP_MAX_MD_SIZE bytes will be written.  After calling
97           EVP_DigestFinal_ex() no additional calls to EVP_DigestUpdate() can
98           be made, but EVP_DigestInit_ex() can be called to initialize a new
99           digest operation.
100
101       EVP_DigestFinalXOF()
102           Interfaces to extendable-output functions, XOFs, such as SHAKE128
103           and SHAKE256.  It retrieves the digest value from ctx and places it
104           in len-sized <B>md.  After calling this function no additional
105           calls to EVP_DigestUpdate() can be made, but EVP_DigestInit_ex()
106           can be called to initialize a new operation.
107
108       EVP_MD_CTX_copy_ex()
109           Can be used to copy the message digest state from in to out. This
110           is useful if large amounts of data are to be hashed which only
111           differ in the last few bytes.
112
113       EVP_DigestInit()
114           Behaves in the same way as EVP_DigestInit_ex() except it always
115           uses the default digest implementation.
116
117       EVP_DigestFinal()
118           Similar to EVP_DigestFinal_ex() except the digest context ctx is
119           automatically cleaned up.
120
121       EVP_MD_CTX_copy()
122           Similar to EVP_MD_CTX_copy_ex() except the destination out does not
123           have to be initialized.
124
125       EVP_MD_size(), EVP_MD_CTX_size()
126           Return the size of the message digest when passed an EVP_MD or an
127           EVP_MD_CTX structure, i.e. the size of the hash.
128
129       EVP_MD_block_size(), EVP_MD_CTX_block_size()
130           Return the block size of the message digest when passed an EVP_MD
131           or an EVP_MD_CTX structure.
132
133       EVP_MD_type(), EVP_MD_CTX_type()
134           Return the NID of the OBJECT IDENTIFIER representing the given
135           message digest when passed an EVP_MD structure.  For example,
136           "EVP_MD_type(EVP_sha1())" returns NID_sha1. This function is
137           normally used when setting ASN1 OIDs.
138
139       EVP_MD_CTX_md_data()
140           Return the digest method private data for the passed EVP_MD_CTX.
141           The space is allocated by OpenSSL and has the size originally set
142           with EVP_MD_meth_set_app_datasize().
143
144       EVP_MD_CTX_md()
145           Returns the EVP_MD structure corresponding to the passed
146           EVP_MD_CTX.
147
148       EVP_MD_pkey_type()
149           Returns the NID of the public key signing algorithm associated with
150           this digest. For example EVP_sha1() is associated with RSA so this
151           will return NID_sha1WithRSAEncryption. Since digests and signature
152           algorithms are no longer linked this function is only retained for
153           compatibility reasons.
154
155       EVP_md_null()
156           A "null" message digest that does nothing: i.e. the hash it returns
157           is of zero length.
158
159       EVP_get_digestbyname(), EVP_get_digestbynid(), EVP_get_digestbyobj()
160           Returns an EVP_MD structure when passed a digest name, a digest NID
161           or an ASN1_OBJECT structure respectively.
162
163       EVP_MD_CTX_set_pkey_ctx()
164           Assigns an EVP_PKEY_CTX to EVP_MD_CTX. This is usually used to
165           provide a customzied EVP_PKEY_CTX to EVP_DigestSignInit(3) or
166           EVP_DigestVerifyInit(3). The pctx passed to this function should be
167           freed by the caller. A NULL pctx pointer is also allowed to clear
168           the EVP_PKEY_CTX assigned to ctx. In such case, freeing the cleared
169           EVP_PKEY_CTX or not depends on how the EVP_PKEY_CTX is created.
170

FLAGS

172       EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and
173       EVP_MD_CTX_test_flags() can be used the manipulate and test these
174       EVP_MD_CTX flags:
175
176       EVP_MD_CTX_FLAG_ONESHOT
177           This flag instructs the digest to optimize for one update only, if
178           possible.
179
180       EVP_MD_CTX_FLAG_NO_INIT
181           This flag instructs EVP_DigestInit() and similar not to initialise
182           the implementation specific data.
183
184       EVP_MD_CTX_FLAG_FINALISE
185           Some functions such as EVP_DigestSign only finalise copies of
186           internal contexts so additional data can be included after the
187           finalisation call.  This is inefficient if this functionality is
188           not required, and can be disabled with this flag.
189

RETURN VALUES

191       EVP_DigestInit_ex(), EVP_DigestUpdate(), EVP_DigestFinal_ex()
192           Returns 1 for success and 0 for failure.
193
194       EVP_MD_CTX_ctrl()
195           Returns 1 if successful or 0 for failure.
196
197       EVP_MD_CTX_copy_ex()
198           Returns 1 if successful or 0 for failure.
199
200       EVP_MD_type(), EVP_MD_pkey_type(), EVP_MD_type()
201           Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef
202           if none exists.
203
204       EVP_MD_size(), EVP_MD_block_size(), EVP_MD_CTX_size(),
205       EVP_MD_CTX_block_size()
206           Returns the digest or block size in bytes.
207
208       EVP_md_null()
209           Returns a pointer to the EVP_MD structure of the "null" message
210           digest.
211
212       EVP_get_digestbyname(), EVP_get_digestbynid(), EVP_get_digestbyobj()
213           Returns either an EVP_MD structure or NULL if an error occurs.
214
215       EVP_MD_CTX_set_pkey_ctx()
216           This function has no return value.
217

NOTES

219       The EVP interface to message digests should almost always be used in
220       preference to the low level interfaces. This is because the code then
221       becomes transparent to the digest used and much more flexible.
222
223       New applications should use the SHA-2 (such as EVP_sha256(3)) or the
224       SHA-3 digest algorithms (such as EVP_sha3_512(3)). The other digest
225       algorithms are still in common use.
226
227       For most applications the impl parameter to EVP_DigestInit_ex() will be
228       set to NULL to use the default digest implementation.
229
230       The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy()
231       are obsolete but are retained to maintain compatibility with existing
232       code. New applications should use EVP_DigestInit_ex(),
233       EVP_DigestFinal_ex() and EVP_MD_CTX_copy_ex() because they can
234       efficiently reuse a digest context instead of initializing and cleaning
235       it up on each call and allow non default implementations of digests to
236       be specified.
237
238       If digest contexts are not cleaned up after use, memory leaks will
239       occur.
240
241       EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
242       EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as macros.
243
244       EVP_MD_CTX_ctrl() sends commands to message digests for additional
245       configuration or control.
246

EXAMPLE

248       This example digests the data "Test Message\n" and "Hello World\n",
249       using the digest name passed on the command line.
250
251        #include <stdio.h>
252        #include <openssl/evp.h>
253
254        main(int argc, char *argv[])
255        {
256            EVP_MD_CTX *mdctx;
257            const EVP_MD *md;
258            char mess1[] = "Test Message\n";
259            char mess2[] = "Hello World\n";
260            unsigned char md_value[EVP_MAX_MD_SIZE];
261            int md_len, i;
262
263            if (argv[1] == NULL) {
264                printf("Usage: mdtest digestname\n");
265                exit(1);
266            }
267
268            md = EVP_get_digestbyname(argv[1]);
269            if (md == NULL) {
270                printf("Unknown message digest %s\n", argv[1]);
271                exit(1);
272            }
273
274            mdctx = EVP_MD_CTX_new();
275            EVP_DigestInit_ex(mdctx, md, NULL);
276            EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
277            EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
278            EVP_DigestFinal_ex(mdctx, md_value, &md_len);
279            EVP_MD_CTX_free(mdctx);
280
281            printf("Digest is: ");
282            for (i = 0; i < md_len; i++)
283                printf("%02x", md_value[i]);
284            printf("\n");
285
286            exit(0);
287        }
288

SEE ALSO

290       dgst(1), evp(7)
291
292       The full list of digest algorithms are provided below.
293
294       EVP_blake2b512(3), EVP_md2(3), EVP_md4(3), EVP_md5(3), EVP_mdc2(3),
295       EVP_ripemd160(3), EVP_sha1(3), EVP_sha224(3), EVP_sha3_224(3),
296       EVP_sm3(3), EVP_whirlpool(3)
297

HISTORY

299       EVP_MD_CTX_create() and EVP_MD_CTX_destroy() were renamed to
300       EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0.
301
302       The link between digests and signing algorithms was fixed in OpenSSL
303       1.0 and later, so now EVP_sha1() can be used with RSA and DSA.
304
305       EVP_dss1() was removed in OpenSSL 1.1.0.
306
307       EVP_MD_CTX_set_pkey_ctx() was added in 1.1.1.
308
310       Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
311
312       Licensed under the OpenSSL license (the "License").  You may not use
313       this file except in compliance with the License.  You can obtain a copy
314       in the file LICENSE in the source distribution or at
315       <https://www.openssl.org/source/license.html>.
316
317
318
3191.1.1                             2018-09-11                 EVP_DIGESTINIT(3)
Impressum