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>
26 static int rc2_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
27 const unsigned char *iv
, int enc
);
28 static int rc2_meth_to_magic(EVP_CIPHER_CTX
*ctx
);
29 static int rc2_magic_to_meth(int i
);
30 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
);
31 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
);
32 static int rc2_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
);
35 int key_bits
; /* effective key bits */
36 RC2_KEY ks
; /* key schedule */
39 # define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx)
41 IMPLEMENT_BLOCK_CIPHER(rc2
, ks
, RC2
, EVP_RC2_KEY
, NID_rc2
,
43 RC2_KEY_LENGTH
, 8, 64,
44 EVP_CIPH_VARIABLE_LENGTH
| EVP_CIPH_CTRL_INIT
,
46 rc2_set_asn1_type_and_iv
, rc2_get_asn1_type_and_iv
,
48 # define RC2_40_MAGIC 0xa0
49 # define RC2_64_MAGIC 0x78
50 # define RC2_128_MAGIC 0x3a
51 static const EVP_CIPHER r2_64_cbc_cipher
= {
53 8, 8 /* 64 bit */ , 8,
54 EVP_CIPH_CBC_MODE
| EVP_CIPH_VARIABLE_LENGTH
| EVP_CIPH_CTRL_INIT
,
59 rc2_set_asn1_type_and_iv
,
60 rc2_get_asn1_type_and_iv
,
65 static const EVP_CIPHER r2_40_cbc_cipher
= {
67 8, 5 /* 40 bit */ , 8,
68 EVP_CIPH_CBC_MODE
| EVP_CIPH_VARIABLE_LENGTH
| EVP_CIPH_CTRL_INIT
,
73 rc2_set_asn1_type_and_iv
,
74 rc2_get_asn1_type_and_iv
,
79 const EVP_CIPHER
*EVP_rc2_64_cbc(void)
81 return &r2_64_cbc_cipher
;
84 const EVP_CIPHER
*EVP_rc2_40_cbc(void)
86 return &r2_40_cbc_cipher
;
89 static int rc2_init_key(EVP_CIPHER_CTX
*ctx
, const unsigned char *key
,
90 const unsigned char *iv
, int enc
)
92 RC2_set_key(&data(ctx
)->ks
, EVP_CIPHER_CTX_key_length(ctx
),
93 key
, data(ctx
)->key_bits
);
97 static int rc2_meth_to_magic(EVP_CIPHER_CTX
*e
)
101 if (EVP_CIPHER_CTX_ctrl(e
, EVP_CTRL_GET_RC2_KEY_BITS
, 0, &i
) <= 0)
104 return RC2_128_MAGIC
;
113 static int rc2_magic_to_meth(int i
)
115 if (i
== RC2_128_MAGIC
)
117 else if (i
== RC2_64_MAGIC
)
119 else if (i
== RC2_40_MAGIC
)
122 EVPerr(EVP_F_RC2_MAGIC_TO_METH
, EVP_R_UNSUPPORTED_KEY_SIZE
);
127 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
133 unsigned char iv
[EVP_MAX_IV_LENGTH
];
136 l
= EVP_CIPHER_CTX_iv_length(c
);
137 OPENSSL_assert(l
<= sizeof(iv
));
138 i
= ASN1_TYPE_get_int_octetstring(type
, &num
, iv
, l
);
141 key_bits
= rc2_magic_to_meth((int)num
);
144 if (i
> 0 && !EVP_CipherInit_ex(c
, NULL
, NULL
, NULL
, iv
, -1))
146 if (EVP_CIPHER_CTX_ctrl(c
, EVP_CTRL_SET_RC2_KEY_BITS
, key_bits
,
148 || EVP_CIPHER_CTX_set_key_length(c
, key_bits
/ 8) <= 0)
154 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX
*c
, ASN1_TYPE
*type
)
160 num
= rc2_meth_to_magic(c
);
161 j
= EVP_CIPHER_CTX_iv_length(c
);
162 i
= ASN1_TYPE_set_int_octetstring(type
, num
,
163 (unsigned char *)EVP_CIPHER_CTX_original_iv(c
),
169 static int rc2_ctrl(EVP_CIPHER_CTX
*c
, int type
, int arg
, void *ptr
)
173 data(c
)->key_bits
= EVP_CIPHER_CTX_key_length(c
) * 8;
176 case EVP_CTRL_GET_RC2_KEY_BITS
:
177 *(int *)ptr
= data(c
)->key_bits
;
180 case EVP_CTRL_SET_RC2_KEY_BITS
:
182 data(c
)->key_bits
= arg
;
187 case EVP_CTRL_PBE_PRF_NID
:
188 *(int *)ptr
= NID_hmacWithMD5
;