e_rc2.c revision 1.14
1240330Smarcel/* $OpenBSD: e_rc2.c,v 1.14 2022/01/20 11:31:37 inoguchi Exp $ */
2240330Smarcel/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3240330Smarcel * All rights reserved.
4240330Smarcel *
5240330Smarcel * This package is an SSL implementation written
6240330Smarcel * by Eric Young (eay@cryptsoft.com).
7240330Smarcel * The implementation was written so as to conform with Netscapes SSL.
8240330Smarcel *
9240330Smarcel * This library is free for commercial and non-commercial use as long as
10240330Smarcel * the following conditions are aheared to.  The following conditions
11240330Smarcel * apply to all code found in this distribution, be it the RC4, RSA,
12240330Smarcel * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13240330Smarcel * included with this distribution is covered by the same copyright terms
14240330Smarcel * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15240330Smarcel *
16240330Smarcel * Copyright remains Eric Young's, and as such any Copyright notices in
17240330Smarcel * the code are not to be removed.
18240330Smarcel * If this package is used in a product, Eric Young should be given attribution
19240330Smarcel * as the author of the parts of the library used.
20238152Sobrien * This can be in the form of a textual message at program startup or
21238152Sobrien * in documentation (online or textual) provided with the package.
22238152Sobrien *
23238152Sobrien * Redistribution and use in source and binary forms, with or without
24238152Sobrien * modification, are permitted provided that the following conditions
25238152Sobrien * are met:
26238152Sobrien * 1. Redistributions of source code must retain the copyright
27238152Sobrien *    notice, this list of conditions and the following disclaimer.
28238152Sobrien * 2. Redistributions in binary form must reproduce the above copyright
29238152Sobrien *    notice, this list of conditions and the following disclaimer in the
30238152Sobrien *    documentation and/or other materials provided with the distribution.
31238152Sobrien * 3. All advertising materials mentioning features or use of this software
32238152Sobrien *    must display the following acknowledgement:
33238152Sobrien *    "This product includes cryptographic software written by
34238152Sobrien *     Eric Young (eay@cryptsoft.com)"
35238152Sobrien *    The word 'cryptographic' can be left out if the rouines from the library
36237578Sobrien *    being used are not cryptographic related :-).
37237578Sobrien * 4. If you include any Windows specific code (or a derivative thereof) from
38237578Sobrien *    the apps directory (application code) you must include an acknowledgement:
39237578Sobrien *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40237578Sobrien *
41237578Sobrien * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42237578Sobrien * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43237578Sobrien * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44237578Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45237578Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46237578Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47237578Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48237578Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49237578Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50237578Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51237578Sobrien * SUCH DAMAGE.
52237578Sobrien *
53237578Sobrien * The licence and distribution terms for any publically available version or
54237578Sobrien * derivative of this code cannot be changed.  i.e. this code cannot simply be
55237578Sobrien * copied and put under another distribution licence
56237578Sobrien * [including the GNU Public Licence.]
57236769Sobrien */
58236769Sobrien
59236769Sobrien#include <stdio.h>
60236769Sobrien
61236769Sobrien#include <openssl/opensslconf.h>
62236769Sobrien
63236769Sobrien#ifndef OPENSSL_NO_RC2
64236769Sobrien
65236769Sobrien#include <openssl/err.h>
66236769Sobrien#include <openssl/evp.h>
67236769Sobrien#include <openssl/objects.h>
68236769Sobrien#include <openssl/rc2.h>
69236769Sobrien
70236769Sobrien#include "evp_locl.h"
71236769Sobrien
72236769Sobrienstatic int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
73236769Sobrien    const unsigned char *iv, int enc);
74236769Sobrienstatic int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
75236769Sobrienstatic int rc2_magic_to_meth(int i);
76236769Sobrienstatic int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
77236769Sobrienstatic int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
78236769Sobrienstatic int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
79236769Sobrien
80236769Sobrientypedef struct {
81236769Sobrien	int key_bits;	/* effective key bits */
82236769Sobrien	RC2_KEY ks;	/* key schedule */
83236769Sobrien} EVP_RC2_KEY;
84236769Sobrien
85236769Sobrien#define data(ctx)	((EVP_RC2_KEY *)(ctx)->cipher_data)
86236769Sobrien
87236769SobrienIMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
88236769Sobrien    8,
89236769Sobrien    RC2_KEY_LENGTH, 8, 64,
90236769Sobrien    EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
91236769Sobrien    rc2_init_key, NULL,
92236769Sobrien    rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
93236769Sobrien    rc2_ctrl)
94236769Sobrien
95236769Sobrien#define RC2_40_MAGIC	0xa0
96236769Sobrien#define RC2_64_MAGIC	0x78
97236769Sobrien#define RC2_128_MAGIC	0x3a
98236769Sobrien
99236769Sobrienstatic const EVP_CIPHER r2_64_cbc_cipher = {
100236769Sobrien	NID_rc2_64_cbc,
101236769Sobrien	8, 8 /* 64 bit */, 8,
102236769Sobrien	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
103236769Sobrien	rc2_init_key,
104236769Sobrien	rc2_cbc_cipher,
105236769Sobrien	NULL,
106236769Sobrien	sizeof(EVP_RC2_KEY),
107236769Sobrien	rc2_set_asn1_type_and_iv,
108236769Sobrien	rc2_get_asn1_type_and_iv,
109236769Sobrien	rc2_ctrl,
110236769Sobrien	NULL
111236769Sobrien};
112236769Sobrien
113236769Sobrienstatic const EVP_CIPHER r2_40_cbc_cipher = {
114236769Sobrien	NID_rc2_40_cbc,
115236769Sobrien	8, 5 /* 40 bit */, 8,
116236769Sobrien	EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
117236769Sobrien	rc2_init_key,
118236769Sobrien	rc2_cbc_cipher,
119236769Sobrien	NULL,
120236769Sobrien	sizeof(EVP_RC2_KEY),
121236769Sobrien	rc2_set_asn1_type_and_iv,
122236769Sobrien	rc2_get_asn1_type_and_iv,
123236769Sobrien	rc2_ctrl,
124236769Sobrien	NULL
125236769Sobrien};
126236769Sobrien
127236769Sobrienconst EVP_CIPHER *
128236769SobrienEVP_rc2_64_cbc(void)
129236769Sobrien{
130236769Sobrien	return (&r2_64_cbc_cipher);
131236769Sobrien}
132236769Sobrien
133236769Sobrienconst EVP_CIPHER *
134236769SobrienEVP_rc2_40_cbc(void)
135236769Sobrien{
136236769Sobrien	return (&r2_40_cbc_cipher);
137236769Sobrien}
138236769Sobrien
139236769Sobrienstatic int
140236769Sobrienrc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
141236769Sobrien    const unsigned char *iv, int enc)
142236769Sobrien{
143236769Sobrien	RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
144236769Sobrien	    key, data(ctx)->key_bits);
145236769Sobrien	return 1;
146236769Sobrien}
147236769Sobrien
148236769Sobrienstatic int
149236769Sobrienrc2_meth_to_magic(EVP_CIPHER_CTX *e)
150236769Sobrien{
151236769Sobrien	int i;
152236769Sobrien
153236769Sobrien	if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0)
154236769Sobrien		return (0);
155236769Sobrien	if (i == 128)
156236769Sobrien		return (RC2_128_MAGIC);
157236769Sobrien	else if (i == 64)
158236769Sobrien		return (RC2_64_MAGIC);
159236769Sobrien	else if (i == 40)
160236769Sobrien		return (RC2_40_MAGIC);
161236769Sobrien	else
162236769Sobrien		return (0);
163236769Sobrien}
164236769Sobrien
165236769Sobrienstatic int
166236769Sobrienrc2_magic_to_meth(int i)
167236769Sobrien{
168236769Sobrien	if (i == RC2_128_MAGIC)
169236769Sobrien		return 128;
170236769Sobrien	else if (i == RC2_64_MAGIC)
171236769Sobrien		return 64;
172236769Sobrien	else if (i == RC2_40_MAGIC)
173236769Sobrien		return 40;
174236769Sobrien	else {
175236769Sobrien		EVPerror(EVP_R_UNSUPPORTED_KEY_SIZE);
176236769Sobrien		return (0);
177236769Sobrien	}
178236769Sobrien}
179236769Sobrien
180236769Sobrienstatic int
181236769Sobrienrc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
182236769Sobrien{
183236769Sobrien	long num = 0;
184236769Sobrien	int i = 0;
185236769Sobrien	int key_bits;
186236769Sobrien	unsigned int l;
187236769Sobrien	unsigned char iv[EVP_MAX_IV_LENGTH];
188236769Sobrien
189236769Sobrien	if (type != NULL) {
190236769Sobrien		l = EVP_CIPHER_CTX_iv_length(c);
191236769Sobrien		if (l > sizeof(iv)) {
192236769Sobrien			EVPerror(EVP_R_IV_TOO_LARGE);
193236769Sobrien			return -1;
194236769Sobrien		}
195236769Sobrien		i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
196236769Sobrien		if (i != (int)l)
197236769Sobrien			return (-1);
198236769Sobrien		key_bits = rc2_magic_to_meth((int)num);
199236769Sobrien		if (!key_bits)
200236769Sobrien			return (-1);
201236769Sobrien		if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
202236769Sobrien			return -1;
203236769Sobrien		if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS,
204236769Sobrien		    key_bits, NULL) <= 0)
205236769Sobrien			return -1;
206236769Sobrien		if (!EVP_CIPHER_CTX_set_key_length(c, key_bits / 8))
207236769Sobrien			return -1;
208236769Sobrien	}
209236769Sobrien	return (i);
210236769Sobrien}
211236769Sobrien
212236769Sobrienstatic int
213236769Sobrienrc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
214236769Sobrien{
215236769Sobrien	long num;
216236769Sobrien	int i = 0, j;
217236769Sobrien
218236769Sobrien	if (type != NULL) {
219236769Sobrien		num = rc2_meth_to_magic(c);
220236769Sobrien		j = EVP_CIPHER_CTX_iv_length(c);
221236769Sobrien		i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j);
222236769Sobrien	}
223236769Sobrien	return (i);
224236769Sobrien}
225236769Sobrien
226236769Sobrienstatic int
227236769Sobrienrc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
228236769Sobrien{
229236769Sobrien	switch (type) {
230236769Sobrien	case EVP_CTRL_INIT:
231236769Sobrien		data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
232236769Sobrien		return 1;
233236769Sobrien
234236769Sobrien	case EVP_CTRL_GET_RC2_KEY_BITS:
235236769Sobrien		*(int *)ptr = data(c)->key_bits;
236236769Sobrien		return 1;
237236769Sobrien
238236769Sobrien	case EVP_CTRL_SET_RC2_KEY_BITS:
239236769Sobrien		if (arg > 0) {
240236769Sobrien			data(c)->key_bits = arg;
241236769Sobrien			return 1;
242236769Sobrien		}
243236769Sobrien		return 0;
244236769Sobrien
245236769Sobrien#ifdef PBE_PRF_TEST
246236769Sobrien	case EVP_CTRL_PBE_PRF_NID:
247236769Sobrien		*(int *)ptr = NID_hmacWithMD5;
248236769Sobrien		return 1;
249236769Sobrien#endif
250236769Sobrien
251236769Sobrien	default:
252236769Sobrien		return -1;
253236769Sobrien	}
254236769Sobrien}
255236769Sobrien
256236769Sobrien#endif
257236769Sobrien