EVP_DigestInit.pod revision 238405
18876Srgrimes=pod
24Srgrimes
34Srgrimes=head1 NAME
44Srgrimes
58876SrgrimesEVP_MD_CTX_init, EVP_MD_CTX_create, EVP_DigestInit_ex, EVP_DigestUpdate,
64SrgrimesEVP_DigestFinal_ex, EVP_MD_CTX_cleanup, EVP_MD_CTX_destroy, EVP_MAX_MD_SIZE,
74SrgrimesEVP_MD_CTX_copy_ex, EVP_MD_CTX_copy, EVP_MD_type, EVP_MD_pkey_type, EVP_MD_size,
84SrgrimesEVP_MD_block_size, EVP_MD_CTX_md, EVP_MD_CTX_size, EVP_MD_CTX_block_size, EVP_MD_CTX_type,
94SrgrimesEVP_md_null, EVP_md2, EVP_md5, EVP_sha, EVP_sha1, EVP_sha224, EVP_sha256,
104SrgrimesEVP_sha384, EVP_sha512, EVP_dss, EVP_dss1, EVP_mdc2,
118876SrgrimesEVP_ripemd160, EVP_get_digestbyname, EVP_get_digestbynid, EVP_get_digestbyobj -
128876SrgrimesEVP digest routines
134Srgrimes
144Srgrimes=head1 SYNOPSIS
158876Srgrimes
164Srgrimes #include <openssl/evp.h>
178876Srgrimes
184Srgrimes void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
194Srgrimes EVP_MD_CTX *EVP_MD_CTX_create(void);
204Srgrimes
214Srgrimes int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
228876Srgrimes int EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *d, size_t cnt);
234Srgrimes int EVP_DigestFinal_ex(EVP_MD_CTX *ctx, unsigned char *md,
244Srgrimes        unsigned int *s);
254Srgrimes
2617495Sjoerg int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
274Srgrimes void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
28623Srgrimes
294Srgrimes int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out,const EVP_MD_CTX *in);  
304Srgrimes
314Srgrimes int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
324Srgrimes int EVP_DigestFinal(EVP_MD_CTX *ctx, unsigned char *md,
334Srgrimes        unsigned int *s);
342056Swollman
3517495Sjoerg int EVP_MD_CTX_copy(EVP_MD_CTX *out,EVP_MD_CTX *in);  
362056Swollman
3712734Sbde #define EVP_MAX_MD_SIZE 64	/* SHA512 */
3812734Sbde
3912734Sbde int EVP_MD_type(const EVP_MD *md);
402056Swollman int EVP_MD_pkey_type(const EVP_MD *md);	
412056Swollman int EVP_MD_size(const EVP_MD *md);
424Srgrimes int EVP_MD_block_size(const EVP_MD *md);
434Srgrimes
444Srgrimes const EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx);
454Srgrimes #define EVP_MD_CTX_size(e)		EVP_MD_size(EVP_MD_CTX_md(e))
464Srgrimes #define EVP_MD_CTX_block_size(e)	EVP_MD_block_size((e)->digest)
474Srgrimes #define EVP_MD_CTX_type(e)		EVP_MD_type((e)->digest)
484Srgrimes
494Srgrimes const EVP_MD *EVP_md_null(void);
504Srgrimes const EVP_MD *EVP_md2(void);
514Srgrimes const EVP_MD *EVP_md5(void);
5212720Sphk const EVP_MD *EVP_sha(void);
5312720Sphk const EVP_MD *EVP_sha1(void);
5412720Sphk const EVP_MD *EVP_dss(void);
5512720Sphk const EVP_MD *EVP_dss1(void);
564Srgrimes const EVP_MD *EVP_mdc2(void);
5717495Sjoerg const EVP_MD *EVP_ripemd160(void);
5817495Sjoerg
5917495Sjoerg const EVP_MD *EVP_sha224(void);
6017495Sjoerg const EVP_MD *EVP_sha256(void);
6117495Sjoerg const EVP_MD *EVP_sha384(void);
6217495Sjoerg const EVP_MD *EVP_sha512(void);
6317495Sjoerg
644Srgrimes const EVP_MD *EVP_get_digestbyname(const char *name);
654Srgrimes #define EVP_get_digestbynid(a) EVP_get_digestbyname(OBJ_nid2sn(a))
664Srgrimes #define EVP_get_digestbyobj(a) EVP_get_digestbynid(OBJ_obj2nid(a))
674Srgrimes
684Srgrimes=head1 DESCRIPTION
6912515Sphk
7012515SphkThe EVP digest routines are a high level interface to message digests.
7112515Sphk
7212515SphkEVP_MD_CTX_init() initializes digest context B<ctx>.
7312515Sphk
7412473SbdeEVP_MD_CTX_create() allocates, initializes and returns a digest context.
754Srgrimes
764SrgrimesEVP_DigestInit_ex() sets up digest context B<ctx> to use a digest
774SrgrimesB<type> from ENGINE B<impl>. B<ctx> must be initialized before calling this
784Srgrimesfunction. B<type> will typically be supplied by a functionsuch as EVP_sha1().
794SrgrimesIf B<impl> is NULL then the default implementation of digest B<type> is used.
804Srgrimes
814SrgrimesEVP_DigestUpdate() hashes B<cnt> bytes of data at B<d> into the
824Srgrimesdigest context B<ctx>. This function can be called several times on the
834Srgrimessame B<ctx> to hash additional data.
844Srgrimes
854SrgrimesEVP_DigestFinal_ex() retrieves the digest value from B<ctx> and places
864Srgrimesit in B<md>. If the B<s> parameter is not NULL then the number of
874Srgrimesbytes of data written (i.e. the length of the digest) will be written
884Srgrimesto the integer at B<s>, at most B<EVP_MAX_MD_SIZE> bytes will be written.
894SrgrimesAfter calling EVP_DigestFinal_ex() no additional calls to EVP_DigestUpdate()
904Srgrimescan be made, but EVP_DigestInit_ex() can be called to initialize a new
914Srgrimesdigest operation.
924Srgrimes
934SrgrimesEVP_MD_CTX_cleanup() cleans up digest context B<ctx>, it should be called
944Srgrimesafter a digest context is no longer needed.
954Srgrimes
964SrgrimesEVP_MD_CTX_destroy() cleans up digest context B<ctx> and frees up the
974Srgrimesspace allocated to it, it should be called only on a context created
984Srgrimesusing EVP_MD_CTX_create().
994Srgrimes
1004SrgrimesEVP_MD_CTX_copy_ex() can be used to copy the message digest state from
1014SrgrimesB<in> to B<out>. This is useful if large amounts of data are to be
1024Srgrimeshashed which only differ in the last few bytes. B<out> must be initialized
1034Srgrimesbefore calling this function.
1044Srgrimes
1054SrgrimesEVP_DigestInit() behaves in the same way as EVP_DigestInit_ex() except
1064Srgrimesthe passed context B<ctx> does not have to be initialized, and it always
1074Srgrimesuses the default digest implementation.
1084Srgrimes
1094SrgrimesEVP_DigestFinal() is similar to EVP_DigestFinal_ex() except the digest
1104Srgrimescontext B<ctx> is automatically cleaned up.
1114Srgrimes
1124SrgrimesEVP_MD_CTX_copy() is similar to EVP_MD_CTX_copy_ex() except the destination
1134SrgrimesB<out> does not have to be initialized.
1144Srgrimes
1154SrgrimesEVP_MD_size() and EVP_MD_CTX_size() return the size of the message digest
1164Srgrimeswhen passed an B<EVP_MD> or an B<EVP_MD_CTX> structure, i.e. the size of the
1174Srgrimeshash.
1184Srgrimes
1194SrgrimesEVP_MD_block_size() and EVP_MD_CTX_block_size() return the block size of the
1204Srgrimesmessage digest when passed an B<EVP_MD> or an B<EVP_MD_CTX> structure.
1214Srgrimes
1224SrgrimesEVP_MD_type() and EVP_MD_CTX_type() return the NID of the OBJECT IDENTIFIER
1234Srgrimesrepresenting the given message digest when passed an B<EVP_MD> structure.
1244SrgrimesFor example EVP_MD_type(EVP_sha1()) returns B<NID_sha1>. This function is
1254Srgrimesnormally used when setting ASN1 OIDs.
1264Srgrimes
1274SrgrimesEVP_MD_CTX_md() returns the B<EVP_MD> structure corresponding to the passed
1284SrgrimesB<EVP_MD_CTX>.
1294Srgrimes
1304SrgrimesEVP_MD_pkey_type() returns the NID of the public key signing algorithm associated
1314Srgrimeswith this digest. For example EVP_sha1() is associated with RSA so this will
1324Srgrimesreturn B<NID_sha1WithRSAEncryption>. Since digests and signature algorithms
1334Srgrimesare no longer linked this function is only retained for compatibility
1344Srgrimesreasons.
1354Srgrimes
1364SrgrimesEVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_sha224(), EVP_sha256(),
1374SrgrimesEVP_sha384(), EVP_sha512(), EVP_mdc2() and EVP_ripemd160() return B<EVP_MD>
1384Srgrimesstructures for the MD2, MD5, SHA, SHA1, SHA224, SHA256, SHA384, SHA512, MDC2
1394Srgrimesand RIPEMD160 digest algorithms respectively. 
1404Srgrimes
1414SrgrimesEVP_dss() and EVP_dss1() return B<EVP_MD> structures for SHA and SHA1 digest
1424Srgrimesalgorithms but using DSS (DSA) for the signature algorithm. Note: there is 
1434Srgrimesno need to use these pseudo-digests in OpenSSL 1.0.0 and later, they are
1444Srgrimeshowever retained for compatibility.
1454Srgrimes
1464SrgrimesEVP_md_null() is a "null" message digest that does nothing: i.e. the hash it
1474Srgrimesreturns is of zero length.
1484Srgrimes
1494SrgrimesEVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
1504Srgrimesreturn an B<EVP_MD> structure when passed a digest name, a digest NID or
1514Srgrimesan ASN1_OBJECT structure respectively. The digest table must be initialized
1524Srgrimesusing, for example, OpenSSL_add_all_digests() for these functions to work.
1534Srgrimes
1544Srgrimes=head1 RETURN VALUES
1554Srgrimes
1564SrgrimesEVP_DigestInit_ex(), EVP_DigestUpdate() and EVP_DigestFinal_ex() return 1 for
1574Srgrimessuccess and 0 for failure.
1584Srgrimes
1594SrgrimesEVP_MD_CTX_copy_ex() returns 1 if successful or 0 for failure.
1604Srgrimes
1614SrgrimesEVP_MD_type(), EVP_MD_pkey_type() and EVP_MD_type() return the NID of the
1624Srgrimescorresponding OBJECT IDENTIFIER or NID_undef if none exists.
1634Srgrimes
1644SrgrimesEVP_MD_size(), EVP_MD_block_size(), EVP_MD_CTX_size(e), EVP_MD_size(),
1654SrgrimesEVP_MD_CTX_block_size()	and EVP_MD_block_size() return the digest or block
1664Srgrimessize in bytes.
1674Srgrimes
1684SrgrimesEVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(), EVP_dss(),
1694SrgrimesEVP_dss1(), EVP_mdc2() and EVP_ripemd160() return pointers to the
1704Srgrimescorresponding EVP_MD structures.
1714Srgrimes
1724SrgrimesEVP_get_digestbyname(), EVP_get_digestbynid() and EVP_get_digestbyobj()
1734Srgrimesreturn either an B<EVP_MD> structure or NULL if an error occurs.
1744Srgrimes
1754Srgrimes=head1 NOTES
1764Srgrimes
1774SrgrimesThe B<EVP> interface to message digests should almost always be used in
1784Srgrimespreference to the low level interfaces. This is because the code then becomes
1794Srgrimestransparent to the digest used and much more flexible.
1804Srgrimes
1814SrgrimesNew applications should use the SHA2 digest algorithms such as SHA256. 
18217495SjoergThe other digest algorithms are still in common use.
1834Srgrimes
1844SrgrimesFor most applications the B<impl> parameter to EVP_DigestInit_ex() will be
1854Srgrimesset to NULL to use the default digest implementation.
1864Srgrimes
1874SrgrimesThe functions EVP_DigestInit(), EVP_DigestFinal() and EVP_MD_CTX_copy() are 
18817495Sjoergobsolete but are retained to maintain compatibility with existing code. New
18917495Sjoergapplications should use EVP_DigestInit_ex(), EVP_DigestFinal_ex() and 
19017495SjoergEVP_MD_CTX_copy_ex() because they can efficiently reuse a digest context
19117495Sjoerginstead of initializing and cleaning it up on each call and allow non default
19217495Sjoergimplementations of digests to be specified.
19317495Sjoerg
19417495SjoergIn OpenSSL 0.9.7 and later if digest contexts are not cleaned up after use
19517495Sjoergmemory leaks will occur. 
19617495Sjoerg
19717495SjoergStack allocation of EVP_MD_CTX structures is common, for example:
19817495Sjoerg
19917495Sjoerg EVP_MD_CTX mctx;
20017495Sjoerg EVP_MD_CTX_init(&mctx);
20117495Sjoerg
20217495SjoergThis will cause binary compatibility issues if the size of EVP_MD_CTX
20317495Sjoergstructure changes (this will only happen with a major release of OpenSSL).
20417495SjoergApplications wishing to avoid this should use EVP_MD_CTX_create() instead:
20517495Sjoerg
20617495Sjoerg EVP_MD_CTX *mctx;
20717495Sjoerg mctx = EVP_MD_CTX_create();
20817495Sjoerg
20917495Sjoerg
21017495Sjoerg=head1 EXAMPLE
21117495Sjoerg
21217495SjoergThis example digests the data "Test Message\n" and "Hello World\n", using the
21317495Sjoergdigest name passed on the command line.
21417495Sjoerg
21517495Sjoerg #include <stdio.h>
21617495Sjoerg #include <openssl/evp.h>
21717495Sjoerg
21817495Sjoerg main(int argc, char *argv[])
21917495Sjoerg {
22017495Sjoerg EVP_MD_CTX *mdctx;
22117495Sjoerg const EVP_MD *md;
2224Srgrimes char mess1[] = "Test Message\n";
2234Srgrimes char mess2[] = "Hello World\n";
2244Srgrimes unsigned char md_value[EVP_MAX_MD_SIZE];
2254Srgrimes int md_len, i;
2264Srgrimes
2274Srgrimes OpenSSL_add_all_digests();
2284Srgrimes
2294Srgrimes if(!argv[1]) {
2304Srgrimes 	printf("Usage: mdtest digestname\n");
2314Srgrimes	exit(1);
2324Srgrimes }
2334Srgrimes
2344Srgrimes md = EVP_get_digestbyname(argv[1]);
2354Srgrimes
2364Srgrimes if(!md) {
2374Srgrimes 	printf("Unknown message digest %s\n", argv[1]);
2384Srgrimes	exit(1);
2394Srgrimes }
2404Srgrimes
2414Srgrimes mdctx = EVP_MD_CTX_create();
2424Srgrimes EVP_DigestInit_ex(mdctx, md, NULL);
2434Srgrimes EVP_DigestUpdate(mdctx, mess1, strlen(mess1));
2444Srgrimes EVP_DigestUpdate(mdctx, mess2, strlen(mess2));
2454Srgrimes EVP_DigestFinal_ex(mdctx, md_value, &md_len);
2464Srgrimes EVP_MD_CTX_destroy(mdctx);
24712473Sbde
2482112Swollman printf("Digest is: ");
2492112Swollman for(i = 0; i < md_len; i++) printf("%02x", md_value[i]);
2502112Swollman printf("\n");
2512112Swollman }
2522112Swollman
2534Srgrimes=head1 SEE ALSO
2544Srgrimes
2554SrgrimesL<evp(3)|evp(3)>, L<hmac(3)|hmac(3)>, L<md2(3)|md2(3)>,
2564SrgrimesL<md5(3)|md5(3)>, L<mdc2(3)|mdc2(3)>, L<ripemd(3)|ripemd(3)>,
25717495SjoergL<sha(3)|sha(3)>, L<dgst(1)|dgst(1)>
25817495Sjoerg
25917495Sjoerg=head1 HISTORY
26017495Sjoerg
26117495SjoergEVP_DigestInit(), EVP_DigestUpdate() and EVP_DigestFinal() are
26217495Sjoergavailable in all versions of SSLeay and OpenSSL.
26317495Sjoerg
26417495SjoergEVP_MD_CTX_init(), EVP_MD_CTX_create(), EVP_MD_CTX_copy_ex(),
26517495SjoergEVP_MD_CTX_cleanup(), EVP_MD_CTX_destroy(), EVP_DigestInit_ex()
26617495Sjoergand EVP_DigestFinal_ex() were added in OpenSSL 0.9.7.
26717495Sjoerg
26817495SjoergEVP_md_null(), EVP_md2(), EVP_md5(), EVP_sha(), EVP_sha1(),
26917495SjoergEVP_dss(), EVP_dss1(), EVP_mdc2() and EVP_ripemd160() were
27017495Sjoergchanged to return truely const EVP_MD * in OpenSSL 0.9.7.
2714Srgrimes
2724SrgrimesThe link between digests and signing algorithms was fixed in OpenSSL 1.0 and
2734Srgrimeslater, so now EVP_sha1() can be used with RSA and DSA, there is no need to
2744Srgrimesuse EVP_dss1() any more.
2754Srgrimes
2764SrgrimesOpenSSL 1.0 and later does not include the MD2 digest algorithm in the
2774Srgrimesdefault configuration due to its security weaknesses.
2784Srgrimes
2794Srgrimes=cut
2804Srgrimes