1BIO_F_MD(3ossl)                     OpenSSL                    BIO_F_MD(3ossl)
2
3
4

NAME

6       BIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO
7       filter
8

SYNOPSIS

10        #include <openssl/bio.h>
11        #include <openssl/evp.h>
12
13        const BIO_METHOD *BIO_f_md(void);
14        int BIO_set_md(BIO *b, EVP_MD *md);
15        int BIO_get_md(BIO *b, EVP_MD **mdp);
16        int BIO_get_md_ctx(BIO *b, EVP_MD_CTX **mdcp);
17

DESCRIPTION

19       BIO_f_md() returns the message digest BIO method. This is a filter BIO
20       that digests any data passed through it, it is a BIO wrapper for the
21       digest routines EVP_DigestInit(), EVP_DigestUpdate() and
22       EVP_DigestFinal().
23
24       Any data written or read through a digest BIO using BIO_read_ex() and
25       BIO_write_ex() is digested.
26
27       BIO_gets(), if its size parameter is large enough finishes the digest
28       calculation and returns the digest value. BIO_puts() is not supported.
29
30       BIO_reset() reinitialises a digest BIO.
31
32       BIO_set_md() sets the message digest of BIO b to md: this must be
33       called to initialize a digest BIO before any data is passed through it.
34       It is a BIO_ctrl() macro.
35
36       BIO_get_md() places the a pointer to the digest BIOs digest method in
37       mdp, it is a BIO_ctrl() macro.
38
39       BIO_get_md_ctx() returns the digest BIOs context into mdcp.
40

NOTES

42       The context returned by BIO_get_md_ctx() can be used in calls to
43       EVP_DigestFinal() and also the signature routines EVP_SignFinal() and
44       EVP_VerifyFinal().
45
46       The context returned by BIO_get_md_ctx() is an internal context
47       structure. Changes made to this context will affect the digest BIO
48       itself and the context pointer will become invalid when the digest BIO
49       is freed.
50
51       After the digest has been retrieved from a digest BIO it must be
52       reinitialized by calling BIO_reset(), or BIO_set_md() before any more
53       data is passed through it.
54
55       If an application needs to call BIO_gets() or BIO_puts() through a
56       chain containing digest BIOs then this can be done by prepending a
57       buffering BIO.
58
59       Calling BIO_get_md_ctx() will return the context and initialize the BIO
60       state. This allows applications to initialize the context externally if
61       the standard calls such as BIO_set_md() are not sufficiently flexible.
62

RETURN VALUES

64       BIO_f_md() returns the digest BIO method.
65
66       BIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and
67       <=0 for failure.
68

EXAMPLES

70       The following example creates a BIO chain containing an SHA1 and MD5
71       digest BIO and passes the string "Hello World" through it. Error
72       checking has been omitted for clarity.
73
74        BIO *bio, *mdtmp;
75        char message[] = "Hello World";
76
77        bio = BIO_new(BIO_s_null());
78        mdtmp = BIO_new(BIO_f_md());
79        BIO_set_md(mdtmp, EVP_sha1());
80        /*
81         * For BIO_push() we want to append the sink BIO and keep a note of
82         * the start of the chain.
83         */
84        bio = BIO_push(mdtmp, bio);
85        mdtmp = BIO_new(BIO_f_md());
86        BIO_set_md(mdtmp, EVP_md5());
87        bio = BIO_push(mdtmp, bio);
88        /* Note: mdtmp can now be discarded */
89        BIO_write(bio, message, strlen(message));
90
91       The next example digests data by reading through a chain instead:
92
93        BIO *bio, *mdtmp;
94        char buf[1024];
95        int rdlen;
96
97        bio = BIO_new_file(file, "rb");
98        mdtmp = BIO_new(BIO_f_md());
99        BIO_set_md(mdtmp, EVP_sha1());
100        bio = BIO_push(mdtmp, bio);
101        mdtmp = BIO_new(BIO_f_md());
102        BIO_set_md(mdtmp, EVP_md5());
103        bio = BIO_push(mdtmp, bio);
104        do {
105            rdlen = BIO_read(bio, buf, sizeof(buf));
106            /* Might want to do something with the data here */
107        } while (rdlen > 0);
108
109       This next example retrieves the message digests from a BIO chain and
110       outputs them. This could be used with the examples above.
111
112        BIO *mdtmp;
113        unsigned char mdbuf[EVP_MAX_MD_SIZE];
114        int mdlen;
115        int i;
116
117        mdtmp = bio;   /* Assume bio has previously been set up */
118        do {
119            EVP_MD *md;
120
121            mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
122            if (!mdtmp)
123                break;
124            BIO_get_md(mdtmp, &md);
125            printf("%s digest", OBJ_nid2sn(EVP_MD_get_type(md)));
126            mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
127            for (i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]);
128            printf("\n");
129            mdtmp = BIO_next(mdtmp);
130        } while (mdtmp);
131
132        BIO_free_all(bio);
133

BUGS

135       The lack of support for BIO_puts() and the non standard behaviour of
136       BIO_gets() could be regarded as anomalous. It could be argued that
137       BIO_gets() and BIO_puts() should be passed to the next BIO in the chain
138       and digest the data passed through and that digests should be retrieved
139       using a separate BIO_ctrl() call.
140

HISTORY

142       Before OpenSSL 1.0.0., the call to BIO_get_md_ctx() would only work if
143       the BIO was initialized first.
144
146       Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
147
148       Licensed under the Apache License 2.0 (the "License").  You may not use
149       this file except in compliance with the License.  You can obtain a copy
150       in the file LICENSE in the source distribution or at
151       <https://www.openssl.org/source/license.html>.
152
153
154
1553.0.5                             2022-11-01                   BIO_F_MD(3ossl)
Impressum