update libressl to v2.7.4
[unleashed.git] / lib / libcrypto / man / EVP_DigestInit.3
blob98a06183d3e5204588cccc4ce1d92654bc684ff8
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
4 .\"
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.
9 .\"
10 .\" Redistribution and use in source and binary forms, with or without
11 .\" modification, are permitted provided that the following conditions
12 .\" are met:
13 .\"
14 .\" 1. Redistributions of source code must retain the above copyright
15 .\"    notice, this list of conditions and the following disclaimer.
16 .\"
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
20 .\"    distribution.
21 .\"
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/)"
26 .\"
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.
31 .\"
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.
35 .\"
36 .\" 6. Redistributions of any form whatsoever must retain the following
37 .\"    acknowledgment:
38 .\"    "This product includes software developed by the OpenSSL Project
39 .\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
40 .\"
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.
53 .\"
54 .Dd $Mdocdate: March 23 2018 $
55 .Dt EVP_DIGESTINIT 3
56 .Os
57 .Sh NAME
58 .Nm EVP_MD_CTX_new ,
59 .Nm EVP_MD_CTX_reset ,
60 .Nm EVP_MD_CTX_free ,
61 .Nm EVP_MD_CTX_init ,
62 .Nm EVP_MD_CTX_create ,
63 .Nm EVP_MD_CTX_cleanup ,
64 .Nm EVP_MD_CTX_destroy ,
65 .Nm EVP_MD_CTX_ctrl ,
66 .Nm EVP_DigestInit_ex ,
67 .Nm EVP_DigestUpdate ,
68 .Nm EVP_DigestFinal_ex ,
69 .Nm EVP_MD_CTX_copy_ex ,
70 .Nm EVP_DigestInit ,
71 .Nm EVP_DigestFinal ,
72 .Nm EVP_MD_CTX_copy ,
73 .Nm EVP_MAX_MD_SIZE ,
74 .Nm EVP_MD_type ,
75 .Nm EVP_MD_pkey_type ,
76 .Nm EVP_MD_size ,
77 .Nm EVP_MD_block_size ,
78 .Nm EVP_MD_CTX_md ,
79 .Nm EVP_MD_CTX_size ,
80 .Nm EVP_MD_CTX_block_size ,
81 .Nm EVP_MD_CTX_type ,
82 .Nm EVP_md_null ,
83 .Nm EVP_md5 ,
84 .Nm EVP_md5_sha1 ,
85 .Nm EVP_sha1 ,
86 .Nm EVP_sha224 ,
87 .Nm EVP_sha256 ,
88 .Nm EVP_sha384 ,
89 .Nm EVP_sha512 ,
90 .Nm EVP_dss ,
91 .Nm EVP_dss1 ,
92 .Nm EVP_ripemd160 ,
93 .Nm EVP_get_digestbyname ,
94 .Nm EVP_get_digestbynid ,
95 .Nm EVP_get_digestbyobj
96 .Nd EVP digest routines
97 .Sh SYNOPSIS
98 .In openssl/evp.h
99 .Ft EVP_MD_CTX *
100 .Fn EVP_MD_CTX_new void
101 .Ft int
102 .Fo EVP_MD_CTX_reset
103 .Fa "EVP_MD_CTX *ctx"
105 .Ft void
106 .Fo EVP_MD_CTX_free
107 .Fa "EVP_MD_CTX *ctx"
109 .Ft void
110 .Fo EVP_MD_CTX_init
111 .Fa "EVP_MD_CTX *ctx"
113 .Ft EVP_MD_CTX *
114 .Fn EVP_MD_CTX_create void
115 .Ft int
116 .Fo EVP_MD_CTX_cleanup
117 .Fa "EVP_MD_CTX *ctx"
119 .Ft void
120 .Fo EVP_MD_CTX_destroy
121 .Fa "EVP_MD_CTX *ctx"
123 .Ft int
124 .Fo EVP_MD_CTX_ctrl
125 .Fa "EVP_MD_CTX *ctx"
126 .Fa "int cmd"
127 .Fa "int p1"
128 .Fa "void* p2"
130 .Ft int
131 .Fo EVP_DigestInit_ex
132 .Fa "EVP_MD_CTX *ctx"
133 .Fa "const EVP_MD *type"
134 .Fa "ENGINE *impl"
136 .Ft int
137 .Fo EVP_DigestUpdate
138 .Fa "EVP_MD_CTX *ctx"
139 .Fa "const void *d"
140 .Fa "size_t cnt"
142 .Ft int
143 .Fo EVP_DigestFinal_ex
144 .Fa "EVP_MD_CTX *ctx"
145 .Fa "unsigned char *md"
146 .Fa "unsigned int *s"
148 .Ft int
149 .Fo EVP_MD_CTX_copy_ex
150 .Fa "EVP_MD_CTX *out"
151 .Fa "const EVP_MD_CTX *in"
153 .Ft int
154 .Fo EVP_DigestInit
155 .Fa "EVP_MD_CTX *ctx"
156 .Fa "const EVP_MD *type"
158 .Ft int
159 .Fo EVP_DigestFinal
160 .Fa "EVP_MD_CTX *ctx"
161 .Fa "unsigned char *md"
162 .Fa "unsigned int *s"
164 .Ft int
165 .Fo EVP_MD_CTX_copy
166 .Fa "EVP_MD_CTX *out"
167 .Fa "EVP_MD_CTX *in"
169 .Fd #define EVP_MAX_MD_SIZE 64  /* SHA512 */
170 .Ft int
171 .Fo EVP_MD_type
172 .Fa "const EVP_MD *md"
174 .Ft int
175 .Fo EVP_MD_pkey_type
176 .Fa "const EVP_MD *md"
178 .Ft int
179 .Fo EVP_MD_size
180 .Fa "const EVP_MD *md"
182 .Ft int
183 .Fo EVP_MD_block_size
184 .Fa "const EVP_MD *md"
186 .Ft const EVP_MD *
187 .Fo EVP_MD_CTX_md
188 .Fa "const EVP_MD_CTX *ctx"
190 .Ft int
191 .Fo EVP_MD_CTX_size
192 .Fa "const EVP_MD *ctx"
194 .Ft int
195 .Fo EVP_MD_CTX_block_size
196 .Fa "const EVP_MD *ctx"
198 .Ft int
199 .Fo EVP_MD_CTX_type
200 .Fa "const EVP_MD *ctx"
202 .Ft const EVP_MD *
203 .Fn EVP_md_null void
204 .Ft const EVP_MD *
205 .Fn EVP_md5 void
206 .Ft const EVP_MD *
207 .Fn EVP_md5_sha1 void
208 .Ft const EVP_MD *
209 .Fn EVP_sha1 void
210 .Ft const EVP_MD *
211 .Fn EVP_sha224 void
212 .Ft const EVP_MD *
213 .Fn EVP_sha256 void
214 .Ft const EVP_MD *
215 .Fn EVP_sha384 void
216 .Ft const EVP_MD *
217 .Fn EVP_sha512 void
218 .Ft const EVP_MD *
219 .Fn EVP_dss void
220 .Ft const EVP_MD *
221 .Fn EVP_dss1 void
222 .Ft const EVP_MD *
223 .Fn EVP_ripemd160 void
224 .Ft const EVP_MD *
225 .Fo EVP_get_digestbyname
226 .Fa "const char *name"
228 .Ft const EVP_MD *
229 .Fo EVP_get_digestbynid
230 .Fa "int type"
232 .Ft const EVP_MD *
233 .Fo EVP_get_digestbyobj
234 .Fa "const ASN1_OBJECT *o"
236 .Sh DESCRIPTION
237 The EVP digest routines are a high level interface to message digests
238 and should be used instead of the cipher-specific functions.
240 .Fn EVP_MD_CTX_new
241 allocates a new, empty digest context.
243 .Fn EVP_MD_CTX_reset
244 cleans up
245 .Fa ctx
246 and resets it to the state it had after
247 .Fn EVP_MD_CTX_new ,
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.
252 .Fn EVP_MD_CTX_free
253 cleans up
254 .Fa ctx
255 and frees the space allocated to it.
257 .Fn EVP_MD_CTX_init
258 is a deprecated function to clear a digest context on the stack
259 before use.
260 Do not use it on a digest context returned from
261 .Fn EVP_MD_CTX_new
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
269 .Fn EVP_MD_CTX_new ,
270 .Fn EVP_MD_CTX_reset ,
272 .Fn EVP_MD_CTX_free ,
273 respectively.
275 .Fn EVP_MD_CTX_ctrl
276 performs digest-specific control actions on the context
277 .Fa ctx .
279 .Fn EVP_DigestInit_ex
280 sets up the digest context
281 .Fa ctx
282 to use a digest
283 .Fa type
284 from
285 .Vt ENGINE
286 .Fa impl .
288 .Fa type
289 will typically be supplied by a function such as
290 .Fn EVP_sha1 .
292 .Fa impl
294 .Dv NULL ,
295 then the default implementation of digest
296 .Fa type
297 is used.
299 .Fa ctx
300 points to an unused object on the stack, it must be initialized with
301 .Fn EVP_MD_CTX_init
302 before calling this function.
304 .Fn EVP_DigestUpdate
305 hashes
306 .Fa cnt
307 bytes of data at
308 .Fa d
309 into the digest context
310 .Fa ctx .
311 This function can be called several times on the same
312 .Fa ctx
313 to hash additional data.
315 .Fn EVP_DigestFinal_ex
316 retrieves the digest value from
317 .Fa ctx
318 and places it in
319 .Fa md .
320 If the
321 .Fa s
322 parameter is not
323 .Dv NULL ,
324 then the number of bytes of data written (i.e. the length of the
325 digest) will be written to the integer at
326 .Fa s ;
327 at most
328 .Dv EVP_MAX_MD_SIZE
329 bytes will be written.
330 After calling
331 .Fn EVP_DigestFinal_ex ,
332 no additional calls to
333 .Fn EVP_DigestUpdate
334 can be made, but
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
340 .Fa in
342 .Fa out .
343 This is useful if large amounts of data are to be hashed which only
344 differ in the last few bytes.
346 .Fa out
347 points to an unused object on the stack, it must be initialized with
348 .Fn EVP_MD_CTX_init
349 before calling this function.
351 .Fn EVP_DigestInit
352 is a deprecated function behaving like
353 .Fn EVP_DigestInit_ex
354 except that it always uses the default digest implementation
355 and that it requires
356 .Fn EVP_MD_CTX_reset
357 before it can be used on a context that was already used.
359 .Fn EVP_DigestFinal
360 is a deprecated function behaving like
361 .Fn EVP_DigestFinal_ex
362 except that the digest context
363 .Fa ctx
364 is automatically cleaned up after use by calling
365 .Fn EVP_MD_CTX_reset
366 internally.
368 .Fn EVP_MD_CTX_copy
369 is a deprecated function behaving like
370 .Fn EVP_MD_CTX_copy_ex
371 except that it requires
372 .Fn EVP_MD_CTX_reset
373 before a context that was already used can be passed as
374 .Fa out .
376 .Fn EVP_MD_size
378 .Fn EVP_MD_CTX_size
379 return the size of the message digest when passed an
380 .Vt EVP_MD
381 or an
382 .Vt EVP_MD_CTX
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
389 .Vt EVP_MD
390 or an
391 .Vt EVP_MD_CTX
392 structure.
394 .Fn EVP_MD_type
396 .Fn EVP_MD_CTX_type
397 return the NID of the OBJECT IDENTIFIER representing the given message
398 digest when passed an
399 .Vt EVP_MD
400 structure.
401 For example
402 .Fn EVP_MD_type EVP_sha1()
403 returns
404 .Dv NID_sha1 .
405 This function is normally used when setting ASN.1 OIDs.
407 .Fn EVP_MD_pkey_type
408 returns the NID of the public key signing algorithm associated with this
409 digest.
410 For example
411 .Fn EVP_sha1
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.
417 .Fn EVP_md5 ,
418 .Fn EVP_sha1 ,
419 .Fn EVP_sha224 ,
420 .Fn EVP_sha256 ,
421 .Fn EVP_sha384 ,
422 .Fn EVP_sha512 ,
424 .Fn EVP_ripemd160
425 return
426 .Vt EVP_MD
427 structures for the MD5, SHA1, SHA224, SHA256, SHA384, SHA512 and
428 RIPEMD160 digest algorithms respectively.
430 .Fn EVP_md5_sha1
431 returns an
432 .Vt EVP_MD
433 structure that provides concatenated MD5 and SHA1 message digests.
435 .Fn EVP_dss
437 .Fn EVP_dss1
438 return
439 .Vt EVP_MD
440 structures for SHA1 digest algorithms but using DSS (DSA) for the
441 signature algorithm.
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.
445 .Fn EVP_md_null
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
453 return an
454 .Vt EVP_MD
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
472 much more flexible.
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
478 .Fa impl
479 parameter to
480 .Fn EVP_DigestInit_ex
481 will be set to NULL to use the default digest implementation.
483 The functions
484 .Fn EVP_DigestInit ,
485 .Fn EVP_DigestFinal ,
487 .Fn EVP_MD_CTX_copy
488 are obsolete but are retained to maintain compatibility with existing
489 code.
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.
500 .Sh RETURN VALUES
501 .Fn EVP_MD_CTX_new
503 .Fn EVP_MD_CTX_create
504 return the new
505 .Vt EVP_MD_CTX
506 object or
507 .Dv NULL
508 for failure.
510 .Fn EVP_MD_CTX_reset
512 .Fn EVP_MD_CTX_cleanup
513 always return 1.
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 ,
520 .Fn EVP_DigestInit ,
521 .Fn EVP_DigestFinal ,
523 .Fn EVP_MD_CTX_copy
524 return 1 for success or 0 for failure.
526 .Fn EVP_MD_type ,
527 .Fn EVP_MD_pkey_type ,
529 .Fn EVP_MD_CTX_type
530 return the NID of the corresponding OBJECT IDENTIFIER or
531 .Dv NID_undef
532 if none exists.
534 .Fn EVP_MD_size ,
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.
541 .Fn EVP_MD_CTX_md
542 returns the
543 .Vt EVP_MD
544 object used by
545 .Fa ctx ,
547 .Dv NULL
549 .Fa ctx
551 .Dv NULL .
553 .Fn EVP_md_null ,
554 .Fn EVP_md5 ,
555 .Fn EVP_md5_sha1 ,
556 .Fn EVP_sha1 ,
557 .Fn EVP_dss ,
558 .Fn EVP_dss1 ,
560 .Fn EVP_ripemd160
561 return pointers to the corresponding
562 .Vt EVP_MD
563 structures.
565 .Fn EVP_get_digestbyname ,
566 .Fn EVP_get_digestbynid ,
568 .Fn EVP_get_digestbyobj
569 return either an
570 .Vt EVP_MD
571 structure or
572 .Dv NULL
573 if an error occurs.
574 .Sh EXAMPLES
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
578 #include <stdio.h>
579 #include <openssl/evp.h>
582 main(int argc, char *argv[])
584         EVP_MD_CTX *mdctx;
585         const EVP_MD *md;
586         const char mess1[] = "Test Message\en";
587         const char mess2[] = "Hello World\en";
588         unsigned char md_value[EVP_MAX_MD_SIZE];
589         int md_len, i;
591         OpenSSL_add_all_digests();
593         if (argc <= 1) {
594                 printf("Usage: mdtest digestname\en");
595                 exit(1);
596         }
598         md = EVP_get_digestbyname(argv[1]);
599         if (md == NULL) {
600                 printf("Unknown message digest %s\en", argv[1]);
601                 exit(1);
602         }
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]);
614         printf("\en");
616         return 0;
619 .Sh SEE ALSO
620 .Xr evp 3
621 .Sh HISTORY
622 .Fn EVP_DigestInit ,
623 .Fn EVP_DigestUpdate ,
624 .Fn EVP_DigestFinal ,
625 .Dv EVP_MAX_MD_SIZE ,
626 .Fn EVP_MD_type ,
627 .Fn EVP_MD_pkey_type ,
628 .Fn EVP_MD_size ,
629 .Fn EVP_MD_CTX_size ,
630 .Fn EVP_MD_CTX_type ,
631 .Fn EVP_md_null ,
632 .Fn EVP_md5 ,
633 .Fn EVP_sha1 ,
634 .Fn EVP_dss ,
635 .Fn EVP_dss1 ,
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 ,
644 .Fn EVP_rc4_40 ,
645 .Fn EVP_rc2_40_cbc ,
647 .Fn EVP_ripemd160
648 first appeared in SSLeay 0.9.0.
649 All these functions have been available since
650 .Ox 2.4 .
652 .Fn EVP_MD_CTX_copy
653 first appeared in OpenSSL 0.9.2b and has been available since
654 .Ox 2.6 .
656 .Fn EVP_MD_CTX_md
657 first appeared in OpenSSL 0.9.5 and has been available since
658 .Ox 2.7 .
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
669 .Ox 3.2 .
671 .Fn EVP_sha224 ,
672 .Fn EVP_sha256 ,
673 .Fn EVP_sha384 ,
675 .Fn EVP_sha512
676 first appeared in OpenSSL 0.8.7h and have been available since
677 .Ox 4.0 .
679 .Fn EVP_MD_CTX_ctrl
680 first appeared in OpenSSL 1.1.0 and has been available since
681 .Ox 5.7 .
683 .Fn EVP_MD_CTX_new ,
684 .Fn EVP_MD_CTX_reset ,
685 .Fn EVP_MD_CTX_free ,
687 .Fn EVP_md5_sha1
688 first appeared in OpenSSL 1.1.0 and have been available since
689 .Ox 6.3 .
691 The link between digests and signing algorithms was fixed in OpenSSL 1.0
692 and later, so now
693 .Fn EVP_sha1
694 can be used with RSA and DSA; there is no need to use
695 .Fn EVP_dss1
696 any more.