os/ossrv/ssl/libcrypto/src/crypto/evp/bio_enc.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* crypto/evp/bio_enc.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
 
sl@0
    62
#include <stdio.h>
sl@0
    63
#include <errno.h>
sl@0
    64
#include "cryptlib.h"
sl@0
    65
#include <openssl/buffer.h>
sl@0
    66
#include <openssl/evp.h>
sl@0
    67
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    68
#include "libcrypto_wsd_macros.h"
sl@0
    69
#include "libcrypto_wsd.h"
sl@0
    70
#endif
sl@0
    71
sl@0
    72
static int enc_write(BIO *h, const char *buf, int num);
sl@0
    73
static int enc_read(BIO *h, char *buf, int size);
sl@0
    74
/*static int enc_puts(BIO *h, const char *str); */
sl@0
    75
/*static int enc_gets(BIO *h, char *str, int size); */
sl@0
    76
static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
sl@0
    77
static int enc_new(BIO *h);
sl@0
    78
static int enc_free(BIO *data);
sl@0
    79
static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
sl@0
    80
#define ENC_BLOCK_SIZE	(1024*4)
sl@0
    81
#define BUF_OFFSET	(EVP_MAX_BLOCK_LENGTH*2)
sl@0
    82
sl@0
    83
typedef struct enc_struct
sl@0
    84
	{
sl@0
    85
	int buf_len;
sl@0
    86
	int buf_off;
sl@0
    87
	int cont;		/* <= 0 when finished */
sl@0
    88
	int finished;
sl@0
    89
	int ok;			/* bad decrypt */
sl@0
    90
	EVP_CIPHER_CTX cipher;
sl@0
    91
	/* buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate
sl@0
    92
	 * can return up to a block more data than is presented to it
sl@0
    93
	 */
sl@0
    94
	char buf[ENC_BLOCK_SIZE+BUF_OFFSET+2];
sl@0
    95
	} BIO_ENC_CTX;
sl@0
    96
sl@0
    97
#ifndef EMULATOR
sl@0
    98
static BIO_METHOD methods_enc=
sl@0
    99
	{
sl@0
   100
	BIO_TYPE_CIPHER,"cipher",
sl@0
   101
	enc_write,
sl@0
   102
	enc_read,
sl@0
   103
	NULL, /* enc_puts, */
sl@0
   104
	NULL, /* enc_gets, */
sl@0
   105
	enc_ctrl,
sl@0
   106
	enc_new,
sl@0
   107
	enc_free,
sl@0
   108
	enc_callback_ctrl,
sl@0
   109
	};
sl@0
   110
#else
sl@0
   111
GET_STATIC_VAR_FROM_TLS(methods_enc,bio_enc,BIO_METHOD)
sl@0
   112
#define methods_enc (*GET_WSD_VAR_NAME(methods_enc,bio_enc, s)())
sl@0
   113
const BIO_METHOD temp_s_methods_enc=
sl@0
   114
	{
sl@0
   115
	BIO_TYPE_CIPHER,"cipher",
sl@0
   116
	enc_write,
sl@0
   117
	enc_read,
sl@0
   118
	NULL, /* enc_puts, */
sl@0
   119
	NULL, /* enc_gets, */
sl@0
   120
	enc_ctrl,
sl@0
   121
	enc_new,
sl@0
   122
	enc_free,
sl@0
   123
	enc_callback_ctrl,
sl@0
   124
	};
sl@0
   125
sl@0
   126
#endif	
sl@0
   127
sl@0
   128
EXPORT_C BIO_METHOD *BIO_f_cipher(void)
sl@0
   129
	{
sl@0
   130
	return(&methods_enc);
sl@0
   131
	}
sl@0
   132
sl@0
   133
static int enc_new(BIO *bi)
sl@0
   134
	{
sl@0
   135
	BIO_ENC_CTX *ctx;
sl@0
   136
sl@0
   137
	ctx=(BIO_ENC_CTX *)OPENSSL_malloc(sizeof(BIO_ENC_CTX));
sl@0
   138
	if (ctx == NULL) return(0);
sl@0
   139
	EVP_CIPHER_CTX_init(&ctx->cipher);
sl@0
   140
sl@0
   141
	ctx->buf_len=0;
sl@0
   142
	ctx->buf_off=0;
sl@0
   143
	ctx->cont=1;
sl@0
   144
	ctx->finished=0;
sl@0
   145
	ctx->ok=1;
sl@0
   146
sl@0
   147
	bi->init=0;
sl@0
   148
	bi->ptr=(char *)ctx;
sl@0
   149
	bi->flags=0;
sl@0
   150
	return(1);
sl@0
   151
	}
sl@0
   152
