os/ossrv/ssl/libcrypto/src/crypto/evp/e_rc2.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/e_rc2.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
#include <stdio.h>
sl@0
    60
#include "cryptlib.h"
sl@0
    61
sl@0
    62
#ifndef OPENSSL_NO_RC2
sl@0
    63
sl@0
    64
#include <openssl/evp.h>
sl@0
    65
#include <openssl/objects.h>
sl@0
    66
#include "evp_locl.h"
sl@0
    67
#include <openssl/rc2.h>
sl@0
    68
sl@0
    69
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
sl@0
    70
			const unsigned char *iv,int enc);
sl@0
    71
static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
sl@0
    72
static int rc2_magic_to_meth(int i);
sl@0
    73
static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
sl@0
    74
static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
sl@0
    75
static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
sl@0
    76
sl@0
    77
typedef struct
sl@0
    78
	{
sl@0
    79
	int key_bits;	/* effective key bits */
sl@0
    80
	RC2_KEY ks;	/* key schedule */
sl@0
    81
	} EVP_RC2_KEY;
sl@0
    82
sl@0
    83
#define data(ctx)	((EVP_RC2_KEY *)(ctx)->cipher_data)
sl@0
    84
sl@0
    85
IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
sl@0
    86
			8,
sl@0
    87
			RC2_KEY_LENGTH, 8, 64,
sl@0
    88
			EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
sl@0
    89
			rc2_init_key, NULL,
sl@0
    90
			rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, 
sl@0
    91
			rc2_ctrl)
sl@0
    92
sl@0
    93
#define RC2_40_MAGIC	0xa0
sl@0
    94
#define RC2_64_MAGIC	0x78
sl@0
    95
#define RC2_128_MAGIC	0x3a
sl@0
    96
sl@0
    97
static const EVP_CIPHER r2_64_cbc_cipher=
sl@0
    98
	{
sl@0
    99
	NID_rc2_64_cbc,
sl@0
   100
	8,8 /* 64 bit */,8,
sl@0
   101
	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
sl@0
   102
	rc2_init_key,
sl@0
   103
	rc2_cbc_cipher,
sl@0
   104
	NULL,
sl@0
   105
	sizeof(EVP_RC2_KEY),
sl@0
   106
	rc2_set_asn1_type_and_iv,
sl@0
   107
	rc2_get_asn1_type_and_iv,
sl@0
   108
	rc2_ctrl,
sl@0
   109
	NULL
sl@0
   110
	};
sl@0
   111
sl@0
   112
static const EVP_CIPHER r2_40_cbc_cipher=
sl@0
   113
	{
sl@0
   114
	NID_rc2_40_cbc,
sl@0
   115
	8,5 /* 40 bit */,8,
sl@0
   116
	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
sl@0
   117
	rc2_init_key,
sl@0
   118
	rc2_cbc_cipher,
sl@0
   119
	NULL,
sl@0
   120
	sizeof(EVP_RC2_KEY),
sl@0
   121
	rc2_set_asn1_type_and_iv,
sl@0
   122
	rc2_get_asn1_type_and_iv,
sl@0
   123
	rc2_ctrl,
sl@0
   124
	NULL
sl@0
   125
	};
sl@0
   126
sl@0
   127
EXPORT_C const EVP_CIPHER *EVP_rc2_64_cbc(void)
sl@0
   128
	{
sl@0
   129
	return(&r2_64_cbc_cipher);
sl@0
   130
	}
sl@0
   131
sl@0
   132
EXPORT_C const EVP_CIPHER *EVP_rc2_40_cbc(void)
sl@0
   133
	{
sl@0
   134
	return(&r2_40_cbc_cipher);
sl@0
   135
	}
sl@0
   136
	
sl@0
   137
static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
sl@0
   138
			const unsigned char *iv, int enc)
sl@0
   139
	{
sl@0
   140
	RC2_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
sl@0
   141
		    key,data(ctx)->key_bits);
