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