168651Skris/* crypto/evp/e_rc2.c */
268651Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
368651Skris * All rights reserved.
468651Skris *
568651Skris * This package is an SSL implementation written
668651Skris * by Eric Young (eay@cryptsoft.com).
768651Skris * The implementation was written so as to conform with Netscapes SSL.
868651Skris *
968651Skris * This library is free for commercial and non-commercial use as long as
1068651Skris * the following conditions are aheared to.  The following conditions
1168651Skris * apply to all code found in this distribution, be it the RC4, RSA,
1268651Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1368651Skris * included with this distribution is covered by the same copyright terms
1468651Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1568651Skris *
1668651Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1768651Skris * the code are not to be removed.
1868651Skris * If this package is used in a product, Eric Young should be given attribution
1968651Skris * as the author of the parts of the library used.
2068651Skris * This can be in the form of a textual message at program startup or
2168651Skris * in documentation (online or textual) provided with the package.
2268651Skris *
2368651Skris * Redistribution and use in source and binary forms, with or without
2468651Skris * modification, are permitted provided that the following conditions
2568651Skris * are met:
2668651Skris * 1. Redistributions of source code must retain the copyright
2768651Skris *    notice, this list of conditions and the following disclaimer.
2868651Skris * 2. Redistributions in binary form must reproduce the above copyright
2968651Skris *    notice, this list of conditions and the following disclaimer in the
3068651Skris *    documentation and/or other materials provided with the distribution.
3168651Skris * 3. All advertising materials mentioning features or use of this software
3268651Skris *    must display the following acknowledgement:
3368651Skris *    "This product includes cryptographic software written by
3468651Skris *     Eric Young (eay@cryptsoft.com)"
3568651Skris *    The word 'cryptographic' can be left out if the rouines from the library
3668651Skris *    being used are not cryptographic related :-).
3768651Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3868651Skris *    the apps directory (application code) you must include an acknowledgement:
3968651Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4068651Skris *
4168651Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4268651Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4368651Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4468651Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4568651Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4668651Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4768651Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4868651Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4968651Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5068651Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5168651Skris * SUCH DAMAGE.
5268651Skris *
5368651Skris * The licence and distribution terms for any publically available version or
5468651Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5568651Skris * copied and put under another distribution licence
5668651Skris * [including the GNU Public Licence.]
5768651Skris */
5868651Skris
59160814Ssimon#include <stdio.h>
60160814Ssimon#include "cryptlib.h"
61160814Ssimon
62109998Smarkm#ifndef OPENSSL_NO_RC2
6368651Skris
6468651Skris#include <openssl/evp.h>
6568651Skris#include <openssl/objects.h>
6668651Skris#include "evp_locl.h"
67109998Smarkm#include <openssl/rc2.h>
6868651Skris
6968651Skrisstatic int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
7068651Skris			const unsigned char *iv,int enc);
7168651Skrisstatic int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
7268651Skrisstatic int rc2_magic_to_meth(int i);
7368651Skrisstatic int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
7468651Skrisstatic int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
7568651Skrisstatic int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
7668651Skris
77109998Smarkmtypedef struct
78109998Smarkm	{
79109998Smarkm	int key_bits;	/* effective key bits */
80109998Smarkm	RC2_KEY ks;	/* key schedule */
81109998Smarkm	} EVP_RC2_KEY;
82109998Smarkm
83109998Smarkm#define data(ctx)	((EVP_RC2_KEY *)(ctx)->cipher_data)
84109998Smarkm
85109998SmarkmIMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
8668651Skris			8,
87109998Smarkm			RC2_KEY_LENGTH, 8, 64,
8868651Skris			EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
8968651Skris			rc2_init_key, NULL,
9068651Skris			rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
9168651Skris			rc2_ctrl)
9268651Skris
9368651Skris#define RC2_40_MAGIC	0xa0
9468651Skris#define RC2_64_MAGIC	0x78
9568651Skris#define RC2_128_MAGIC	0x3a
9668651Skris
97109998Smarkmstatic const EVP_CIPHER r2_64_cbc_cipher=
9868651Skris	{
9968651Skris	NID_rc2_64_cbc,
10068651Skris	8,8 /* 64 bit */,8,
10168651Skris	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
10268651Skris	rc2_init_key,
10368651Skris	rc2_cbc_cipher,
10468651Skris	NULL,
105109998Smarkm	sizeof(EVP_RC2_KEY),
10668651Skris	rc2_set_asn1_type_and_iv,
10768651Skris	rc2_get_asn1_type_and_iv,
10868651Skris	rc2_ctrl,
10968651Skris	NULL
11068651Skris	};
11168651Skris
112109998Smarkmstatic const EVP_CIPHER r2_40_cbc_cipher=
11368651Skris	{
11468651Skris	NID_rc2_40_cbc,
11568651Skris	8,5 /* 40 bit */,8,
11668651Skris	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
11768651Skris	rc2_init_key,
11868651Skris	rc2_cbc_cipher,
11968651Skris	NULL,
120109998Smarkm	sizeof(EVP_RC2_KEY),
12168651Skris	rc2_set_asn1_type_and_iv,
12268651Skris	rc2_get_asn1_type_and_iv,
12368651Skris	rc2_ctrl,
12468651Skris	NULL
12568651Skris	};
12668651Skris
127109998Smarkmconst EVP_CIPHER *EVP_rc2_64_cbc(void)
12868651Skris	{
12968651Skris	return(&r2_64_cbc_cipher);
13068651Skris	}
13168651Skris
132109998Smarkmconst EVP_CIPHER *EVP_rc2_40_cbc(void)
13368651Skris	{
13468651Skris	return(&r2_40_cbc_cipher);
13568651Skris	}
13668651Skris
13768651Skrisstatic int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
13868651Skris			const unsigned char *iv, int enc)
13968651Skris	{
140109998Smarkm	RC2_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
141109998Smarkm		    key,data(ctx)->key_bits);
14268651Skris	return 1;
14368651Skris	}
14468651Skris
14568651Skrisstatic int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
14668651Skris	{
14768651Skris	int i;
14868651Skris
14968651Skris	EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
15068651Skris	if 	(i == 128) return(RC2_128_MAGIC);
15168651Skris	else if (i == 64)  return(RC2_64_MAGIC);
15268651Skris	else if (i == 40)  return(RC2_40_MAGIC);
15368651Skris	else return(0);
15468651Skris	}
15568651Skris
15668651Skrisstatic int rc2_magic_to_meth(int i)
15768651Skris	{
15868651Skris	if      (i == RC2_128_MAGIC) return 128;
15968651Skris	else if (i == RC2_64_MAGIC)  return 64;
16068651Skris	else if (i == RC2_40_MAGIC)  return 40;
16168651Skris	else
16268651Skris		{
16368651Skris		EVPerr(EVP_F_RC2_MAGIC_TO_METH,EVP_R_UNSUPPORTED_KEY_SIZE);
16468651Skris		return(0);
16568651Skris		}
16668651Skris	}
16768651Skris
16868651Skrisstatic int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
16968651Skris	{
17068651Skris	long num=0;
171160814Ssimon	int i=0;
17268651Skris	int key_bits;
173160814Ssimon	unsigned int l;
17468651Skris	unsigned char iv[EVP_MAX_IV_LENGTH];
17568651Skris
17668651Skris	if (type != NULL)
17768651Skris		{
17868651Skris		l=EVP_CIPHER_CTX_iv_length(c);
179160814Ssimon		OPENSSL_assert(l <= sizeof(iv));
18068651Skris		i=ASN1_TYPE_get_int_octetstring(type,&num,iv,l);
181160814Ssimon		if (i != (int)l)
18268651Skris			return(-1);
18368651Skris		key_bits =rc2_magic_to_meth((int)num);
18468651Skris		if (!key_bits)
18568651Skris			return(-1);
186238405Sjkim		if(i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
187238405Sjkim			return -1;
18868651Skris		EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
18968651Skris		EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
19068651Skris		}
19168651Skris	return(i);
19268651Skris	}
19368651Skris
19468651Skrisstatic int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
19568651Skris	{
19668651Skris	long num;
19768651Skris	int i=0,j;
19868651Skris
19968651Skris	if (type != NULL)
20068651Skris		{
20168651Skris		num=rc2_meth_to_magic(c);
20268651Skris		j=EVP_CIPHER_CTX_iv_length(c);
20368651Skris		i=ASN1_TYPE_set_int_octetstring(type,num,c->oiv,j);
20468651Skris		}
20568651Skris	return(i);
20668651Skris	}
20768651Skris
20868651Skrisstatic int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
20968651Skris	{
210109998Smarkm	switch(type)
211109998Smarkm		{
212109998Smarkm	case EVP_CTRL_INIT:
213109998Smarkm		data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
214109998Smarkm		return 1;
21568651Skris
216109998Smarkm	case EVP_CTRL_GET_RC2_KEY_BITS:
217109998Smarkm		*(int *)ptr = data(c)->key_bits;
218109998Smarkm		return 1;
219109998Smarkm
220109998Smarkm	case EVP_CTRL_SET_RC2_KEY_BITS:
221109998Smarkm		if(arg > 0)
222109998Smarkm			{
223109998Smarkm			data(c)->key_bits = arg;
22468651Skris			return 1;
22568651Skris			}
226109998Smarkm		return 0;
227238405Sjkim#ifdef PBE_PRF_TEST
228238405Sjkim	case EVP_CTRL_PBE_PRF_NID:
229238405Sjkim		*(int *)ptr = NID_hmacWithMD5;
230238405Sjkim		return 1;
231238405Sjkim#endif
23268651Skris
233109998Smarkm	default:
234109998Smarkm		return -1;
23568651Skris		}
23668651Skris	}
23768651Skris
23868651Skris#endif
239