1 .\" $OpenBSD: EVP_DigestInit.3,v 1.14 2018/03/23 23:18:17 schwarze Exp $
2 .\" full merge up to: OpenSSL 7f572e95 Dec 2 13:57:04 2015 +0000
3 .\" selective merge up to: OpenSSL a95d7574 Jul 2 12:16:38 2017 -0400
5 .\" This file was written by Dr. Stephen Henson <steve@openssl.org>
6 .\" and Richard Levitte <levitte@openssl.org>.
7 .\" Copyright (c) 2000-2004, 2009, 2012-2016 The OpenSSL Project.
8 .\" All rights reserved.
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
14 .\" 1. Redistributions of source code must retain the above copyright
15 .\" notice, this list of conditions and the following disclaimer.
17 .\" 2. Redistributions in binary form must reproduce the above copyright
18 .\" notice, this list of conditions and the following disclaimer in
19 .\" the documentation and/or other materials provided with the
22 .\" 3. All advertising materials mentioning features or use of this
23 .\" software must display the following acknowledgment:
24 .\" "This product includes software developed by the OpenSSL Project
25 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
27 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 .\" endorse or promote products derived from this software without
29 .\" prior written permission. For written permission, please contact
30 .\" openssl-core@openssl.org.
32 .\" 5. Products derived from this software may not be called "OpenSSL"
33 .\" nor may "OpenSSL" appear in their names without prior written
34 .\" permission of the OpenSSL Project.
36 .\" 6. Redistributions of any form whatsoever must retain the following
38 .\" "This product includes software developed by the OpenSSL Project
39 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
41 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
54 .Dd $Mdocdate: March 23 2018 $
59 .Nm EVP_MD_CTX_reset ,
62 .Nm EVP_MD_CTX_create ,
63 .Nm EVP_MD_CTX_cleanup ,
64 .Nm EVP_MD_CTX_destroy ,
66 .Nm EVP_DigestInit_ex ,
67 .Nm EVP_DigestUpdate ,
68 .Nm EVP_DigestFinal_ex ,
69 .Nm EVP_MD_CTX_copy_ex ,
75 .Nm EVP_MD_pkey_type ,
77 .Nm EVP_MD_block_size ,
80 .Nm EVP_MD_CTX_block_size ,
93 .Nm EVP_get_digestbyname ,
94 .Nm EVP_get_digestbynid ,
95 .Nm EVP_get_digestbyobj
96 .Nd EVP digest routines
100 .Fn EVP_MD_CTX_new void
103 .Fa "EVP_MD_CTX *ctx"
107 .Fa "EVP_MD_CTX *ctx"
111 .Fa "EVP_MD_CTX *ctx"
114 .Fn EVP_MD_CTX_create void
116 .Fo EVP_MD_CTX_cleanup
117 .Fa "EVP_MD_CTX *ctx"
120 .Fo EVP_MD_CTX_destroy
121 .Fa "EVP_MD_CTX *ctx"
125 .Fa "EVP_MD_CTX *ctx"
131 .Fo EVP_DigestInit_ex
132 .Fa "EVP_MD_CTX *ctx"
133 .Fa "const EVP_MD *type"
138 .Fa "EVP_MD_CTX *ctx"
143 .Fo EVP_DigestFinal_ex
144 .Fa "EVP_MD_CTX *ctx"
145 .Fa "unsigned char *md"
146 .Fa "unsigned int *s"
149 .Fo EVP_MD_CTX_copy_ex
150 .Fa "EVP_MD_CTX *out"
151 .Fa "const EVP_MD_CTX *in"
155 .Fa "EVP_MD_CTX *ctx"
156 .Fa "const EVP_MD *type"
160 .Fa "EVP_MD_CTX *ctx"
161 .Fa "unsigned char *md"
162 .Fa "unsigned int *s"
166 .Fa "EVP_MD_CTX *out"
169 .Fd #define EVP_MAX_MD_SIZE 64 /* SHA512 */
172 .Fa "const EVP_MD *md"
176 .Fa "const EVP_MD *md"
180 .Fa "const EVP_MD *md"
183 .Fo EVP_MD_block_size
184 .Fa "const EVP_MD *md"
188 .Fa "const EVP_MD_CTX *ctx"
192 .Fa "const EVP_MD *ctx"
195 .Fo EVP_MD_CTX_block_size
196 .Fa "const EVP_MD *ctx"
200 .Fa "const EVP_MD *ctx"
207 .Fn EVP_md5_sha1 void
223 .Fn EVP_ripemd160 void
225 .Fo EVP_get_digestbyname
226 .Fa "const char *name"
229 .Fo EVP_get_digestbynid
233 .Fo EVP_get_digestbyobj
234 .Fa "const ASN1_OBJECT *o"
237 The EVP digest routines are a high level interface to message digests
238 and should be used instead of the cipher-specific functions.
241 allocates a new, empty digest context.
246 and resets it to the state it had after
248 such that it can be reused.
249 It is also suitable for digest contexts on the stack that were
250 used and are no longer needed.
255 and frees the space allocated to it.
258 is a deprecated function to clear a digest context on the stack
260 Do not use it on a digest context returned from
262 or one one that was already used.
264 .Fn EVP_MD_CTX_create ,
265 .Fn EVP_MD_CTX_cleanup ,
267 .Fn EVP_MD_CTX_destroy
268 are deprecated aliases for
270 .Fn EVP_MD_CTX_reset ,
272 .Fn EVP_MD_CTX_free ,
276 performs digest-specific control actions on the context
279 .Fn EVP_DigestInit_ex
280 sets up the digest context
289 will typically be supplied by a function such as
295 then the default implementation of digest
300 points to an unused object on the stack, it must be initialized with
302 before calling this function.
309 into the digest context
311 This function can be called several times on the same
313 to hash additional data.
315 .Fn EVP_DigestFinal_ex
316 retrieves the digest value from
324 then the number of bytes of data written (i.e. the length of the
325 digest) will be written to the integer at
329 bytes will be written.
331 .Fn EVP_DigestFinal_ex ,
332 no additional calls to
335 .Fn EVP_DigestInit_ex
336 can be called to initialize a new digest operation.
338 .Fn EVP_MD_CTX_copy_ex
339 can be used to copy the message digest state from
343 This is useful if large amounts of data are to be hashed which only
344 differ in the last few bytes.
347 points to an unused object on the stack, it must be initialized with
349 before calling this function.
352 is a deprecated function behaving like
353 .Fn EVP_DigestInit_ex
354 except that it always uses the default digest implementation
357 before it can be used on a context that was already used.
360 is a deprecated function behaving like
361 .Fn EVP_DigestFinal_ex
362 except that the digest context
364 is automatically cleaned up after use by calling
369 is a deprecated function behaving like
370 .Fn EVP_MD_CTX_copy_ex
371 except that it requires
373 before a context that was already used can be passed as
379 return the size of the message digest when passed an
383 structure, i.e. the size of the hash.
385 .Fn EVP_MD_block_size
387 .Fn EVP_MD_CTX_block_size
388 return the block size of the message digest when passed an
397 return the NID of the OBJECT IDENTIFIER representing the given message
398 digest when passed an
402 .Fn EVP_MD_type EVP_sha1()
405 This function is normally used when setting ASN.1 OIDs.
408 returns the NID of the public key signing algorithm associated with this
412 is associated with RSA so this will return
413 .Dv NID_sha1WithRSAEncryption .
414 Since digests and signature algorithms are no longer linked this
415 function is only retained for compatibility reasons.
427 structures for the MD5, SHA1, SHA224, SHA256, SHA384, SHA512 and
428 RIPEMD160 digest algorithms respectively.
433 structure that provides concatenated MD5 and SHA1 message digests.
440 structures for SHA1 digest algorithms but using DSS (DSA) for the
442 Note: there is no need to use these pseudo-digests in OpenSSL 1.0.0 and
443 later; they are however retained for compatibility.
446 is a "null" message digest that does nothing:
447 i.e. the hash it returns is of zero length.
449 .Fn EVP_get_digestbyname ,
450 .Fn EVP_get_digestbynid ,
452 .Fn EVP_get_digestbyobj
455 structure when passed a digest name, a digest NID, or an ASN1_OBJECT
456 structure respectively.
457 The digest table must be initialized using, for example,
458 .Xr OpenSSL_add_all_digests 3
459 for these functions to work.
461 .Fn EVP_MD_CTX_size ,
462 .Fn EVP_MD_CTX_block_size ,
463 .Fn EVP_MD_CTX_type ,
464 .Fn EVP_get_digestbynid ,
466 .Fn EVP_get_digestbyobj
467 are implemented as macros.
469 The EVP interface to message digests should almost always be used
470 in preference to the low level interfaces.
471 This is because the code then becomes transparent to the digest used and
474 New applications should use the SHA2 digest algorithms such as SHA256.
475 The other digest algorithms are still in common use.
477 For most applications the
480 .Fn EVP_DigestInit_ex
481 will be set to NULL to use the default digest implementation.
485 .Fn EVP_DigestFinal ,
488 are obsolete but are retained to maintain compatibility with existing
490 New applications should use
491 .Fn EVP_DigestInit_ex ,
492 .Fn EVP_DigestFinal_ex ,
494 .Fn EVP_MD_CTX_copy_ex
495 because they can efficiently reuse a digest context instead of
496 initializing and cleaning it up on each call and allow non-default
497 implementations of digests to be specified.
499 If digest contexts are not cleaned up after use, memory leaks will occur.
503 .Fn EVP_MD_CTX_create
512 .Fn EVP_MD_CTX_cleanup
515 .Fn EVP_MD_CTX_ctrl ,
516 .Fn EVP_DigestInit_ex ,
517 .Fn EVP_DigestUpdate ,
518 .Fn EVP_DigestFinal_ex ,
519 .Fn EVP_MD_CTX_copy_ex ,
521 .Fn EVP_DigestFinal ,
524 return 1 for success or 0 for failure.
527 .Fn EVP_MD_pkey_type ,
530 return the NID of the corresponding OBJECT IDENTIFIER or
535 .Fn EVP_MD_block_size ,
536 .Fn EVP_MD_CTX_size ,
538 .Fn EVP_MD_CTX_block_size
539 return the digest or block size in bytes.
561 return pointers to the corresponding
565 .Fn EVP_get_digestbyname ,
566 .Fn EVP_get_digestbynid ,
568 .Fn EVP_get_digestbyobj
575 This example digests the data "Test Message\en" and "Hello World\en",
576 using the digest name passed on the command line.
577 .Bd -literal -offset indent
579 #include <openssl/evp.h>
582 main(int argc, char *argv[])
586 const char mess1[] = "Test Message\en";
587 const char mess2[] = "Hello World\en";
588 unsigned char md_value[EVP_MAX_MD_SIZE];
591 OpenSSL_add_all_digests();
594 printf("Usage: mdtest digestname\en");
598 md = EVP_get_digestbyname(argv[1]);
600 printf("Unknown message digest %s\en", argv[1]);
604 mdctx = EVP_MD_CTX_new();
605 EVP_DigestInit_ex(mdctx, md, NULL);
606 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
607 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
608 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
609 EVP_MD_CTX_free(mdctx);
611 printf("Digest is: ");
612 for(i = 0; i < md_len; i++)
613 printf("%02x", md_value[i]);
623 .Fn EVP_DigestUpdate ,
624 .Fn EVP_DigestFinal ,
625 .Dv EVP_MAX_MD_SIZE ,
627 .Fn EVP_MD_pkey_type ,
629 .Fn EVP_MD_CTX_size ,
630 .Fn EVP_MD_CTX_type ,
636 .Fn EVP_get_digestbyname ,
637 .Fn EVP_get_digestbynid ,
639 .Fn EVP_get_digestbyobj
640 appeared in SSLeay 0.8.1b or earlier.
641 .Fn EVP_MD_block_size ,
642 .Fn EVP_MD_CTX_size ,
643 .Fn EVP_MD_CTX_block_size ,
648 first appeared in SSLeay 0.9.0.
649 All these functions have been available since
653 first appeared in OpenSSL 0.9.2b and has been available since
657 first appeared in OpenSSL 0.9.5 and has been available since
660 .Fn EVP_MD_CTX_init ,
661 .Fn EVP_MD_CTX_create ,
662 .Fn EVP_MD_CTX_cleanup ,
663 .Fn EVP_MD_CTX_destroy ,
664 .Fn EVP_DigestInit_ex ,
665 .Fn EVP_DigestFinal_ex ,
667 .Fn EVP_MD_CTX_copy_ex
668 first appeared in OpenSSL 0.9.7 and have been available since
676 first appeared in OpenSSL 0.8.7h and have been available since
680 first appeared in OpenSSL 1.1.0 and has been available since
684 .Fn EVP_MD_CTX_reset ,
685 .Fn EVP_MD_CTX_free ,
688 first appeared in OpenSSL 1.1.0 and have been available since
691 The link between digests and signing algorithms was fixed in OpenSSL 1.0
694 can be used with RSA and DSA; there is no need to use