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.
8296465Sdelphij *
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).
15296465Sdelphij *
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.
22296465Sdelphij *
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 :-).
37296465Sdelphij * 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)"
40296465Sdelphij *
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.
52296465Sdelphij *
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
64296465Sdelphij# include <openssl/evp.h>
65296465Sdelphij# include <openssl/objects.h>
66296465Sdelphij# include "evp_locl.h"
67296465Sdelphij# include <openssl/rc2.h>
6868651Skris
6968651Skrisstatic int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
70296465Sdelphij                        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
77296465Sdelphijtypedef struct {
78296465Sdelphij    int key_bits;               /* effective key bits */
79296465Sdelphij    RC2_KEY ks;                 /* key schedule */
80296465Sdelphij} EVP_RC2_KEY;
81109998Smarkm
82296465Sdelphij# define data(ctx)       ((EVP_RC2_KEY *)(ctx)->cipher_data)
83109998Smarkm
84109998SmarkmIMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
85296465Sdelphij                       8,
86296465Sdelphij                       RC2_KEY_LENGTH, 8, 64,
87296465Sdelphij                       EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
88296465Sdelphij                       rc2_init_key, NULL,
89296465Sdelphij                       rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
90296465Sdelphij                       rc2_ctrl)
91296465Sdelphij# define RC2_40_MAGIC    0xa0
92296465Sdelphij# define RC2_64_MAGIC    0x78
93296465Sdelphij# define RC2_128_MAGIC   0x3a
94296465Sdelphijstatic const EVP_CIPHER r2_64_cbc_cipher = {
95296465Sdelphij    NID_rc2_64_cbc,
96296465Sdelphij    8, 8 /* 64 bit */ , 8,
97296465Sdelphij    EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
98296465Sdelphij    rc2_init_key,
99296465Sdelphij    rc2_cbc_cipher,
100296465Sdelphij    NULL,
101296465Sdelphij    sizeof(EVP_RC2_KEY),
102296465Sdelphij    rc2_set_asn1_type_and_iv,
103296465Sdelphij    rc2_get_asn1_type_and_iv,
104296465Sdelphij    rc2_ctrl,
105296465Sdelphij    NULL
106296465Sdelphij};
10768651Skris
108296465Sdelphijstatic const EVP_CIPHER r2_40_cbc_cipher = {
109296465Sdelphij    NID_rc2_40_cbc,
110296465Sdelphij    8, 5 /* 40 bit */ , 8,
111296465Sdelphij    EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
112296465Sdelphij    rc2_init_key,
113296465Sdelphij    rc2_cbc_cipher,
114296465Sdelphij    NULL,
115296465Sdelphij    sizeof(EVP_RC2_KEY),
116296465Sdelphij    rc2_set_asn1_type_and_iv,
117296465Sdelphij    rc2_get_asn1_type_and_iv,
118296465Sdelphij    rc2_ctrl,
119296465Sdelphij    NULL
120296465Sdelphij};
12168651Skris
122109998Smarkmconst EVP_CIPHER *EVP_rc2_64_cbc(void)
123296465Sdelphij{
124296465Sdelphij    return (&r2_64_cbc_cipher);
125296465Sdelphij}
12668651Skris
127109998Smarkmconst EVP_CIPHER *EVP_rc2_40_cbc(void)
128296465Sdelphij{
129296465Sdelphij    return (&r2_40_cbc_cipher);
130296465Sdelphij}
131296465Sdelphij
13268651Skrisstatic int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
133296465Sdelphij                        const unsigned char *iv, int enc)
134296465Sdelphij{
135296465Sdelphij    RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
136296465Sdelphij                key, data(ctx)->key_bits);
137296465Sdelphij    return 1;
138296465Sdelphij}
13968651Skris
14068651Skrisstatic int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
141296465Sdelphij{
142296465Sdelphij    int i;
14368651Skris
144296465Sdelphij    EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
145296465Sdelphij    if (i == 128)
146296465Sdelphij        return (RC2_128_MAGIC);
147296465Sdelphij    else if (i == 64)
148296465Sdelphij        return (RC2_64_MAGIC);
149296465Sdelphij    else if (i == 40)
150296465Sdelphij        return (RC2_40_MAGIC);
151296465Sdelphij    else
152296465Sdelphij        return (0);
153296465Sdelphij}
15468651Skris
15568651Skrisstatic int rc2_magic_to_meth(int i)
156296465Sdelphij{
157296465Sdelphij    if (i == RC2_128_MAGIC)
158296465Sdelphij        return 128;
159296465Sdelphij    else if (i == RC2_64_MAGIC)
160296465Sdelphij        return 64;
161296465Sdelphij    else if (i == RC2_40_MAGIC)
162296465Sdelphij        return 40;
163296465Sdelphij    else {
164296465Sdelphij        EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
165296465Sdelphij        return (0);
166296465Sdelphij    }
167296465Sdelphij}
16868651Skris
16968651Skrisstatic int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
170296465Sdelphij{
171296465Sdelphij    long num = 0;
172296465Sdelphij    int i = 0;
173296465Sdelphij    int key_bits;
174296465Sdelphij    unsigned int l;
175296465Sdelphij    unsigned char iv[EVP_MAX_IV_LENGTH];
17668651Skris
177296465Sdelphij    if (type != NULL) {
178296465Sdelphij        l = EVP_CIPHER_CTX_iv_length(c);
179296465Sdelphij        OPENSSL_assert(l <= sizeof(iv));
180296465Sdelphij        i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
181296465Sdelphij        if (i != (int)l)
182296465Sdelphij            return (-1);
183296465Sdelphij        key_bits = rc2_magic_to_meth((int)num);
184296465Sdelphij        if (!key_bits)
185296465Sdelphij            return (-1);
186296465Sdelphij        if (i > 0)
187296465Sdelphij            EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1);
188296465Sdelphij        EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
189296465Sdelphij        EVP_CIPHER_CTX_set_key_length(c, key_bits / 8);
190296465Sdelphij    }
191296465Sdelphij    return (i);
192296465Sdelphij}
19368651Skris
19468651Skrisstatic int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
195296465Sdelphij{
196296465Sdelphij    long num;
197296465Sdelphij    int i = 0, j;
19868651Skris
199296465Sdelphij    if (type != NULL) {
200296465Sdelphij        num = rc2_meth_to_magic(c);
201296465Sdelphij        j = EVP_CIPHER_CTX_iv_length(c);
202296465Sdelphij        i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j);
203296465Sdelphij    }
204296465Sdelphij    return (i);
205296465Sdelphij}
20668651Skris
20768651Skrisstatic int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
208296465Sdelphij{
209296465Sdelphij    switch (type) {
210296465Sdelphij    case EVP_CTRL_INIT:
211296465Sdelphij        data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
212296465Sdelphij        return 1;
21368651Skris
214296465Sdelphij    case EVP_CTRL_GET_RC2_KEY_BITS:
215296465Sdelphij        *(int *)ptr = data(c)->key_bits;
216296465Sdelphij        return 1;
21768651Skris
218296465Sdelphij    case EVP_CTRL_SET_RC2_KEY_BITS:
219296465Sdelphij        if (arg > 0) {
220296465Sdelphij            data(c)->key_bits = arg;
221296465Sdelphij            return 1;
222296465Sdelphij        }
223296465Sdelphij        return 0;
22468651Skris
225296465Sdelphij    default:
226296465Sdelphij        return -1;
227296465Sdelphij    }
228296465Sdelphij}
229296465Sdelphij
23068651Skris#endif
231