sl@0
   142
	return 1;
sl@0
   143
	}
sl@0
   144
sl@0
   145
static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
sl@0
   146
	{
sl@0
   147
	int i;
sl@0
   148
sl@0
   149
	EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
sl@0
   150
	if 	(i == 128) return(RC2_128_MAGIC);
sl@0
   151
	else if (i == 64)  return(RC2_64_MAGIC);
sl@0
   152
	else if (i == 40)  return(RC2_40_MAGIC);
sl@0
   153
	else return(0);
sl@0
   154
	}
sl@0
   155
sl@0
   156
static int rc2_magic_to_meth(int i)
sl@0
   157
	{
sl@0
   158
	if      (i == RC2_128_MAGIC) return 128;
sl@0
   159
	else if (i == RC2_64_MAGIC)  return 64;
sl@0
   160
	else if (i == RC2_40_MAGIC)  return 40;
sl@0
   161
	else
sl@0
   162
		{
sl@0
   163
		EVPerr(EVP_F_RC2_MAGIC_TO_METH,EVP_R_UNSUPPORTED_KEY_SIZE);
sl@0
   164
		return(0);
sl@0
   165
		}
sl@0
   166
	}
sl@0
   167
sl@0
   168
static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
sl@0
   169
	{
sl@0
   170
	long num=0;
sl@0
   171
	int i=0;
sl@0
   172
	int key_bits;
sl@0
   173
	unsigned int l;
sl@0
   174
	unsigned char iv[EVP_MAX_IV_LENGTH];
sl@0
   175
sl@0
   176
	if (type != NULL)
sl@0
   177
		{
sl@0
   178
		l=EVP_CIPHER_CTX_iv_length(c);
sl@0
   179
		OPENSSL_assert(l <= sizeof(iv));
sl@0
   180
		i=ASN1_TYPE_get_int_octetstring(type,&num,iv,l);
sl@0
   181
		if (i != (int)l)
sl@0
   182
			return(-1);
sl@0
   183
		key_bits =rc2_magic_to_meth((int)num);
sl@0
   184
		if (!key_bits)
sl@0
   185
			return(-1);
sl@0
   186
		if(i > 0) EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1);
sl@0
   187
		EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
sl@0
   188
		EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
sl@0
   189
		}
sl@0
   190
	return(i);
sl@0
   191
	}
sl@0
   192
sl@0
   193
static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
sl@0
   194
	{
sl@0
   195
	long num;
sl@0
   196
	int i=0,j;
sl@0
   197
sl@0
   198
	if (type != NULL)
sl@0
   199
		{
sl@0
   200
		num=rc2_meth_to_magic(c);
sl@0
   201
		j=EVP_CIPHER_CTX_iv_length(c);
sl@0
   202
		i=ASN1_TYPE_set_int_octetstring(type,num,c->oiv,j);
sl@0
   203
		}
sl@0
   204
	return(i);
sl@0
   205
	}
sl@0
   206
sl@0
   207
static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
sl@0
   208
	{
sl@0
   209
	switch(type)
sl@0
   210
		{
sl@0
   211
	case EVP_CTRL_INIT:
sl@0
   212
		data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
sl@0
   213
		return 1;
sl@0
   214
sl@0
   215
	case EVP_CTRL_GET_RC2_KEY_BITS:
sl@0
   216
		*(int *)ptr = data(c)->key_bits;
sl@0
   217
		return 1;
sl@0
   218
			
sl@0
   219
	case EVP_CTRL_SET_RC2_KEY_BITS:
sl@0
   220
		if(arg > 0)
sl@0
   221
			{
sl@0
   222
			data(c)->key_bits = arg;
sl@0
   223
			return 1;
sl@0
   224
			}
sl@0
   225
		return 0;
sl@0
   226
sl@0
   227
	default:
sl@0
   228
		return -1;
sl@0
   229
		}
sl@0
   230
	}
sl@0
   231
sl@0
   232
#endif