]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/e_rc2.c
Rename OPENSSL_CTX prefix to OSSL_LIB_CTX
[thirdparty/openssl.git] / crypto / evp / e_rc2.c
1 /*
2 * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 /*
11 * RC2 low level APIs are deprecated for public use, but still ok for internal
12 * use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <stdio.h>
17 #include "internal/cryptlib.h"
18
19 #ifndef OPENSSL_NO_RC2
20
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"
26
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);
34
35 typedef struct {
36 int key_bits; /* effective key bits */
37 RC2_KEY ks; /* key schedule */
38 } EVP_RC2_KEY;
39
40 # define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx)
41
42 IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2,
43 8,
44 RC2_KEY_LENGTH, 8, 64,
45 EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
46 rc2_init_key, NULL,
47 rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv,
48 rc2_ctrl)
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 = {
53 NID_rc2_64_cbc,
54 8, 8 /* 64 bit */ , 8,
55 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
56 rc2_init_key,
57 rc2_cbc_cipher,
58 NULL,
59 sizeof(EVP_RC2_KEY),
60 rc2_set_asn1_type_and_iv,
61 rc2_get_asn1_type_and_iv,
62 rc2_ctrl,
63 NULL
64 };
65
66 static const EVP_CIPHER r2_40_cbc_cipher = {
67 NID_rc2_40_cbc,
68 8, 5 /* 40 bit */ , 8,
69 EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT,
70 rc2_init_key,
71 rc2_cbc_cipher,
72 NULL,
73 sizeof(EVP_RC2_KEY),
74 rc2_set_asn1_type_and_iv,
75 rc2_get_asn1_type_and_iv,
76 rc2_ctrl,
77 NULL
78 };
79
80 const EVP_CIPHER *EVP_rc2_64_cbc(void)
81 {
82 return &r2_64_cbc_cipher;
83 }
84
85 const EVP_CIPHER *EVP_rc2_40_cbc(void)
86 {
87 return &r2_40_cbc_cipher;
88 }
89
90 static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
91 const unsigned char *iv, int enc)
92 {
93 RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx),
94 key, data(ctx)->key_bits);
95 return 1;
96 }
97
98 static int rc2_meth_to_magic(EVP_CIPHER_CTX *e)
99 {
100 int i;
101
102 if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0)
103 return 0;
104 if (i == 128)
105 return RC2_128_MAGIC;
106 else if (i == 64)
107 return RC2_64_MAGIC;
108 else if (i == 40)
109 return RC2_40_MAGIC;
110 else
111 return 0;
112 }
113
114 static int rc2_magic_to_meth(int i)
115 {
116 if (i == RC2_128_MAGIC)
117 return 128;
118 else if (i == RC2_64_MAGIC)
119 return 64;
120 else if (i == RC2_40_MAGIC)
121 return 40;
122 else {
123 EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE);
124 return 0;
125 }
126 }
127
128 static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
129 {
130 long num = 0;
131 int i = 0;
132 int key_bits;
133 unsigned int l;
134 unsigned char iv[EVP_MAX_IV_LENGTH];
135
136 if (type != NULL) {
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);
140 if (i != (int)l)
141 return -1;
142 key_bits = rc2_magic_to_meth((int)num);
143 if (!key_bits)
144 return -1;
145 if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1))
146 return -1;
147 if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits,
148 NULL) <= 0
149 || EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0)
150 return -1;
151 }
152 return i;
153 }
154
155 static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
156 {
157 long num;
158 int i = 0, j;
159
160 if (type != NULL) {
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);
164 }
165 return i;
166 }
167
168 static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
169 {
170 switch (type) {
171 case EVP_CTRL_INIT:
172 data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8;
173 return 1;
174
175 case EVP_CTRL_GET_RC2_KEY_BITS:
176 *(int *)ptr = data(c)->key_bits;
177 return 1;
178
179 case EVP_CTRL_SET_RC2_KEY_BITS:
180 if (arg > 0) {
181 data(c)->key_bits = arg;
182 return 1;
183 }
184 return 0;
185 # ifdef PBE_PRF_TEST
186 case EVP_CTRL_PBE_PRF_NID:
187 *(int *)ptr = NID_hmacWithMD5;
188 return 1;
189 # endif
190
191 default:
192 return -1;
193 }
194 }
195
196 #endif