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