xref: /freebsd/crypto/openssl/crypto/evp/e_rc2.c (revision 6f9291cea8b06d251243fd47a7234018541832a3)
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 
77*6f9291ceSJung-uk Kim typedef struct {
785c87c606SMark Murray     int key_bits;               /* effective key bits */
795c87c606SMark Murray     RC2_KEY ks;                 /* key schedule */
805c87c606SMark Murray } EVP_RC2_KEY;
815c87c606SMark Murray 
825c87c606SMark Murray # define data(ctx)       ((EVP_RC2_KEY *)(ctx)->cipher_data)
835c87c606SMark Murray 
845c87c606SMark Murray IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
85ddd58736SKris Kennaway                        8,
865c87c606SMark Murray                        RC2_KEY_LENGTH, 8, 64,
87ddd58736SKris Kennaway                        EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
88ddd58736SKris Kennaway                        rc2_init_key, NULL,
89ddd58736SKris Kennaway                        rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
90ddd58736SKris Kennaway                        rc2_ctrl)
91ddd58736SKris Kennaway # define RC2_40_MAGIC    0xa0
92ddd58736SKris Kennaway # define RC2_64_MAGIC    0x78
93ddd58736SKris Kennaway # define RC2_128_MAGIC   0x3a
94*6f9291ceSJung-uk Kim static const EVP_CIPHER r2_64_cbc_cipher = {
95ddd58736SKris Kennaway     NID_rc2_64_cbc,
96ddd58736SKris Kennaway     8, 8 /* 64 bit */ , 8,
97ddd58736SKris Kennaway     EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
98ddd58736SKris Kennaway     rc2_init_key,
99ddd58736SKris Kennaway     rc2_cbc_cipher,
100ddd58736SKris Kennaway     NULL,
1015c87c606SMark Murray     sizeof(EVP_RC2_KEY),
102ddd58736SKris Kennaway     rc2_set_asn1_type_and_iv,
103ddd58736SKris Kennaway     rc2_get_asn1_type_and_iv,
104ddd58736SKris Kennaway     rc2_ctrl,
105ddd58736SKris Kennaway     NULL
106ddd58736SKris Kennaway };
107ddd58736SKris Kennaway 
108*6f9291ceSJung-uk Kim static const EVP_CIPHER r2_40_cbc_cipher = {
109ddd58736SKris Kennaway     NID_rc2_40_cbc,
110ddd58736SKris Kennaway     8, 5 /* 40 bit */ , 8,
111ddd58736SKris Kennaway     EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
112ddd58736SKris Kennaway     rc2_init_key,
113ddd58736SKris Kennaway     rc2_cbc_cipher,
114ddd58736SKris Kennaway     NULL,
1155c87c606SMark Murray     sizeof(EVP_RC2_KEY),
116ddd58736SKris Kennaway     rc2_set_asn1_type_and_iv,
117ddd58736SKris Kennaway     rc2_get_asn1_type_and_iv,
118ddd58736SKris Kennaway     rc2_ctrl,
119ddd58736SKris Kennaway     NULL
120ddd58736SKris Kennaway };
121ddd58736SKris Kennaway 
1225c87c606SMark Murray const EVP_CIPHER *EVP_rc2_64_cbc(void)
123ddd58736SKris Kennaway {
124ddd58736SKris Kennaway     return (&r2_64_cbc_cipher);
125ddd58736SKris Kennaway }
126ddd58736SKris Kennaway 
1275c87c606SMark Murray const EVP_CIPHER *EVP_rc2_40_cbc(void)
128ddd58736SKris Kennaway {
129ddd58736SKris Kennaway     return (&r2_40_cbc_cipher);
130ddd58736SKris Kennaway }
131ddd58736SKris Kennaway 
132ddd58736SKris Kennaway static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
133ddd58736SKris Kennaway                         const unsigned char *iv, int enc)
134ddd58736SKris Kennaway {
1355c87c606SMark Murray     RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
1365c87c606SMark Murray                 key, data(ctx)->key_bits);
137ddd58736SKris Kennaway     return 1;
138ddd58736SKris Kennaway }
139ddd58736SKris Kennaway 
140ddd58736SKris Kennaway static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
141ddd58736SKris Kennaway {
142ddd58736SKris Kennaway     int i;
143ddd58736SKris Kennaway 
144ddd58736SKris Kennaway     EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
145*6f9291ceSJung-uk Kim     if (i == 128)
146*6f9291ceSJung-uk Kim         return (RC2_128_MAGIC);
147*6f9291ceSJung-uk Kim     else if (i == 64)
148*6f9291ceSJung-uk Kim         return (RC2_64_MAGIC);
149*6f9291ceSJung-uk Kim     else if (i == 40)
150*6f9291ceSJung-uk Kim         return (RC2_40_MAGIC);
151*6f9291ceSJung-uk Kim     else
152*6f9291ceSJung-uk Kim         return (0);
153ddd58736SKris Kennaway }
154ddd58736SKris Kennaway 
155ddd58736SKris Kennaway static int rc2_magic_to_meth(int i)
156ddd58736SKris Kennaway {
157*6f9291ceSJung-uk Kim     if (i == RC2_128_MAGIC)
158*6f9291ceSJung-uk Kim         return 128;
159*6f9291ceSJung-uk Kim     else if (i == RC2_64_MAGIC)
160*6f9291ceSJung-uk Kim         return 64;
161*6f9291ceSJung-uk Kim     else if (i == RC2_40_MAGIC)
162*6f9291ceSJung-uk Kim         return 40;
163*6f9291ceSJung-uk Kim     else {
164ddd58736SKris Kennaway         EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
165ddd58736SKris Kennaway         return (0);
166ddd58736SKris Kennaway     }
167ddd58736SKris Kennaway }
168ddd58736SKris Kennaway 
169ddd58736SKris Kennaway static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
170ddd58736SKris Kennaway {
171ddd58736SKris Kennaway     long num = 0;
1723b4e3dcbSSimon L. B. Nielsen     int i = 0;
173ddd58736SKris Kennaway     int key_bits;
1743b4e3dcbSSimon L. B. Nielsen     unsigned int l;
175ddd58736SKris Kennaway     unsigned char iv[EVP_MAX_IV_LENGTH];
176ddd58736SKris Kennaway 
177*6f9291ceSJung-uk Kim     if (type != NULL) {
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);
1861f13597dSJung-uk Kim         if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
1871f13597dSJung-uk Kim             return -1;
188ddd58736SKris Kennaway         EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
189ddd58736SKris Kennaway         EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
190ddd58736SKris Kennaway     }
191ddd58736SKris Kennaway     return (i);
192ddd58736SKris Kennaway }
193ddd58736SKris Kennaway 
194ddd58736SKris Kennaway static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
195ddd58736SKris Kennaway {
196ddd58736SKris Kennaway     long num;
197ddd58736SKris Kennaway     int i = 0, j;
198ddd58736SKris Kennaway 
199*6f9291ceSJung-uk Kim     if (type != NULL) {
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 {
209*6f9291ceSJung-uk Kim     switch (type) {
210ddd58736SKris Kennaway     case EVP_CTRL_INIT:
2115c87c606SMark Murray         data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
212ddd58736SKris Kennaway         return 1;
213ddd58736SKris Kennaway 
214ddd58736SKris Kennaway     case EVP_CTRL_GET_RC2_KEY_BITS:
2155c87c606SMark Murray         *(int *)ptr = data(c)->key_bits;
216ddd58736SKris Kennaway         return 1;
217ddd58736SKris Kennaway 
218ddd58736SKris Kennaway     case EVP_CTRL_SET_RC2_KEY_BITS:
219*6f9291ceSJung-uk Kim         if (arg > 0) {
2205c87c606SMark Murray             data(c)->key_bits = arg;
221ddd58736SKris Kennaway             return 1;
222ddd58736SKris Kennaway         }
223ddd58736SKris Kennaway         return 0;
2241f13597dSJung-uk Kim # ifdef PBE_PRF_TEST
2251f13597dSJung-uk Kim     case EVP_CTRL_PBE_PRF_NID:
2261f13597dSJung-uk Kim         *(int *)ptr = NID_hmacWithMD5;
2271f13597dSJung-uk Kim         return 1;
2281f13597dSJung-uk Kim # endif
229ddd58736SKris Kennaway 
230ddd58736SKris Kennaway     default:
231ddd58736SKris Kennaway         return -1;
232ddd58736SKris Kennaway     }
233ddd58736SKris Kennaway }
234ddd58736SKris Kennaway 
235ddd58736SKris Kennaway #endif
236