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