1 /* crypto/evp/bio_ok.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
60 From: Arne Ansper <arne@cyber.ee>
64 I wrote function which took BIO* as argument, read data from it
65 and processed it. Then I wanted to store the input file in
66 encrypted form. OK I pushed BIO_f_cipher to the BIO stack
67 and everything was OK. BUT if user types wrong password
68 BIO_f_cipher outputs only garbage and my function crashes. Yes
69 I can and I should fix my function, but BIO_f_cipher is
70 easy way to add encryption support to many exisiting applications
71 and it's hard to debug and fix them all.
73 So I wanted another BIO which would catch the incorrect passwords and
74 file damages which cause garbage on BIO_f_cipher's output.
76 The easy way is to push the BIO_f_md and save the checksum at
77 the end of the file. However there are several problems with this
80 1) you must somehow separate checksum from actual data.
81 2) you need lot's of memory when reading the file, because you
82 must read to the end of the file and verify the checksum before
83 leting the application to read the data.
85 BIO_f_reliable tries to solve both problems, so that you can
86 read and write arbitraly long streams using only fixed amount
89 BIO_f_reliable splits data stream into blocks. Each block is prefixed
90 with it's length and suffixed with it's digest. So you need only
91 several Kbytes of memory to buffer single block before verifying
94 BIO_f_reliable goes futher and adds several important capabilities:
96 1) the digest of the block is computed over the whole stream
97 -- so nobody can rearrange the blocks or remove or replace them.
99 2) to detect invalid passwords right at the start BIO_f_reliable
100 adds special prefix to the stream. In order to avoid known plain-text
101 attacks this prefix is generated as follows:
103 *) digest is initialized with random seed instead of
105 *) same seed is written to ouput
106 *) well-known text is then hashed and the output
107 of the digest is also written to output.
109 reader can now read the seed from stream, hash the same string
110 and then compare the digest output.
112 Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I
113 initialy wrote and tested this code on x86 machine and wrote the
114 digests out in machine-dependent order :( There are people using
115 this code and I cannot change this easily without making existing
116 data files unreadable.
122 #include "cryptlib.h"
129 static int ok_write(BIO *h,char *buf,int num);
130 static int ok_read(BIO *h,char *buf,int size);
131 static long ok_ctrl(BIO *h,int cmd,long arg1,char *arg2);
132 static int ok_new(BIO *h);
133 static int ok_free(BIO *data);
134 static void sig_out(BIO* b);
135 static void sig_in(BIO* b);
136 static void block_out(BIO* b);
137 static void block_in(BIO* b);
139 static int ok_write();
140 static int ok_read();
141 static long ok_ctrl();
143 static int ok_free();
144 static void sig_out();
145 static void sig_in();
146 static void block_out();
147 static void block_in();
150 #define OK_BLOCK_SIZE (1024*4)
151 #define OK_BLOCK_BLOCK 4
152 #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE)
153 #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back."
157 ((unsigned long int)((((unsigned long int)(x) & 0x000000ffU) << 24) | \
158 (((unsigned long int)(x) & 0x0000ff00U) << 8) | \
159 (((unsigned long int)(x) & 0x00ff0000U) >> 8) | \
160 (((unsigned long int)(x) & 0xff000000U) >> 24)))
162 #define swapem(x) (x)
165 typedef struct ok_struct
171 int cont; /* <= 0 when finished */
174 int blockout; /* output block is ready */
175 int sigio; /* must process signature */
179 static BIO_METHOD methods_ok=
181 BIO_TYPE_CIPHER,"reliable",
191 BIO_METHOD *BIO_f_reliable()
196 static int ok_new(bi)
201 ctx=(BIO_OK_CTX *)Malloc(sizeof(BIO_OK_CTX));
202 if (ctx == NULL) return(0);
219 static int ok_free(a)
222 if (a == NULL) return(0);
223 memset(a->ptr,0,sizeof(BIO_OK_CTX));
231 static int ok_read(b,out,outl)
239 if (out == NULL) return(0);
240 ctx=(BIO_OK_CTX *)b->ptr;
242 if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
247 /* copy clean bytes to output buffer */
250 i=ctx->buf_len-ctx->buf_off;
251 if (i > outl) i=outl;
252 memcpy(out,&(ctx->buf[ctx->buf_off]),i);
258 /* all clean bytes are out */
259 if (ctx->buf_len == ctx->buf_off)
263 /* copy start of the next block into proper place */
264 if(ctx->buf_len_save- ctx->buf_off_save > 0)
266 ctx->buf_len= ctx->buf_len_save- ctx->buf_off_save;
267 memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]),
278 /* output buffer full -- cancel */
279 if (outl == 0) break;
281 /* no clean bytes in buffer -- fill it */
282 n=IOBS- ctx->buf_len;
283 i=BIO_read(b->next_bio,&(ctx->buf[ctx->buf_len]),n);
285 if (i <= 0) break; /* nothing new */
289 /* no signature yet -- check if we got one */
290 if (ctx->sigio == 1) sig_in(b);
292 /* signature ok -- check if we got block */
293 if (ctx->sigio == 0) block_in(b);
295 /* invalid block -- cancel */
296 if (ctx->cont <= 0) break;
300 BIO_clear_retry_flags(b);
301 BIO_copy_next_retry(b);
305 static int ok_write(b,in,inl)
313 ctx=(BIO_OK_CTX *)b->ptr;
316 if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) return(0);
318 if(ctx->sigio) sig_out(b);
321 BIO_clear_retry_flags(b);
322 n=ctx->buf_len-ctx->buf_off;
323 while (ctx->blockout && n > 0)
325 i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
328 BIO_copy_next_retry(b);
329 if(!BIO_should_retry(b))
337 /* at this point all pending data has been written */
339 if (ctx->buf_len == ctx->buf_off)
341 ctx->buf_len=OK_BLOCK_BLOCK;
345 if ((in == NULL) || (inl <= 0)) return(0);
347 n= (inl+ ctx->buf_len > OK_BLOCK_SIZE+ OK_BLOCK_BLOCK) ?
348 OK_BLOCK_SIZE+ OK_BLOCK_BLOCK- ctx->buf_len : inl;
350 memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])),(unsigned char *)in,n);
355 if(ctx->buf_len >= OK_BLOCK_SIZE+ OK_BLOCK_BLOCK)
361 BIO_clear_retry_flags(b);
362 BIO_copy_next_retry(b);
366 static long ok_ctrl(b,cmd,num,ptr)
378 ctx=(BIO_OK_CTX *)b->ptr;
391 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
393 case BIO_CTRL_EOF: /* More to read */
397 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
399 case BIO_CTRL_PENDING: /* More to read in buffer */
400 case BIO_CTRL_WPENDING: /* More to read in buffer */
401 ret=ctx->blockout ? ctx->buf_len-ctx->buf_off : 0;
403 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
406 /* do a final write */
407 if(ctx->blockout == 0)
410 while (ctx->blockout)
412 i=ok_write(b,NULL,0);
421 ctx->buf_off=ctx->buf_len=0;
424 /* Finally flush the underlying BIO */
425 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
427 case BIO_C_DO_STATE_MACHINE:
428 BIO_clear_retry_flags(b);
429 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
430 BIO_copy_next_retry(b);
437 EVP_DigestInit(&(ctx->md),md);
444 *ppmd=ctx->md.digest;
450 ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
456 static void longswap(char* ptr, int len)
461 for(i= 0;i < len;i+= 4){
462 *((unsigned long *)&(ptr[i]))= swapem(*((unsigned long *)&(ptr[i])));
467 static void sig_out(BIO* b)
472 ctx=(BIO_OK_CTX *)b->ptr;
475 if(ctx->buf_len+ 2* md->digest->md_size > OK_BLOCK_SIZE) return;
477 EVP_DigestInit(md, md->digest);
478 RAND_bytes(&(md->md.base[0]), md->digest->md_size);
479 memcpy(&(ctx->buf[ctx->buf_len]), &(md->md.base[0]), md->digest->md_size);
480 longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size);
481 ctx->buf_len+= md->digest->md_size;
483 EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
484 md->digest->final(&(ctx->buf[ctx->buf_len]), &(md->md.base[0]));
485 ctx->buf_len+= md->digest->md_size;
490 static void sig_in(BIO* b)
494 unsigned char tmp[EVP_MAX_MD_SIZE];
497 ctx=(BIO_OK_CTX *)b->ptr;
500 if(ctx->buf_len- ctx->buf_off < 2* md->digest->md_size) return;
502 EVP_DigestInit(md, md->digest);
503 memcpy(&(md->md.base[0]), &(ctx->buf[ctx->buf_off]), md->digest->md_size);
504 longswap(&(md->md.base[0]), md->digest->md_size);
505 ctx->buf_off+= md->digest->md_size;
507 EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN));
508 md->digest->final(tmp, &(md->md.base[0]));
509 ret= memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0;
510 ctx->buf_off+= md->digest->md_size;
514 if(ctx->buf_len != ctx->buf_off)
516 memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), ctx->buf_len- ctx->buf_off);
518 ctx->buf_len-= ctx->buf_off;
527 static void block_out(BIO* b)
533 ctx=(BIO_OK_CTX *)b->ptr;
536 tl= ctx->buf_len- OK_BLOCK_BLOCK;
538 memcpy(ctx->buf, &tl, OK_BLOCK_BLOCK);
540 EVP_DigestUpdate(md, &(ctx->buf[OK_BLOCK_BLOCK]), tl);
541 md->digest->final(&(ctx->buf[ctx->buf_len]), &(md->md.base[0]));
542 ctx->buf_len+= md->digest->md_size;
546 static void block_in(BIO* b)
551 unsigned char tmp[EVP_MAX_MD_SIZE];
553 ctx=(BIO_OK_CTX *)b->ptr;
556 memcpy(&tl, ctx->buf, OK_BLOCK_BLOCK);
558 if (ctx->buf_len < tl+ OK_BLOCK_BLOCK+ md->digest->md_size) return;
560 EVP_DigestUpdate(md, &(ctx->buf[OK_BLOCK_BLOCK]), tl);
561 md->digest->final(tmp, &(md->md.base[0]));
562 if(memcmp(&(ctx->buf[tl+ OK_BLOCK_BLOCK]), tmp, md->digest->md_size) == 0)
564 /* there might be parts from next block lurking around ! */
565 ctx->buf_off_save= tl+ OK_BLOCK_BLOCK+ md->digest->md_size;
566 ctx->buf_len_save= ctx->buf_len;
567 ctx->buf_off= OK_BLOCK_BLOCK;
568 ctx->buf_len= tl+ OK_BLOCK_BLOCK;