2 * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 * RC2 low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
19 #ifndef OPENSSL_NO_RC2
21 # include <openssl/evp.h>
22 # include <openssl/objects.h>
23 # include "crypto/evp.h"
24 # include <openssl/rc2.h>
25 # include "evp_local.h"
27 static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
28 const unsigned char *iv, int enc);
29 static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
30 static int rc2_magic_to_meth(int i);
31 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
32 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
33 static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
36 int key_bits; /* effective key bits */
37 RC2_KEY ks; /* key schedule */
40 # define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx)
42 IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
44 RC2_KEY_LENGTH, 8, 64,
45 EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
47 rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
49 # define RC2_40_MAGIC 0xa0
50 # define RC2_64_MAGIC 0x78
51 # define RC2_128_MAGIC 0x3a
52 static const EVP_CIPHER r2_64_cbc_cipher = {
54 8, 8 /* 64 bit */ , 8,
55 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
61 rc2_set_asn1_type_and_iv,
62 rc2_get_asn1_type_and_iv,
67 static const EVP_CIPHER r2_40_cbc_cipher = {
69 8, 5 /* 40 bit */ , 8,
70 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
76 rc2_set_asn1_type_and_iv,
77 rc2_get_asn1_type_and_iv,
82 const EVP_CIPHER *EVP_rc2_64_cbc(void)
84 return &r2_64_cbc_cipher;
87 const EVP_CIPHER *EVP_rc2_40_cbc(void)
89 return &r2_40_cbc_cipher;
92 static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
93 const unsigned char *iv, int enc)
95 RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_get_key_length(ctx),
96 key, data(ctx)->key_bits);
100 static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
104 if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0)
107 return RC2_128_MAGIC;
116 static int rc2_magic_to_meth(int i)
118 if (i == RC2_128_MAGIC)
120 else if (i == RC2_64_MAGIC)
122 else if (i == RC2_40_MAGIC)
125 ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_SIZE);
130 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
136 unsigned char iv[EVP_MAX_IV_LENGTH];
139 l = EVP_CIPHER_CTX_get_iv_length(c);
140 OPENSSL_assert(l <= sizeof(iv));
141 i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
144 key_bits = rc2_magic_to_meth((int)num);
147 if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
149 if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits,
151 || EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
157 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
163 num = rc2_meth_to_magic(c);
164 j = EVP_CIPHER_CTX_get_iv_length(c);
165 i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j);
170 static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
174 data(c)->key_bits = EVP_CIPHER_CTX_get_key_length(c) * 8;
177 case EVP_CTRL_GET_RC2_KEY_BITS:
178 *(int *)ptr = data(c)->key_bits;
181 case EVP_CTRL_SET_RC2_KEY_BITS:
183 data(c)->key_bits = arg;
188 case EVP_CTRL_PBE_PRF_NID:
189 *(int *)ptr = NID_hmacWithMD5;