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