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