More new BIO docs, correct some old ones.
[openssl.git] / doc / crypto / BIO_f_md.pod
1 =pod
2
3 =head1 NAME
4
5         BIO_f_md - message digest BIO
6
7 =head1 SYNOPSIS
8
9  #include <openssl/bio.h>
10  #include <openssl/evp.h>
11
12  BIO_METHOD *   BIO_f_md(void);
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
19 BIO_f_md() returns the message digest BIO method. This is a filter
20 BIO that digests any data passed through it, it is a BIO wrapper
21 for the digest routines EVP_DigestInit(), EVP_DigestUpdate()
22 and EVP_DigestFinal().
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 B<size> parameter is large enough finishes the
28 digest calculation and returns the digest value. BIO_puts() is
29 not supported.
30
31 BIO_reset() reinitializes a digest BIO.
32
33 BIO_set_md() sets the message digest of BIO B<b> to B<md>: this
34 must be called to initialise a digest BIO before any data is
35 passed through it. It is a BIO_ctrl() macro.
36
37 BIO_get_md() places the a pointer to the digest BIOs digest method
38 in B<mdp>, it is a BIO_ctrl() macro.
39
40 BIO_get_md_ctx() returns the digest BIOs context into B<mdcp>.
41
42
43
44 =head1 NOTES
45
46 The context returned by BIO_get_md_ctx() can be used in calls
47 to EVP_DigestFinal() and also the signature routines EVP_SignFinal()
48 and EVP_VerifyFinal().
49
50 The context returned by BIO_get_md_ctx() is an internal context
51 structure. Changes made to this context will affect the digest
52 BIO itself and the context pointer will become invalid when the digest
53 BIO is freed.
54
55 After the digest has been retrieved from a digest BIO it must be
56 reinitialized by calling BIO_reset(), or BIO_set_md() before any more
57 data is passed through it.
58
59 If an application needs to call BIO_gets() or BIO_puts() through
60 a chain containing digest BIOs then this can be done by prepending
61 a buffering BIO.
62
63 =head1 RETURN VALUES
64
65 BIO_f_md() returns the digest BIO method.
66
67 BIO_set_md(), BIO_get_md() and BIO_md_ctx() return 1 for success and
68 0 for failure.
69
70 =head1 EXAMPLES
71
72 The following example creates a BIO chain containing an SHA1 and MD5
73 digest BIO and passes the string "Hello World" through it. Error
74 checking has been omitted for clarity.
75
76  BIO *bio, *mdtmp;
77  char message[] = "Hello World";
78  bio = BIO_new(BIO_s_null());
79  mdtmp = BIO_new(BIO_f_md());
80  BIO_set_md(mdtmp, EVP_sha1());
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  bio = BIO_new_file(file, "rb");
97  mdtmp = BIO_new(BIO_f_md());
98  BIO_set_md(mdtmp, EVP_sha1());
99  bio = BIO_push(mdtmp, bio);
100  mdtmp = BIO_new(BIO_f_md());
101  BIO_set_md(mdtmp, EVP_md5());
102  bio = BIO_push(mdtmp, bio);
103  do {
104         rdlen = BIO_read(bio, buf, sizeof(buf));
105         /* Might want to do something with the data here */
106  } while(rdlen > 0);
107
108 This next example retrieves the message digests from a BIO chain and
109 outputs them. This could be used with the examples above.
110
111  BIO *mdtmp;
112  unsigned char mdbuf[EVP_MAX_MD_SIZE];
113  int mdlen;
114  int i;
115  mdtmp = bio;   /* Assume bio has previously been set up */
116  do {
117         EVP_MD *md;
118         mdtmp = BIO_find_type(mdtmp, BIO_TYPE_MD);
119         if(!mdtmp) break;
120         BIO_get_md(mdtmp, &md);
121         printf("%s digest", OBJ_nid2sn(EVP_MD_type(md)));
122         mdlen = BIO_gets(mdtmp, mdbuf, EVP_MAX_MD_SIZE);
123         for(i = 0; i < mdlen; i++) printf(":%02X", mdbuf[i]);
124         printf("\n");
125         mdtmp = BIO_next(mdtmp);
126  } while(mdtmp);
127
128  BIO_free_all(bio);
129
130 =head1 BUGS
131
132 The lack of support for BIO_puts() and the non standard behaviour of
133 BIO_gets() could be regarded as anomalous. It could be argued that BIO_gets()
134 and BIO_puts() should be passed to the next BIO in the chain and digest
135 the data passed through and that digests should be retrieved using a
136 separate BIO_ctrl() call.
137
138 =head1 SEE ALSO
139
140 TBA