1BIO_f_md(3) OpenSSL BIO_f_md(3)
2
3
4
6 BIO_f_md, BIO_set_md, BIO_get_md, BIO_get_md_ctx - message digest BIO
7 filter
8
10 #include <openssl/bio.h>
11 #include <openssl/evp.h>
12
13 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
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 EVP_DigestFi‐
22 nal().
23
24 Any data written or read through a digest BIO using BIO_read() and
25 BIO_write() 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
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 struc‐
47 ture. Changes made to this context will affect the digest BIO itself
48 and the context pointer will become invalid when the digest BIO is
49 freed.
50
51 After the digest has been retrieved from a digest BIO it must be reini‐
52 tialized by calling BIO_reset(), or BIO_set_md() before any more data
53 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
60 BIO_f_md() returns the digest BIO method.
61
62 BIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and 0
63 for failure.
64
66 The following example creates a BIO chain containing an SHA1 and MD5
67 digest BIO and passes the string "Hello World" through it. Error check‐
68 ing has been omitted for clarity.
69
70 BIO *bio, *mdtmp;
71 char message[] = "Hello World";
72 bio = BIO_new(BIO_s_null());
73 mdtmp = BIO_new(BIO_f_md());
74 BIO_set_md(mdtmp, EVP_sha1());
75 /* For BIO_push() we want to append the sink BIO and keep a note of
76 * the start of the chain.
77 */
78 bio = BIO_push(mdtmp, bio);
79 mdtmp = BIO_new(BIO_f_md());
80 BIO_set_md(mdtmp, EVP_md5());
81 bio = BIO_push(mdtmp, bio);
82 /* Note: mdtmp can now be discarded */
83 BIO_write(bio, message, strlen(message));
84
85 The next example digests data by reading through a chain instead:
86
87 BIO *bio, *mdtmp;
88 char buf[1024];
89 int rdlen;
90 bio = BIO_new_file(file, "rb");
91 mdtmp = BIO_new(BIO_f_md());
92 BIO_set_md(mdtmp, EVP_sha1());
93 bio = BIO_push(mdtmp, bio);
94 mdtmp = BIO_new(BIO_f_md());
95 BIO_set_md(mdtmp, EVP_md5());
96 bio = BIO_push(mdtmp, bio);
97 do {
98 rdlen = BIO_read(bio, buf, sizeof(buf));
99 /* Might want to do something with the data here */
100 } while(rdlen > 0);
101
102 This next example retrieves the message digests from a BIO chain and
103 outputs them. This could be used with the examples above.
104
105 BIO *mdtmp;
106 unsigned char mdbuf[EVP_MAX_MD_SIZE];
107 int mdlen;
108 int i;
109 mdtmp = bio; /* Assume bio has previously been set up */
110 do {
111 EVP_MD *md;
112 mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
113 if(!mdtmp) break;
114 BIO_get_md(mdtmp, &md);
115 printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
116 mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
117 for(i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]);
118 printf("\n");
119 mdtmp = BIO_next(mdtmp);
120 } while(mdtmp);
121
122 BIO_free_all(bio);
123
125 The lack of support for BIO_puts() and the non standard behaviour of
126 BIO_gets() could be regarded as anomalous. It could be argued that
127 BIO_gets() and BIO_puts() should be passed to the next BIO in the chain
128 and digest the data passed through and that digests should be retrieved
129 using a separate BIO_ctrl() call.
130
132 TBA
133
134
135
1360.9.8b 2001-09-07 BIO_f_md(3)