/src/openssl/crypto/evp/e_des3.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2024 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 | | * DES 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 | | #ifndef OPENSSL_NO_DES |
19 | | # include <openssl/objects.h> |
20 | | # include "crypto/evp.h" |
21 | | # include "crypto/sha.h" |
22 | | # include <openssl/des.h> |
23 | | # include <openssl/rand.h> |
24 | | # include "evp_local.h" |
25 | | |
26 | | typedef struct { |
27 | | union { |
28 | | OSSL_UNION_ALIGN; |
29 | | DES_key_schedule ks[3]; |
30 | | } ks; |
31 | | union { |
32 | | void (*cbc) (const void *, void *, size_t, |
33 | | const DES_key_schedule *, unsigned char *); |
34 | | } stream; |
35 | | } DES_EDE_KEY; |
36 | 0 | # define ks1 ks.ks[0] |
37 | 0 | # define ks2 ks.ks[1] |
38 | 0 | # define ks3 ks.ks[2] |
39 | | |
40 | | # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__)) |
41 | | /* ---------^^^ this is not a typo, just a way to detect that |
42 | | * assembler support was in general requested... */ |
43 | | # include "crypto/sparc_arch.h" |
44 | | |
45 | | # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES) |
46 | | |
47 | | void des_t4_key_expand(const void *key, DES_key_schedule *ks); |
48 | | void des_t4_ede3_cbc_encrypt(const void *inp, void *out, size_t len, |
49 | | const DES_key_schedule ks[3], unsigned char iv[8]); |
50 | | void des_t4_ede3_cbc_decrypt(const void *inp, void *out, size_t len, |
51 | | const DES_key_schedule ks[3], unsigned char iv[8]); |
52 | | # endif |
53 | | |
54 | | static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
55 | | const unsigned char *iv, int enc); |
56 | | |
57 | | static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
58 | | const unsigned char *iv, int enc); |
59 | | |
60 | | static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr); |
61 | | |
62 | 0 | # define data(ctx) EVP_C_DATA(DES_EDE_KEY,ctx) |
63 | | |
64 | | /* |
65 | | * Because of various casts and different args can't use |
66 | | * IMPLEMENT_BLOCK_CIPHER |
67 | | */ |
68 | | |
69 | | static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
70 | | const unsigned char *in, size_t inl) |
71 | 0 | { |
72 | 0 | BLOCK_CIPHER_ecb_loop() |
73 | 0 | DES_ecb3_encrypt((const_DES_cblock *)(in + i), |
74 | 0 | (DES_cblock *)(out + i), |
75 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
76 | 0 | &data(ctx)->ks3, EVP_CIPHER_CTX_is_encrypting(ctx)); |
77 | 0 | return 1; |
78 | 0 | } |
79 | | |
80 | | static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
81 | | const unsigned char *in, size_t inl) |
82 | 0 | { |
83 | 0 | while (inl >= EVP_MAXCHUNK) { |
84 | 0 | int num = EVP_CIPHER_CTX_get_num(ctx); |
85 | 0 | DES_ede3_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK, |
86 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
87 | 0 | &data(ctx)->ks3, |
88 | 0 | (DES_cblock *)ctx->iv, |
89 | 0 | &num); |
90 | 0 | EVP_CIPHER_CTX_set_num(ctx, num); |
91 | 0 | inl -= EVP_MAXCHUNK; |
92 | 0 | in += EVP_MAXCHUNK; |
93 | 0 | out += EVP_MAXCHUNK; |
94 | 0 | } |
95 | 0 | if (inl) { |
96 | 0 | int num = EVP_CIPHER_CTX_get_num(ctx); |
97 | 0 | DES_ede3_ofb64_encrypt(in, out, (long)inl, |
98 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
99 | 0 | &data(ctx)->ks3, |
100 | 0 | (DES_cblock *)ctx->iv, |
101 | 0 | &num); |
102 | 0 | EVP_CIPHER_CTX_set_num(ctx, num); |
103 | 0 | } |
104 | 0 | return 1; |
105 | 0 | } |
106 | | |
107 | | static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
108 | | const unsigned char *in, size_t inl) |
109 | 0 | { |
110 | 0 | DES_EDE_KEY *dat = data(ctx); |
111 | |
|
112 | 0 | if (dat->stream.cbc != NULL) { |
113 | 0 | (*dat->stream.cbc) (in, out, inl, dat->ks.ks, |
114 | 0 | ctx->iv); |
115 | 0 | return 1; |
116 | 0 | } |
117 | | |
118 | 0 | while (inl >= EVP_MAXCHUNK) { |
119 | 0 | DES_ede3_cbc_encrypt(in, out, (long)EVP_MAXCHUNK, |
120 | 0 | &dat->ks1, &dat->ks2, &dat->ks3, |
121 | 0 | (DES_cblock *)ctx->iv, |
122 | 0 | EVP_CIPHER_CTX_is_encrypting(ctx)); |
123 | 0 | inl -= EVP_MAXCHUNK; |
124 | 0 | in += EVP_MAXCHUNK; |
125 | 0 | out += EVP_MAXCHUNK; |
126 | 0 | } |
127 | 0 | if (inl) |
128 | 0 | DES_ede3_cbc_encrypt(in, out, (long)inl, |
129 | 0 | &dat->ks1, &dat->ks2, &dat->ks3, |
130 | 0 | (DES_cblock *)ctx->iv, |
131 | 0 | EVP_CIPHER_CTX_is_encrypting(ctx)); |
132 | 0 | return 1; |
133 | 0 | } |
134 | | |
135 | | static int des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
136 | | const unsigned char *in, size_t inl) |
137 | 0 | { |
138 | 0 | while (inl >= EVP_MAXCHUNK) { |
139 | 0 | int num = EVP_CIPHER_CTX_get_num(ctx); |
140 | 0 | DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK, |
141 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
142 | 0 | &data(ctx)->ks3, (DES_cblock *)ctx->iv, |
143 | 0 | &num, EVP_CIPHER_CTX_is_encrypting(ctx)); |
144 | 0 | EVP_CIPHER_CTX_set_num(ctx, num); |
145 | 0 | inl -= EVP_MAXCHUNK; |
146 | 0 | in += EVP_MAXCHUNK; |
147 | 0 | out += EVP_MAXCHUNK; |
148 | 0 | } |
149 | 0 | if (inl) { |
150 | 0 | int num = EVP_CIPHER_CTX_get_num(ctx); |
151 | 0 | DES_ede3_cfb64_encrypt(in, out, (long)inl, |
152 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
153 | 0 | &data(ctx)->ks3, (DES_cblock *)ctx->iv, |
154 | 0 | &num, EVP_CIPHER_CTX_is_encrypting(ctx)); |
155 | 0 | EVP_CIPHER_CTX_set_num(ctx, num); |
156 | 0 | } |
157 | 0 | return 1; |
158 | 0 | } |
159 | | |
160 | | /* |
161 | | * Although we have a CFB-r implementation for 3-DES, it doesn't pack the |
162 | | * right way, so wrap it here |
163 | | */ |
164 | | static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
165 | | const unsigned char *in, size_t inl) |
166 | 0 | { |
167 | 0 | size_t n; |
168 | 0 | unsigned char c[1]; |
169 | 0 | unsigned char d[1] = { 0 }; /* Appease Coverity */ |
170 | |
|
171 | 0 | if (!EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) |
172 | 0 | inl *= 8; |
173 | 0 | for (n = 0; n < inl; ++n) { |
174 | 0 | c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0; |
175 | 0 | DES_ede3_cfb_encrypt(c, d, 1, 1, |
176 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
177 | 0 | &data(ctx)->ks3, (DES_cblock *)ctx->iv, |
178 | 0 | EVP_CIPHER_CTX_is_encrypting(ctx)); |
179 | 0 | out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) |
180 | 0 | | ((d[0] & 0x80) >> (unsigned int)(n % 8)); |
181 | 0 | } |
182 | |
|
183 | 0 | return 1; |
184 | 0 | } |
185 | | |
186 | | static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
187 | | const unsigned char *in, size_t inl) |
188 | 0 | { |
189 | 0 | while (inl >= EVP_MAXCHUNK) { |
190 | 0 | DES_ede3_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK, |
191 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
192 | 0 | &data(ctx)->ks3, (DES_cblock *)ctx->iv, |
193 | 0 | EVP_CIPHER_CTX_is_encrypting(ctx)); |
194 | 0 | inl -= EVP_MAXCHUNK; |
195 | 0 | in += EVP_MAXCHUNK; |
196 | 0 | out += EVP_MAXCHUNK; |
197 | 0 | } |
198 | 0 | if (inl) |
199 | 0 | DES_ede3_cfb_encrypt(in, out, 8, (long)inl, |
200 | 0 | &data(ctx)->ks1, &data(ctx)->ks2, |
201 | 0 | &data(ctx)->ks3, (DES_cblock *)ctx->iv, |
202 | 0 | EVP_CIPHER_CTX_is_encrypting(ctx)); |
203 | 0 | return 1; |
204 | 0 | } |
205 | | |
206 | | BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64, |
207 | | EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1, |
208 | | des_ede_init_key, NULL, NULL, NULL, des3_ctrl) |
209 | | # define des_ede3_cfb64_cipher des_ede_cfb64_cipher |
210 | | # define des_ede3_ofb_cipher des_ede_ofb_cipher |
211 | | # define des_ede3_cbc_cipher des_ede_cbc_cipher |
212 | | # define des_ede3_ecb_cipher des_ede_ecb_cipher |
213 | | BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64, |
214 | | EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1, |
215 | | des_ede3_init_key, NULL, NULL, NULL, des3_ctrl) |
216 | | |
217 | | BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 1, |
218 | | EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1, |
219 | | des_ede3_init_key, NULL, NULL, NULL, des3_ctrl) |
220 | | |
221 | | BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 8, |
222 | | EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1, |
223 | | des_ede3_init_key, NULL, NULL, NULL, des3_ctrl) |
224 | | |
225 | | static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
226 | | const unsigned char *iv, int enc) |
227 | 0 | { |
228 | 0 | DES_cblock *deskey = (DES_cblock *)key; |
229 | 0 | DES_EDE_KEY *dat = data(ctx); |
230 | |
|
231 | 0 | dat->stream.cbc = NULL; |
232 | | # if defined(SPARC_DES_CAPABLE) |
233 | | if (SPARC_DES_CAPABLE) { |
234 | | int mode = EVP_CIPHER_CTX_get_mode(ctx); |
235 | | |
236 | | if (mode == EVP_CIPH_CBC_MODE) { |
237 | | des_t4_key_expand(&deskey[0], &dat->ks1); |
238 | | des_t4_key_expand(&deskey[1], &dat->ks2); |
239 | | memcpy(&dat->ks3, &dat->ks1, sizeof(dat->ks1)); |
240 | | dat->stream.cbc = enc ? des_t4_ede3_cbc_encrypt : |
241 | | des_t4_ede3_cbc_decrypt; |
242 | | return 1; |
243 | | } |
244 | | } |
245 | | # endif |
246 | 0 | DES_set_key_unchecked(&deskey[0], &dat->ks1); |
247 | 0 | DES_set_key_unchecked(&deskey[1], &dat->ks2); |
248 | 0 | memcpy(&dat->ks3, &dat->ks1, sizeof(dat->ks1)); |
249 | 0 | return 1; |
250 | 0 | } |
251 | | |
252 | | static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key, |
253 | | const unsigned char *iv, int enc) |
254 | 0 | { |
255 | 0 | DES_cblock *deskey = (DES_cblock *)key; |
256 | 0 | DES_EDE_KEY *dat = data(ctx); |
257 | |
|
258 | 0 | dat->stream.cbc = NULL; |
259 | | # if defined(SPARC_DES_CAPABLE) |
260 | | if (SPARC_DES_CAPABLE) { |
261 | | int mode = EVP_CIPHER_CTX_get_mode(ctx); |
262 | | |
263 | | if (mode == EVP_CIPH_CBC_MODE) { |
264 | | des_t4_key_expand(&deskey[0], &dat->ks1); |
265 | | des_t4_key_expand(&deskey[1], &dat->ks2); |
266 | | des_t4_key_expand(&deskey[2], &dat->ks3); |
267 | | dat->stream.cbc = enc ? des_t4_ede3_cbc_encrypt : |
268 | | des_t4_ede3_cbc_decrypt; |
269 | | return 1; |
270 | | } |
271 | | } |
272 | | # endif |
273 | 0 | DES_set_key_unchecked(&deskey[0], &dat->ks1); |
274 | 0 | DES_set_key_unchecked(&deskey[1], &dat->ks2); |
275 | 0 | DES_set_key_unchecked(&deskey[2], &dat->ks3); |
276 | 0 | return 1; |
277 | 0 | } |
278 | | |
279 | | static int des3_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) |
280 | 0 | { |
281 | |
|
282 | 0 | DES_cblock *deskey = ptr; |
283 | 0 | int kl; |
284 | |
|
285 | 0 | switch (type) { |
286 | 0 | case EVP_CTRL_RAND_KEY: |
287 | 0 | kl = EVP_CIPHER_CTX_get_key_length(ctx); |
288 | 0 | if (kl < 0 || RAND_priv_bytes(ptr, kl) <= 0) |
289 | 0 | return 0; |
290 | 0 | DES_set_odd_parity(deskey); |
291 | 0 | if (kl >= 16) |
292 | 0 | DES_set_odd_parity(deskey + 1); |
293 | 0 | if (kl >= 24) |
294 | 0 | DES_set_odd_parity(deskey + 2); |
295 | 0 | return 1; |
296 | | |
297 | 0 | default: |
298 | 0 | return -1; |
299 | 0 | } |
300 | 0 | } |
301 | | |
302 | | const EVP_CIPHER *EVP_des_ede(void) |
303 | 0 | { |
304 | 0 | return &des_ede_ecb; |
305 | 0 | } |
306 | | |
307 | | const EVP_CIPHER *EVP_des_ede3(void) |
308 | 0 | { |
309 | 0 | return &des_ede3_ecb; |
310 | 0 | } |
311 | | |
312 | | |
313 | | # include <openssl/sha.h> |
314 | | |
315 | | static const unsigned char wrap_iv[8] = { |
316 | | 0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05 |
317 | | }; |
318 | | |
319 | | static int des_ede3_unwrap(EVP_CIPHER_CTX *ctx, unsigned char *out, |
320 | | const unsigned char *in, size_t inl) |
321 | 0 | { |
322 | 0 | unsigned char icv[8], iv[8], sha1tmp[SHA_DIGEST_LENGTH]; |
323 | 0 | int rv = -1; |
324 | 0 | if (inl < 24) |
325 | 0 | return -1; |
326 | 0 | if (out == NULL) |
327 | 0 | return inl - 16; |
328 | 0 | memcpy(ctx->iv, wrap_iv, 8); |
329 | | /* Decrypt first block which will end up as icv */ |
330 | 0 | des_ede_cbc_cipher(ctx, icv, in, 8); |
331 | | /* Decrypt central blocks */ |
332 | | /* |
333 | | * If decrypting in place move whole output along a block so the next |
334 | | * des_ede_cbc_cipher is in place. |
335 | | */ |
336 | 0 | if (out == in) { |
337 | 0 | memmove(out, out + 8, inl - 8); |
338 | 0 | in -= 8; |
339 | 0 | } |
340 | 0 | des_ede_cbc_cipher(ctx, out, in + 8, inl - 16); |
341 | | /* Decrypt final block which will be IV */ |
342 | 0 | des_ede_cbc_cipher(ctx, iv, in + inl - 8, 8); |
343 | | /* Reverse order of everything */ |
344 | 0 | BUF_reverse(icv, NULL, 8); |
345 | 0 | BUF_reverse(out, NULL, inl - 16); |
346 | 0 | BUF_reverse(ctx->iv, iv, 8); |
347 | | /* Decrypt again using new IV */ |
348 | 0 | des_ede_cbc_cipher(ctx, out, out, inl - 16); |
349 | 0 | des_ede_cbc_cipher(ctx, icv, icv, 8); |
350 | 0 | if (ossl_sha1(out, inl - 16, sha1tmp) /* Work out hash of first portion */ |
351 | 0 | && CRYPTO_memcmp(sha1tmp, icv, 8) == 0) |
352 | 0 | rv = inl - 16; |
353 | 0 | OPENSSL_cleanse(icv, 8); |
354 | 0 | OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH); |
355 | 0 | OPENSSL_cleanse(iv, 8); |
356 | 0 | OPENSSL_cleanse(ctx->iv, 8); |
357 | 0 | if (rv == -1) |
358 | 0 | OPENSSL_cleanse(out, inl - 16); |
359 | |
|
360 | 0 | return rv; |
361 | 0 | } |
362 | | |
363 | | static int des_ede3_wrap(EVP_CIPHER_CTX *ctx, unsigned char *out, |
364 | | const unsigned char *in, size_t inl) |
365 | 0 | { |
366 | 0 | unsigned char sha1tmp[SHA_DIGEST_LENGTH]; |
367 | 0 | if (out == NULL) |
368 | 0 | return inl + 16; |
369 | | /* Copy input to output buffer + 8 so we have space for IV */ |
370 | 0 | memmove(out + 8, in, inl); |
371 | | /* Work out ICV */ |
372 | 0 | if (!ossl_sha1(in, inl, sha1tmp)) |
373 | 0 | return -1; |
374 | 0 | memcpy(out + inl + 8, sha1tmp, 8); |
375 | 0 | OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH); |
376 | | /* Generate random IV */ |
377 | 0 | if (RAND_bytes(ctx->iv, 8) <= 0) |
378 | 0 | return -1; |
379 | 0 | memcpy(out, ctx->iv, 8); |
380 | | /* Encrypt everything after IV in place */ |
381 | 0 | des_ede_cbc_cipher(ctx, out + 8, out + 8, inl + 8); |
382 | 0 | BUF_reverse(out, NULL, inl + 16); |
383 | 0 | memcpy(ctx->iv, wrap_iv, 8); |
384 | 0 | des_ede_cbc_cipher(ctx, out, out, inl + 16); |
385 | 0 | return inl + 16; |
386 | 0 | } |
387 | | |
388 | | static int des_ede3_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, |
389 | | const unsigned char *in, size_t inl) |
390 | 0 | { |
391 | | /* |
392 | | * Sanity check input length: we typically only wrap keys so EVP_MAXCHUNK |
393 | | * is more than will ever be needed. Also input length must be a multiple |
394 | | * of 8 bits. |
395 | | */ |
396 | 0 | if (inl >= EVP_MAXCHUNK || inl % 8) |
397 | 0 | return -1; |
398 | | |
399 | 0 | if (ossl_is_partially_overlapping(out, in, inl)) { |
400 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_PARTIALLY_OVERLAPPING); |
401 | 0 | return 0; |
402 | 0 | } |
403 | | |
404 | 0 | if (EVP_CIPHER_CTX_is_encrypting(ctx)) |
405 | 0 | return des_ede3_wrap(ctx, out, in, inl); |
406 | 0 | else |
407 | 0 | return des_ede3_unwrap(ctx, out, in, inl); |
408 | 0 | } |
409 | | |
410 | | static const EVP_CIPHER des3_wrap = { |
411 | | NID_id_smime_alg_CMS3DESwrap, |
412 | | 8, 24, 0, |
413 | | EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER |
414 | | | EVP_CIPH_FLAG_DEFAULT_ASN1, |
415 | | EVP_ORIG_GLOBAL, |
416 | | des_ede3_init_key, des_ede3_wrap_cipher, |
417 | | NULL, |
418 | | sizeof(DES_EDE_KEY), |
419 | | NULL, NULL, NULL, NULL |
420 | | }; |
421 | | |
422 | | const EVP_CIPHER *EVP_des_ede3_wrap(void) |
423 | 0 | { |
424 | 0 | return &des3_wrap; |
425 | 0 | } |
426 | | |
427 | | #endif |