sl@0
   153
static int enc_free(BIO *a)
sl@0
   154
	{
sl@0
   155
	BIO_ENC_CTX *b;
sl@0
   156
sl@0
   157
	if (a == NULL) return(0);
sl@0
   158
	b=(BIO_ENC_CTX *)a->ptr;
sl@0
   159
	EVP_CIPHER_CTX_cleanup(&(b->cipher));
sl@0
   160
	OPENSSL_cleanse(a->ptr,sizeof(BIO_ENC_CTX));
sl@0
   161
	OPENSSL_free(a->ptr);
sl@0
   162
	a->ptr=NULL;
sl@0
   163
	a->init=0;
sl@0
   164
	a->flags=0;
sl@0
   165
	return(1);
sl@0
   166
	}
sl@0
   167
	
sl@0
   168
static int enc_read(BIO *b, char *out, int outl)
sl@0
   169
	{
sl@0
   170
	int ret=0,i;
sl@0
   171
	BIO_ENC_CTX *ctx;
sl@0
   172
sl@0
   173
	if (out == NULL) return(0);
sl@0
   174
	ctx=(BIO_ENC_CTX *)b->ptr;
sl@0
   175
sl@0
   176
	if ((ctx == NULL) || (b->next_bio == NULL)) return(0);
sl@0
   177
sl@0
   178
	/* First check if there are bytes decoded/encoded */
sl@0
   179
	if (ctx->buf_len > 0)
sl@0
   180
		{
sl@0
   181
		i=ctx->buf_len-ctx->buf_off;
sl@0
   182
		if (i > outl) i=outl;
sl@0
   183
		memcpy(out,&(ctx->buf[ctx->buf_off]),i);
sl@0
   184
		ret=i;
sl@0
   185
		out+=i;
sl@0
   186
		outl-=i;
sl@0
   187
		ctx->buf_off+=i;
sl@0
   188
		if (ctx->buf_len == ctx->buf_off)
sl@0
   189
			{
sl@0
   190
			ctx->buf_len=0;
sl@0
   191
			ctx->buf_off=0;
sl@0
   192
			}
sl@0
   193
		}
sl@0
   194
sl@0
   195
	/* At this point, we have room of outl bytes and an empty
sl@0
   196
	 * buffer, so we should read in some more. */
sl@0
   197
sl@0
   198
	while (outl > 0)
sl@0
   199
		{
sl@0
   200
		if (ctx->cont <= 0) break;
sl@0
   201
sl@0
   202
		/* read in at IV offset, read the EVP_Cipher
sl@0
   203
		 * documentation about why */
sl@0
   204
		i=BIO_read(b->next_bio,&(ctx->buf[BUF_OFFSET]),ENC_BLOCK_SIZE);
sl@0
   205
sl@0
   206
		if (i <= 0)
sl@0
   207
			{
sl@0
   208
			/* Should be continue next time we are called? */
sl@0
   209
			if (!BIO_should_retry(b->next_bio))
sl@0
   210
				{
sl@0
   211
				ctx->cont=i;
sl@0
   212
				i=EVP_CipherFinal_ex(&(ctx->cipher),
sl@0
   213
					(unsigned char *)ctx->buf,
sl@0
   214
					&(ctx->buf_len));
sl@0
   215
				ctx->ok=i;
sl@0
   216
				ctx->buf_off=0;
sl@0
   217
				}
sl@0
   218
			else 
sl@0
   219
				{
sl@0
   220
				ret=(ret == 0)?i:ret;
sl@0
   221
				break;
sl@0
   222
				}
sl@0
   223
			}
sl@0
   224
		else
sl@0
   225
			{
sl@0
   226
			EVP_CipherUpdate(&(ctx->cipher),
sl@0
   227
				(unsigned char *)ctx->buf,&ctx->buf_len,
sl@0
   228
				(unsigned char *)&(ctx->buf[BUF_OFFSET]),i);
sl@0
   229
			ctx->cont=1;
sl@0
   230
			/* Note: it is possible for EVP_CipherUpdate to
sl@0
   231
			 * decrypt zero bytes because this is or looks like
sl@0
   232
			 * the final block: if this happens we should retry
sl@0
   233
			 * and either read more data or decrypt the final
sl@0
   234
			 * block
sl@0
   235
			 */
sl@0
   236
			if(ctx->buf_len == 0) continue;
sl@0
   237
			}
sl@0
   238
sl@0
   239
		if (ctx->buf_len <= outl)
sl@0
   240
			i=ctx->buf_len;
sl@0
   241
		else
sl@0
   242
			i=outl;
sl@0
   243
		if (i <= 0) break;
sl@0
   244
		memcpy(out,ctx->buf,i);
sl@0
   245
		ret+=i;
sl@0
   246
		ctx->buf_off=i;
sl@0
   247
		outl-=i;
sl@0
   248
		out+=i;
sl@0
   249
		}
sl@0
   250
sl@0
   251
	BIO_clear_retry_flags(b);
sl@0
   252
	BIO_copy_next_retry(b);
sl@0
   253
	return((ret == 0)?ctx->cont:ret);
sl@0
   254
	}
