1EVP_DigestInit(3)                   OpenSSL                  EVP_DigestInit(3)
2
3
4

NAME

6       EVP_MD_CTX_init, EVP_MD_CTX_create, EVP_DigestInit_ex, EVP_DigestUp‐
7       date, EVP_DigestFinal_ex, EVP_MD_CTX_cleanup, EVP_MD_CTX_destroy,
8       EVP_MAX_MD_SIZE, EVP_MD_CTX_copy_ex, EVP_MD_CTX_copy, EVP_MD_type,
9       EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size, EVP_MD_CTX_md,
10       EVP_MD_CTX_size, EVP_MD_CTX_block_size, EVP_MD_CTX_type, EVP_md_null,
11       EVP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_dss, EVP_dss1, EVP_mdc2,
12       EVP_ripemd160, EVP_get_digestbyname, EVP_get_digestbynid,
13       EVP_get_digestbyobj - EVP digest routines
14

SYNOPSIS

16        #include <openssl/evp.h>
17
18        void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
19        EVP_MD_CTX *EVP_MD_CTX_create(void);
20
21        int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
22        int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
23        int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md,
24               unsigned int *s);
25
26        int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
27        void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
28
29        int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out,const EVP_MD_CTX *in);
30
31        int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
32        int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md,
33               unsigned int *s);
34
35        int EVP_MD_CTX_copy(EVP_MD_CTX *out,EVP_MD_CTX *in);
36
37        #define EVP_MAX_MD_SIZE (16+20) /* The SSLv3 md5+sha1 type */
38
39        #define EVP_MD_type(e)                 ((e)->type)
40        #define EVP_MD_pkey_type(e)            ((e)->pkey_type)
41        #define EVP_MD_size(e)                 ((e)->md_size)
42        #define EVP_MD_block_size(e)           ((e)->block_size)
43
44        #define EVP_MD_CTX_md(e)               (e)->digest)
45        #define EVP_MD_CTX_size(e)             EVP_MD_size((e)->digest)
46        #define EVP_MD_CTX_block_size(e)       EVP_MD_block_size((e)->digest)
47        #define EVP_MD_CTX_type(e)             EVP_MD_type((e)->digest)
48
49        const EVP_MD *EVP_md_null(void);
50        const EVP_MD *EVP_md2(void);
51        const EVP_MD *EVP_md5(void);
52        const EVP_MD *EVP_sha(void);
53        const EVP_MD *EVP_sha1(void);
54        const EVP_MD *EVP_dss(void);
55        const EVP_MD *EVP_dss1(void);
56        const EVP_MD *EVP_mdc2(void);
57        const EVP_MD *EVP_ripemd160(void);
58
59        const EVP_MD *EVP_get_digestbyname(const char *name);
60        #define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
61        #define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a))
62

DESCRIPTION

64       The EVP digest routines are a high level interface to message digests.
65
66       EVP_MD_CTX_init() initializes digest contet ctx.
67
68       EVP_MD_CTX_create() allocates, initializes and returns a digest contet.
69
70       EVP_DigestInit_ex() sets up digest context ctx to use a digest type
71       from ENGINE impl. ctx must be initialized before calling this function.
72       type will typically be supplied by a functionsuch as EVP_sha1().  If
73       impl is NULL then the default implementation of digest type is used.
74
75       EVP_DigestUpdate() hashes cnt bytes of data at d into the digest con‐
76       text ctx. This function can be called several times on the same ctx to
77       hash additional data.
78
79       EVP_DigestFinal_ex() retrieves the digest value from ctx and places it
80       in md. If the s parameter is not NULL then the number of bytes of data
81       written (i.e. the length of the digest) will be written to the integer
82       at s, at most EVP_MAX_MD_SIZE bytes will be written.  After calling
83       EVP_DigestFinal_ex() no additional calls to EVP_DigestUpdate() can be
84       made, but EVP_DigestInit_ex() can be called to initialize a new digest
85       operation.
86
87       EVP_MD_CTX_cleanup() cleans up digest context ctx, it should be called
88       after a digest context is no longer needed.
89
90       EVP_MD_CTX_destroy() cleans up digest context ctx and frees up the
91       space allocated to it, it should be called only on a context created
92       using EVP_MD_CTX_create().
93
94       EVP_MD_CTX_copy_ex() can be used to copy the message digest state from
95       in to out. This is useful if large amounts of data are to be hashed
96       which only differ in the last few bytes. out must be initialized before
97       calling this function.
98
99       EVP_DigestInit() behaves in the same way as EVP_DigestInit_ex() except
100       the passed context ctx does not have to be initialized, and it always
101       uses the default digest implementation.
102
103       EVP_DigestFinal() is similar to EVP_DigestFinal_ex() except the digest
104       contet ctx is automatically cleaned up.
105
106       EVP_MD_CTX_copy() is similar to EVP_MD_CTX_copy_ex() except the desti‐
107       nation out does not have to be initialized.
108
109       EVP_MD_size() and EVP_MD_CTX_size() return the size of the message
110       digest when passed an EVP_MD or an EVP_MD_CTX structure, i.e. the size
111       of the hash.
112
113       EVP_MD_block_size() and EVP_MD_CTX_block_size() return the block size
114       of the message digest when passed an EVP_MD or an EVP_MD_CTX structure.
115
116       EVP_MD_type() and EVP_MD_CTX_type() return the NID of the OBJECT IDEN‐
117       TIFIER representing the given message digest when passed an EVP_MD
118       structure.  For example EVP_MD_type(EVP_sha1()) returns NID_sha1. This
119       function is normally used when setting ASN1 OIDs.
120
121       EVP_MD_CTX_md() returns the EVP_MD structure corresponding to the
122       passed EVP_MD_CTX.
123
124       EVP_MD_pkey_type() returns the NID of the public key signing algorithm
125       associated with this digest. For example EVP_sha1() is associated with
126       RSA so this will return NID_sha1WithRSAEncryption. This "link" between
127       digests and signature algorithms may not be retained in future versions
128       of OpenSSL.
129
130       EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_mdc2() and
131       EVP_ripemd160() return EVP_MD structures for the MD2, MD5, SHA, SHA1,
132       MDC2 and RIPEMD160 digest algorithms respectively. The associated sig‐
133       nature algorithm is RSA in each case.
134
135       EVP_dss() and EVP_dss1() return EVP_MD structures for SHA and SHA1
136       digest algorithms but using DSS (DSA) for the signature algorithm.
137
138       EVP_md_null() is a "null" message digest that does nothing: i.e. the
139       hash it returns is of zero length.
140
141       EVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
142       return an EVP_MD structure when passed a digest name, a digest NID or
143       an ASN1_OBJECT structure respectively. The digest table must be ini‐
144       tialized using, for example, OpenSSL_add_all_digests() for these func‐
145       tions to work.
146

