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 (16+20) /* The SSLv3 md5+sha1 type */
39
40
41 #define EVP_MD_type(e) ((e)->type)
42 #define EVP_MD_pkey_type(e) ((e)->pkey_type)
43 #define EVP_MD_size(e) ((e)->md_size)
44 #define EVP_MD_block_size(e) ((e)->block_size)
45
46 #define EVP_MD_CTX_md(e) (e)->digest)
47 #define EVP_MD_CTX_size(e) EVP_MD_size((e)->digest)
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_sha224(void);
57 const EVP_MD *EVP_sha256(void);
58 const EVP_MD *EVP_sha384(void);
59 const EVP_MD *EVP_sha512(void);
60 const EVP_MD *EVP_dss(void);
61 const EVP_MD *EVP_dss1(void);
62 const EVP_MD *EVP_mdc2(void);
63 const EVP_MD *EVP_ripemd160(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. This "link" between
134 digests and signature algorithms may not be retained in future versions
135 of OpenSSL.
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. The associated signature algorithm is RSA in each case.
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 SHA1 is the digest of choice for new applications. The other digest
184 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
201 This example digests the data "Test Message\n" and "Hello World\n",
202 using the digest name passed on the command line.
203
204 #include <stdio.h>
205 #include <openssl/evp.h>
206
207 main(int argc, char *argv[])
208 {
209 EVP_MD_CTX mdctx;
210 const EVP_MD *md;
211 char mess1[] = "Test Message\n";
212 char mess2[] = "Hello World\n";
213 unsigned char md_value[EVP_MAX_MD_SIZE];
214 int md_len, i;
215
216 OpenSSL_add_all_digests();
217
218 if(!argv[1]) {
219 printf("Usage: mdtest digestname\n");
220 exit(1);
221 }
222
223 md = EVP_get_digestbyname(argv[1]);
224
225 if(!md) {
226 printf("Unknown message digest %s\n", argv[1]);
227 exit(1);
228 }
229
230 EVP_MD_CTX_init(&mdctx);
231 EVP_DigestInit_ex(&mdctx, md, NULL);
232 EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
233 EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
234 EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
235 EVP_MD_CTX_cleanup(&mdctx);
236
237 printf("Digest is: ");
238 for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
239 printf("\n");
240 }
241
243 evp(3), hmac(3), md2(3), md5(3), mdc2(3), ripemd(3), sha(3), dgst(1)
244
246 EVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal() are
247 available in all versions of SSLeay and OpenSSL.
248
249 EVP_MD_CTX_init(), EVP_MD_CTX_create(), EVP_MD_CTX_copy_ex(),
250 EVP_MD_CTX_cleanup(), EVP_MD_CTX_destroy(), EVP_DigestInit_ex() and
251 EVP_DigestFinal_ex() were added in OpenSSL 0.9.7.
252
253 EVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_dss(),
254 EVP_dss1(), EVP_mdc2() and EVP_ripemd160() were changed to return
255 truely const EVP_MD * in OpenSSL 0.9.7.
256
257 The link between digests and signing algorithms was fixed in OpenSSL
258 1.0 and later, so now EVP_sha1() can be used with RSA and DSA, there is
259 no need to use EVP_dss1() any more.
260
261 OpenSSL 1.0 and later does not include the MD2 digest algorithm in the
262 default configuration due to its security weaknesses.
263
264
265
2661.0.0e 2011-09-07 EVP_DigestInit(3)