]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_DigestInit.pod
Wrong release version for EVP_DigestSqueeze() in documentation
[thirdparty/openssl.git] / doc / man3 / EVP_DigestInit.pod
CommitLineData
3cbe1980
DSH
1=pod
2
3=head1 NAME
4
3fd70262 5EVP_MD_fetch, EVP_MD_up_ref, EVP_MD_free,
ae3ff60e 6EVP_MD_get_params, EVP_MD_gettable_params,
0324ae3e
P
7EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_dup,
8EVP_MD_CTX_copy, EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl,
ae3ff60e 9EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
e6879a31 10EVP_MD_settable_ctx_params, EVP_MD_gettable_ctx_params,
ae3ff60e 11EVP_MD_CTX_settable_params, EVP_MD_CTX_gettable_params,
d5e5e2ff 12EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
4d49b685 13EVP_Q_digest, EVP_Digest, EVP_DigestInit_ex2, EVP_DigestInit_ex, EVP_DigestInit,
5a7e9991 14EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
53664908 15EVP_DigestSqueeze,
6ea964cd 16EVP_MD_is_a, EVP_MD_get0_name, EVP_MD_get0_description,
31b7f23d
TM
17EVP_MD_names_do_all, EVP_MD_get0_provider, EVP_MD_get_type,
18EVP_MD_get_pkey_type, EVP_MD_get_size, EVP_MD_get_block_size, EVP_MD_get_flags,
19EVP_MD_CTX_get0_name, EVP_MD_CTX_md, EVP_MD_CTX_get0_md, EVP_MD_CTX_get1_md,
ed576acd
TM
20EVP_MD_CTX_get_type, EVP_MD_CTX_get_size, EVP_MD_CTX_get_block_size,
21EVP_MD_CTX_get0_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
a9cf71a3 22EVP_md_null,
37842dfa 23EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
ed576acd 24EVP_MD_CTX_get_pkey_ctx, EVP_MD_CTX_set_pkey_ctx,
31b7f23d
TM
25EVP_MD_do_all_provided,
26EVP_MD_type, EVP_MD_nid, EVP_MD_name, EVP_MD_pkey_type, EVP_MD_size,
27EVP_MD_block_size, EVP_MD_flags, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
28EVP_MD_CTX_type, EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_md_data
c540f00f 29- EVP digest routines
3cbe1980
DSH
30
31=head1 SYNOPSIS
32
33 #include <openssl/evp.h>
34
b4250010 35 EVP_MD *EVP_MD_fetch(OSSL_LIB_CTX *ctx, const char *algorithm,
2cafb1df 36 const char *properties);
3fd70262
RL
37 int EVP_MD_up_ref(EVP_MD *md);
38 void EVP_MD_free(EVP_MD *md);
ae3ff60e
RL
39 int EVP_MD_get_params(const EVP_MD *digest, OSSL_PARAM params[]);
40 const OSSL_PARAM *EVP_MD_gettable_params(const EVP_MD *digest);
25191fff
RL
41 EVP_MD_CTX *EVP_MD_CTX_new(void);
42 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
43 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
52ad5b60 44 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
4e7991b4 45 int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
d5e5e2ff 46 int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
e6879a31
MC
47 const OSSL_PARAM *EVP_MD_settable_ctx_params(const EVP_MD *md);
48 const OSSL_PARAM *EVP_MD_gettable_ctx_params(const EVP_MD *md);
49 const OSSL_PARAM *EVP_MD_CTX_settable_params(EVP_MD_CTX *ctx);
50 const OSSL_PARAM *EVP_MD_CTX_gettable_params(EVP_MD_CTX *ctx);
a9cf71a3
RL
51 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
52 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
53 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
e72d734d 54
4d49b685 55 int EVP_Q_digest(OSSL_LIB_CTX *libctx, const char *name, const char *propq,
006de767
RL
56 const void *data, size_t datalen,
57 unsigned char *md, size_t *mdlen);
37842dfa
AS
58 int EVP_Digest(const void *data, size_t count, unsigned char *md,
59 unsigned int *size, const EVP_MD *type, ENGINE *impl);
5a7e9991
P
60 int EVP_DigestInit_ex2(EVP_MD_CTX *ctx, const EVP_MD *type,
61 const OSSL_PARAM params[]);
e72d734d 62 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
109d3123 63 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
e9b77246 64 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
53664908 65 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *out, size_t outlen);
66 int EVP_DigestSqueeze(EVP_MD_CTX *ctx, unsigned char *out, size_t outlen);
e72d734d 67
0324ae3e 68 EVP_MD_CTX *EVP_MD_CTX_dup(const EVP_MD_CTX *in);
aebb9aac 69 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
e72d734d
DSH
70
71 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
e9b77246 72 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
3cbe1980 73
aebb9aac 74 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
e72d734d 75
ed576acd
TM
76 const char *EVP_MD_get0_name(const EVP_MD *md);
77 const char *EVP_MD_get0_description(const EVP_MD *md);
251e610c 78 int EVP_MD_is_a(const EVP_MD *md, const char *name);
d84f5515
MC
79 int EVP_MD_names_do_all(const EVP_MD *md,
80 void (*fn)(const char *name, void *data),
81 void *data);
ed576acd
TM
82 const OSSL_PROVIDER *EVP_MD_get0_provider(const EVP_MD *md);
83 int EVP_MD_get_type(const EVP_MD *md);
84 int EVP_MD_get_pkey_type(const EVP_MD *md);
85 int EVP_MD_get_size(const EVP_MD *md);
86 int EVP_MD_get_block_size(const EVP_MD *md);
87 unsigned long EVP_MD_get_flags(const EVP_MD *md);
3cbe1980 88
f6c95e46
RS
89 const EVP_MD *EVP_MD_CTX_get0_md(const EVP_MD_CTX *ctx);
90 EVP_MD *EVP_MD_CTX_get1_md(EVP_MD_CTX *ctx);
ed576acd
TM
91 const char *EVP_MD_CTX_get0_name(const EVP_MD_CTX *ctx);
92 int EVP_MD_CTX_get_size(const EVP_MD_CTX *ctx);
93 int EVP_MD_CTX_get_block_size(const EVP_MD_CTX *ctx);
94 int EVP_MD_CTX_get_type(const EVP_MD_CTX *ctx);
95 void *EVP_MD_CTX_get0_md_data(const EVP_MD_CTX *ctx);
3cbe1980 96
13588350 97 const EVP_MD *EVP_md_null(void);
e4adad92 98
3cbe1980 99 const EVP_MD *EVP_get_digestbyname(const char *name);
25191fff
RL
100 const EVP_MD *EVP_get_digestbynid(int type);
101 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
3cbe1980 102
ed576acd 103 EVP_PKEY_CTX *EVP_MD_CTX_get_pkey_ctx(const EVP_MD_CTX *ctx);
675f4cee
PY
104 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
105
b4250010 106 void EVP_MD_do_all_provided(OSSL_LIB_CTX *libctx,
251e610c
RL
107 void (*fn)(EVP_MD *mac, void *arg),
108 void *arg);
c540f00f 109
31b7f23d
TM
110 #define EVP_MD_type EVP_MD_get_type
111 #define EVP_MD_nid EVP_MD_get_type
112 #define EVP_MD_name EVP_MD_get0_name
113 #define EVP_MD_pkey_type EVP_MD_get_pkey_type
114 #define EVP_MD_size EVP_MD_get_size
115 #define EVP_MD_block_size EVP_MD_get_block_size
116 #define EVP_MD_flags EVP_MD_get_flags
117 #define EVP_MD_CTX_size EVP_MD_CTX_get_size
118 #define EVP_MD_CTX_block_size EVP_MD_CTX_get_block_size
119 #define EVP_MD_CTX_type EVP_MD_CTX_get_type
120 #define EVP_MD_CTX_pkey_ctx EVP_MD_CTX_get_pkey_ctx
121 #define EVP_MD_CTX_md_data EVP_MD_CTX_get0_md_data
122
3dbf8243
MC
123The following functions have been deprecated since OpenSSL 3.0, and can be
124hidden entirely by defining B<OPENSSL_API_COMPAT> with a suitable version value,
125see L<openssl_user_macros(7)>:
1409b5f6 126
f6c95e46
RS
127 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
128
1409b5f6
RS
129 int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
130 const void *data, size_t count);
131
132 void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
133 int (*update)(EVP_MD_CTX *ctx,
134 const void *data, size_t count));
135
3cbe1980
DSH
136=head1 DESCRIPTION
137
8c1cbc72 138The EVP digest routines are a high-level interface to message digests,
3fd70262
RL
139and should be used instead of the digest-specific functions.
140
141The B<EVP_MD> type is a structure for digest method implementation.
3cbe1980 142
bbda8ce9 143=over 4
25191fff 144
2cafb1df
RL
145=item EVP_MD_fetch()
146
041a96e7
RL
147Fetches the digest implementation for the given I<algorithm> from any
148provider offering it, within the criteria given by the I<properties>.
906bced1 149See L<crypto(7)/ALGORITHM FETCHING> for further information.
2cafb1df 150
3fd70262
RL
151The returned value must eventually be freed with EVP_MD_free().
152
153Fetched B<EVP_MD> structures are reference counted.
154
155=item EVP_MD_up_ref()
156
157Increments the reference count for an B<EVP_MD> structure.
158
159=item EVP_MD_free()
160
161Decrements the reference count for the fetched B<EVP_MD> structure.
162If the reference count drops to 0 then the structure is freed.
2cafb1df 163
bbda8ce9 164=item EVP_MD_CTX_new()
e72d734d 165
b45497c3 166Allocates and returns a digest context.
e72d734d 167
bbda8ce9 168=item EVP_MD_CTX_reset()
52ad5b60 169
041a96e7 170Resets the digest context I<ctx>. This can be used to reuse an already
bbda8ce9 171existing context.
3cbe1980 172
bbda8ce9 173=item EVP_MD_CTX_free()
3cbe1980 174
041a96e7 175Cleans up digest context I<ctx> and frees up the space allocated to it.
3cbe1980 176
bbda8ce9
RT
177=item EVP_MD_CTX_ctrl()
178
b5b91a79 179I<This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
d5e5e2ff 180is the mechanism that should be used to set and get parameters that are used by
b5b91a79
RL
181providers.>
182
041a96e7
RL
183Performs digest-specific control actions on context I<ctx>. The control command
184is indicated in I<cmd> and any additional arguments in I<p1> and I<p2>.
5a7e9991 185EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex2(). Other restrictions
37842dfa 186may apply depending on the control type and digest implementation.
b5b91a79
RL
187
188If this function happens to be used with a fetched B<EVP_MD>, it will
189translate the controls that are known to OpenSSL into L<OSSL_PARAM(3)>
190parameters with keys defined by OpenSSL and call EVP_MD_CTX_get_params() or
191EVP_MD_CTX_set_params() as is appropriate for each control command.
192
193See L</CONTROLS> below for more information, including what translations are
194being done.
bbda8ce9 195
ae3ff60e
RL
196=item EVP_MD_get_params()
197
041a96e7 198Retrieves the requested list of I<params> from a MD I<md>.
2710e8a8 199See L</PARAMETERS> below for more information.
ae3ff60e
RL
200
201=item EVP_MD_CTX_get_params()
d5e5e2ff 202
041a96e7 203Retrieves the requested list of I<params> from a MD context I<ctx>.
2710e8a8 204See L</PARAMETERS> below for more information.
d5e5e2ff 205
ae3ff60e 206=item EVP_MD_CTX_set_params()
d5e5e2ff 207
041a96e7 208Sets the list of I<params> into a MD context I<ctx>.
2710e8a8 209See L</PARAMETERS> below for more information.
d5e5e2ff 210
d618ac6f
P
211=item EVP_MD_gettable_params()
212
318a9dfa
RL
213Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
214that can be used with EVP_MD_get_params().
d618ac6f
P
215
216=item EVP_MD_gettable_ctx_params(), EVP_MD_CTX_gettable_params()
217
318a9dfa 218Get a constant L<OSSL_PARAM(3)> array that describes the retrievable parameters
d618ac6f
P
219that can be used with EVP_MD_CTX_get_params(). EVP_MD_gettable_ctx_params()
220returns the parameters that can be retrieved from the algorithm, whereas
221EVP_MD_CTX_gettable_params() returns the parameters that can be retrieved
318a9dfa 222in the context's current state.
d618ac6f
P
223
224=item EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params()
225
318a9dfa 226Get a constant L<OSSL_PARAM(3)> array that describes the settable parameters
d618ac6f
P
227that can be used with EVP_MD_CTX_set_params(). EVP_MD_settable_ctx_params()
228returns the parameters that can be set from the algorithm, whereas
229EVP_MD_CTX_settable_params() returns the parameters that can be set in the
318a9dfa 230context's current state.
ae3ff60e 231
a9cf71a3
RL
232=item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
233
041a96e7 234Sets, clears and tests I<ctx> flags. See L</FLAGS> below for more information.
a9cf71a3 235
4d49b685 236=item EVP_Q_digest() is a quick one-shot digest function.
006de767
RL
237
238It hashes I<datalen> bytes of data at I<data> using the digest algorithm
239I<name>, which is fetched using the optional I<libctx> and I<propq> parameters.
240The digest value is placed in I<md> and its length is written at I<mdlen>
4d49b685
DDO
241if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
242
37842dfa
AS
243=item EVP_Digest()
244
245A wrapper around the Digest Init_ex, Update and Final_ex functions.
041a96e7
RL
246Hashes I<count> bytes of data at I<data> using a digest I<type> from ENGINE
247I<impl>. The digest value is placed in I<md> and its length is written at I<size>
37842dfa 248if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
041a96e7 249If I<impl> is NULL the default implementation of digest I<type> is used.
37842dfa 250
5a7e9991
P
251=item EVP_DigestInit_ex2()
252
253Sets up digest context I<ctx> to use a digest I<type>.
254I<type> is typically supplied by a function such as EVP_sha1(), or a
255value explicitly fetched with EVP_MD_fetch().
256
257The parameters B<params> are set on the context after initialisation.
258
259The I<type> parameter can be NULL if I<ctx> has been already initialized
260with another EVP_DigestInit_ex() call and has not been reset with
261EVP_MD_CTX_reset().
262
bbda8ce9
RT
263=item EVP_DigestInit_ex()
264
041a96e7
RL
265Sets up digest context I<ctx> to use a digest I<type>.
266I<type> is typically supplied by a function such as EVP_sha1(), or a
2cafb1df
RL
267value explicitly fetched with EVP_MD_fetch().
268
041a96e7 269If I<impl> is non-NULL, its implementation of the digest I<type> is used if
2cafb1df 270there is one, and if not, the default implementation is used.
bbda8ce9 271
2b407d05
TM
272The I<type> parameter can be NULL if I<ctx> has been already initialized
273with another EVP_DigestInit_ex() call and has not been reset with
274EVP_MD_CTX_reset().
275
bbda8ce9
RT
276=item EVP_DigestUpdate()
277
041a96e7
RL
278Hashes I<cnt> bytes of data at I<d> into the digest context I<ctx>. This
279function can be called several times on the same I<ctx> to hash additional
bbda8ce9
RT
280data.
281
282=item EVP_DigestFinal_ex()
283
041a96e7 284Retrieves the digest value from I<ctx> and places it in I<md>. If the I<s>
bbda8ce9 285parameter is not NULL then the number of bytes of data written (i.e. the
041a96e7 286length of the digest) will be written to the integer at I<s>, at most
6a0ae393
TM
287B<EVP_MAX_MD_SIZE> bytes will be written unless the digest implementation
288allows changing the digest size and it is set to a larger value by the
289application. After calling EVP_DigestFinal_ex() no additional calls to
290EVP_DigestUpdate() can be made, but EVP_DigestInit_ex2() can be called to
291initialize a new digest operation.
bbda8ce9
RT
292
293=item EVP_DigestFinalXOF()
294
295Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
53664908 296It retrieves the digest value from I<ctx> and places it in I<outlen>-sized I<out>.
bbda8ce9 297After calling this function no additional calls to EVP_DigestUpdate() can be
5a7e9991 298made, but EVP_DigestInit_ex2() can be called to initialize a new operation.
53664908 299EVP_DigestFinalXOF() may only be called once
300
301=item EVP_DigestSqueeze()
302
303Similar to EVP_DigestFinalXOF() but allows multiple calls to be made to
304squeeze variable length output data.
305EVP_DigestFinalXOF() should not be called after this.
bbda8ce9 306
0324ae3e
P
307=item EVP_MD_CTX_dup()
308
309Can be used to duplicate the message digest state from I<in>. This is useful
310to avoid multiple EVP_MD_fetch() calls or if large amounts of data are to be
311hashed which only differ in the last few bytes.
312
bbda8ce9
RT
313=item EVP_MD_CTX_copy_ex()
314
041a96e7 315Can be used to copy the message digest state from I<in> to I<out>. This is
bbda8ce9 316useful if large amounts of data are to be hashed which only differ in the last
b45497c3 317few bytes.
bbda8ce9
RT
318
319=item EVP_DigestInit()
320
5a7e9991
P
321Behaves in the same way as EVP_DigestInit_ex2() except it doesn't set any
322parameters and calls EVP_MD_CTX_reset() so it cannot be used with an I<type>
323of NULL.
bbda8ce9
RT
324
325=item EVP_DigestFinal()
326
2b407d05
TM
327Similar to EVP_DigestFinal_ex() except after computing the digest
328the digest context I<ctx> is automatically cleaned up with EVP_MD_CTX_reset().
bbda8ce9
RT
329
330=item EVP_MD_CTX_copy()
cd8d1456 331
041a96e7 332Similar to EVP_MD_CTX_copy_ex() except the destination I<out> does not have to
bbda8ce9 333be initialized.
e72d734d 334
251e610c
RL
335=item EVP_MD_is_a()
336
337Returns 1 if I<md> is an implementation of an algorithm that's
338identifiable with I<name>, otherwise 0.
339
e4a1d023
RL
340If I<md> is a legacy digest (it's the return value from the likes of
341EVP_sha256() rather than the result of an EVP_MD_fetch()), only cipher
342names registered with the default library context (see
b4250010 343L<OSSL_LIB_CTX(3)>) will be considered.
e4a1d023 344
ed576acd
TM
345=item EVP_MD_get0_name(),
346EVP_MD_CTX_get0_name()
c750bc08 347
251e610c 348Return the name of the given message digest. For fetched message
f651c727
RL
349digests with multiple names, only one of them is returned; it's
350recommended to use EVP_MD_names_do_all() instead.
351
352=item EVP_MD_names_do_all()
353
354Traverses all names for the I<md>, and calls I<fn> with each name and
355I<data>. This is only useful with fetched B<EVP_MD>s.
c750bc08 356
ed576acd 357=item EVP_MD_get0_description()
03888233
RL
358
359Returns a description of the digest, meant for display and human consumption.
360The description is at the discretion of the digest implementation.
361
ed576acd 362=item EVP_MD_get0_provider()
1d2622d4
RL
363
364Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
365B<EVP_MD>.
366
ed576acd
TM
367=item EVP_MD_get_size(),
368EVP_MD_CTX_get_size()
e72d734d 369
bbda8ce9
RT
370Return the size of the message digest when passed an B<EVP_MD> or an
371B<EVP_MD_CTX> structure, i.e. the size of the hash.
e72d734d 372
ed576acd
TM
373=item EVP_MD_get_block_size(),
374EVP_MD_CTX_get_block_size()
3cbe1980 375
bbda8ce9
RT
376Return the block size of the message digest when passed an B<EVP_MD> or an
377B<EVP_MD_CTX> structure.
3cbe1980 378
ed576acd
TM
379=item EVP_MD_get_type(),
380EVP_MD_CTX_get_type()
3cbe1980 381
bbda8ce9 382Return the NID of the OBJECT IDENTIFIER representing the given message digest
ed576acd 383when passed an B<EVP_MD> structure. For example, C<EVP_MD_get_type(EVP_sha1())>
bbda8ce9 384returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
3cbe1980 385
ed576acd 386=item EVP_MD_CTX_get0_md_data()
a9cf71a3
RL
387
388Return the digest method private data for the passed B<EVP_MD_CTX>.
389The space is allocated by OpenSSL and has the size originally set with
390EVP_MD_meth_set_app_datasize().
391
f6c95e46 392=item EVP_MD_CTX_get0_md(), EVP_MD_CTX_get1_md()
3cbe1980 393
f6c95e46
RS
394EVP_MD_CTX_get0_md() returns
395the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
5a7e9991 396will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex2() (or
b7c913c8
MC
397other similar function) when the EVP_MD_CTX was first initialised. Note that
398where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
399this function will not have its reference count incremented and therefore it
400should not be used after the EVP_MD_CTX is freed.
f6c95e46
RS
401EVP_MD_CTX_get1_md() is the same except the ownership is passed to the
402caller and is from the passed B<EVP_MD_CTX>.
3cbe1980 403
37842dfa
AS
404=item EVP_MD_CTX_set_update_fn()
405
041a96e7 406Sets the update function for I<ctx> to I<update>.
1409b5f6 407This is the function that is called by EVP_DigestUpdate(). If not set, the
37842dfa
AS
408update function from the B<EVP_MD> type specified at initialization is used.
409
410=item EVP_MD_CTX_update_fn()
411
041a96e7 412Returns the update function for I<ctx>.
37842dfa 413
ed576acd 414=item EVP_MD_get_flags()
37842dfa 415
041a96e7 416Returns the I<md> flags. Note that these are different from the B<EVP_MD_CTX>
37842dfa
AS
417ones. See L<EVP_MD_meth_set_flags(3)> for more information.
418
ed576acd 419=item EVP_MD_get_pkey_type()
3cbe1980 420
bbda8ce9
RT
421Returns the NID of the public key signing algorithm associated with this
422digest. For example EVP_sha1() is associated with RSA so this will return
423B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
424longer linked this function is only retained for compatibility reasons.
3cbe1980 425
bbda8ce9
RT
426=item EVP_md_null()
427
428A "null" message digest that does nothing: i.e. the hash it returns is of zero
429length.
430
431=item EVP_get_digestbyname(),
432EVP_get_digestbynid(),
433EVP_get_digestbyobj()
434
435Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
436B<ASN1_OBJECT> structure respectively.
437
971dbab4
MC
438The EVP_get_digestbyname() function is present for backwards compatibility with
439OpenSSL prior to version 3 and is different to the EVP_MD_fetch() function
440since it does not attempt to "fetch" an implementation of the cipher.
441Additionally, it only knows about digests that are built-in to OpenSSL and have
442an associated NID. Similarly EVP_get_digestbynid() and EVP_get_digestbyobj()
443also return objects without an associated implementation.
444
445When the digest objects returned by these functions are used (such as in a call
446to EVP_DigestInit_ex()) an implementation of the digest will be implicitly
447fetched from the loaded providers. This fetch could fail if no suitable
448implementation is available. Use EVP_MD_fetch() instead to explicitly fetch
449the algorithm and an associated implementation from a provider.
450
451See L<crypto(7)/ALGORITHM FETCHING> for more information about fetching.
452
453The digest objects returned from these functions do not need to be freed with
454EVP_MD_free().
455
ed576acd 456=item EVP_MD_CTX_get_pkey_ctx()
37842dfa 457
041a96e7 458Returns the B<EVP_PKEY_CTX> assigned to I<ctx>. The returned pointer should not
37842dfa
AS
459be freed by the caller.
460
675f4cee
PY
461=item EVP_MD_CTX_set_pkey_ctx()
462
463Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
37842dfa 464a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
041a96e7
RL
465L<EVP_DigestVerifyInit(3)>. The I<pctx> passed to this function should be freed
466by the caller. A NULL I<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
467assigned to I<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
81c79453 468depends on how the B<EVP_PKEY_CTX> is created.
675f4cee 469
251e610c 470=item EVP_MD_do_all_provided()
c540f00f
RL
471
472Traverses all messages digests implemented by all activated providers
473in the given library context I<libctx>, and for each of the implementations,
474calls the given function I<fn> with the implementation method and the given
475I<arg> as argument.
476
bbda8ce9 477=back
3cbe1980 478
2710e8a8 479=head1 PARAMETERS
d5e5e2ff
SL
480
481See L<OSSL_PARAM(3)> for information about passing parameters.
482
483EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
484
485=over 4
486
af53092c 487=item "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>) <unsigned integer>
d5e5e2ff
SL
488
489Sets the digest length for extendable output functions.
53664908 490The value should not exceed what can be given using a B<size_t>.
491It may be used by BLAKE2B-512, SHAKE-128 and SHAKE-256 to set the
492output length used by EVP_DigestFinal_ex() and EVP_DigestFinal().
d5e5e2ff 493
af53092c 494=item "pad-type" (B<OSSL_DIGEST_PARAM_PAD_TYPE>) <unsigned integer>
d5e5e2ff 495
041a96e7 496Sets the padding type.
d5e5e2ff
SL
497It is used by the MDC2 algorithm.
498
499=back
500
501EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
502
503=over 4
504
0c452a51 505=item "micalg" (B<OSSL_PARAM_DIGEST_KEY_MICALG>) <UTF8 string>.
d5e5e2ff
SL
506
507Gets the digest Message Integrity Check algorithm string. This is used when
508creating S/MIME multipart/signed messages, as specified in RFC 3851.
509It may be used by external engines or providers.
510
511=back
512
37842dfa
AS
513=head1 CONTROLS
514
515EVP_MD_CTX_ctrl() can be used to send the following standard controls:
516
517=over 4
518
519=item EVP_MD_CTRL_MICALG
520
521Gets the digest Message Integrity Check algorithm string. This is used when
522creating S/MIME multipart/signed messages, as specified in RFC 3851.
041a96e7 523The string value is written to I<p2>.
37842dfa 524
b5b91a79
RL
525When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
526an L<OSSL_PARAM(3)> item with the key "micalg" (B<OSSL_DIGEST_PARAM_MICALG>).
527
37842dfa
AS
528=item EVP_MD_CTRL_XOF_LEN
529
041a96e7 530This control sets the digest length for extendable output functions to I<p1>.
37842dfa 531Sending this control directly should not be necessary, the use of
041a96e7 532EVP_DigestFinalXOF() is preferred.
37842dfa
AS
533Currently used by SHAKE.
534
b5b91a79
RL
535When used with a fetched B<EVP_MD>, EVP_MD_CTX_get_params() gets called with
536an L<OSSL_PARAM(3)> item with the key "xoflen" (B<OSSL_DIGEST_PARAM_XOFLEN>).
537
37842dfa
AS
538=back
539
a9cf71a3
RL
540=head1 FLAGS
541
542EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
543can be used the manipulate and test these B<EVP_MD_CTX> flags:
544
545=over 4
546
547=item EVP_MD_CTX_FLAG_ONESHOT
548
549This flag instructs the digest to optimize for one update only, if possible.
550
551=for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
552
553=for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
554
555=for comment We currently avoid documenting flags that are only bit holder:
556EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
557
558=item EVP_MD_CTX_FLAG_NO_INIT
559
560This flag instructs EVP_DigestInit() and similar not to initialise the
561implementation specific data.
562
563=item EVP_MD_CTX_FLAG_FINALISE
564
565Some functions such as EVP_DigestSign only finalise copies of internal
566contexts so additional data can be included after the finalisation call.
567This is inefficient if this functionality is not required, and can be
568disabled with this flag.
569
570=back
571
3cbe1980
DSH
572=head1 RETURN VALUES
573
bbda8ce9
RT
574=over 4
575
2cafb1df
RL
576=item EVP_MD_fetch()
577
578Returns a pointer to a B<EVP_MD> for success or NULL for failure.
579
3fd70262
RL
580=item EVP_MD_up_ref()
581
582Returns 1 for success or 0 for failure.
583
4d49b685
DDO
584=item EVP_Q_digest(),
585EVP_Digest(),
586EVP_DigestInit_ex2(),
5a7e9991 587EVP_DigestInit_ex(),
d4c5d8ff 588EVP_DigestInit(),
bbda8ce9 589EVP_DigestUpdate(),
4d49b685
DDO
590EVP_DigestFinal_ex(),
591EVP_DigestFinalXOF(), and
592EVP_DigestFinal()
bbda8ce9 593
4d49b685 594return 1 for
13588350 595success and 0 for failure.
3cbe1980 596
bbda8ce9
RT
597=item EVP_MD_CTX_ctrl()
598
599Returns 1 if successful or 0 for failure.
600
d5e5e2ff
SL
601=item EVP_MD_CTX_set_params(),
602EVP_MD_CTX_get_params()
603
604Returns 1 if successful or 0 for failure.
605
ae3ff60e
RL
606=item EVP_MD_CTX_settable_params(),
607EVP_MD_CTX_gettable_params()
608
318a9dfa 609Return an array of constant L<OSSL_PARAM(3)>s, or NULL if there is none
ae3ff60e
RL
610to get.
611
0324ae3e
P
612=item EVP_MD_CTX_dup()
613
614Returns a new EVP_MD_CTX if successful or NULL on failure.
615
bbda8ce9 616=item EVP_MD_CTX_copy_ex()
52ad5b60 617
bbda8ce9 618Returns 1 if successful or 0 for failure.
3cbe1980 619
ed576acd
TM
620=item EVP_MD_get_type(),
621EVP_MD_get_pkey_type()
3cbe1980 622
bbda8ce9
RT
623Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
624exists.
3cbe1980 625
ed576acd
TM
626=item EVP_MD_get_size(),
627EVP_MD_get_block_size(),
628EVP_MD_CTX_get_size(),
629EVP_MD_CTX_get_block_size()
3cbe1980 630
dd1f2842 631Returns the digest or block size in bytes or -1 for failure.
bbda8ce9
RT
632
633=item EVP_md_null()
634
635Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
636
637=item EVP_get_digestbyname(),
638EVP_get_digestbynid(),
639EVP_get_digestbyobj()
640
641Returns either an B<EVP_MD> structure or NULL if an error occurs.
642
675f4cee
PY
643=item EVP_MD_CTX_set_pkey_ctx()
644
645This function has no return value.
646
d84f5515
MC
647=item EVP_MD_names_do_all()
648
649Returns 1 if the callback was called for all names. A return value of 0 means
650that the callback was not called for any names.
651
bbda8ce9 652=back
3cbe1980
DSH
653
654=head1 NOTES
655
656The B<EVP> interface to message digests should almost always be used in
8c1cbc72 657preference to the low-level interfaces. This is because the code then becomes
3cbe1980
DSH
658transparent to the digest used and much more flexible.
659
bbda8ce9 660New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
89f66fe2 661digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
662are still in common use.
3cbe1980 663
041a96e7 664For most applications the I<impl> parameter to EVP_DigestInit_ex() will be
e72d734d
DSH
665set to NULL to use the default digest implementation.
666
d4c5d8ff
TM
667Ignoring failure returns of EVP_DigestInit_ex(), EVP_DigestInit_ex2(), or
668EVP_DigestInit() can lead to undefined behavior on subsequent calls
669updating or finalizing the B<EVP_MD_CTX> such as the EVP_DigestUpdate() or
670EVP_DigestFinal() functions. The only valid calls on the B<EVP_MD_CTX>
671when initialization fails are calls that attempt another initialization of
672the context or release the context.
673
7b3e11c5 674The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
e72d734d 675obsolete but are retained to maintain compatibility with existing code. New
7b3e11c5 676applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
e72d734d
DSH
677EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
678instead of initializing and cleaning it up on each call and allow non default
679implementations of digests to be specified.
680
fa332bba 681If digest contexts are not cleaned up after use,
7b3e11c5 682memory leaks will occur.
13588350 683
ed576acd
TM
684EVP_MD_CTX_get0_name(), EVP_MD_CTX_get_size(), EVP_MD_CTX_get_block_size(),
685EVP_MD_CTX_get_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are
686defined as macros.
5a34fcd7 687
52ad5b60
TS
688EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
689or control.
5a34fcd7 690
cda77422 691=head1 EXAMPLES
3cbe1980
DSH
692
693This example digests the data "Test Message\n" and "Hello World\n", using the
694digest name passed on the command line.
695
696 #include <stdio.h>
19ac1bf2 697 #include <string.h>
3cbe1980
DSH
698 #include <openssl/evp.h>
699
19ac1bf2 700 int main(int argc, char *argv[])
3cbe1980 701 {
2947af32
BB
702 EVP_MD_CTX *mdctx;
703 const EVP_MD *md;
704 char mess1[] = "Test Message\n";
705 char mess2[] = "Hello World\n";
706 unsigned char md_value[EVP_MAX_MD_SIZE];
19ac1bf2 707 unsigned int md_len, i;
2947af32
BB
708
709 if (argv[1] == NULL) {
710 printf("Usage: mdtest digestname\n");
711 exit(1);
712 }
713
714 md = EVP_get_digestbyname(argv[1]);
715 if (md == NULL) {
716 printf("Unknown message digest %s\n", argv[1]);
717 exit(1);
718 }
719
720 mdctx = EVP_MD_CTX_new();
d4c5d8ff
TM
721 if (!EVP_DigestInit_ex2(mdctx, md, NULL)) {
722 printf("Message digest initialization failed.\n");
723 EVP_MD_CTX_free(mdctx);
724 exit(1);
725 }
726 if (!EVP_DigestUpdate(mdctx, mess1, strlen(mess1))) {
727 printf("Message digest update failed.\n");
728 EVP_MD_CTX_free(mdctx);
729 exit(1);
730 }
731 if (!EVP_DigestUpdate(mdctx, mess2, strlen(mess2))) {
732 printf("Message digest update failed.\n");
733 EVP_MD_CTX_free(mdctx);
734 exit(1);
735 }
736 if (!EVP_DigestFinal_ex(mdctx, md_value, &md_len)) {
737 printf("Message digest finalization failed.\n");
738 EVP_MD_CTX_free(mdctx);
739 exit(1);
740 }
2947af32
BB
741 EVP_MD_CTX_free(mdctx);
742
743 printf("Digest is: ");
744 for (i = 0; i < md_len; i++)
745 printf("%02x", md_value[i]);
746 printf("\n");
747
748 exit(0);
3cbe1980
DSH
749 }
750
3cbe1980
DSH
751=head1 SEE ALSO
752
37842dfa 753L<EVP_MD_meth_new(3)>,
1903a9b7 754L<openssl-dgst(1)>,
d5e5e2ff
SL
755L<evp(7)>,
756L<OSSL_PROVIDER(3)>,
8809fdff
P
757L<OSSL_PARAM(3)>,
758L<property(7)>,
759L<crypto(7)/ALGORITHM FETCHING>,
760L<provider-digest(7)>,
761L<life_cycle-digest(7)>
4facdbb5 762
bbda8ce9
RT
763The full list of digest algorithms are provided below.
764
765L<EVP_blake2b512(3)>,
766L<EVP_md2(3)>,
767L<EVP_md4(3)>,
768L<EVP_md5(3)>,
769L<EVP_mdc2(3)>,
770L<EVP_ripemd160(3)>,
771L<EVP_sha1(3)>,
772L<EVP_sha224(3)>,
773L<EVP_sha3_224(3)>,
67e247fa 774L<EVP_sm3(3)>,
bbda8ce9
RT
775L<EVP_whirlpool(3)>
776
3cbe1980
DSH
777=head1 HISTORY
778
fc5ecadd
DMSP
779The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
780EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
25191fff 781
3fa39ed7 782The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
a95d7574
RS
783later, so now EVP_sha1() can be used with RSA and DSA.
784
fc5ecadd 785The EVP_dss1() function was removed in OpenSSL 1.1.0.
3fa39ed7 786
1409b5f6 787The EVP_MD_CTX_set_pkey_ctx() function was added in OpenSSL 1.1.1.
675f4cee 788
4d49b685
DDO
789The EVP_Q_digest(), EVP_DigestInit_ex2(),
790EVP_MD_fetch(), EVP_MD_free(), EVP_MD_up_ref(),
d618ac6f
P
791EVP_MD_get_params(), EVP_MD_CTX_set_params(), EVP_MD_CTX_get_params(),
792EVP_MD_gettable_params(), EVP_MD_gettable_ctx_params(),
793EVP_MD_settable_ctx_params(), EVP_MD_CTX_settable_params() and
794EVP_MD_CTX_gettable_params() functions were added in OpenSSL 3.0.
795
31b7f23d
TM
796The EVP_MD_type(), EVP_MD_nid(), EVP_MD_name(), EVP_MD_pkey_type(),
797EVP_MD_size(), EVP_MD_block_size(), EVP_MD_flags(), EVP_MD_CTX_size(),
798EVP_MD_CTX_block_size(), EVP_MD_CTX_type(), and EVP_MD_CTX_md_data()
799functions were renamed to include C<get> or C<get0> in their names in
800OpenSSL 3.0, respectively. The old names are kept as non-deprecated
801alias macros.
802
f6c95e46
RS
803The EVP_MD_CTX_md() function was deprecated in OpenSSL 3.0; use
804EVP_MD_CTX_get0_md() instead.
805EVP_MD_CTX_update_fn() and EVP_MD_CTX_set_update_fn() were deprecated
1409b5f6 806in OpenSSL 3.0.
d5e5e2ff 807
a8e32a6b
SS
808The EVP_MD_CTX_dup() function was added in OpenSSL 3.1.
809
810The EVP_DigestSqueeze() function was added in OpenSSL 3.3.
0324ae3e 811
e2f92610
RS
812=head1 COPYRIGHT
813
da1c088f 814Copyright 2000-2023 The OpenSSL Project Authors. All Rights Reserved.
e2f92610 815
4746f25a 816Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
817this file except in compliance with the License. You can obtain a copy
818in the file LICENSE in the source distribution or at
819L<https://www.openssl.org/source/license.html>.
820
821=cut