1EVP_DigestInit(3) OpenSSL EVP_DigestInit(3)
2
3
4
6 EVP_MD_CTX_init, EVP_MD_CTX_create, EVP_DigestInit_ex,
7 EVP_DigestUpdate, EVP_DigestFinal_ex, EVP_MD_CTX_cleanup,
8 EVP_MD_CTX_destroy, EVP_MAX_MD_SIZE, EVP_MD_CTX_copy_ex,
9 EVP_DigestInit, EVP_DigestFinal, EVP_MD_CTX_copy, EVP_MD_type,
10 EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_CTX_md,
11 EVP_MD_CTX_size, EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_md_null,
12 EVP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha224, EVP_sha256,
13 EVP_sha384, EVP_sha512, EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160,
14 EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj - EVP
15 digest routines
16
18 #include <openssl/evp.h>
19
20 void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
21 EVP_MD_CTX *EVP_MD_CTX_create(void);
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,
26 unsigned int *s);
27
28 int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
29 void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
30
31 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out,const EVP_MD_CTX *in);
32
33 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
34 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md,
35 unsigned int *s);
36
37 int EVP_MD_CTX_copy(EVP_MD_CTX *out,EVP_MD_CTX *in);
38
39 #define EVP_MAX_MD_SIZE 64 /* SHA512 */
40
41 int EVP_MD_type(const EVP_MD *md);
42 int EVP_MD_pkey_type(const EVP_MD *md);
43 int EVP_MD_size(const EVP_MD *md);
44 int EVP_MD_block_size(const EVP_MD *md);
45
46 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
47 #define EVP_MD_CTX_size(e) EVP_MD_size(EVP_MD_CTX_md(e))
48 #define EVP_MD_CTX_block_size(e) EVP_MD_block_size((e)->digest)
49 #define EVP_MD_CTX_type(e) EVP_MD_type((e)->digest)
50
51 const EVP_MD *EVP_md_null(void);
52 const EVP_MD *EVP_md2(void);
53 const EVP_MD *EVP_md5(void);
54 const EVP_MD *EVP_sha(void);
55 const EVP_MD *EVP_sha1(void);
56 const EVP_MD *EVP_dss(void);
57 const EVP_MD *EVP_dss1(void);
58 const EVP_MD *EVP_mdc2(void);
59 const EVP_MD *EVP_ripemd160(void);
60
61 const EVP_MD *EVP_sha224(void);
62 const EVP_MD *EVP_sha256(void);
63 const EVP_MD *EVP_sha384(void);
64 const EVP_MD *EVP_sha512(void);
65
66 const EVP_MD *EVP_get_digestbyname(const char *name);
67 #define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
68 #define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a))
69
71 The EVP digest routines are a high level interface to message digests.
72
73 EVP_MD_CTX_init() initializes digest context ctx.
74
75 EVP_MD_CTX_create() allocates, initializes and returns a digest
76 context.
77
78 EVP_DigestInit_ex() sets up digest context ctx to use a digest type
79 from ENGINE impl. ctx must be initialized before calling this function.
80 type will typically be supplied by a function such as EVP_sha1(). If
81 impl is NULL then the default implementation of digest type is used.
82
83 EVP_DigestUpdate() hashes cnt bytes of data at d into the digest
84 context ctx. This function can be called several times on the same ctx
85 to hash additional data.
86
87 EVP_DigestFinal_ex() retrieves the digest value from ctx and places it
88 in md. If the s parameter is not NULL then the number of bytes of data
89 written (i.e. the length of the digest) will be written to the integer
90 at s, at most EVP_MAX_MD_SIZE bytes will be written. After calling
91 EVP_DigestFinal_ex() no additional calls to EVP_DigestUpdate() can be
92 made, but EVP_DigestInit_ex() can be called to initialize a new digest
93 operation.
94
95 EVP_MD_CTX_cleanup() cleans up digest context ctx, it should be called
96 after a digest context is no longer needed.
97
98 EVP_MD_CTX_destroy() cleans up digest context ctx and frees up the
99 space allocated to it, it should be called only on a context created
100 using EVP_MD_CTX_create().
101
102 EVP_MD_CTX_copy_ex() can be used to copy the message digest state from
103 in to out. This is useful if large amounts of data are to be hashed
104 which only differ in the last few bytes. out must be initialized before
105 calling this function.
106
107 EVP_DigestInit() behaves in the same way as EVP_DigestInit_ex() except
108 the passed context ctx does not have to be initialized, and it always
109 uses the default digest implementation.
110
111 EVP_DigestFinal() is similar to EVP_DigestFinal_ex() except the digest
112 context ctx is automatically cleaned up.
113
114 EVP_MD_CTX_copy() is similar to EVP_MD_CTX_copy_ex() except the
115 destination out does not have to be initialized.
116
117 EVP_MD_size() and EVP_MD_CTX_size() return the size of the message
118 digest when passed an EVP_MD or an EVP_MD_CTX structure, i.e. the size
119 of the hash.
120
121 EVP_MD_block_size() and EVP_MD_CTX_block_size() return the block size
122 of the message digest when passed an EVP_MD or an EVP_MD_CTX structure.
123
124 EVP_MD_type() and EVP_MD_CTX_type() return the NID of the OBJECT
125 IDENTIFIER representing the given message digest when passed an EVP_MD
126 structure. For example EVP_MD_type(EVP_sha1()) returns NID_sha1. This
127 function is normally used when setting ASN1 OIDs.
128
129 EVP_MD_CTX_md() returns the EVP_MD structure corresponding to the
130 passed EVP_MD_CTX.
131
132 EVP_MD_pkey_type() returns the NID of the public key signing algorithm
133 associated with this digest. For example EVP_sha1() is associated with
134 RSA so this will return NID_sha1WithRSAEncryption. Since digests and
135 signature algorithms are no longer linked this function is only
136 retained for compatibility reasons.
137
138 EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_sha224(),
139 EVP_sha256(), EVP_sha384(), EVP_sha512(), EVP_mdc2() and
140 EVP_ripemd160() return EVP_MD structures for the MD2, MD5, SHA, SHA1,
141 SHA224, SHA256, SHA384, SHA512, MDC2 and RIPEMD160 digest algorithms
142 respectively.
143
144 EVP_dss() and EVP_dss1() return EVP_MD structures for SHA and SHA1
145 digest algorithms but using DSS (DSA) for the signature algorithm.
146 Note: there is no need to use these pseudo-digests in OpenSSL 1.0.0 and
147 later, they are however retained for compatibility.
148
149 EVP_md_null() is a "null" message digest that does nothing: i.e. the
150 hash it returns is of zero length.
151
152 EVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
153 return an EVP_MD structure when passed a digest name, a digest NID or
154 an ASN1_OBJECT structure respectively. The digest table must be
155 initialized using, for example, OpenSSL_add_all_digests() for these
156 functions to work.
157
159 EVP_DigestInit_ex(), EVP_DigestUpdate() and EVP_DigestFinal_ex() return
160 1 for success and 0 for failure.
161
162 EVP_MD_CTX_copy_ex() returns 1 if successful or 0 for failure.
163
164 EVP_MD_type(), EVP_MD_pkey_type() and EVP_MD_type() return the NID of
165 the corresponding OBJECT IDENTIFIER or NID_undef if none exists.
166
167 EVP_MD_size(), EVP_MD_block_size(), EVP_MD_CTX_size() and
168 EVP_MD_CTX_block_size() return the digest or block size in bytes.
169
170 EVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(),
171 EVP_sha224(), EVP_sha256(), EVP_sha384(), EVP_sha512(), EVP_dss(),
172 EVP_dss1(), EVP_mdc2() and EVP_ripemd160() return pointers to the
173 corresponding EVP_MD structures.
174
175 EVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
176 return either an EVP_MD structure or NULL if an error occurs.
177
179 The EVP interface to message digests should almost always be used in
180 preference to the low level interfaces. This is because the code then
181 becomes transparent to the digest used and much more flexible.
182
183 New applications should use the SHA2 digest algorithms such as SHA256.
184 The other digest algorithms are still in common use.
185
186 For most applications the impl parameter to EVP_DigestInit_ex() will be
187 set to NULL to use the default digest implementation.
188
189 The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy()
190 are obsolete but are retained to maintain compatibility with existing
191 code. New applications should use EVP_DigestInit_ex(),
192 EVP_DigestFinal_ex() and EVP_MD_CTX_copy_ex() because they can
193 efficiently reuse a digest context instead of initializing and cleaning
194 it up on each call and allow non default implementations of digests to
195 be specified.
196
197 In OpenSSL 0.9.7 and later if digest contexts are not cleaned up after
198 use memory leaks will occur.
199
200 Stack allocation of EVP_MD_CTX structures is common, for example:
201
202 EVP_MD_CTX mctx;
203 EVP_MD_CTX_init(&mctx);
204
205 This will cause binary compatibility issues if the size of EVP_MD_CTX
206 structure changes (this will only happen with a major release of
207 OpenSSL). Applications wishing to avoid this should use
208 EVP_MD_CTX_create() instead:
209
210 EVP_MD_CTX *mctx;
211 mctx = EVP_MD_CTX_create();
212
214 This example digests the data "Test Message\n" and "Hello World\n",
215 using the digest name passed on the command line.
216
217 #include <stdio.h>
218 #include <openssl/evp.h>
219
220 main(int argc, char *argv[])
221 {
222 EVP_MD_CTX *mdctx;
223 const EVP_MD *md;
224 char mess1[] = "Test Message\n";
225 char mess2[] = "Hello World\n";
226 unsigned char md_value[EVP_MAX_MD_SIZE];
227 int md_len, i;
228
229 OpenSSL_add_all_digests();
230
231 if(!argv[1]) {
232 printf("Usage: mdtest digestname\n");
233 exit(1);
234 }
235
236 md = EVP_get_digestbyname(argv[1]);
237
238 if(!md) {
239 printf("Unknown message digest %s\n", argv[1]);
240 exit(1);
241 }
242
243 mdctx = EVP_MD_CTX_create();
244 EVP_DigestInit_ex(mdctx, md, NULL);
245 EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
246 EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
247 EVP_DigestFinal_ex(mdctx, md_value, &md_len);
248 EVP_MD_CTX_destroy(mdctx);
249
250 printf("Digest is: ");
251 for(i = 0; i < md_len; i++)
252 printf("%02x", md_value[i]);
253 printf("\n");
254
255 /* Call this once before exit. */
256 EVP_cleanup();
257 exit(0);
258 }
259
261 dgst(1), evp(3)
262
264 EVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal() are
265 available in all versions of SSLeay and OpenSSL.
266
267 EVP_MD_CTX_init(), EVP_MD_CTX_create(), EVP_MD_CTX_copy_ex(),
268 EVP_MD_CTX_cleanup(), EVP_MD_CTX_destroy(), EVP_DigestInit_ex() and
269 EVP_DigestFinal_ex() were added in OpenSSL 0.9.7.
270
271 EVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_dss(),
272 EVP_dss1(), EVP_mdc2() and EVP_ripemd160() were changed to return truly
273 const EVP_MD * in OpenSSL 0.9.7.
274
275 The link between digests and signing algorithms was fixed in OpenSSL
276 1.0 and later, so now EVP_sha1() can be used with RSA and DSA; there is
277 no need to use EVP_dss1() any more.
278
279 OpenSSL 1.0 and later does not include the MD2 digest algorithm in the
280 default configuration due to its security weaknesses.
281
282
283
2841.0.2o 2019-02-08 EVP_DigestInit(3)