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