2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 #include "internal/cryptlib.h"
13 #ifndef OPENSSL_NO_RC2
15 # include <openssl/evp.h>
16 # include <openssl/objects.h>
17 # include "internal/evp_int.h"
18 # include <openssl/rc2.h>
20 static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
21 const unsigned char *iv, int enc);
22 static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx);
23 static int rc2_magic_to_meth(int i);
24 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
25 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type);
26 static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
29 int key_bits; /* effective key bits */
30 RC2_KEY ks; /* key schedule */
33 # define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx)
35 IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
37 RC2_KEY_LENGTH, 8, 64,
38 EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
40 rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
42 # define RC2_40_MAGIC 0xa0
43 # define RC2_64_MAGIC 0x78
44 # define RC2_128_MAGIC 0x3a
45 static const EVP_CIPHER r2_64_cbc_cipher = {
47 8, 8 /* 64 bit */ , 8,
48 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
53 rc2_set_asn1_type_and_iv,
54 rc2_get_asn1_type_and_iv,
59 static const EVP_CIPHER r2_40_cbc_cipher = {
61 8, 5 /* 40 bit */ , 8,
62 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
67 rc2_set_asn1_type_and_iv,
68 rc2_get_asn1_type_and_iv,
73 const EVP_CIPHER *EVP_rc2_64_cbc(void)
75 return (&r2_64_cbc_cipher);
78 const EVP_CIPHER *EVP_rc2_40_cbc(void)
80 return (&r2_40_cbc_cipher);
83 static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
84 const unsigned char *iv, int enc)
86 RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
87 key, data(ctx)->key_bits);
91 static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
95 EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i);
97 return (RC2_128_MAGIC);
99 return (RC2_64_MAGIC);
101 return (RC2_40_MAGIC);
106 static int rc2_magic_to_meth(int i)
108 if (i == RC2_128_MAGIC)
110 else if (i == RC2_64_MAGIC)
112 else if (i == RC2_40_MAGIC)
115 EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
120 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
126 unsigned char iv[EVP_MAX_IV_LENGTH];
129 l = EVP_CIPHER_CTX_iv_length(c);
130 OPENSSL_assert(l <= sizeof(iv));
131 i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l);
134 key_bits = rc2_magic_to_meth((int)num);
137 if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
139 EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL);
140 if (EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
146 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
152 num = rc2_meth_to_magic(c);
153 j = EVP_CIPHER_CTX_iv_length(c);
154 i = ASN1_TYPE_set_int_octetstring(type, num,
155 (unsigned char *)EVP_CIPHER_CTX_original_iv(c),
161 static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
165 data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
168 case EVP_CTRL_GET_RC2_KEY_BITS:
169 *(int *)ptr = data(c)->key_bits;
172 case EVP_CTRL_SET_RC2_KEY_BITS:
174 data(c)->key_bits = arg;
179 case EVP_CTRL_PBE_PRF_NID:
180 *(int *)ptr = NID_hmacWithMD5;