]> git.ipfire.org Git - thirdparty/openssl.git/blame - doc/man3/EVP_DigestInit.pod
Extend dladdr() for AIX, consequence from changes for openssl#6368.
[thirdparty/openssl.git] / doc / man3 / EVP_DigestInit.pod
CommitLineData
3cbe1980
DSH
1=pod
2
3=head1 NAME
4
25191fff 5EVP_MD_CTX_new, EVP_MD_CTX_reset, EVP_MD_CTX_free, EVP_MD_CTX_copy_ex,
a9cf71a3
RL
6EVP_MD_CTX_ctrl, EVP_MD_CTX_set_flags, EVP_MD_CTX_clear_flags,
7EVP_MD_CTX_test_flags, EVP_DigestInit_ex, EVP_DigestInit, EVP_DigestUpdate,
cd8d1456
AP
8EVP_DigestFinal_ex, EVP_DigestFinalXOF, EVP_DigestFinal,
9EVP_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size,
10EVP_MD_block_size, EVP_MD_CTX_md, EVP_MD_CTX_size,
a9cf71a3
RL
11EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_MD_CTX_md_data,
12EVP_md_null,
bbda8ce9 13EVP_get_digestbyname, EVP_get_digestbynid,
e4adad92 14EVP_get_digestbyobj - EVP digest routines
3cbe1980
DSH
15
16=head1 SYNOPSIS
17
18 #include <openssl/evp.h>
19
25191fff
RL
20 EVP_MD_CTX *EVP_MD_CTX_new(void);
21 int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
22 void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
52ad5b60 23 void EVP_MD_CTX_ctrl(EVP_MD_CTX *ctx, int cmd, int p1, void* p2);
a9cf71a3
RL
24 void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
25 void EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags);
26 int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags);
e72d734d
DSH
27
28 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
109d3123 29 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
e9b77246 30 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
cd8d1456 31 int EVP_DigestFinalXOF(EVP_MD_CTX *ctx, unsigned char *md, size_t len);
e72d734d 32
aebb9aac 33 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in);
e72d734d
DSH
34
35 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
e9b77246 36 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md, unsigned int *s);
3cbe1980 37
aebb9aac 38 int EVP_MD_CTX_copy(EVP_MD_CTX *out, EVP_MD_CTX *in);
e72d734d 39
5a34fcd7 40 int EVP_MD_type(const EVP_MD *md);
05ea606a 41 int EVP_MD_pkey_type(const EVP_MD *md);
5a34fcd7
DSH
42 int EVP_MD_size(const EVP_MD *md);
43 int EVP_MD_block_size(const EVP_MD *md);
3cbe1980 44
5a34fcd7 45 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
25191fff
RL
46 int EVP_MD_CTX_size(const EVP_MD *ctx);
47 int EVP_MD_CTX_block_size(const EVP_MD *ctx);
48 int EVP_MD_CTX_type(const EVP_MD *ctx);
a9cf71a3 49 void *EVP_MD_CTX_md_data(const EVP_MD_CTX *ctx);
3cbe1980 50
13588350 51 const EVP_MD *EVP_md_null(void);
e4adad92 52
3cbe1980 53 const EVP_MD *EVP_get_digestbyname(const char *name);
25191fff
RL
54 const EVP_MD *EVP_get_digestbynid(int type);
55 const EVP_MD *EVP_get_digestbyobj(const ASN1_OBJECT *o);
3cbe1980
DSH
56
57=head1 DESCRIPTION
58
c7497f34
RS
59The EVP digest routines are a high level interface to message digests,
60and should be used instead of the cipher-specific functions.
3cbe1980 61
bbda8ce9 62=over 4
25191fff 63
bbda8ce9 64=item EVP_MD_CTX_new()
e72d734d 65
b45497c3 66Allocates and returns a digest context.
e72d734d 67
bbda8ce9 68=item EVP_MD_CTX_reset()
52ad5b60 69
bbda8ce9
RT
70Resets the digest context B<ctx>. This can be used to reuse an already
71existing context.
3cbe1980 72
bbda8ce9 73=item EVP_MD_CTX_free()
3cbe1980 74
bbda8ce9 75Cleans up digest context B<ctx> and frees up the space allocated to it.
3cbe1980 76
bbda8ce9
RT
77=item EVP_MD_CTX_ctrl()
78
79Performs digest-specific control actions on context B<ctx>.
80
a9cf71a3
RL
81=item EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags(), EVP_MD_CTX_test_flags()
82
83Sets, clears and tests B<ctx> flags. See L</FLAGS> below for more information.
84
bbda8ce9
RT
85=item EVP_DigestInit_ex()
86
87Sets up digest context B<ctx> to use a digest B<type> from ENGINE B<impl>.
b45497c3
P
88B<type> will typically be supplied by a function such as EVP_sha1(). If
89B<impl> is NULL then the default implementation of digest B<type> is used.
bbda8ce9
RT
90
91=item EVP_DigestUpdate()
92
93Hashes B<cnt> bytes of data at B<d> into the digest context B<ctx>. This
94function can be called several times on the same B<ctx> to hash additional
95data.
96
97=item EVP_DigestFinal_ex()
98
99Retrieves the digest value from B<ctx> and places it in B<md>. If the B<s>
100parameter is not NULL then the number of bytes of data written (i.e. the
101length of the digest) will be written to the integer at B<s>, at most
102B<EVP_MAX_MD_SIZE> bytes will be written. After calling EVP_DigestFinal_ex()
cd8d1456 103no additional calls to EVP_DigestUpdate() can be made, but
bbda8ce9
RT
104EVP_DigestInit_ex() can be called to initialize a new digest operation.
105
106=item EVP_DigestFinalXOF()
107
108Interfaces to extendable-output functions, XOFs, such as SHAKE128 and SHAKE256.
109It retrieves the digest value from B<ctx> and places it in B<len>-sized <B>md.
110After calling this function no additional calls to EVP_DigestUpdate() can be
111made, but EVP_DigestInit_ex() can be called to initialize a new operation.
112
113=item EVP_MD_CTX_copy_ex()
114
115Can be used to copy the message digest state from B<in> to B<out>. This is
116useful if large amounts of data are to be hashed which only differ in the last
b45497c3 117few bytes.
bbda8ce9
RT
118
119=item EVP_DigestInit()
120
b45497c3
P
121Behaves in the same way as EVP_DigestInit_ex() except it always uses the
122default digest implementation.
bbda8ce9
RT
123
124=item EVP_DigestFinal()
125
126Similar to EVP_DigestFinal_ex() except the digest context B<ctx> is
127automatically cleaned up.
128
129=item EVP_MD_CTX_copy()
cd8d1456 130
bbda8ce9
RT
131Similar to EVP_MD_CTX_copy_ex() except the destination B<out> does not have to
132be initialized.
e72d734d 133
bbda8ce9
RT
134=item EVP_MD_size(),
135EVP_MD_CTX_size()
e72d734d 136
bbda8ce9
RT
137Return the size of the message digest when passed an B<EVP_MD> or an
138B<EVP_MD_CTX> structure, i.e. the size of the hash.
e72d734d 139
bbda8ce9
RT
140=item EVP_MD_block_size(),
141EVP_MD_CTX_block_size()
3cbe1980 142
bbda8ce9
RT
143Return the block size of the message digest when passed an B<EVP_MD> or an
144B<EVP_MD_CTX> structure.
3cbe1980 145
bbda8ce9
RT
146=item EVP_MD_type(),
147EVP_MD_CTX_type()
3cbe1980 148
bbda8ce9
RT
149Return the NID of the OBJECT IDENTIFIER representing the given message digest
150when passed an B<EVP_MD> structure. For example, C<EVP_MD_type(EVP_sha1())>
151returns B<NID_sha1>. This function is normally used when setting ASN1 OIDs.
3cbe1980 152
a9cf71a3
RL
153=item EVP_MD_CTX_md_data()
154
155Return the digest method private data for the passed B<EVP_MD_CTX>.
156The space is allocated by OpenSSL and has the size originally set with
157EVP_MD_meth_set_app_datasize().
158
bbda8ce9 159=item EVP_MD_CTX_md()
3cbe1980 160
bbda8ce9 161Returns the B<EVP_MD> structure corresponding to the passed B<EVP_MD_CTX>.
3cbe1980 162
bbda8ce9 163=item EVP_MD_pkey_type()
3cbe1980 164
bbda8ce9
RT
165Returns the NID of the public key signing algorithm associated with this
166digest. For example EVP_sha1() is associated with RSA so this will return
167B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms are no
168longer linked this function is only retained for compatibility reasons.
3cbe1980 169
bbda8ce9
RT
170=item EVP_md_null()
171
172A "null" message digest that does nothing: i.e. the hash it returns is of zero
173length.
174
175=item EVP_get_digestbyname(),
176EVP_get_digestbynid(),
177EVP_get_digestbyobj()
178
179Returns an B<EVP_MD> structure when passed a digest name, a digest B<NID> or an
180B<ASN1_OBJECT> structure respectively.
181
182=back
3cbe1980 183
a9cf71a3
RL
184=head1 FLAGS
185
186EVP_MD_CTX_set_flags(), EVP_MD_CTX_clear_flags() and EVP_MD_CTX_test_flags()
187can be used the manipulate and test these B<EVP_MD_CTX> flags:
188
189=over 4
190
191=item EVP_MD_CTX_FLAG_ONESHOT
192
193This flag instructs the digest to optimize for one update only, if possible.
194
195=for comment EVP_MD_CTX_FLAG_CLEANED is internal, don't mention it
196
197=for comment EVP_MD_CTX_FLAG_REUSE is internal, don't mention it
198
199=for comment We currently avoid documenting flags that are only bit holder:
200EVP_MD_CTX_FLAG_NON_FIPS_ALLOW, EVP_MD_CTX_FLAGS_PAD_*
201
202=item EVP_MD_CTX_FLAG_NO_INIT
203
204This flag instructs EVP_DigestInit() and similar not to initialise the
205implementation specific data.
206
207=item EVP_MD_CTX_FLAG_FINALISE
208
209Some functions such as EVP_DigestSign only finalise copies of internal
210contexts so additional data can be included after the finalisation call.
211This is inefficient if this functionality is not required, and can be
212disabled with this flag.
213
214=back
215
3cbe1980
DSH
216=head1 RETURN VALUES
217
bbda8ce9
RT
218=over 4
219
220=item EVP_DigestInit_ex(),
221EVP_DigestUpdate(),
222EVP_DigestFinal_ex()
223
224Returns 1 for
13588350 225success and 0 for failure.
3cbe1980 226
bbda8ce9
RT
227=item EVP_MD_CTX_ctrl()
228
229Returns 1 if successful or 0 for failure.
230
231=item EVP_MD_CTX_copy_ex()
52ad5b60 232
bbda8ce9 233Returns 1 if successful or 0 for failure.
3cbe1980 234
bbda8ce9
RT
235=item EVP_MD_type(),
236EVP_MD_pkey_type(),
237EVP_MD_type()
3cbe1980 238
bbda8ce9
RT
239Returns the NID of the corresponding OBJECT IDENTIFIER or NID_undef if none
240exists.
3cbe1980 241
bbda8ce9
RT
242=item EVP_MD_size(),
243EVP_MD_block_size(),
244EVP_MD_CTX_size(),
245EVP_MD_CTX_block_size()
3cbe1980 246
bbda8ce9
RT
247Returns the digest or block size in bytes.
248
249=item EVP_md_null()
250
251Returns a pointer to the B<EVP_MD> structure of the "null" message digest.
252
253=item EVP_get_digestbyname(),
254EVP_get_digestbynid(),
255EVP_get_digestbyobj()
256
257Returns either an B<EVP_MD> structure or NULL if an error occurs.
258
259=back
3cbe1980
DSH
260
261=head1 NOTES
262
263The B<EVP> interface to message digests should almost always be used in
264preference to the low level interfaces. This is because the code then becomes
265transparent to the digest used and much more flexible.
266
bbda8ce9 267New applications should use the SHA-2 (such as L<EVP_sha256(3)>) or the SHA-3
89f66fe2 268digest algorithms (such as L<EVP_sha3_512(3)>). The other digest algorithms
269are still in common use.
3cbe1980 270
e72d734d
DSH
271For most applications the B<impl> parameter to EVP_DigestInit_ex() will be
272set to NULL to use the default digest implementation.
273
7b3e11c5 274The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are
e72d734d 275obsolete but are retained to maintain compatibility with existing code. New
7b3e11c5 276applications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and
e72d734d
DSH
277EVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
278instead of initializing and cleaning it up on each call and allow non default
279implementations of digests to be specified.
280
a528d4f0 281If digest contexts are not cleaned up after use
7b3e11c5 282memory leaks will occur.
13588350 283
25191fff
RL
284EVP_MD_CTX_size(), EVP_MD_CTX_block_size(), EVP_MD_CTX_type(),
285EVP_get_digestbynid() and EVP_get_digestbyobj() are defined as
286macros.
5a34fcd7 287
52ad5b60
TS
288EVP_MD_CTX_ctrl() sends commands to message digests for additional configuration
289or control.
5a34fcd7 290
3cbe1980
DSH
291=head1 EXAMPLE
292
293This example digests the data "Test Message\n" and "Hello World\n", using the
294digest name passed on the command line.
295
296 #include <stdio.h>
297 #include <openssl/evp.h>
298
299 main(int argc, char *argv[])
300 {
2947af32
BB
301 EVP_MD_CTX *mdctx;
302 const EVP_MD *md;
303 char mess1[] = "Test Message\n";
304 char mess2[] = "Hello World\n";
305 unsigned char md_value[EVP_MAX_MD_SIZE];
306 int md_len, i;
307
308 if (argv[1] == NULL) {
309 printf("Usage: mdtest digestname\n");
310 exit(1);
311 }
312
313 md = EVP_get_digestbyname(argv[1]);
314 if (md == NULL) {
315 printf("Unknown message digest %s\n", argv[1]);
316 exit(1);
317 }
318
319 mdctx = EVP_MD_CTX_new();
320 EVP_DigestInit_ex(mdctx, md, NULL);
321 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
322 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
323 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
324 EVP_MD_CTX_free(mdctx);
325
326 printf("Digest is: ");
327 for (i = 0; i < md_len; i++)
328 printf("%02x", md_value[i]);
329 printf("\n");
330
331 exit(0);
3cbe1980
DSH
332 }
333
3cbe1980
DSH
334=head1 SEE ALSO
335
9b86974e 336L<dgst(1)>,
b97fdb57 337L<evp(7)>
4facdbb5 338
bbda8ce9
RT
339The full list of digest algorithms are provided below.
340
341L<EVP_blake2b512(3)>,
342L<EVP_md2(3)>,
343L<EVP_md4(3)>,
344L<EVP_md5(3)>,
345L<EVP_mdc2(3)>,
346L<EVP_ripemd160(3)>,
347L<EVP_sha1(3)>,
348L<EVP_sha224(3)>,
349L<EVP_sha3_224(3)>,
67e247fa 350L<EVP_sm3(3)>,
bbda8ce9
RT
351L<EVP_whirlpool(3)>
352
3cbe1980
DSH
353=head1 HISTORY
354
25191fff 355EVP_MD_CTX_create() and EVP_MD_CTX_destroy() were renamed to
a95d7574 356EVP_MD_CTX_new() and EVP_MD_CTX_free() in OpenSSL 1.1.0.
25191fff 357
3fa39ed7 358The link between digests and signing algorithms was fixed in OpenSSL 1.0 and
a95d7574
RS
359later, so now EVP_sha1() can be used with RSA and DSA.
360
bbda8ce9 361EVP_dss1() was removed in OpenSSL 1.1.0.
3fa39ed7 362
e2f92610
RS
363=head1 COPYRIGHT
364
0d664759 365Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
e2f92610
RS
366
367Licensed under the OpenSSL license (the "License"). You may not use
368this file except in compliance with the License. You can obtain a copy
369in the file LICENSE in the source distribution or at
370L<https://www.openssl.org/source/license.html>.
371
372=cut