]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_DigestInit.pod
Fix a grammar nit in CRYPTO_get_ex_new_index.pod
[thirdparty/openssl.git] / doc / man3 / EVP_DigestInit.pod
CommitLineData
3cbe1980
DSH
1=pod
2
3=head1 NAME
4
37842dfa
AS
5EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy,
6EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl, EVP_MD_CTX_set_flags,
7EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
8EVP_Digest, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
cd8d1456 9EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
37842dfa
AS
10EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_flags,
11EVP_MD_CTX_md, EVP_MD_CTX_type, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
12EVP_MD_CTX_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
a9cf71a3 13EVP_md_null,
37842dfa
AS
14EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
15EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_set_pkey_ctx - EVP digest routines
3cbe1980
DSH
16
17=head1 SYNOPSIS
18
19 #include <openssl/evp.h>
20
25191fff
RL
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);
52ad5b60 24 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
a9cf71a3
RL
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);
e72d734d 28
37842dfa
AS
29 int EVP_Digest(const void *data, size_t count, unsigned char *md,
30 unsigned int *size, const EVP_MD *type, ENGINE *impl);
e72d734d 31 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
109d3123 32 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
e9b77246 33 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
cd8d1456 34 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
e72d734d 35
aebb9aac 36 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
e72d734d
DSH
37
38 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
e9b77246 39 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
3cbe1980 40
aebb9aac 41 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
e72d734d 42
5a34fcd7 43 int EVP_MD_type(const EVP_MD *md);
05ea606a 44 int EVP_MD_pkey_type(const EVP_MD *md);
5a34fcd7
DSH
45 int EVP_MD_size(const EVP_MD *md);
46 int EVP_MD_block_size(const EVP_MD *md);
37842dfa 47 unsigned long EVP_MD_flags(const EVP_MD *md);
3cbe1980 48
5a34fcd7 49 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
25191fff
RL
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);
a9cf71a3 53 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
37842dfa
AS
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));
3cbe1980 59
13588350 60 const EVP_MD *EVP_md_null(void);
e4adad92 61
3cbe1980 62 const EVP_MD *EVP_get_digestbyname(const char *name);
25191fff
RL
63 const EVP_MD *EVP_get_digestbynid(int type);
64 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
3cbe1980 65
37842dfa 66 EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
675f4cee
PY
67 void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
68
3cbe1980
DSH
69=head1 DESCRIPTION
70
c7497f34
RS
71The EVP digest routines are a high level interface to message digests,
72and should be used instead of the cipher-specific functions.
3cbe1980 73
bbda8ce9 74=over 4
25191fff 75
bbda8ce9 76=item EVP_MD_CTX_new()
e72d734d 77
b45497c3 78Allocates and returns a digest context.
e72d734d 79
bbda8ce9 80=item EVP_MD_CTX_reset()
52ad5b60 81
bbda8ce9
RT
82Resets the digest context B<ctx>. This can be used to reuse an already
83existing context.
3cbe1980 84
bbda8ce9 85=item EVP_MD_CTX_free()
3cbe1980 86
bbda8ce9 87Cleans up digest context B<ctx> and frees up the space allocated to it.
3cbe1980 88
bbda8ce9
RT
89=item EVP_MD_CTX_ctrl()
90
37842dfa
AS
91Performs digest-specific control actions on context B<ctx>. The control command
92is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
93EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
94may apply depending on the control type and digest implementation.
95See L</CONTROLS> below for more information.
bbda8ce9 96
a9cf71a3
RL
97=item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
98
99Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
100
37842dfa
AS
101=item EVP_Digest()
102
103A wrapper around the Digest Init_ex, Update and Final_ex functions.
104Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
105B<impl>. The digest value is placed in B<md> and its length is written at B<size>
106if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
107If B<impl> is NULL the default implementation of digest B<type> is used.
108
bbda8ce9
RT
109=item EVP_DigestInit_ex()
110
111Sets up digest context B<ctx> to use a digest B<type> from ENGINE B<impl>.
b45497c3
P
112B<type> will typically be supplied by a function such as EVP_sha1(). If
113B<impl> is NULL then the default implementation of digest B<type> is used.
bbda8ce9
RT
114
115=item EVP_DigestUpdate()
116
117Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
118function can be called several times on the same B<ctx> to hash additional
119data.
120
121=item EVP_DigestFinal_ex()
122
123Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
124parameter is not NULL then the number of bytes of data written (i.e. the
125length of the digest) will be written to the integer at B<s>, at most
126B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
cd8d1456 127no additional calls to EVP_DigestUpdate() can be made, but
bbda8ce9
RT
128EVP_DigestInit_ex() can be called to initialize a new digest operation.
129
130=item EVP_DigestFinalXOF()
131
132Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
133It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
134After calling this function no additional calls to EVP_DigestUpdate() can be
135made, but EVP_DigestInit_ex() can be called to initialize a new operation.
136
137=item EVP_MD_CTX_copy_ex()
138
139Can be used to copy the message digest state from B<in> to B<out>. This is
140useful if large amounts of data are to be hashed which only differ in the last
b45497c3 141few bytes.
bbda8ce9
RT
142
143=item EVP_DigestInit()
144
b45497c3
P
145Behaves in the same way as EVP_DigestInit_ex() except it always uses the
146default digest implementation.
bbda8ce9
RT
147
148=item EVP_DigestFinal()
149
150Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
151automatically cleaned up.
152
153=item EVP_MD_CTX_copy()
cd8d1456 154
bbda8ce9
RT
155Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
156be initialized.
e72d734d 157
bbda8ce9
RT
158=item EVP_MD_size(),
159EVP_MD_CTX_size()
e72d734d 160
bbda8ce9
RT
161Return the size of the message digest when passed an B<EVP_MD> or an
162B<EVP_MD_CTX> structure, i.e. the size of the hash.
e72d734d 163
bbda8ce9
RT
164=item EVP_MD_block_size(),
165EVP_MD_CTX_block_size()
3cbe1980 166
bbda8ce9
RT
167Return the block size of the message digest when passed an B<EVP_MD> or an
168B<EVP_MD_CTX> structure.
3cbe1980 169
bbda8ce9
RT
170=item EVP_MD_type(),
171EVP_MD_CTX_type()
3cbe1980 172
bbda8ce9
RT
173Return the NID of the OBJECT IDENTIFIER representing the given message digest
174when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
175returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
3cbe1980 176
a9cf71a3
RL
177=item EVP_MD_CTX_md_data()
178
179Return the digest method private data for the passed B<EVP_MD_CTX>.
180The space is allocated by OpenSSL and has the size originally set with
181EVP_MD_meth_set_app_datasize().
182
bbda8ce9 183=item EVP_MD_CTX_md()
3cbe1980 184
bbda8ce9 185Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>.
3cbe1980 186
37842dfa
AS
187=item EVP_MD_CTX_set_update_fn()
188
189Sets the update function for B<ctx> to B<update>.
190This is the function that is called by EVP_DigestUpdate. If not set, the
191update function from the B<EVP_MD> type specified at initialization is used.
192
193=item EVP_MD_CTX_update_fn()
194
195Returns the update function for B<ctx>.
196
197=item EVP_MD_flags()
198
199Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
200ones. See L<EVP_MD_meth_set_flags(3)> for more information.
201
bbda8ce9 202=item EVP_MD_pkey_type()
3cbe1980 203
bbda8ce9
RT
204Returns the NID of the public key signing algorithm associated with this
205digest. For example EVP_sha1() is associated with RSA so this will return
206B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
207longer linked this function is only retained for compatibility reasons.
3cbe1980 208
bbda8ce9
RT
209=item EVP_md_null()
210
211A "null" message digest that does nothing: i.e. the hash it returns is of zero
212length.
213
214=item EVP_get_digestbyname(),
215EVP_get_digestbynid(),
216EVP_get_digestbyobj()
217
218Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
219B<ASN1_OBJECT> structure respectively.
220
37842dfa
AS
221=item EVP_MD_CTX_pkey_ctx()
222
223Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
224be freed by the caller.
225
675f4cee
PY
226=item EVP_MD_CTX_set_pkey_ctx()
227
228Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
37842dfa 229a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
81c79453 230L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
f922dac8 231by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
81c79453
PY
232assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
233depends on how the B<EVP_PKEY_CTX> is created.
675f4cee 234
bbda8ce9 235=back
3cbe1980 236
37842dfa
AS
237=head1 CONTROLS
238
239EVP_MD_CTX_ctrl() can be used to send the following standard controls:
240
241=over 4
242
243=item EVP_MD_CTRL_MICALG
244
245Gets the digest Message Integrity Check algorithm string. This is used when
246creating S/MIME multipart/signed messages, as specified in RFC 3851.
247The string value is written to B<p2>.
248
249=item EVP_MD_CTRL_XOF_LEN
250
251This control sets the digest length for extendable output functions to B<p1>.
252Sending this control directly should not be necessary, the use of
253C<EVP_DigestFinalXOF()> is preferred.
254Currently used by SHAKE.
255
256=back
257
a9cf71a3
RL
258=head1 FLAGS
259
260EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
261can be used the manipulate and test these B<EVP_MD_CTX> flags:
262
263=over 4
264
265=item EVP_MD_CTX_FLAG_ONESHOT
266
267This flag instructs the digest to optimize for one update only, if possible.
268
269=for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
270
271=for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
272
273=for comment We currently avoid documenting flags that are only bit holder:
274EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
275
276=item EVP_MD_CTX_FLAG_NO_INIT
277
278This flag instructs EVP_DigestInit() and similar not to initialise the
279implementation specific data.
280
281=item EVP_MD_CTX_FLAG_FINALISE
282
283Some functions such as EVP_DigestSign only finalise copies of internal
284contexts so additional data can be included after the finalisation call.
285This is inefficient if this functionality is not required, and can be
286disabled with this flag.
287
288=back
289
3cbe1980
DSH
290=head1 RETURN VALUES
291
bbda8ce9
RT
292=over 4
293
294=item EVP_DigestInit_ex(),
295EVP_DigestUpdate(),
296EVP_DigestFinal_ex()
297
298Returns 1 for
13588350 299success and 0 for failure.
3cbe1980 300
bbda8ce9
RT
301=item EVP_MD_CTX_ctrl()
302
303Returns 1 if successful or 0 for failure.
304
305=item EVP_MD_CTX_copy_ex()
52ad5b60 306
bbda8ce9 307Returns 1 if successful or 0 for failure.
3cbe1980 308
bbda8ce9 309=item EVP_MD_type(),
37842dfa 310EVP_MD_pkey_type()
3cbe1980 311
bbda8ce9
RT
312Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
313exists.
3cbe1980 314
bbda8ce9
RT
315=item EVP_MD_size(),
316EVP_MD_block_size(),
317EVP_MD_CTX_size(),
318EVP_MD_CTX_block_size()
3cbe1980 319
bbda8ce9
RT
320Returns the digest or block size in bytes.
321
322=item EVP_md_null()
323
324Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
325
326=item EVP_get_digestbyname(),
327EVP_get_digestbynid(),
328EVP_get_digestbyobj()
329
330Returns either an B<EVP_MD> structure or NULL if an error occurs.
331
675f4cee
PY
332=item EVP_MD_CTX_set_pkey_ctx()
333
334This function has no return value.
335
bbda8ce9 336=back
3cbe1980
DSH
337
338=head1 NOTES
339
340The B<EVP> interface to message digests should almost always be used in
341preference to the low level interfaces. This is because the code then becomes
342transparent to the digest used and much more flexible.
343
bbda8ce9 344New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
89f66fe2 345digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
346are still in common use.
3cbe1980 347
e72d734d
DSH
348For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
349set to NULL to use the default digest implementation.
350
7b3e11c5 351The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
e72d734d 352obsolete but are retained to maintain compatibility with existing code. New
7b3e11c5 353applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
e72d734d
DSH
354EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
355instead of initializing and cleaning it up on each call and allow non default
356implementations of digests to be specified.
357
fa332bba 358If digest contexts are not cleaned up after use,
7b3e11c5 359memory leaks will occur.
13588350 360
25191fff
RL
361EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
362EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
363macros.
5a34fcd7 364
52ad5b60
TS
365EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
366or control.
5a34fcd7 367
3cbe1980
DSH
368=head1 EXAMPLE
369
370This example digests the data "Test Message\n" and "Hello World\n", using the
371digest name passed on the command line.
372
373 #include <stdio.h>
19ac1bf2 374 #include <string.h>
3cbe1980
DSH
375 #include <openssl/evp.h>
376
19ac1bf2 377 int main(int argc, char *argv[])
3cbe1980 378 {
2947af32
BB
379 EVP_MD_CTX *mdctx;
380 const EVP_MD *md;
381 char mess1[] = "Test Message\n";
382 char mess2[] = "Hello World\n";
383 unsigned char md_value[EVP_MAX_MD_SIZE];
19ac1bf2 384 unsigned int md_len, i;
2947af32
BB
385
386 if (argv[1] == NULL) {
387 printf("Usage: mdtest digestname\n");
388 exit(1);
389 }
390
391 md = EVP_get_digestbyname(argv[1]);
392 if (md == NULL) {
393 printf("Unknown message digest %s\n", argv[1]);
394 exit(1);
395 }
396
397 mdctx = EVP_MD_CTX_new();
398 EVP_DigestInit_ex(mdctx, md, NULL);
399 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
400 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
401 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
402 EVP_MD_CTX_free(mdctx);
403
404 printf("Digest is: ");
405 for (i = 0; i < md_len; i++)
406 printf("%02x", md_value[i]);
407 printf("\n");
408
409 exit(0);
3cbe1980
DSH
410 }
411
3cbe1980
DSH
412=head1 SEE ALSO
413
37842dfa 414L<EVP_MD_meth_new(3)>,
9b86974e 415L<dgst(1)>,
b97fdb57 416L<evp(7)>
4facdbb5 417
bbda8ce9
RT
418The full list of digest algorithms are provided below.
419
420L<EVP_blake2b512(3)>,
421L<EVP_md2(3)>,
422L<EVP_md4(3)>,
423L<EVP_md5(3)>,
424L<EVP_mdc2(3)>,
425L<EVP_ripemd160(3)>,
426L<EVP_sha1(3)>,
427L<EVP_sha224(3)>,
428L<EVP_sha3_224(3)>,
67e247fa 429L<EVP_sm3(3)>,
bbda8ce9
RT
430L<EVP_whirlpool(3)>
431
3cbe1980
DSH
432=head1 HISTORY
433
fc5ecadd
DMSP
434The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
435EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
25191fff 436
3fa39ed7 437The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
a95d7574
RS
438later, so now EVP_sha1() can be used with RSA and DSA.
439
fc5ecadd 440The EVP_dss1() function was removed in OpenSSL 1.1.0.
3fa39ed7 441
fc5ecadd 442The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
675f4cee 443
e2f92610
RS
444=head1 COPYRIGHT
445
0d664759 446Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
e2f92610 447
4746f25a 448Licensed under the Apache License 2.0 (the "License"). You may not use
e2f92610
RS
449this file except in compliance with the License. You can obtain a copy
450in the file LICENSE in the source distribution or at
451L<https://www.openssl.org/source/license.html>.
452
453=cut