]>
Commit | Line | Data |
---|---|---|
62867571 | 1 | /* |
c486283c | 2 | * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved. |
d02b48c6 | 3 | * |
4a8b0c55 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
d02b48c6 RE |
8 | */ |
9 | ||
ee2993ab P |
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 | ||
d02b48c6 | 16 | #include <stdio.h> |
b39fc560 | 17 | #include "internal/cryptlib.h" |
c7e7fc3e RL |
18 | |
19 | #ifndef OPENSSL_NO_RC2 | |
20 | ||
0f113f3e MC |
21 | # include <openssl/evp.h> |
22 | # include <openssl/objects.h> | |
25f2138b | 23 | # include "crypto/evp.h" |
0f113f3e | 24 | # include <openssl/rc2.h> |
d02b48c6 | 25 | |
1921eaad | 26 | static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e | 27 | const unsigned char *iv, int enc); |
49528751 DSH |
28 | static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx); |
29 | static int rc2_magic_to_meth(int i); | |
dfeab068 RE |
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); | |
49528751 | 32 | static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); |
dfeab068 | 33 | |
0f113f3e MC |
34 | typedef struct { |
35 | int key_bits; /* effective key bits */ | |
36 | RC2_KEY ks; /* key schedule */ | |
37 | } EVP_RC2_KEY; | |
dbad1690 | 38 | |
6435f0f6 | 39 | # define data(ctx) EVP_C_DATA(EVP_RC2_KEY,ctx) |
dbad1690 BL |
40 | |
41 | IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2, | |
0f113f3e MC |
42 | 8, |
43 | RC2_KEY_LENGTH, 8, 64, | |
44 | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, | |
45 | rc2_init_key, NULL, | |
46 | rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, | |
47 | rc2_ctrl) | |
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 = { | |
52 | NID_rc2_64_cbc, | |
53 | 8, 8 /* 64 bit */ , 8, | |
54 | EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, | |
55 | rc2_init_key, | |
56 | rc2_cbc_cipher, | |
57 | NULL, | |
58 | sizeof(EVP_RC2_KEY), | |
59 | rc2_set_asn1_type_and_iv, | |
60 | rc2_get_asn1_type_and_iv, | |
61 | rc2_ctrl, | |
62 | NULL | |
63 | }; | |
64 | ||
65 | static const EVP_CIPHER r2_40_cbc_cipher = { | |
66 | NID_rc2_40_cbc, | |
67 | 8, 5 /* 40 bit */ , 8, | |
68 | EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, | |
69 | rc2_init_key, | |
70 | rc2_cbc_cipher, | |
71 | NULL, | |
72 | sizeof(EVP_RC2_KEY), | |
73 | rc2_set_asn1_type_and_iv, | |
74 | rc2_get_asn1_type_and_iv, | |
75 | rc2_ctrl, | |
76 | NULL | |
77 | }; | |
d02b48c6 | 78 | |
13588350 | 79 | const EVP_CIPHER *EVP_rc2_64_cbc(void) |
0f113f3e | 80 | { |
26a7d938 | 81 | return &r2_64_cbc_cipher; |
0f113f3e | 82 | } |
dfeab068 | 83 | |
13588350 | 84 | const EVP_CIPHER *EVP_rc2_40_cbc(void) |
0f113f3e | 85 | { |
26a7d938 | 86 | return &r2_40_cbc_cipher; |
0f113f3e MC |
87 | } |
88 | ||
1921eaad | 89 | static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
0f113f3e MC |
90 | const unsigned char *iv, int enc) |
91 | { | |
92 | RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), | |
93 | key, data(ctx)->key_bits); | |
94 | return 1; | |
95 | } | |
d02b48c6 | 96 | |
49528751 | 97 | static int rc2_meth_to_magic(EVP_CIPHER_CTX *e) |
0f113f3e MC |
98 | { |
99 | int i; | |
100 | ||
434893af MC |
101 | if (EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i) <= 0) |
102 | return 0; | |
0f113f3e | 103 | if (i == 128) |
26a7d938 | 104 | return RC2_128_MAGIC; |
0f113f3e | 105 | else if (i == 64) |
26a7d938 | 106 | return RC2_64_MAGIC; |
0f113f3e | 107 | else if (i == 40) |
26a7d938 | 108 | return RC2_40_MAGIC; |
0f113f3e | 109 | else |
26a7d938 | 110 | return 0; |
0f113f3e | 111 | } |
dfeab068 | 112 | |
49528751 | 113 | static int rc2_magic_to_meth(int i) |
0f113f3e MC |
114 | { |
115 | if (i == RC2_128_MAGIC) | |
116 | return 128; | |
117 | else if (i == RC2_64_MAGIC) | |
118 | return 64; | |
119 | else if (i == RC2_40_MAGIC) | |
120 | return 40; | |
121 | else { | |
122 | EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE); | |
26a7d938 | 123 | return 0; |
0f113f3e MC |
124 | } |
125 | } | |
dfeab068 | 126 | |
6b691a5c | 127 | static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) |
0f113f3e MC |
128 | { |
129 | long num = 0; | |
130 | int i = 0; | |
131 | int key_bits; | |
132 | unsigned int l; | |
133 | unsigned char iv[EVP_MAX_IV_LENGTH]; | |
134 | ||
135 | if (type != NULL) { | |
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); | |
139 | if (i != (int)l) | |
d356dc56 | 140 | return -1; |
0f113f3e MC |
141 | key_bits = rc2_magic_to_meth((int)num); |
142 | if (!key_bits) | |
d356dc56 | 143 | return -1; |
0f113f3e MC |
144 | if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1)) |
145 | return -1; | |
434893af MC |
146 | if (EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, |
147 | NULL) <= 0 | |
148 | || EVP_CIPHER_CTX_set_key_length(c, key_bits / 8) <= 0) | |
d356dc56 | 149 | return -1; |
0f113f3e | 150 | } |
d356dc56 | 151 | return i; |
0f113f3e | 152 | } |
dfeab068 | 153 | |
6b691a5c | 154 | static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) |
0f113f3e MC |
155 | { |
156 | long num; | |
157 | int i = 0, j; | |
158 | ||
159 | if (type != NULL) { | |
160 | num = rc2_meth_to_magic(c); | |
161 | j = EVP_CIPHER_CTX_iv_length(c); | |
936166af RL |
162 | i = ASN1_TYPE_set_int_octetstring(type, num, |
163 | (unsigned char *)EVP_CIPHER_CTX_original_iv(c), | |
164 | j); | |
0f113f3e | 165 | } |
26a7d938 | 166 | return i; |
0f113f3e | 167 | } |
dfeab068 | 168 | |
49528751 | 169 | static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
0f113f3e MC |
170 | { |
171 | switch (type) { | |
172 | case EVP_CTRL_INIT: | |
173 | data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8; | |
174 | return 1; | |
175 | ||
176 | case EVP_CTRL_GET_RC2_KEY_BITS: | |
177 | *(int *)ptr = data(c)->key_bits; | |
178 | return 1; | |
179 | ||
180 | case EVP_CTRL_SET_RC2_KEY_BITS: | |
181 | if (arg > 0) { | |
182 | data(c)->key_bits = arg; | |
183 | return 1; | |
184 | } | |
185 | return 0; | |
186 | # ifdef PBE_PRF_TEST | |
187 | case EVP_CTRL_PBE_PRF_NID: | |
188 | *(int *)ptr = NID_hmacWithMD5; | |
189 | return 1; | |
190 | # endif | |
191 | ||
192 | default: | |
193 | return -1; | |
194 | } | |
195 | } | |
49528751 | 196 | |
d02b48c6 | 197 | #endif |