sl@0
   255
sl@0
   256
static int enc_write(BIO *b, const char *in, int inl)
sl@0
   257
	{
sl@0
   258
	int ret=0,n,i;
sl@0
   259
	BIO_ENC_CTX *ctx;
sl@0
   260
sl@0
   261
	ctx=(BIO_ENC_CTX *)b->ptr;
sl@0
   262
	ret=inl;
sl@0
   263
sl@0
   264
	BIO_clear_retry_flags(b);
sl@0
   265
	n=ctx->buf_len-ctx->buf_off;
sl@0
   266
	while (n > 0)
sl@0
   267
		{
sl@0
   268
		i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
sl@0
   269
		if (i <= 0)
sl@0
   270
			{
sl@0
   271
			BIO_copy_next_retry(b);
sl@0
   272
			return(i);
sl@0
   273
			}
sl@0
   274
		ctx->buf_off+=i;
sl@0
   275
		n-=i;
sl@0
   276
		}
sl@0
   277
	/* at this point all pending data has been written */
sl@0
   278
sl@0
   279
	if ((in == NULL) || (inl <= 0)) return(0);
sl@0
   280
sl@0
   281
	ctx->buf_off=0;
sl@0
   282
	while (inl > 0)
sl@0
   283
		{
sl@0
   284
		n=(inl > ENC_BLOCK_SIZE)?ENC_BLOCK_SIZE:inl;
sl@0
   285
		EVP_CipherUpdate(&(ctx->cipher),
sl@0
   286
			(unsigned char *)ctx->buf,&ctx->buf_len,
sl@0
   287
			(unsigned char *)in,n);
sl@0
   288
		inl-=n;
sl@0
   289
		in+=n;
sl@0
   290
sl@0
   291
		ctx->buf_off=0;
sl@0
   292
		n=ctx->buf_len;
sl@0
   293
		while (n > 0)
sl@0
   294
			{
sl@0
   295
			i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
sl@0
   296
			if (i <= 0)
sl@0
   297
				{
sl@0
   298
				BIO_copy_next_retry(b);
sl@0
   299
				return (ret == inl) ? i : ret - inl;
sl@0
   300
				}
sl@0
   301
			n-=i;
sl@0
   302
			ctx->buf_off+=i;
sl@0
   303
			}
sl@0
   304
		ctx->buf_len=0;
sl@0
   305
		ctx->buf_off=0;
sl@0
   306
		}
sl@0
   307
	BIO_copy_next_retry(b);
sl@0
   308
	return(ret);
sl@0
   309
	}
sl@0
   310
sl@0
   311
