xref: /openbsd/lib/libcrypto/man/BIO_f_md.3 (revision 274d7c50)
1.\"	$OpenBSD: BIO_f_md.3,v 1.11 2019/06/06 01:06:58 schwarze Exp $
2.\"	OpenSSL a528d4f0 Oct 27 13:40:11 2015 -0400
3.\"
4.\" This file was written by Dr. Stephen Henson <steve@openssl.org>.
5.\" Copyright (c) 2000, 2006, 2009, 2016 The OpenSSL Project.
6.\" All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\"
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\"
15.\" 2. Redistributions in binary form must reproduce the above copyright
16.\"    notice, this list of conditions and the following disclaimer in
17.\"    the documentation and/or other materials provided with the
18.\"    distribution.
19.\"
20.\" 3. All advertising materials mentioning features or use of this
21.\"    software must display the following acknowledgment:
22.\"    "This product includes software developed by the OpenSSL Project
23.\"    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
24.\"
25.\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26.\"    endorse or promote products derived from this software without
27.\"    prior written permission. For written permission, please contact
28.\"    openssl-core@openssl.org.
29.\"
30.\" 5. Products derived from this software may not be called "OpenSSL"
31.\"    nor may "OpenSSL" appear in their names without prior written
32.\"    permission of the OpenSSL Project.
33.\"
34.\" 6. Redistributions of any form whatsoever must retain the following
35.\"    acknowledgment:
36.\"    "This product includes software developed by the OpenSSL Project
37.\"    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
38.\"
39.\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40.\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43.\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48.\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50.\" OF THE POSSIBILITY OF SUCH DAMAGE.
51.\"
52.Dd $Mdocdate: June 6 2019 $
53.Dt BIO_F_MD 3
54.Os
55.Sh NAME
56.Nm BIO_f_md ,
57.Nm BIO_set_md ,
58.Nm BIO_get_md ,
59.Nm BIO_get_md_ctx
60.Nd message digest BIO filter
61.Sh SYNOPSIS
62.In openssl/bio.h
63.In openssl/evp.h
64.Ft const BIO_METHOD *
65.Fo BIO_f_md
66.Fa void
67.Fc
68.Ft int
69.Fo BIO_set_md
70.Fa "BIO *b"
71.Fa "EVP_MD *md"
72.Fc
73.Ft int
74.Fo BIO_get_md
75.Fa "BIO *b"
76.Fa "EVP_MD **mdp"
77.Fc
78.Ft int
79.Fo BIO_get_md_ctx
80.Fa "BIO *b"
81.Fa "EVP_MD_CTX **mdcp"
82.Fc
83.Sh DESCRIPTION
84.Fn BIO_f_md
85returns the message digest BIO method.
86This is a filter BIO that digests any data passed through it.
87It is a BIO wrapper for the digest routines
88.Xr EVP_DigestInit 3 ,
89.Xr EVP_DigestUpdate 3 ,
90and
91.Xr EVP_DigestFinal 3 .
92.Pp
93Any data written or read through a digest BIO using
94.Xr BIO_read 3
95and
96.Xr BIO_write 3
97is digested.
98.Pp
99.Xr BIO_gets 3 ,
100if its
101.Sy size
102parameter is large enough,
103finishes the digest calculation and returns the digest value.
104.Xr BIO_puts 3
105is
106not supported.
107.Pp
108.Xr BIO_reset 3
109reinitialises a digest BIO.
110.Pp
111.Fn BIO_set_md
112sets the message digest of BIO
113.Fa b
114to
115.Fa md :
116this must be called to initialize a digest BIO
117before any data is passed through it.
118It is a
119.Xr BIO_ctrl 3
120macro.
121.Pp
122.Fn BIO_get_md
123places a pointer to the digest BIOs digest method in
124.Fa mdp .
125It is a
126.Xr BIO_ctrl 3
127macro.
128.Pp
129.Fn BIO_get_md_ctx
130returns the digest BIOs context in
131.Fa mdcp .
132.Pp
133The context returned by
134.Fn BIO_get_md_ctx
135can be used in calls to
136.Xr EVP_DigestFinal 3
137and also in the signature routines
138.Xr EVP_SignFinal 3
139and
140.Xr EVP_VerifyFinal 3 .
141.Pp
142The context returned by
143.Fn BIO_get_md_ctx
144is an internal context structure.
145Changes made to this context will affect the digest BIO itself, and
146the context pointer will become invalid when the digest BIO is freed.
147.Pp
148After the digest has been retrieved from a digest BIO,
149it must be reinitialized by calling
150.Xr BIO_reset 3
151or
152.Fn BIO_set_md
153before any more data is passed through it.
154.Pp
155If an application needs to call
156.Xr BIO_gets 3
157or
158.Xr BIO_puts 3
159through a chain containing digest BIOs,
160then this can be done by prepending a buffering BIO.
161.Pp
162Calling
163.Fn BIO_get_md_ctx
164will return the context and initialize the
165.Vt BIO
166state.
167This allows applications to initialize the context externally
168if the standard calls such as
169.Fn BIO_set_md
170are not sufficiently flexible.
171.Sh RETURN VALUES
172.Fn BIO_f_md
173returns the digest BIO method.
174.Pp
175.Fn BIO_set_md ,
176.Fn BIO_get_md ,
177and
178.Fn BIO_get_md_ctx
179return 1 for success and 0 for failure.
180.Sh EXAMPLES
181The following example creates a BIO chain containing a SHA-1 and MD5
182digest BIO and passes the string "Hello World" through it.
183Error checking has been omitted for clarity.
184.Bd -literal -offset 2n
185BIO *bio, *mdtmp;
186const char message[] = "Hello World";
187bio = BIO_new(BIO_s_null());
188mdtmp = BIO_new(BIO_f_md());
189BIO_set_md(mdtmp, EVP_sha1());
190/*
191 * For BIO_push() we want to append the sink BIO
192 * and keep a note of the start of the chain.
193 */
194bio = BIO_push(mdtmp, bio);
195mdtmp = BIO_new(BIO_f_md());
196BIO_set_md(mdtmp, EVP_md5());
197bio = BIO_push(mdtmp, bio);
198/* Note: mdtmp can now be discarded */
199BIO_write(bio, message, strlen(message));
200.Ed
201.Pp
202The next example digests data by reading through a chain instead:
203.Bd -literal -offset 2n
204BIO *bio, *mdtmp;
205char buf[1024];
206int rdlen;
207
208bio = BIO_new_file(file, "rb");
209mdtmp = BIO_new(BIO_f_md());
210BIO_set_md(mdtmp, EVP_sha1());
211bio = BIO_push(mdtmp, bio);
212mdtmp = BIO_new(BIO_f_md());
213BIO_set_md(mdtmp, EVP_md5());
214bio = BIO_push(mdtmp, bio);
215do {
216	rdlen = BIO_read(bio, buf, sizeof(buf));
217	/* Might want to do something with the data here */
218} while (rdlen > 0);
219.Ed
220.Pp
221This next example retrieves the message digests from a BIO chain
222and outputs them.
223This could be used with the examples above.
224.Bd -literal -offset 2n
225BIO *mdtmp;
226unsigned char mdbuf[EVP_MAX_MD_SIZE];
227int mdlen;
228int i;
229
230mdtmp = bio;	/* Assume bio has previously been set up */
231do {
232	EVP_MD *md;
233	mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
234	if (!mdtmp)
235		break;
236	BIO_get_md(mdtmp, &md);
237	printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
238	mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
239	for(i = 0; i < mdlen; i++)
240		printf(":%02X", mdbuf[i]);
241	printf("\en");
242	mdtmp = BIO_next(mdtmp);
243} while(mdtmp);
244BIO_free_all(bio);
245.Ed
246.Sh SEE ALSO
247.Xr BIO_new 3 ,
248.Xr EVP_DigestInit 3
249.Sh HISTORY
250.Fn BIO_f_md ,
251.Fn BIO_set_md ,
252and
253.Fn BIO_get_md
254first appeared in SSLeay 0.6.0.
255.Fn BIO_get_md_ctx
256first appeared in SSLeay 0.8.1.
257These functions have been available since
258.Ox 2.4 .
259.Pp
260Before OpenSSL 1.0.0, the call to
261.Fn BIO_get_md_ctx
262would only work if the
263.Vt BIO
264had been initialized, for example by calling
265.Fn BIO_set_md .
266.Sh BUGS
267The lack of support for
268.Xr BIO_puts 3
269and the non-standard behaviour of
270.Xr BIO_gets 3
271could be regarded as anomalous.
272It could be argued that
273.Xr BIO_gets 3
274and
275.Xr BIO_puts 3
276should be passed to the next BIO in the chain and digest the data
277passed through and that digests should be retrieved using a separate
278.Xr BIO_ctrl 3
279call.
280