5 EVP_DigestInit, EVP_DigestUpdate, EVP_DigestFinal, EVP_MAX_MD_SIZE,
6 EVP_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size, EVP_MD_block_size,
7 EVP_MD_CTX_md, EVP_MD_CTX_size, EVP_MD_CTX_block_size, EVP_MD_CTX_type,
8 EVP_md_null, EVP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_dss, EVP_dss1, EVP_mdc2,
9 EVP_ripemd160, EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj -
14 #include <openssl/evp.h>
16 int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
17 int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, unsigned int cnt);
18 int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md,
21 #define EVP_MAX_MD_SIZE (16+20) /* The SSLv3 md5+sha1 type */
23 int EVP_MD_CTX_copy(EVP_MD_CTX *out,EVP_MD_CTX *in);
25 #define EVP_MD_type(e) ((e)->type)
26 #define EVP_MD_pkey_type(e) ((e)->pkey_type)
27 #define EVP_MD_size(e) ((e)->md_size)
28 #define EVP_MD_block_size(e) ((e)->block_size)
30 #define EVP_MD_CTX_md(e) (e)->digest)
31 #define EVP_MD_CTX_size(e) EVP_MD_size((e)->digest)
32 #define EVP_MD_CTX_block_size(e) EVP_MD_block_size((e)->digest)
33 #define EVP_MD_CTX_type(e) EVP_MD_type((e)->digest)
35 const EVP_MD *EVP_md_null(void);
36 const EVP_MD *EVP_md2(void);
37 const EVP_MD *EVP_md5(void);
38 const EVP_MD *EVP_sha(void);
39 const EVP_MD *EVP_sha1(void);
40 const EVP_MD *EVP_dss(void);
41 const EVP_MD *EVP_dss1(void);
42 const EVP_MD *EVP_mdc2(void);
43 const EVP_MD *EVP_ripemd160(void);
45 const EVP_MD *EVP_get_digestbyname(const char *name);
46 #define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
47 #define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a))
51 The EVP digest routines are a high level interface to message digests.
53 EVP_DigestInit() initializes a digest context B<ctx> to use a digest
54 B<type>: this will typically be supplied by a function such as
57 EVP_DigestUpdate() hashes B<cnt> bytes of data at B<d> into the
58 digest context B<ctx>. This function can be called several times on the
59 same B<ctx> to hash additional data.
61 EVP_DigestFinal() retrieves the digest value from B<ctx> and places
62 it in B<md>. If the B<s> parameter is not NULL then the number of
63 bytes of data written (i.e. the length of the digest) will be written
64 to the integer at B<s>, at most B<EVP_MAX_MD_SIZE> bytes will be written.
65 After calling EVP_DigestFinal() no additional calls to EVP_DigestUpdate()
66 can be made, but EVP_DigestInit() can be called to initialize a new
69 EVP_MD_CTX_copy() can be used to copy the message digest state from
70 B<in> to B<out>. This is useful if large amounts of data are to be
71 hashed which only differ in the last few bytes.
73 EVP_MD_size() and EVP_MD_CTX_size() return the size of the message digest
74 when passed an B<EVP_MD> or an B<EVP_MD_CTX> structure, i.e. the size of the
77 EVP_MD_block_size() and EVP_MD_CTX_block_size() return the block size of the
78 message digest when passed an B<EVP_MD> or an B<EVP_MD_CTX> structure.
80 EVP_MD_type() and EVP_MD_CTX_type() return the NID of the OBJECT IDENTIFIER
81 representing the given message digest when passed an B<EVP_MD> structure.
82 For example EVP_MD_type(EVP_sha1()) returns B<NID_sha1>. This function is
83 normally used when setting ASN1 OIDs.
85 EVP_MD_CTX_md() returns the B<EVP_MD> structure corresponding to the passed
88 EVP_MD_pkey_type() returns the NID of the public key signing algorithm associated
89 with this digest. For example EVP_sha1() is associated with RSA so this will
90 return B<NID_sha1WithRSAEncryption>. This "link" between digests and signature
91 algorithms may not be retained in future versions of OpenSSL.
93 EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_mdc2() and EVP_ripemd160()
94 return B<EVP_MD> structures for the MD2, MD5, SHA, SHA1, MDC2 and RIPEMD160 digest
95 algorithms respectively. The associated signature algorithm is RSA in each case.
97 EVP_dss() and EVP_dss1() return B<EVP_MD> structures for SHA and SHA1 digest
98 algorithms but using DSS (DSA) for the signature algorithm.
100 EVP_md_null() is a "null" message digest that does nothing: i.e. the hash it
101 returns is of zero length.
103 EVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
104 return an B<EVP_MD> structure when passed a digest name, a digest NID or
105 an ASN1_OBJECT structure respectively. The digest table must be initialized
106 using, for example, OpenSSL_add_all_digests() for these functions to work.
110 EVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal() return 1 for
111 success and 0 for failure.
113 EVP_MD_CTX_copy() returns 1 if successful or 0 for failure.
115 EVP_MD_type(), EVP_MD_pkey_type() and EVP_MD_type() return the NID of the
116 corresponding OBJECT IDENTIFIER or NID_undef if none exists.
118 EVP_MD_size(), EVP_MD_block_size(), EVP_MD_CTX_size(e), EVP_MD_size(),
119 EVP_MD_CTX_block_size() and EVP_MD_block_size() return the digest or block
122 EVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_dss(),
123 EVP_dss1(), EVP_mdc2() and EVP_ripemd160() return pointers to the
124 corresponding EVP_MD structures.
126 EVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
127 return either an B<EVP_MD> structure or NULL if an error occurs.
131 The B<EVP> interface to message digests should almost always be used in
132 preference to the low level interfaces. This is because the code then becomes
133 transparent to the digest used and much more flexible.
135 SHA1 is the digest of choice for new applications. The other digest algorithms
136 are still in common use.
138 The functions EVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal(),
139 did not return errors in OpenSSL versions before 0.9.7 or earlier. Software only
140 versions of digest algorithms will never return error codes for these functions.
144 This example digests the data "Test Message\n" and "Hello World\n", using the
145 digest name passed on the command line.
148 #include <openssl/evp.h>
150 main(int argc, char *argv[])
154 char mess1[] = "Test Message\n";
155 char mess2[] = "Hello World\n";
156 unsigned char md_value[EVP_MAX_MD_SIZE];
159 OpenSSL_add_all_digests();
162 printf("Usage: mdtest digestname\n");
166 md = EVP_get_digestbyname(argv[1]);
169 printf("Unknown message digest %s\n", argv[1]);
173 EVP_DigestInit(&mdctx, md);
174 EVP_DigestUpdate(&mdctx, mess1, strlen(mess1));
175 EVP_DigestUpdate(&mdctx, mess2, strlen(mess2));
176 EVP_DigestFinal(&mdctx, md_value, &md_len);
178 printf("Digest is: ");
179 for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
185 The link between digests and signing algorithms results in a situation where
186 EVP_sha1() must be used with RSA and EVP_dss1() must be used with DSS
187 even though they are identical digests.
189 The size of an B<EVP_MD_CTX> structure is determined at compile time: this results
190 in code that must be recompiled if the size of B<EVP_MD_CTX> increases.
194 L<evp(3)|evp(3)>, L<hmac(3)|hmac(3)>, L<md2(3)|md2(3)>,
195 L<md5(3)|md5(3)>, L<mdc2(3)|mdc2(3)>, L<ripemd(3)|ripemd(3)>,
196 L<sha(3)|sha(3)>, L<digest(1)|digest(1)>
200 EVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal() are
201 available in all versions of SSLeay and OpenSSL.