381ca599a74560a724af1b386b44db3d09e54f3f
[openssl.git] / crypto / evp / bio_md.c
1 /* crypto/evp/bio_md.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3  * All rights reserved.
4  *
5  * This package is an SSL implementation written
6  * by Eric Young (eay@cryptsoft.com).
7  * The implementation was written so as to conform with Netscapes SSL.
8  *
9  * This library is free for commercial and non-commercial use as long as
10  * the following conditions are aheared to.  The following conditions
11  * apply to all code found in this distribution, be it the RC4, RSA,
12  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13  * included with this distribution is covered by the same copyright terms
14  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15  *
16  * Copyright remains Eric Young's, and as such any Copyright notices in
17  * the code are not to be removed.
18  * If this package is used in a product, Eric Young should be given attribution
19  * as the author of the parts of the library used.
20  * This can be in the form of a textual message at program startup or
21  * in documentation (online or textual) provided with the package.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  * 3. All advertising materials mentioning features or use of this software
32  *    must display the following acknowledgement:
33  *    "This product includes cryptographic software written by
34  *     Eric Young (eay@cryptsoft.com)"
35  *    The word 'cryptographic' can be left out if the rouines from the library
36  *    being used are not cryptographic related :-).
37  * 4. If you include any Windows specific code (or a derivative thereof) from
38  *    the apps directory (application code) you must include an acknowledgement:
39  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  *
53  * The licence and distribution terms for any publically available version or
54  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55  * copied and put under another distribution licence
56  * [including the GNU Public Licence.]
57  */
58
59 #include <stdio.h>
60 #include <errno.h>
61 #include "internal/cryptlib.h"
62 #include <openssl/buffer.h>
63 #include <openssl/evp.h>
64 #include "internal/evp_int.h"
65 #include "evp_locl.h"
66
67 /*
68  * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
69  */
70
71 static int md_write(BIO *h, char const *buf, int num);
72 static int md_read(BIO *h, char *buf, int size);
73 /*
74  * static int md_puts(BIO *h, const char *str);
75  */
76 static int md_gets(BIO *h, char *str, int size);
77 static long md_ctrl(BIO *h, int cmd, long arg1, void *arg2);
78 static int md_new(BIO *h);
79 static int md_free(BIO *data);
80 static long md_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
81
82 static BIO_METHOD methods_md = {
83     BIO_TYPE_MD, "message digest",
84     md_write,
85     md_read,
86     NULL,                       /* md_puts, */
87     md_gets,
88     md_ctrl,
89     md_new,
90     md_free,
91     md_callback_ctrl,
92 };
93
94 BIO_METHOD *BIO_f_md(void)
95 {
96     return (&methods_md);
97 }
98
99 static int md_new(BIO *bi)
100 {
101     EVP_MD_CTX *ctx;
102
103     ctx = EVP_MD_CTX_create();
104     if (ctx == NULL)
105         return (0);
106
107     bi->init = 0;
108     bi->ptr = (char *)ctx;
109     bi->flags = 0;
110     return (1);
111 }
112
113 static int md_free(BIO *a)
114 {
115     if (a == NULL)
116         return (0);
117     EVP_MD_CTX_destroy(a->ptr);
118     a->ptr = NULL;
119     a->init = 0;
120     a->flags = 0;
121     return (1);
122 }
123
124 static int md_read(BIO *b, char *out, int outl)
125 {
126     int ret = 0;
127     EVP_MD_CTX *ctx;
128
129     if (out == NULL)
130         return (0);
131     ctx = b->ptr;
132
133     if ((ctx == NULL) || (b->next_bio == NULL))
134         return (0);
135
136     ret = BIO_read(b->next_bio, out, outl);
137     if (b->init) {
138         if (ret > 0) {
139             if (EVP_DigestUpdate(ctx, (unsigned char *)out,
140                                  (unsigned int)ret) <= 0)
141                 return (-1);
142         }
143     }
144     BIO_clear_retry_flags(b);
145     BIO_copy_next_retry(b);
146     return (ret);
147 }
148
149 static int md_write(BIO *b, const char *in, int inl)
150 {
151     int ret = 0;
152     EVP_MD_CTX *ctx;
153
154     if ((in == NULL) || (inl <= 0))
155         return (0);
156     ctx = b->ptr;
157
158     if ((ctx != NULL) && (b->next_bio != NULL))
159         ret = BIO_write(b->next_bio, in, inl);
160     if (b->init) {
161         if (ret > 0) {
162             if (!EVP_DigestUpdate(ctx, (const unsigned char *)in,
163                                   (unsigned int)ret)) {
164                 BIO_clear_retry_flags(b);
165                 return 0;
166             }
167         }
168     }
169     if (b->next_bio != NULL) {
170         BIO_clear_retry_flags(b);
171         BIO_copy_next_retry(b);
172     }
173     return (ret);
174 }
175
176 static long md_ctrl(BIO *b, int cmd, long num, void *ptr)
177 {
178     EVP_MD_CTX *ctx, *dctx, **pctx;
179     const EVP_MD **ppmd;
180     EVP_MD *md;
181     long ret = 1;
182     BIO *dbio;
183
184     ctx = b->ptr;
185
186     switch (cmd) {
187     case BIO_CTRL_RESET:
188         if (b->init)
189             ret = EVP_DigestInit_ex(ctx, ctx->digest, NULL);
190         else
191             ret = 0;
192         if (ret > 0)
193             ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
194         break;
195     case BIO_C_GET_MD:
196         if (b->init) {
197             ppmd = ptr;
198             *ppmd = ctx->digest;
199         } else
200             ret = 0;
201         break;
202     case BIO_C_GET_MD_CTX:
203         pctx = ptr;
204         *pctx = ctx;
205         b->init = 1;
206         break;
207     case BIO_C_SET_MD_CTX:
208         if (b->init)
209             b->ptr = ptr;
210         else
211             ret = 0;
212         break;
213     case BIO_C_DO_STATE_MACHINE:
214         BIO_clear_retry_flags(b);
215         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
216         BIO_copy_next_retry(b);
217         break;
218
219     case BIO_C_SET_MD:
220         md = ptr;
221         ret = EVP_DigestInit_ex(ctx, md, NULL);
222         if (ret > 0)
223             b->init = 1;
224         break;
225     case BIO_CTRL_DUP:
226         dbio = ptr;
227         dctx = dbio->ptr;
228         if (!EVP_MD_CTX_copy_ex(dctx, ctx))
229             return 0;
230         b->init = 1;
231         break;
232     default:
233         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
234         break;
235     }
236     return (ret);
237 }
238
239 static long md_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
240 {
241     long ret = 1;
242
243     if (b->next_bio == NULL)
244         return (0);
245     switch (cmd) {
246     default:
247         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
248         break;
249     }
250     return (ret);
251 }
252
253 static int md_gets(BIO *bp, char *buf, int size)
254 {
255     EVP_MD_CTX *ctx;
256     unsigned int ret;
257
258     ctx = bp->ptr;
259     if (size < ctx->digest->md_size)
260         return (0);
261     if (EVP_DigestFinal_ex(ctx, (unsigned char *)buf, &ret) <= 0)
262         return -1;
263
264     return ((int)ret);
265 }
266
267 /*-
268 static int md_puts(bp,str)
269 BIO *bp;
270 char *str;
271         {
272         return(-1);
273         }
274 */