RETURN VALUES

148       EVP_DigestInit_ex(), EVP_DigestUpdate() and EVP_DigestFinal_ex() return
149       1 for success and 0 for failure.
150
151       EVP_MD_CTX_copy_ex() returns 1 if successful or 0 for failure.
152
153       EVP_MD_type(), EVP_MD_pkey_type() and EVP_MD_type() return the NID of
154       the corresponding OBJECT IDENTIFIER or NID_undef if none exists.
155
156       EVP_MD_size(), EVP_MD_block_size(), EVP_MD_CTX_size(e), EVP_MD_size(),
157       EVP_MD_CTX_block_size()  and EVP_MD_block_size() return the digest or
158       block size in bytes.
159
160       EVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_dss(),
161       EVP_dss1(), EVP_mdc2() and EVP_ripemd160() return pointers to the cor‐
162       responding EVP_MD structures.
163
164       EVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
165       return either an EVP_MD structure or NULL if an error occurs.
166

NOTES

168       The EVP interface to message digests should almost always be used in
169       preference to the low level interfaces. This is because the code then
170       becomes transparent to the digest used and much more flexible.
171
172       SHA1 is the digest of choice for new applications. The other digest
173       algorithms are still in common use.
174
175       For most applications the impl parameter to EVP_DigestInit_ex() will be
176       set to NULL to use the default digest implementation.
177
178       The functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy()
179       are obsolete but are retained to maintain compatibility with existing
180       code. New applications should use EVP_DigestInit_ex(), EVP_DigestFi‐
181       nal_ex() and EVP_MD_CTX_copy_ex() because they can efficiently reuse a
182       digest context instead of initializing and cleaning it up on each call
183       and allow non default implementations of digests to be specified.
184
185       In OpenSSL 0.9.7 and later if digest contexts are not cleaned up after
186       use memory leaks will occur.
187

EXAMPLE

189       This example digests the data "Test Message\n" and "Hello World\n",
190       using the digest name passed on the command line.
191
192        #include <stdio.h>
193        #include <openssl/evp.h>
194
195        main(int argc, char *argv[])
196        {
197        EVP_MD_CTX mdctx;
198        const EVP_MD *md;
199        char mess1[] = "Test Message\n";
200        char mess2[] = "Hello World\n";
201        unsigned char md_value[EVP_MAX_MD_SIZE];
202        int md_len, i;
203
204        OpenSSL_add_all_digests();
205
206        if(!argv[1]) {
207               printf("Usage: mdtest digestname\n");
208               exit(1);
209        }
210
211        md = EVP_get_digestbyname(argv[1]);
212
213        if(!md) {
214               printf("Unknown message digest %s\n", argv[1]);
215               exit(1);
216        }
217
218        EVP_MD_CTX_init(&mdctx);
219        EVP_DigestInit_ex(&mdctx, md, NULL);
220        EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
221        EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
222        EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
223        EVP_MD_CTX_cleanup(&mdctx);
224
225        printf("Digest is: ");
226        for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
227        printf("\n");
228        }
229

BUGS

231       The link between digests and signing algorithms results in a situation
232       where EVP_sha1() must be used with RSA and EVP_dss1() must be used with
233       DSS even though they are identical digests.
234

SEE ALSO

236       evp(3), hmac(3), md2(3), md5(3), mdc2(3), ripemd(3), sha(3), dgst(1)
237

HISTORY

239       EVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal() are avail‐
240       able in all versions of SSLeay and OpenSSL.
241
242       EVP_MD_CTX_init(), EVP_MD_CTX_create(), EVP_MD_CTX_copy_ex(),
243       EVP_MD_CTX_cleanup(), EVP_MD_CTX_destroy(), EVP_DigestInit_ex() and
244       EVP_DigestFinal_ex() were added in OpenSSL 0.9.7.
245
246       EVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_dss(),
247       EVP_dss1(), EVP_mdc2() and EVP_ripemd160() were changed to return tru‐
248       ely const EVP_MD * in OpenSSL 0.9.7.
249
250
251
2520.9.8b                            2004-05-20                 EVP_DigestInit(3)
Impressum