1e71b7053SJung-uk Kim=pod 2e71b7053SJung-uk Kim 3e71b7053SJung-uk Kim=head1 NAME 4e71b7053SJung-uk Kim 5e71b7053SJung-uk KimBIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO filter 6e71b7053SJung-uk Kim 7e71b7053SJung-uk Kim=head1 SYNOPSIS 8e71b7053SJung-uk Kim 9b077aed3SPierre Pronchery=for openssl multiple includes 10e71b7053SJung-uk Kim 11e71b7053SJung-uk Kim #include <openssl/bio.h> 12e71b7053SJung-uk Kim #include <openssl/evp.h> 13e71b7053SJung-uk Kim 14e71b7053SJung-uk Kim const BIO_METHOD *BIO_f_md(void); 15e71b7053SJung-uk Kim int BIO_set_md(BIO *b, EVP_MD *md); 16e71b7053SJung-uk Kim int BIO_get_md(BIO *b, EVP_MD **mdp); 17e71b7053SJung-uk Kim int BIO_get_md_ctx(BIO *b, EVP_MD_CTX **mdcp); 18e71b7053SJung-uk Kim 19e71b7053SJung-uk Kim=head1 DESCRIPTION 20e71b7053SJung-uk Kim 21e71b7053SJung-uk KimBIO_f_md() returns the message digest BIO method. This is a filter 22*e0c4386eSCy SchubertBIO that digests any data passed through it. It is a BIO wrapper 23e71b7053SJung-uk Kimfor the digest routines EVP_DigestInit(), EVP_DigestUpdate() 24e71b7053SJung-uk Kimand EVP_DigestFinal(). 25e71b7053SJung-uk Kim 26e71b7053SJung-uk KimAny data written or read through a digest BIO using BIO_read_ex() and 27e71b7053SJung-uk KimBIO_write_ex() is digested. 28e71b7053SJung-uk Kim 29e71b7053SJung-uk KimBIO_gets(), if its B<size> parameter is large enough finishes the 30e71b7053SJung-uk Kimdigest calculation and returns the digest value. BIO_puts() is 31e71b7053SJung-uk Kimnot supported. 32e71b7053SJung-uk Kim 33e71b7053SJung-uk KimBIO_reset() reinitialises a digest BIO. 34e71b7053SJung-uk Kim 35e71b7053SJung-uk KimBIO_set_md() sets the message digest of BIO B<b> to B<md>: this 36e71b7053SJung-uk Kimmust be called to initialize a digest BIO before any data is 37e71b7053SJung-uk Kimpassed through it. It is a BIO_ctrl() macro. 38e71b7053SJung-uk Kim 39*e0c4386eSCy SchubertBIO_get_md() places a pointer to the digest BIOs digest method 40*e0c4386eSCy Schubertin B<mdp>. It is a BIO_ctrl() macro. 41e71b7053SJung-uk Kim 42e71b7053SJung-uk KimBIO_get_md_ctx() returns the digest BIOs context into B<mdcp>. 43e71b7053SJung-uk Kim 44e71b7053SJung-uk Kim=head1 NOTES 45e71b7053SJung-uk Kim 46e71b7053SJung-uk KimThe context returned by BIO_get_md_ctx() can be used in calls 47e71b7053SJung-uk Kimto EVP_DigestFinal() and also the signature routines EVP_SignFinal() 48e71b7053SJung-uk Kimand EVP_VerifyFinal(). 49e71b7053SJung-uk Kim 50e71b7053SJung-uk KimThe context returned by BIO_get_md_ctx() is an internal context 51e71b7053SJung-uk Kimstructure. Changes made to this context will affect the digest 52e71b7053SJung-uk KimBIO itself and the context pointer will become invalid when the digest 53e71b7053SJung-uk KimBIO is freed. 54e71b7053SJung-uk Kim 55e71b7053SJung-uk KimAfter the digest has been retrieved from a digest BIO it must be 56e71b7053SJung-uk Kimreinitialized by calling BIO_reset(), or BIO_set_md() before any more 57e71b7053SJung-uk Kimdata is passed through it. 58e71b7053SJung-uk Kim 59e71b7053SJung-uk KimIf an application needs to call BIO_gets() or BIO_puts() through 60e71b7053SJung-uk Kima chain containing digest BIOs then this can be done by prepending 61e71b7053SJung-uk Kima buffering BIO. 62e71b7053SJung-uk Kim 63e71b7053SJung-uk KimCalling BIO_get_md_ctx() will return the context and initialize the BIO 64e71b7053SJung-uk Kimstate. This allows applications to initialize the context externally 65e71b7053SJung-uk Kimif the standard calls such as BIO_set_md() are not sufficiently flexible. 66e71b7053SJung-uk Kim 67e71b7053SJung-uk Kim=head1 RETURN VALUES 68e71b7053SJung-uk Kim 69e71b7053SJung-uk KimBIO_f_md() returns the digest BIO method. 70e71b7053SJung-uk Kim 71e71b7053SJung-uk KimBIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and 72b077aed3SPierre Pronchery<=0 for failure. 73e71b7053SJung-uk Kim 74e71b7053SJung-uk Kim=head1 EXAMPLES 75e71b7053SJung-uk Kim 76e71b7053SJung-uk KimThe following example creates a BIO chain containing an SHA1 and MD5 77e71b7053SJung-uk Kimdigest BIO and passes the string "Hello World" through it. Error 78e71b7053SJung-uk Kimchecking has been omitted for clarity. 79e71b7053SJung-uk Kim 80e71b7053SJung-uk Kim BIO *bio, *mdtmp; 81e71b7053SJung-uk Kim char message[] = "Hello World"; 82e71b7053SJung-uk Kim 83e71b7053SJung-uk Kim bio = BIO_new(BIO_s_null()); 84e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 85e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_sha1()); 86e71b7053SJung-uk Kim /* 87e71b7053SJung-uk Kim * For BIO_push() we want to append the sink BIO and keep a note of 88e71b7053SJung-uk Kim * the start of the chain. 89e71b7053SJung-uk Kim */ 90e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 91e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 92e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_md5()); 93e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 94e71b7053SJung-uk Kim /* Note: mdtmp can now be discarded */ 95e71b7053SJung-uk Kim BIO_write(bio, message, strlen(message)); 96e71b7053SJung-uk Kim 97e71b7053SJung-uk KimThe next example digests data by reading through a chain instead: 98e71b7053SJung-uk Kim 99e71b7053SJung-uk Kim BIO *bio, *mdtmp; 100e71b7053SJung-uk Kim char buf[1024]; 101e71b7053SJung-uk Kim int rdlen; 102e71b7053SJung-uk Kim 103e71b7053SJung-uk Kim bio = BIO_new_file(file, "rb"); 104e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 105e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_sha1()); 106e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 107e71b7053SJung-uk Kim mdtmp = BIO_new(BIO_f_md()); 108e71b7053SJung-uk Kim BIO_set_md(mdtmp, EVP_md5()); 109e71b7053SJung-uk Kim bio = BIO_push(mdtmp, bio); 110e71b7053SJung-uk Kim do { 111e71b7053SJung-uk Kim rdlen = BIO_read(bio, buf, sizeof(buf)); 112e71b7053SJung-uk Kim /* Might want to do something with the data here */ 113e71b7053SJung-uk Kim } while (rdlen > 0); 114e71b7053SJung-uk Kim 115e71b7053SJung-uk KimThis next example retrieves the message digests from a BIO chain and 116e71b7053SJung-uk Kimoutputs them. This could be used with the examples above. 117e71b7053SJung-uk Kim 118e71b7053SJung-uk Kim BIO *mdtmp; 119e71b7053SJung-uk Kim unsigned char mdbuf[EVP_MAX_MD_SIZE]; 120e71b7053SJung-uk Kim int mdlen; 121e71b7053SJung-uk Kim int i; 122e71b7053SJung-uk Kim 123e71b7053SJung-uk Kim mdtmp = bio; /* Assume bio has previously been set up */ 124e71b7053SJung-uk Kim do { 125e71b7053SJung-uk Kim EVP_MD *md; 126e71b7053SJung-uk Kim 127e71b7053SJung-uk Kim mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD); 128e71b7053SJung-uk Kim if (!mdtmp) 129e71b7053SJung-uk Kim break; 130e71b7053SJung-uk Kim BIO_get_md(mdtmp, &md); 131b077aed3SPierre Pronchery printf("%s digest", OBJ_nid2sn(EVP_MD_get_type(md))); 132e71b7053SJung-uk Kim mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE); 133e71b7053SJung-uk Kim for (i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]); 134e71b7053SJung-uk Kim printf("\n"); 135e71b7053SJung-uk Kim mdtmp = BIO_next(mdtmp); 136e71b7053SJung-uk Kim } while (mdtmp); 137e71b7053SJung-uk Kim 138e71b7053SJung-uk Kim BIO_free_all(bio); 139e71b7053SJung-uk Kim 140e71b7053SJung-uk Kim=head1 BUGS 141e71b7053SJung-uk Kim 142e71b7053SJung-uk KimThe lack of support for BIO_puts() and the non standard behaviour of 143e71b7053SJung-uk KimBIO_gets() could be regarded as anomalous. It could be argued that BIO_gets() 144e71b7053SJung-uk Kimand BIO_puts() should be passed to the next BIO in the chain and digest 145e71b7053SJung-uk Kimthe data passed through and that digests should be retrieved using a 146e71b7053SJung-uk Kimseparate BIO_ctrl() call. 147e71b7053SJung-uk Kim 148e71b7053SJung-uk Kim=head1 HISTORY 149e71b7053SJung-uk Kim 150e71b7053SJung-uk KimBefore OpenSSL 1.0.0., the call to BIO_get_md_ctx() would only work if the 151e71b7053SJung-uk KimBIO was initialized first. 152e71b7053SJung-uk Kim 153e71b7053SJung-uk Kim=head1 COPYRIGHT 154e71b7053SJung-uk Kim 155b077aed3SPierre ProncheryCopyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved. 156e71b7053SJung-uk Kim 157b077aed3SPierre ProncheryLicensed under the Apache License 2.0 (the "License"). You may not use 158e71b7053SJung-uk Kimthis file except in compliance with the License. You can obtain a copy 159e71b7053SJung-uk Kimin the file LICENSE in the source distribution or at 160e71b7053SJung-uk KimL<https://www.openssl.org/source/license.html>. 161e71b7053SJung-uk Kim 162e71b7053SJung-uk Kim=cut 163