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