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