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_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size,
10 EVP_MD_block_size, EVP_MD_CTX_md, EVP_MD_CTX_size,
11 EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_md_null, EVP_md2, EVP_md5,
12 EVP_sha, EVP_sha1, EVP_sha224, EVP_sha256, EVP_sha384, EVP_sha512,
13 EVP_dss, EVP_dss1, EVP_mdc2, EVP_ripemd160, EVP_get_digestbyname,
14 EVP_get_digestbynid, EVP_get_digestbyobj - EVP digest routines
15
17 #include <openssl/evp.h>
18
19 void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
20 EVP_MD_CTX *EVP_MD_CTX_create(void);
21
22 int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
23 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
24 int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md,
25 unsigned int *s);
26
27 int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
28 void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
29
30 int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out,const EVP_MD_CTX *in);
31
32 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
33 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md,
34 unsigned int *s);
35
36 int EVP_MD_CTX_copy(EVP_MD_CTX *out,EVP_MD_CTX *in);
37
38 #define EVP_MAX_MD_SIZE 64 /* SHA512 */
39
40 int EVP_MD_type(const EVP_MD *md);
41 int EVP_MD_pkey_type(const EVP_MD *md);
42 int EVP_MD_size(const EVP_MD *md);
43 int EVP_MD_block_size(const EVP_MD *md);
44
45 const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
46 #define EVP_MD_CTX_size(e) EVP_MD_size(EVP_MD_CTX_md(e))
47 #define EVP_MD_CTX_block_size(e) EVP_MD_block_size((e)->digest)
48 #define EVP_MD_CTX_type(e) EVP_MD_type((e)->digest)
49
50 const EVP_MD *EVP_md_null(void);
51 const EVP_MD *EVP_md2(void);
52 const EVP_MD *EVP_md5(void);
53 const EVP_MD *EVP_sha(void);
54 const EVP_MD *EVP_sha1(void);
55 const EVP_MD *EVP_dss(void);
56 const EVP_MD *EVP_dss1(void);
57 const EVP_MD *EVP_mdc2(void);
58 const EVP_MD *EVP_ripemd160(void);
59
60 const EVP_MD *EVP_sha224(void);
61 const EVP_MD *EVP_sha256(void);
62 const EVP_MD *EVP_sha384(void);
63 const EVP_MD *EVP_sha512(void);
64
65 const EVP_MD *EVP_get_digestbyname(const char *name);
66 #define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
67 #define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a))
68
70 The EVP digest routines are a high level interface to message digests.
71
72 EVP_MD_CTX_init() initializes digest context ctx.
73
74 EVP_MD_CTX_create() allocates, initializes and returns a digest
75 context.
76
77 EVP_DigestInit_ex() sets up digest context ctx to use a digest type
78 from ENGINE impl. ctx must be initialized before calling this function.
79 type will typically be supplied by a function such as EVP_sha1(). If
80 impl is NULL then the default implementation of digest type is used.
81
82 EVP_DigestUpdate() hashes cnt bytes of data at d into the digest
83 context ctx. This function can be called several times on the same ctx
84 to hash additional data.
85
86 EVP_DigestFinal_ex() retrieves the digest value from ctx and places it
87 in md. If the s parameter is not NULL then the number of bytes of data
88 written (i.e. the length of the digest) will be written to the integer
89 at s, at most EVP_MAX_MD_SIZE bytes will be written. After calling
90 EVP_DigestFinal_ex() no additional calls to EVP_DigestUpdate() can be
91 made, but EVP_DigestInit_ex() can be called to initialize a new digest
92 operation.
93
94 EVP_MD_CTX_cleanup() cleans up digest context ctx, it should be called
95 after a digest context is no longer needed.
96
97 EVP_MD_CTX_destroy() cleans up digest context ctx and frees up the
98 space allocated to it, it should be called only on a context created
99 using EVP_MD_CTX_create().
100
101 EVP_MD_CTX_copy_ex() can be used to copy the message digest state from
102 in to out. This is useful if large amounts of data are to be hashed
103 which only differ in the last few bytes. out must be initialized before
104 calling this function.
105
106 EVP_DigestInit() behaves in the same way as EVP_DigestInit_ex() except
107 the passed context ctx does not have to be initialized, and it always
108 uses the default digest implementation.
109
110 EVP_DigestFinal() is similar to EVP_DigestFinal_ex() except the digest
111 context ctx is automatically cleaned up.
112
113 EVP_MD_CTX_copy() is similar to EVP_MD_CTX_copy_ex() except the
114 destination out does not have to be initialized.
115
116 EVP_MD_size() and EVP_MD_CTX_size() return the size of the message
117 digest when passed an EVP_MD or an EVP_MD_CTX structure, i.e. the size
118 of the hash.
119
120 EVP_MD_block_size() and EVP_MD_CTX_block_size() return the block size
121 of the message digest when passed an EVP_MD or an EVP_MD_CTX structure.
122
123 EVP_MD_type() and EVP_MD_CTX_type() return the NID of the OBJECT
124 IDENTIFIER representing the given message digest when passed an EVP_MD
125 structure. For example EVP_MD_type(EVP_sha1()) returns NID_sha1. This
126 function is normally used when setting ASN1 OIDs.
127
128 EVP_MD_CTX_md() returns the EVP_MD structure corresponding to the
129 passed EVP_MD_CTX.
130
131 EVP_MD_pkey_type() returns the NID of the public key signing algorithm
132 associated with this digest. For example EVP_sha1() is associated with
133 RSA so this will return NID_sha1WithRSAEncryption. Since digests and
134 signature algorithms are no longer linked this function is only
135 retained for compatibility reasons.
136
137 EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_sha224(),
138 EVP_sha256(), EVP_sha384(), EVP_sha512(), EVP_mdc2() and
139 EVP_ripemd160() return EVP_MD structures for the MD2, MD5, SHA, SHA1,
140 SHA224, SHA256, SHA384, SHA512, MDC2 and RIPEMD160 digest algorithms
141 respectively.
142
143 EVP_dss() and EVP_dss1() return EVP_MD structures for SHA and SHA1
144 digest algorithms but using DSS (DSA) for the signature algorithm.
145 Note: there is no need to use these pseudo-digests in OpenSSL 1.0.0 and
146 later, they are however retained for compatibility.
147
148 EVP_md_null() is a "null" message digest that does nothing: i.e. the
149 hash it returns is of zero length.
150
151 EVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
152 return an EVP_MD structure when passed a digest name, a digest NID or
153 an ASN1_OBJECT structure respectively. The digest table must be
154 initialized using, for example, OpenSSL_add_all_digests() for these
155 functions to work.
156
158 EVP_DigestInit_ex(), EVP_DigestUpdate() and EVP_DigestFinal_ex() return
159 1 for success and 0 for failure.
160
161 EVP_MD_CTX_copy_ex() returns 1 if successful or 0 for failure.
162
163 EVP_MD_type(), EVP_MD_pkey_type() and EVP_MD_type() return the NID of
164 the corresponding OBJECT IDENTIFIER or NID_undef if none exists.
165
166 EVP_MD_size(), EVP_MD_block_size(), EVP_MD_CTX_size(e), EVP_MD_size(),
167 EVP_MD_CTX_block_size() and EVP_MD_block_size() return the digest or
168 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++) printf("%02x", md_value[i]);
252 printf("\n");
253 }
254
256 evp(3), hmac(3), md2(3), md5(3), mdc2(3), ripemd(3), sha(3), dgst(1)
257
259 EVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal() are
260 available in all versions of SSLeay and OpenSSL.
261
262 EVP_MD_CTX_init(), EVP_MD_CTX_create(), EVP_MD_CTX_copy_ex(),
263 EVP_MD_CTX_cleanup(), EVP_MD_CTX_destroy(), EVP_DigestInit_ex() and
264 EVP_DigestFinal_ex() were added in OpenSSL 0.9.7.
265
266 EVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_dss(),
267 EVP_dss1(), EVP_mdc2() and EVP_ripemd160() were changed to return
268 truely const EVP_MD * in OpenSSL 0.9.7.
269
270 The link between digests and signing algorithms was fixed in OpenSSL
271 1.0 and later, so now EVP_sha1() can be used with RSA and DSA, there is
272 no need to use EVP_dss1() any more.
273
274 OpenSSL 1.0 and later does not include the MD2 digest algorithm in the
275 default configuration due to its security weaknesses.
276
277
278
2791.0.1e 2017-03-22 EVP_DigestInit(3)