xref: /freebsd/crypto/openssl/crypto/evp/e_rc2.c (revision 3b4e3dcb9f42dc9f4f864acf804677d7a3e0c233)
1ddd58736SKris Kennaway /* crypto/evp/e_rc2.c */
2ddd58736SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ddd58736SKris Kennaway  * All rights reserved.
4ddd58736SKris Kennaway  *
5ddd58736SKris Kennaway  * This package is an SSL implementation written
6ddd58736SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
7ddd58736SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
8ddd58736SKris Kennaway  *
9ddd58736SKris Kennaway  * This library is free for commercial and non-commercial use as long as
10ddd58736SKris Kennaway  * the following conditions are aheared to.  The following conditions
11ddd58736SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
12ddd58736SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ddd58736SKris Kennaway  * included with this distribution is covered by the same copyright terms
14ddd58736SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ddd58736SKris Kennaway  *
16ddd58736SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
17ddd58736SKris Kennaway  * the code are not to be removed.
18ddd58736SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
19ddd58736SKris Kennaway  * as the author of the parts of the library used.
20ddd58736SKris Kennaway  * This can be in the form of a textual message at program startup or
21ddd58736SKris Kennaway  * in documentation (online or textual) provided with the package.
22ddd58736SKris Kennaway  *
23ddd58736SKris Kennaway  * Redistribution and use in source and binary forms, with or without
24ddd58736SKris Kennaway  * modification, are permitted provided that the following conditions
25ddd58736SKris Kennaway  * are met:
26ddd58736SKris Kennaway  * 1. Redistributions of source code must retain the copyright
27ddd58736SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
28ddd58736SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
29ddd58736SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
30ddd58736SKris Kennaway  *    documentation and/or other materials provided with the distribution.
31ddd58736SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
32ddd58736SKris Kennaway  *    must display the following acknowledgement:
33ddd58736SKris Kennaway  *    "This product includes cryptographic software written by
34ddd58736SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
35ddd58736SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
36ddd58736SKris Kennaway  *    being used are not cryptographic related :-).
37ddd58736SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
38ddd58736SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
39ddd58736SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ddd58736SKris Kennaway  *
41ddd58736SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ddd58736SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ddd58736SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ddd58736SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ddd58736SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ddd58736SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ddd58736SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ddd58736SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ddd58736SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ddd58736SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ddd58736SKris Kennaway  * SUCH DAMAGE.
52ddd58736SKris Kennaway  *
53ddd58736SKris Kennaway  * The licence and distribution terms for any publically available version or
54ddd58736SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ddd58736SKris Kennaway  * copied and put under another distribution licence
56ddd58736SKris Kennaway  * [including the GNU Public Licence.]
57ddd58736SKris Kennaway  */
58ddd58736SKris Kennaway 
59ddd58736SKris Kennaway #include <stdio.h>
60ddd58736SKris Kennaway #include "cryptlib.h"
613b4e3dcbSSimon L. B. Nielsen 
623b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_RC2
633b4e3dcbSSimon L. B. Nielsen 
64ddd58736SKris Kennaway #include <openssl/evp.h>
65ddd58736SKris Kennaway #include <openssl/objects.h>
66ddd58736SKris Kennaway #include "evp_locl.h"
675c87c606SMark Murray #include <openssl/rc2.h>
68ddd58736SKris Kennaway 
69ddd58736SKris Kennaway static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
70ddd58736SKris Kennaway 			const unsigned char *iv,int enc);
71ddd58736SKris Kennaway static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
72ddd58736SKris Kennaway static int rc2_magic_to_meth(int i);
73ddd58736SKris Kennaway static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
74ddd58736SKris Kennaway static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
75ddd58736SKris Kennaway static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
76ddd58736SKris Kennaway 
775c87c606SMark Murray typedef struct
785c87c606SMark Murray 	{
795c87c606SMark Murray 	int key_bits;	/* effective key bits */
805c87c606SMark Murray 	RC2_KEY ks;	/* key schedule */
815c87c606SMark Murray 	} EVP_RC2_KEY;
825c87c606SMark Murray 
835c87c606SMark Murray #define data(ctx)	((EVP_RC2_KEY *)(ctx)->cipher_data)
845c87c606SMark Murray 
855c87c606SMark Murray IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
86ddd58736SKris Kennaway 			8,
875c87c606SMark Murray 			RC2_KEY_LENGTH, 8, 64,
88ddd58736SKris Kennaway 			EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
89ddd58736SKris Kennaway 			rc2_init_key, NULL,
90ddd58736SKris Kennaway 			rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
91ddd58736SKris Kennaway 			rc2_ctrl)
92ddd58736SKris Kennaway 
93ddd58736SKris Kennaway #define RC2_40_MAGIC	0xa0
94ddd58736SKris Kennaway #define RC2_64_MAGIC	0x78
95ddd58736SKris Kennaway #define RC2_128_MAGIC	0x3a
96ddd58736SKris Kennaway 
975c87c606SMark Murray static const EVP_CIPHER r2_64_cbc_cipher=
98ddd58736SKris Kennaway 	{
99ddd58736SKris Kennaway 	NID_rc2_64_cbc,
100ddd58736SKris Kennaway 	8,8 /* 64 bit */,8,
101ddd58736SKris Kennaway 	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
102ddd58736SKris Kennaway 	rc2_init_key,
103ddd58736SKris Kennaway 	rc2_cbc_cipher,
104ddd58736SKris Kennaway 	NULL,
1055c87c606SMark Murray 	sizeof(EVP_RC2_KEY),
106ddd58736SKris Kennaway 	rc2_set_asn1_type_and_iv,
107ddd58736SKris Kennaway 	rc2_get_asn1_type_and_iv,
108ddd58736SKris Kennaway 	rc2_ctrl,
109ddd58736SKris Kennaway 	NULL
110ddd58736SKris Kennaway 	};
111ddd58736SKris Kennaway 
1125c87c606SMark Murray static const EVP_CIPHER r2_40_cbc_cipher=
113ddd58736SKris Kennaway 	{
114ddd58736SKris Kennaway 	NID_rc2_40_cbc,
115ddd58736SKris Kennaway 	8,5 /* 40 bit */,8,
116ddd58736SKris Kennaway 	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
117ddd58736SKris Kennaway 	rc2_init_key,
118ddd58736SKris Kennaway 	rc2_cbc_cipher,
119ddd58736SKris Kennaway 	NULL,
1205c87c606SMark Murray 	sizeof(EVP_RC2_KEY),
121ddd58736SKris Kennaway 	rc2_set_asn1_type_and_iv,
122ddd58736SKris Kennaway 	rc2_get_asn1_type_and_iv,
123ddd58736SKris Kennaway 	rc2_ctrl,
124ddd58736SKris Kennaway 	NULL
125ddd58736SKris Kennaway 	};
126ddd58736SKris Kennaway 
1275c87c606SMark Murray const EVP_CIPHER *EVP_rc2_64_cbc(void)
128ddd58736SKris Kennaway 	{
129ddd58736SKris Kennaway 	return(&r2_64_cbc_cipher);
130ddd58736SKris Kennaway 	}
131ddd58736SKris Kennaway 
1325c87c606SMark Murray const EVP_CIPHER *EVP_rc2_40_cbc(void)
133ddd58736SKris Kennaway 	{
134ddd58736SKris Kennaway 	return(&r2_40_cbc_cipher);
135ddd58736SKris Kennaway 	}
136ddd58736SKris Kennaway 
137ddd58736SKris Kennaway static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
138ddd58736SKris Kennaway 			const unsigned char *iv, int enc)
139ddd58736SKris Kennaway 	{
1405c87c606SMark Murray 	RC2_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx),
1415c87c606SMark Murray 		    key,data(ctx)->key_bits);
142ddd58736SKris Kennaway 	return 1;
143ddd58736SKris Kennaway 	}
144ddd58736SKris Kennaway 
145ddd58736SKris Kennaway static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
146ddd58736SKris Kennaway 	{
147ddd58736SKris Kennaway 	int i;
148ddd58736SKris Kennaway 
149ddd58736SKris Kennaway 	EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
150ddd58736SKris Kennaway 	if 	(i == 128) return(RC2_128_MAGIC);
151ddd58736SKris Kennaway 	else if (i == 64)  return(RC2_64_MAGIC);
152ddd58736SKris Kennaway 	else if (i == 40)  return(RC2_40_MAGIC);
153ddd58736SKris Kennaway 	else return(0);
154ddd58736SKris Kennaway 	}
155ddd58736SKris Kennaway 
156ddd58736SKris Kennaway static int rc2_magic_to_meth(int i)
157ddd58736SKris Kennaway 	{
158ddd58736SKris Kennaway 	if      (i == RC2_128_MAGIC) return 128;
159ddd58736SKris Kennaway 	else if (i == RC2_64_MAGIC)  return 64;
160ddd58736SKris Kennaway 	else if (i == RC2_40_MAGIC)  return 40;
161ddd58736SKris Kennaway 	else
162ddd58736SKris Kennaway 		{
163ddd58736SKris Kennaway 		EVPerr(EVP_F_RC2_MAGIC_TO_METH,EVP_R_UNSUPPORTED_KEY_SIZE);
164ddd58736SKris Kennaway 		return(0);
165ddd58736SKris Kennaway 		}
166ddd58736SKris Kennaway 	}
167ddd58736SKris Kennaway 
168ddd58736SKris Kennaway static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
169ddd58736SKris Kennaway 	{
170ddd58736SKris Kennaway 	long num=0;
1713b4e3dcbSSimon L. B. Nielsen 	int i=0;
172ddd58736SKris Kennaway 	int key_bits;
1733b4e3dcbSSimon L. B. Nielsen 	unsigned int l;
174ddd58736SKris Kennaway 	unsigned char iv[EVP_MAX_IV_LENGTH];
175ddd58736SKris Kennaway 
176ddd58736SKris Kennaway 	if (type != NULL)
177ddd58736SKris Kennaway 		{
178ddd58736SKris Kennaway 		l=EVP_CIPHER_CTX_iv_length(c);
1793b4e3dcbSSimon L. B. Nielsen 		OPENSSL_assert(l <= sizeof(iv));
180ddd58736SKris Kennaway 		i=ASN1_TYPE_get_int_octetstring(type,&num,iv,l);
1813b4e3dcbSSimon L. B. Nielsen 		if (i != (int)l)
182ddd58736SKris Kennaway 			return(-1);
183ddd58736SKris Kennaway 		key_bits =rc2_magic_to_meth((int)num);
184ddd58736SKris Kennaway 		if (!key_bits)
185ddd58736SKris Kennaway 			return(-1);
1865c87c606SMark Murray 		if(i > 0) EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1);
187ddd58736SKris Kennaway 		EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
188ddd58736SKris Kennaway 		EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
189ddd58736SKris Kennaway 		}
190ddd58736SKris Kennaway 	return(i);
191ddd58736SKris Kennaway 	}
192ddd58736SKris Kennaway 
193ddd58736SKris Kennaway static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
194ddd58736SKris Kennaway 	{
195ddd58736SKris Kennaway 	long num;
196ddd58736SKris Kennaway 	int i=0,j;
197ddd58736SKris Kennaway 
198ddd58736SKris Kennaway 	if (type != NULL)
199ddd58736SKris Kennaway 		{
200ddd58736SKris Kennaway 		num=rc2_meth_to_magic(c);
201ddd58736SKris Kennaway 		j=EVP_CIPHER_CTX_iv_length(c);
202ddd58736SKris Kennaway 		i=ASN1_TYPE_set_int_octetstring(type,num,c->oiv,j);
203ddd58736SKris Kennaway 		}
204ddd58736SKris Kennaway 	return(i);
205ddd58736SKris Kennaway 	}
206ddd58736SKris Kennaway 
207ddd58736SKris Kennaway static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
208ddd58736SKris Kennaway 	{
2095c87c606SMark Murray 	switch(type)
2105c87c606SMark Murray 		{
211ddd58736SKris Kennaway 	case EVP_CTRL_INIT:
2125c87c606SMark Murray 		data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
213ddd58736SKris Kennaway 		return 1;
214ddd58736SKris Kennaway 
215ddd58736SKris Kennaway 	case EVP_CTRL_GET_RC2_KEY_BITS:
2165c87c606SMark Murray 		*(int *)ptr = data(c)->key_bits;
217ddd58736SKris Kennaway 		return 1;
218ddd58736SKris Kennaway 
219ddd58736SKris Kennaway 	case EVP_CTRL_SET_RC2_KEY_BITS:
2205c87c606SMark Murray 		if(arg > 0)
2215c87c606SMark Murray 			{
2225c87c606SMark Murray 			data(c)->key_bits = arg;
223ddd58736SKris Kennaway 			return 1;
224ddd58736SKris Kennaway 			}
225ddd58736SKris Kennaway 		return 0;
226ddd58736SKris Kennaway 
227ddd58736SKris Kennaway 	default:
228ddd58736SKris Kennaway 		return -1;
229ddd58736SKris Kennaway 		}
230ddd58736SKris Kennaway 	}
231ddd58736SKris Kennaway 
232ddd58736SKris Kennaway #endif
233