2 * Copyright 1995-2020 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,
60 rc2_set_asn1_type_and_iv,
61 rc2_get_asn1_type_and_iv,
66 static const EVP_CIPHER r2_40_cbc_cipher = {
68 8, 5 /* 40 bit */ , 8,
69 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
74 rc2_set_asn1_type_and_iv,
75 rc2_get_asn1_type_and_iv,
80 const EVP_CIPHER *EVP_rc2_64_cbc(void)
82 return &r2_64_cbc_cipher;
85 const EVP_CIPHER *EVP_rc2_40_cbc(void)
87 return &r2_40_cbc_cipher;
90 static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
91 const unsigned char *iv, int enc)
93 RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
94 key, data(ctx)->key_bits);
98 static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
102 if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0)
105 return RC2_128_MAGIC;
114 static int rc2_magic_to_meth(int i)
116 if (i == RC2_128_MAGIC)
118 else if (i == RC2_64_MAGIC)
120 else if (i == RC2_40_MAGIC)
123 EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
128 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
134 unsigned char iv[EVP_MAX_IV_LENGTH];
137 l = EVP_CIPHER_CTX_iv_length(c);
138 OPENSSL_assert(l <= sizeof(iv));
139 i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
142 key_bits = rc2_magic_to_meth((int)num);
145 if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
147 if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits,
149 || EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
155 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
161 num = rc2_meth_to_magic(c);
162 j = EVP_CIPHER_CTX_iv_length(c);
163 i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j);
168 static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
172 data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
175 case EVP_CTRL_GET_RC2_KEY_BITS:
176 *(int *)ptr = data(c)->key_bits;
179 case EVP_CTRL_SET_RC2_KEY_BITS:
181 data(c)->key_bits = arg;
186 case EVP_CTRL_PBE_PRF_NID:
187 *(int *)ptr = NID_hmacWithMD5;