226bc467c4ba1050f6cbc809c9380fe506bea82b
[openssl.git] / doc / man3 / EVP_DigestInit.pod
1 =pod
2
3 =head1 NAME
4
5 EVP_MD_fetch,
6 EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy,
7 EVP_MD_CTX_copy_ex, EVP_MD_CTX_ctrl, EVP_MD_CTX_set_params, EVP_MD_CTX_get_params,
8 EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags, EVP_MD_CTX_test_flags,
9 EVP_Digest, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
10 EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
11 EVP_MD_name, EVP_MD_provider,
12 EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_flags,
13 EVP_MD_CTX_name,
14 EVP_MD_CTX_md, EVP_MD_CTX_type, EVP_MD_CTX_size, EVP_MD_CTX_block_size,
15 EVP_MD_CTX_md_data, EVP_MD_CTX_update_fn, EVP_MD_CTX_set_update_fn,
16 EVP_md_null,
17 EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj,
18 EVP_MD_CTX_pkey_ctx, EVP_MD_CTX_set_pkey_ctx,
19 EVP_MD_do_all_ex
20 - EVP digest routines
21
22 =head1 SYNOPSIS
23
24  #include <openssl/evp.h>
25
26  EVP_MD *EVP_MD_fetch(OPENSSL_CTX *ctx, const char *algorithm,
27                       const char *properties);
28  EVP_MD_CTX *EVP_MD_CTX_new(void);
29  int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
30  void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
31  void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
32  int EVP_MD_CTX_get_params(EVP_MD_CTX *ctx, OSSL_PARAM params[]);
33  int EVP_MD_CTX_set_params(EVP_MD_CTX *ctx, const OSSL_PARAM params[]);
34  void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
35  void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
36  int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
37
38  int EVP_Digest(const void *data, size_t count, unsigned char *md,
39                 unsigned int *size, const EVP_MD *type, ENGINE *impl);
40  int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
41  int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
42  int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
43  int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
44
45  int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
46
47  int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
48  int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
49
50  int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
51
52  const char *EVP_MD_name(const EVP_MD *md);
53  const OSSL_PROVIDER *EVP_MD_provider(const EVP_MD *md);
54  int EVP_MD_type(const EVP_MD *md);
55  int EVP_MD_pkey_type(const EVP_MD *md);
56  int EVP_MD_size(const EVP_MD *md);
57  int EVP_MD_block_size(const EVP_MD *md);
58  unsigned long EVP_MD_flags(const EVP_MD *md);
59
60  const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
61  const char *EVP_MD_CTX_name(const EVP_MD_CTX *ctx);
62  int EVP_MD_CTX_size(const EVP_MD *ctx);
63  int EVP_MD_CTX_block_size(const EVP_MD *ctx);
64  int EVP_MD_CTX_type(const EVP_MD *ctx);
65  void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
66  int (*EVP_MD_CTX_update_fn(EVP_MD_CTX *ctx))(EVP_MD_CTX *ctx,
67                                               const void *data, size_t count);
68  void EVP_MD_CTX_set_update_fn(EVP_MD_CTX *ctx,
69                                int (*update)(EVP_MD_CTX *ctx,
70                                              const void *data, size_t count));
71
72  const EVP_MD *EVP_md_null(void);
73
74  const EVP_MD *EVP_get_digestbyname(const char *name);
75  const EVP_MD *EVP_get_digestbynid(int type);
76  const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
77
78  EVP_PKEY_CTX *EVP_MD_CTX_pkey_ctx(const EVP_MD_CTX *ctx);
79  void EVP_MD_CTX_set_pkey_ctx(EVP_MD_CTX *ctx, EVP_PKEY_CTX *pctx);
80
81  void EVP_MD_do_all_ex(OPENSSL_CTX *libctx,
82                        void (*fn)(EVP_MD *mac, void *arg),
83                        void *arg);
84
85 =head1 DESCRIPTION
86
87 The EVP digest routines are a high level interface to message digests,
88 and should be used instead of the cipher-specific functions.
89
90 =over 4
91
92 =item EVP_MD_fetch()
93
94 Fetches the digest implementation for the given B<algorithm> from any
95 provider offering it, within the criteria given by the B<properties>.
96 See L<provider(7)/Fetching algorithms> for further information.
97
98 The returned value must eventually be freed with L<EVP_MD_meth_free(3)>.
99
100 =item EVP_MD_CTX_new()
101
102 Allocates and returns a digest context.
103
104 =item EVP_MD_CTX_reset()
105
106 Resets the digest context B<ctx>.  This can be used to reuse an already
107 existing context.
108
109 =item EVP_MD_CTX_free()
110
111 Cleans up digest context B<ctx> and frees up the space allocated to it.
112
113 =item EVP_MD_CTX_ctrl()
114
115 This is a legacy method. EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params()
116 is the mechanism that should be used to set and get parameters that are used by
117 providers.
118 Performs digest-specific control actions on context B<ctx>. The control command
119 is indicated in B<cmd> and any additional arguments in B<p1> and B<p2>.
120 EVP_MD_CTX_ctrl() must be called after EVP_DigestInit_ex(). Other restrictions
121 may apply depending on the control type and digest implementation.
122 See L</CONTROLS> below for more information.
123
124 =item EVP_MD_CTX_get_params
125
126 Retrieves the requested list of B<params> from a MD context B<ctx>.
127 See L</PARAMS> below for more information.
128
129 =item EVP_MD_CTX_set_params
130
131 Sets the list of <params> into a MD context B<ctx>.
132 See L</PARAMS> below for more information.
133
134 =item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
135
136 Sets, clears and tests B<ctx> flags.  See L</FLAGS> below for more information.
137
138 =item EVP_Digest()
139
140 A wrapper around the Digest Init_ex, Update and Final_ex functions.
141 Hashes B<count> bytes of data at B<data> using a digest B<type> from ENGINE
142 B<impl>. The digest value is placed in B<md> and its length is written at B<size>
143 if the pointer is not NULL. At most B<EVP_MAX_MD_SIZE> bytes will be written.
144 If B<impl> is NULL the default implementation of digest B<type> is used.
145
146 =item EVP_DigestInit_ex()
147
148 Sets up digest context B<ctx> to use a digest B<type>.
149 B<type> is typically supplied by a function such as EVP_sha1(), or a
150 value explicitly fetched with EVP_MD_fetch().
151
152 If B<impl> is non-NULL, its implementation of the digest B<type> is used if
153 there is one, and if not, the default implementation is used.
154
155 =item EVP_DigestUpdate()
156
157 Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
158 function can be called several times on the same B<ctx> to hash additional
159 data.
160
161 =item EVP_DigestFinal_ex()
162
163 Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
164 parameter is not NULL then the number of bytes of data written (i.e. the
165 length of the digest) will be written to the integer at B<s>, at most
166 B<EVP_MAX_MD_SIZE> bytes will be written.  After calling EVP_DigestFinal_ex()
167 no additional calls to EVP_DigestUpdate() can be made, but
168 EVP_DigestInit_ex() can be called to initialize a new digest operation.
169
170 =item EVP_DigestFinalXOF()
171
172 Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
173 It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
174 After calling this function no additional calls to EVP_DigestUpdate() can be
175 made, but EVP_DigestInit_ex() can be called to initialize a new operation.
176
177 =item EVP_MD_CTX_copy_ex()
178
179 Can be used to copy the message digest state from B<in> to B<out>. This is
180 useful if large amounts of data are to be hashed which only differ in the last
181 few bytes.
182
183 =item EVP_DigestInit()
184
185 Behaves in the same way as EVP_DigestInit_ex() except it always uses the
186 default digest implementation.
187
188 =item EVP_DigestFinal()
189
190 Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
191 automatically cleaned up.
192
193 =item EVP_MD_CTX_copy()
194
195 Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
196 be initialized.
197
198 =item EVP_MD_name(),
199 EVP_MD_CTX_name()
200
201 Return the name of the given message digest.
202
203 =item EVP_MD_provider()
204
205 Returns an B<OSSL_PROVIDER> pointer to the provider that implements the given
206 B<EVP_MD>.
207
208 =item EVP_MD_size(),
209 EVP_MD_CTX_size()
210
211 Return the size of the message digest when passed an B<EVP_MD> or an
212 B<EVP_MD_CTX> structure, i.e. the size of the hash.
213
214 =item EVP_MD_block_size(),
215 EVP_MD_CTX_block_size()
216
217 Return the block size of the message digest when passed an B<EVP_MD> or an
218 B<EVP_MD_CTX> structure.
219
220 =item EVP_MD_type(),
221 EVP_MD_CTX_type()
222
223 Return the NID of the OBJECT IDENTIFIER representing the given message digest
224 when passed an B<EVP_MD> structure.  For example, C<EVP_MD_type(EVP_sha1())>
225 returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
226
227 =item EVP_MD_CTX_md_data()
228
229 Return the digest method private data for the passed B<EVP_MD_CTX>.
230 The space is allocated by OpenSSL and has the size originally set with
231 EVP_MD_meth_set_app_datasize().
232
233 =item EVP_MD_CTX_md()
234
235 Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>. This
236 will be the same B<EVP_MD> object originally passed to EVP_DigestInit_ex() (or
237 other similar function) when the EVP_MD_CTX was first initialised. Note that
238 where explicit fetch is in use (see L<EVP_MD_fetch(3)>) the value returned from
239 this function will not have its reference count incremented and therefore it
240 should not be used after the EVP_MD_CTX is freed.
241
242 =item EVP_MD_CTX_set_update_fn()
243
244 Sets the update function for B<ctx> to B<update>.
245 This is the function that is called by EVP_DigestUpdate. If not set, the
246 update function from the B<EVP_MD> type specified at initialization is used.
247
248 =item EVP_MD_CTX_update_fn()
249
250 Returns the update function for B<ctx>.
251
252 =item EVP_MD_flags()
253
254 Returns the B<md> flags. Note that these are different from the B<EVP_MD_CTX>
255 ones. See L<EVP_MD_meth_set_flags(3)> for more information.
256
257 =item EVP_MD_pkey_type()
258
259 Returns the NID of the public key signing algorithm associated with this
260 digest. For example EVP_sha1() is associated with RSA so this will return
261 B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
262 longer linked this function is only retained for compatibility reasons.
263
264 =item EVP_md_null()
265
266 A "null" message digest that does nothing: i.e. the hash it returns is of zero
267 length.
268
269 =item EVP_get_digestbyname(),
270 EVP_get_digestbynid(),
271 EVP_get_digestbyobj()
272
273 Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
274 B<ASN1_OBJECT> structure respectively.
275
276 =item EVP_MD_CTX_pkey_ctx()
277
278 Returns the B<EVP_PKEY_CTX> assigned to B<ctx>. The returned pointer should not
279 be freed by the caller.
280
281 =item EVP_MD_CTX_set_pkey_ctx()
282
283 Assigns an B<EVP_PKEY_CTX> to B<EVP_MD_CTX>. This is usually used to provide
284 a customized B<EVP_PKEY_CTX> to L<EVP_DigestSignInit(3)> or
285 L<EVP_DigestVerifyInit(3)>. The B<pctx> passed to this function should be freed
286 by the caller. A NULL B<pctx> pointer is also allowed to clear the B<EVP_PKEY_CTX>
287 assigned to B<ctx>. In such case, freeing the cleared B<EVP_PKEY_CTX> or not
288 depends on how the B<EVP_PKEY_CTX> is created.
289
290 =item EVP_MD_do_all_ex()
291
292 Traverses all messages digests implemented by all activated providers
293 in the given library context I<libctx>, and for each of the implementations,
294 calls the given function I<fn> with the implementation method and the given
295 I<arg> as argument.
296
297 =back
298
299 =head1 PARAMS
300
301 See L<OSSL_PARAM(3)> for information about passing parameters.
302
303 EVP_MD_CTX_set_params() can be used with the following OSSL_PARAM keys:
304
305 =over 4
306
307 =item OSSL_PARAM_DIGEST_KEY_XOFLEN <size_t>
308
309 Sets the digest length for extendable output functions.
310 It is used by the SHAKE algorithm.
311
312 =item OSSL_PARAM_DIGEST_KEY_PAD_TYPE <int>
313
314 Sets the pad type.
315 It is used by the MDC2 algorithm.
316
317 =back
318
319 EVP_MD_CTX_get_params() can be used with the following OSSL_PARAM keys:
320
321 =over 4
322
323 =item OSSL_PARAM_DIGEST_KEY_MICALG <utf8string>.
324
325 Gets the digest Message Integrity Check algorithm string. This is used when
326 creating S/MIME multipart/signed messages, as specified in RFC 3851.
327 It may be used by external engines or providers.
328
329 =back
330
331 =head1 CONTROLS
332
333 EVP_MD_CTX_ctrl() can be used to send the following standard controls:
334
335 =over 4
336
337 =item EVP_MD_CTRL_MICALG
338
339 Gets the digest Message Integrity Check algorithm string. This is used when
340 creating S/MIME multipart/signed messages, as specified in RFC 3851.
341 The string value is written to B<p2>.
342
343 =item EVP_MD_CTRL_XOF_LEN
344
345 This control sets the digest length for extendable output functions to B<p1>.
346 Sending this control directly should not be necessary, the use of
347 C<EVP_DigestFinalXOF()> is preferred.
348 Currently used by SHAKE.
349
350 =back
351
352 =head1 FLAGS
353
354 EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
355 can be used the manipulate and test these B<EVP_MD_CTX> flags:
356
357 =over 4
358
359 =item EVP_MD_CTX_FLAG_ONESHOT
360
361 This flag instructs the digest to optimize for one update only, if possible.
362
363 =for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
364
365 =for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
366
367 =for comment We currently avoid documenting flags that are only bit holder:
368 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
369
370 =item EVP_MD_CTX_FLAG_NO_INIT
371
372 This flag instructs EVP_DigestInit() and similar not to initialise the
373 implementation specific data.
374
375 =item EVP_MD_CTX_FLAG_FINALISE
376
377 Some functions such as EVP_DigestSign only finalise copies of internal
378 contexts so additional data can be included after the finalisation call.
379 This is inefficient if this functionality is not required, and can be
380 disabled with this flag.
381
382 =back
383
384 =head1 RETURN VALUES
385
386 =over 4
387
388 =item EVP_MD_fetch()
389
390 Returns a pointer to a B<EVP_MD> for success or NULL for failure.
391
392 =item EVP_DigestInit_ex(),
393 EVP_DigestUpdate(),
394 EVP_DigestFinal_ex()
395
396 Returns 1 for
397 success and 0 for failure.
398
399 =item EVP_MD_CTX_ctrl()
400
401 Returns 1 if successful or 0 for failure.
402
403 =item EVP_MD_CTX_set_params(),
404 EVP_MD_CTX_get_params()
405
406 Returns 1 if successful or 0 for failure.
407
408 =item EVP_MD_CTX_copy_ex()
409
410 Returns 1 if successful or 0 for failure.
411
412 =item EVP_MD_type(),
413 EVP_MD_pkey_type()
414
415 Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
416 exists.
417
418 =item EVP_MD_size(),
419 EVP_MD_block_size(),
420 EVP_MD_CTX_size(),
421 EVP_MD_CTX_block_size()
422
423 Returns the digest or block size in bytes.
424
425 =item EVP_md_null()
426
427 Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
428
429 =item EVP_get_digestbyname(),
430 EVP_get_digestbynid(),
431 EVP_get_digestbyobj()
432
433 Returns either an B<EVP_MD> structure or NULL if an error occurs.
434
435 =item EVP_MD_CTX_set_pkey_ctx()
436
437 This function has no return value.
438
439 =back
440
441 =head1 NOTES
442
443 The B<EVP> interface to message digests should almost always be used in
444 preference to the low level interfaces. This is because the code then becomes
445 transparent to the digest used and much more flexible.
446
447 New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
448 digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
449 are still in common use.
450
451 For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
452 set to NULL to use the default digest implementation.
453
454 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
455 obsolete but are retained to maintain compatibility with existing code. New
456 applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
457 EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
458 instead of initializing and cleaning it up on each call and allow non default
459 implementations of digests to be specified.
460
461 If digest contexts are not cleaned up after use,
462 memory leaks will occur.
463
464 EVP_MD_CTX_name(), EVP_MD_CTX_size(), EVP_MD_CTX_block_size(),
465 EVP_MD_CTX_type(), EVP_get_digestbynid() and EVP_get_digestbyobj() are defined
466 as macros.
467
468 EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
469 or control.
470
471 =head1 EXAMPLE
472
473 This example digests the data "Test Message\n" and "Hello World\n", using the
474 digest name passed on the command line.
475
476  #include <stdio.h>
477  #include <string.h>
478  #include <openssl/evp.h>
479
480  int main(int argc, char *argv[])
481  {
482      EVP_MD_CTX *mdctx;
483      const EVP_MD *md;
484      char mess1[] = "Test Message\n";
485      char mess2[] = "Hello World\n";
486      unsigned char md_value[EVP_MAX_MD_SIZE];
487      unsigned int md_len, i;
488
489      if (argv[1] == NULL) {
490          printf("Usage: mdtest digestname\n");
491          exit(1);
492      }
493
494      md = EVP_get_digestbyname(argv[1]);
495      if (md == NULL) {
496          printf("Unknown message digest %s\n", argv[1]);
497          exit(1);
498      }
499
500      mdctx = EVP_MD_CTX_new();
501      EVP_DigestInit_ex(mdctx, md, NULL);
502      EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
503      EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
504      EVP_DigestFinal_ex(mdctx, md_value, &md_len);
505      EVP_MD_CTX_free(mdctx);
506
507      printf("Digest is: ");
508      for (i = 0; i < md_len; i++)
509          printf("%02x", md_value[i]);
510      printf("\n");
511
512      exit(0);
513  }
514
515 =head1 SEE ALSO
516
517 L<EVP_MD_meth_new(3)>,
518 L<dgst(1)>,
519 L<evp(7)>,
520 L<OSSL_PROVIDER(3)>,
521 L<OSSL_PARAM(3)>
522
523 The full list of digest algorithms are provided below.
524
525 L<EVP_blake2b512(3)>,
526 L<EVP_md2(3)>,
527 L<EVP_md4(3)>,
528 L<EVP_md5(3)>,
529 L<EVP_mdc2(3)>,
530 L<EVP_ripemd160(3)>,
531 L<EVP_sha1(3)>,
532 L<EVP_sha224(3)>,
533 L<EVP_sha3_224(3)>,
534 L<EVP_sm3(3)>,
535 L<EVP_whirlpool(3)>
536 L<provider(7)/Fetching algorithms>
537
538 =head1 HISTORY
539
540 The EVP_MD_CTX_create() and EVP_MD_CTX_destroy() functions were renamed to
541 EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0, respectively.
542
543 The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
544 later, so now EVP_sha1() can be used with RSA and DSA.
545
546 The EVP_dss1() function was removed in OpenSSL 1.1.0.
547
548 The EVP_MD_CTX_set_pkey_ctx() function was added in 1.1.1.
549
550 The EVP_MD_CTX_set_params() and EVP_MD_CTX_get_params() functions were
551 added in 3.0.
552
553 =head1 COPYRIGHT
554
555 Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
556
557 Licensed under the Apache License 2.0 (the "License").  You may not use
558 this file except in compliance with the License.  You can obtain a copy
559 in the file LICENSE in the source distribution or at
560 L<https://www.openssl.org/source/license.html>.
561
562 =cut