static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
sl@0
   312
	{
sl@0
   313
	BIO *dbio;
sl@0
   314
	BIO_ENC_CTX *ctx,*dctx;
sl@0
   315
	long ret=1;
sl@0
   316
	int i;
sl@0
   317
	EVP_CIPHER_CTX **c_ctx;
sl@0
   318
sl@0
   319
	ctx=(BIO_ENC_CTX *)b->ptr;
sl@0
   320
sl@0
   321
	switch (cmd)
sl@0
   322
		{
sl@0
   323
	case BIO_CTRL_RESET:
sl@0
   324
		ctx->ok=1;
sl@0
   325
		ctx->finished=0;
sl@0
   326
		EVP_CipherInit_ex(&(ctx->cipher),NULL,NULL,NULL,NULL,
sl@0
   327
			ctx->cipher.encrypt);
sl@0
   328
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   329
		break;
sl@0
   330
	case BIO_CTRL_EOF:	/* More to read */
sl@0
   331
		if (ctx->cont <= 0)
sl@0
   332
			ret=1;
sl@0
   333
		else
sl@0
   334
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   335
		break;
sl@0
   336
	case BIO_CTRL_WPENDING:
sl@0
   337
		ret=ctx->buf_len-ctx->buf_off;
sl@0
   338
		if (ret <= 0)
sl@0
   339
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   340
		break;
sl@0
   341
	case BIO_CTRL_PENDING: /* More to read in buffer */
sl@0
   342
		ret=ctx->buf_len-ctx->buf_off;
sl@0
   343
		if (ret <= 0)
sl@0
   344
			ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   345
		break;
sl@0
   346
	case BIO_CTRL_FLUSH:
sl@0
   347
		/* do a final write */
sl@0
   348
again:
sl@0
   349
		while (ctx->buf_len != ctx->buf_off)
sl@0
   350
			{
sl@0
   351
			i=enc_write(b,NULL,0);
sl@0
   352
			if (i < 0)
sl@0
   353
				return i;
sl@0
   354
			}
sl@0
   355
sl@0
   356
		if (!ctx->finished)
sl@0
   357
			{
sl@0
   358
			ctx->finished=1;
sl@0
   359
			ctx->buf_off=0;
sl@0
   360
			ret=EVP_CipherFinal_ex(&(ctx->cipher),
sl@0
   361
				(unsigned char *)ctx->buf,
sl@0
   362
				&(ctx->buf_len));
sl@0
   363
			ctx->ok=(int)ret;
sl@0
   364
			if (ret <= 0) break;
sl@0
   365
sl@0
   366
			/* push out the bytes */
sl@0
   367
			goto again;
sl@0
   368
			}
sl@0
   369
		
sl@0
   370
		/* Finally flush the underlying BIO */
sl@0
   371
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   372
		break;
sl@0
   373
	case BIO_C_GET_CIPHER_STATUS:
sl@0
   374
		ret=(long)ctx->ok;
sl@0
   375
		break;
sl@0
   376
	case BIO_C_DO_STATE_MACHINE:
sl@0
   377
		BIO_clear_retry_flags(b);
sl@0
   378
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   379
		BIO_copy_next_retry(b);
sl@0
   380
		break;
sl@0
   381
	case BIO_C_GET_CIPHER_CTX:
sl@0
   382
		c_ctx=(EVP_CIPHER_CTX **)ptr;
sl@0
   383
		(*c_ctx)= &(ctx->cipher);
sl@0
   384
		b->init=1;
sl@0
   385
		break;
sl@0
   386
	case BIO_CTRL_DUP:
sl@0
   387
		dbio=(BIO *)ptr;
sl@0
   388
		dctx=(BIO_ENC_CTX *)dbio->ptr;
sl@0
   389
		memcpy(&(dctx->cipher),&(ctx->cipher),sizeof(ctx->cipher));
sl@0
   390
		dbio->init=1;
sl@0
   391
		break;
sl@0
   392
	default:
sl@0
   393
		ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
sl@0
   394
		break;
sl@0
   395
		}
sl@0
   396
	return(ret);
sl@0
   397
	}
sl@0
   398
sl@0
   399
static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
sl@0
   400
	{
sl@0
   401
	long ret=1;
sl@0
   402
sl@0
   403
	if (b->next_bio == NULL) return(0);
sl@0
   404
	switch (cmd)
sl@0
   405
		{
sl@0
   406
	default:
sl@0
   407
		ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
sl@0
   408
		break;
sl@0
   409
		}
sl@0
   410
	return(ret);
sl@0
   411
	}
sl@0
   412
sl@0
   413
/*
sl@0
   414
void BIO_set_cipher_ctx(b,c)
sl@0
   415
BIO *b;
sl@0
   416
EVP_CIPHER_ctx *c;
sl@0
   417
	{
sl@0
   418
	if (b == NULL) return;
sl@0
   419
sl@0
   420
	if ((b->callback != NULL) &&
sl@0
   421
		(b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
sl@0
   422
		return;
sl@0
   423
sl@0
   424
	b->init=1;
sl@0
   425
	ctx=(BIO_ENC_CTX *)b->ptr;
sl@0
   426
	memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
sl@0
   427
	
sl@0
   428
	if (b->callback != NULL)
sl@0
   429
		b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
sl@0
   430
	}
sl@0
   431
*/
sl@0
   432
sl@0
   433
EXPORT_C void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
sl@0
   434
	     const unsigned char *i, int e)
sl@0
   435
	{
sl@0
   436
	BIO_ENC_CTX *ctx;
sl@0
   437
sl@0
   438
	if (b == NULL) return;
sl@0
   439
sl@0
   440
	if ((b->callback != NULL) &&
sl@0
   441
		(b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,0L) <= 0))
sl@0
   442
		return;
sl@0
   443
sl@0
   444
	b->init=1;
sl@0
   445
	ctx=(BIO_ENC_CTX *)b->ptr;
sl@0
   446
	EVP_CipherInit_ex(&(ctx->cipher),c,NULL, k,i,e);
sl@0
   447
	
sl@0
   448
	if (b->callback != NULL)
sl@0
   449
		b->callback(b,BIO_CB_CTRL,(const char *)c,BIO_CTRL_SET,e,1L);
sl@0
   450
	}
sl@0
   451