diff options
Diffstat (limited to 'src/lib/libcrypto/evp/e_rc2.c')
-rw-r--r-- | src/lib/libcrypto/evp/e_rc2.c | 193 |
1 files changed, 101 insertions, 92 deletions
diff --git a/src/lib/libcrypto/evp/e_rc2.c b/src/lib/libcrypto/evp/e_rc2.c index d4c33b58d4..f6f4504890 100644 --- a/src/lib/libcrypto/evp/e_rc2.c +++ b/src/lib/libcrypto/evp/e_rc2.c | |||
@@ -5,21 +5,21 @@ | |||
5 | * This package is an SSL implementation written | 5 | * This package is an SSL implementation written |
6 | * by Eric Young (eay@cryptsoft.com). | 6 | * by Eric Young (eay@cryptsoft.com). |
7 | * The implementation was written so as to conform with Netscapes SSL. | 7 | * The implementation was written so as to conform with Netscapes SSL. |
8 | * | 8 | * |
9 | * This library is free for commercial and non-commercial use as long as | 9 | * This library is free for commercial and non-commercial use as long as |
10 | * the following conditions are aheared to. The following conditions | 10 | * the following conditions are aheared to. The following conditions |
11 | * apply to all code found in this distribution, be it the RC4, RSA, | 11 | * apply to all code found in this distribution, be it the RC4, RSA, |
12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
13 | * included with this distribution is covered by the same copyright terms | 13 | * included with this distribution is covered by the same copyright terms |
14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
15 | * | 15 | * |
16 | * Copyright remains Eric Young's, and as such any Copyright notices in | 16 | * Copyright remains Eric Young's, and as such any Copyright notices in |
17 | * the code are not to be removed. | 17 | * the code are not to be removed. |
18 | * If this package is used in a product, Eric Young should be given attribution | 18 | * If this package is used in a product, Eric Young should be given attribution |
19 | * as the author of the parts of the library used. | 19 | * as the author of the parts of the library used. |
20 | * This can be in the form of a textual message at program startup or | 20 | * This can be in the form of a textual message at program startup or |
21 | * in documentation (online or textual) provided with the package. | 21 | * in documentation (online or textual) provided with the package. |
22 | * | 22 | * |
23 | * Redistribution and use in source and binary forms, with or without | 23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | 24 | * modification, are permitted provided that the following conditions |
25 | * are met: | 25 | * are met: |
@@ -34,10 +34,10 @@ | |||
34 | * Eric Young (eay@cryptsoft.com)" | 34 | * Eric Young (eay@cryptsoft.com)" |
35 | * The word 'cryptographic' can be left out if the rouines from the library | 35 | * The word 'cryptographic' can be left out if the rouines from the library |
36 | * being used are not cryptographic related :-). | 36 | * being used are not cryptographic related :-). |
37 | * 4. If you include any Windows specific code (or a derivative thereof) from | 37 | * 4. If you include any Windows specific code (or a derivative thereof) from |
38 | * the apps directory (application code) you must include an acknowledgement: | 38 | * the apps directory (application code) you must include an acknowledgement: |
39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
40 | * | 40 | * |
41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
@@ -49,7 +49,7 @@ | |||
49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
51 | * SUCH DAMAGE. | 51 | * SUCH DAMAGE. |
52 | * | 52 | * |
53 | * The licence and distribution terms for any publically available version or | 53 | * The licence and distribution terms for any publically available version or |
54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
55 | * copied and put under another distribution licence | 55 | * copied and put under another distribution licence |
@@ -67,37 +67,35 @@ | |||
67 | #include <openssl/rc2.h> | 67 | #include <openssl/rc2.h> |
68 | 68 | ||
69 | static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 69 | static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
70 | const unsigned char *iv,int enc); | 70 | const unsigned char *iv, int enc); |
71 | static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx); | 71 | static int rc2_meth_to_magic(EVP_CIPHER_CTX *ctx); |
72 | static int rc2_magic_to_meth(int i); | 72 | static int rc2_magic_to_meth(int i); |
73 | static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); | 73 | static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); |
74 | static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); | 74 | static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type); |
75 | static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); | 75 | static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); |
76 | 76 | ||
77 | typedef struct | 77 | typedef struct { |
78 | { | ||
79 | int key_bits; /* effective key bits */ | 78 | int key_bits; /* effective key bits */ |
80 | RC2_KEY ks; /* key schedule */ | 79 | RC2_KEY ks; /* key schedule */ |
81 | } EVP_RC2_KEY; | 80 | } EVP_RC2_KEY; |
82 | 81 | ||
83 | #define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data) | 82 | #define data(ctx) ((EVP_RC2_KEY *)(ctx)->cipher_data) |
84 | 83 | ||
85 | IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2, | 84 | IMPLEMENT_BLOCK_CIPHER(rc2, ks, RC2, EVP_RC2_KEY, NID_rc2, |
86 | 8, | 85 | 8, |
87 | RC2_KEY_LENGTH, 8, 64, | 86 | RC2_KEY_LENGTH, 8, 64, |
88 | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, | 87 | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, |
89 | rc2_init_key, NULL, | 88 | rc2_init_key, NULL, |
90 | rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, | 89 | rc2_set_asn1_type_and_iv, rc2_get_asn1_type_and_iv, |
91 | rc2_ctrl) | 90 | rc2_ctrl) |
92 | 91 | ||
93 | #define RC2_40_MAGIC 0xa0 | 92 | #define RC2_40_MAGIC 0xa0 |
94 | #define RC2_64_MAGIC 0x78 | 93 | #define RC2_64_MAGIC 0x78 |
95 | #define RC2_128_MAGIC 0x3a | 94 | #define RC2_128_MAGIC 0x3a |
96 | 95 | ||
97 | static const EVP_CIPHER r2_64_cbc_cipher= | 96 | static const EVP_CIPHER r2_64_cbc_cipher = { |
98 | { | ||
99 | NID_rc2_64_cbc, | 97 | NID_rc2_64_cbc, |
100 | 8,8 /* 64 bit */,8, | 98 | 8, 8 /* 64 bit */, 8, |
101 | EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, | 99 | EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, |
102 | rc2_init_key, | 100 | rc2_init_key, |
103 | rc2_cbc_cipher, | 101 | rc2_cbc_cipher, |
@@ -107,12 +105,11 @@ static const EVP_CIPHER r2_64_cbc_cipher= | |||
107 | rc2_get_asn1_type_and_iv, | 105 | rc2_get_asn1_type_and_iv, |
108 | rc2_ctrl, | 106 | rc2_ctrl, |
109 | NULL | 107 | NULL |
110 | }; | 108 | }; |
111 | 109 | ||
112 | static const EVP_CIPHER r2_40_cbc_cipher= | 110 | static const EVP_CIPHER r2_40_cbc_cipher = { |
113 | { | ||
114 | NID_rc2_40_cbc, | 111 | NID_rc2_40_cbc, |
115 | 8,5 /* 40 bit */,8, | 112 | 8, 5 /* 40 bit */, 8, |
116 | EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, | 113 | EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | EVP_CIPH_CTRL_INIT, |
117 | rc2_init_key, | 114 | rc2_init_key, |
118 | rc2_cbc_cipher, | 115 | rc2_cbc_cipher, |
@@ -122,93 +119,105 @@ static const EVP_CIPHER r2_40_cbc_cipher= | |||
122 | rc2_get_asn1_type_and_iv, | 119 | rc2_get_asn1_type_and_iv, |
123 | rc2_ctrl, | 120 | rc2_ctrl, |
124 | NULL | 121 | NULL |
125 | }; | 122 | }; |
126 | 123 | ||
127 | const EVP_CIPHER *EVP_rc2_64_cbc(void) | 124 | const EVP_CIPHER * |
128 | { | 125 | EVP_rc2_64_cbc(void) |
129 | return(&r2_64_cbc_cipher); | 126 | { |
130 | } | 127 | return (&r2_64_cbc_cipher); |
131 | 128 | } | |
132 | const EVP_CIPHER *EVP_rc2_40_cbc(void) | 129 | |
133 | { | 130 | const EVP_CIPHER * |
134 | return(&r2_40_cbc_cipher); | 131 | EVP_rc2_40_cbc(void) |
135 | } | 132 | { |
136 | 133 | return (&r2_40_cbc_cipher); | |
137 | static int rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, | 134 | } |
138 | const unsigned char *iv, int enc) | 135 | |
139 | { | 136 | static int |
140 | RC2_set_key(&data(ctx)->ks,EVP_CIPHER_CTX_key_length(ctx), | 137 | rc2_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
141 | key,data(ctx)->key_bits); | 138 | const unsigned char *iv, int enc) |
139 | { | ||
140 | RC2_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), | ||
141 | key, data(ctx)->key_bits); | ||
142 | return 1; | 142 | return 1; |
143 | } | 143 | } |
144 | 144 | ||
145 | static int rc2_meth_to_magic(EVP_CIPHER_CTX *e) | 145 | static int |
146 | { | 146 | rc2_meth_to_magic(EVP_CIPHER_CTX *e) |
147 | { | ||
147 | int i; | 148 | int i; |
148 | 149 | ||
149 | EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i); | 150 | EVP_CIPHER_CTX_ctrl(e, EVP_CTRL_GET_RC2_KEY_BITS, 0, &i); |
150 | if (i == 128) return(RC2_128_MAGIC); | 151 | if (i == 128) |
151 | else if (i == 64) return(RC2_64_MAGIC); | 152 | return (RC2_128_MAGIC); |
152 | else if (i == 40) return(RC2_40_MAGIC); | 153 | else if (i == 64) |
153 | else return(0); | 154 | return (RC2_64_MAGIC); |
154 | } | 155 | else if (i == 40) |
155 | 156 | return (RC2_40_MAGIC); | |
156 | static int rc2_magic_to_meth(int i) | ||
157 | { | ||
158 | if (i == RC2_128_MAGIC) return 128; | ||
159 | else if (i == RC2_64_MAGIC) return 64; | ||
160 | else if (i == RC2_40_MAGIC) return 40; | ||
161 | else | 157 | else |
162 | { | 158 | return (0); |
163 | EVPerr(EVP_F_RC2_MAGIC_TO_METH,EVP_R_UNSUPPORTED_KEY_SIZE); | 159 | } |
164 | return(0); | 160 | |
165 | } | 161 | static int |
162 | rc2_magic_to_meth(int i) | ||
163 | { | ||
164 | if (i == RC2_128_MAGIC) | ||
165 | return 128; | ||
166 | else if (i == RC2_64_MAGIC) | ||
167 | return 64; | ||
168 | else if (i == RC2_40_MAGIC) | ||
169 | return 40; | ||
170 | else { | ||
171 | EVPerr(EVP_F_RC2_MAGIC_TO_METH, EVP_R_UNSUPPORTED_KEY_SIZE); | ||
172 | return (0); | ||
166 | } | 173 | } |
174 | } | ||
167 | 175 | ||
168 | static int rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) | 176 | static int |
169 | { | 177 | rc2_get_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) |
170 | long num=0; | 178 | { |
171 | int i=0; | 179 | long num = 0; |
180 | int i = 0; | ||
172 | int key_bits; | 181 | int key_bits; |
173 | unsigned int l; | 182 | unsigned int l; |
174 | unsigned char iv[EVP_MAX_IV_LENGTH]; | 183 | unsigned char iv[EVP_MAX_IV_LENGTH]; |
175 | 184 | ||
176 | if (type != NULL) | 185 | if (type != NULL) { |
177 | { | 186 | l = EVP_CIPHER_CTX_iv_length(c); |
178 | l=EVP_CIPHER_CTX_iv_length(c); | ||
179 | OPENSSL_assert(l <= sizeof(iv)); | 187 | OPENSSL_assert(l <= sizeof(iv)); |
180 | i=ASN1_TYPE_get_int_octetstring(type,&num,iv,l); | 188 | i = ASN1_TYPE_get_int_octetstring(type, &num, iv, l); |
181 | if (i != (int)l) | 189 | if (i != (int)l) |
182 | return(-1); | 190 | return (-1); |
183 | key_bits =rc2_magic_to_meth((int)num); | 191 | key_bits = rc2_magic_to_meth((int)num); |
184 | if (!key_bits) | 192 | if (!key_bits) |
185 | return(-1); | 193 | return (-1); |
186 | if(i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1)) | 194 | if (i > 0 && !EVP_CipherInit_ex(c, NULL, NULL, NULL, iv, -1)) |
187 | return -1; | 195 | return -1; |
188 | EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, key_bits, NULL); | 196 | EVP_CIPHER_CTX_ctrl(c, EVP_CTRL_SET_RC2_KEY_BITS, |
197 | key_bits, NULL); | ||
189 | EVP_CIPHER_CTX_set_key_length(c, key_bits / 8); | 198 | EVP_CIPHER_CTX_set_key_length(c, key_bits / 8); |
190 | } | ||
191 | return(i); | ||
192 | } | 199 | } |
200 | return (i); | ||
201 | } | ||
193 | 202 | ||
194 | static int rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) | 203 | static int |
195 | { | 204 | rc2_set_asn1_type_and_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) |
205 | { | ||
196 | long num; | 206 | long num; |
197 | int i=0,j; | 207 | int i = 0, j; |
198 | 208 | ||
199 | if (type != NULL) | 209 | if (type != NULL) { |
200 | { | 210 | num = rc2_meth_to_magic(c); |
201 | num=rc2_meth_to_magic(c); | 211 | j = EVP_CIPHER_CTX_iv_length(c); |
202 | j=EVP_CIPHER_CTX_iv_length(c); | 212 | i = ASN1_TYPE_set_int_octetstring(type, num, c->oiv, j); |
203 | i=ASN1_TYPE_set_int_octetstring(type,num,c->oiv,j); | ||
204 | } | ||
205 | return(i); | ||
206 | } | 213 | } |
214 | return (i); | ||
215 | } | ||
207 | 216 | ||
208 | static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | 217 | static int |
209 | { | 218 | rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) |
210 | switch(type) | 219 | { |
211 | { | 220 | switch (type) { |
212 | case EVP_CTRL_INIT: | 221 | case EVP_CTRL_INIT: |
213 | data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8; | 222 | data(c)->key_bits = EVP_CIPHER_CTX_key_length(c) * 8; |
214 | return 1; | 223 | return 1; |
@@ -216,14 +225,14 @@ static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
216 | case EVP_CTRL_GET_RC2_KEY_BITS: | 225 | case EVP_CTRL_GET_RC2_KEY_BITS: |
217 | *(int *)ptr = data(c)->key_bits; | 226 | *(int *)ptr = data(c)->key_bits; |
218 | return 1; | 227 | return 1; |
219 | 228 | ||
220 | case EVP_CTRL_SET_RC2_KEY_BITS: | 229 | case EVP_CTRL_SET_RC2_KEY_BITS: |
221 | if(arg > 0) | 230 | if (arg > 0) { |
222 | { | ||
223 | data(c)->key_bits = arg; | 231 | data(c)->key_bits = arg; |
224 | return 1; | 232 | return 1; |
225 | } | 233 | } |
226 | return 0; | 234 | return 0; |
235 | |||
227 | #ifdef PBE_PRF_TEST | 236 | #ifdef PBE_PRF_TEST |
228 | case EVP_CTRL_PBE_PRF_NID: | 237 | case EVP_CTRL_PBE_PRF_NID: |
229 | *(int *)ptr = NID_hmacWithMD5; | 238 | *(int *)ptr = NID_hmacWithMD5; |
@@ -232,7 +241,7 @@ static int rc2_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) | |||
232 | 241 | ||
233 | default: | 242 | default: |
234 | return -1; | 243 | return -1; |
235 | } | ||
236 | } | 244 | } |
245 | } | ||
237 | 246 | ||
238 | #endif | 247 | #endif |