2 * Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DES low level APIs are deprecated for public use, but still ok for internal
14 #include "internal/deprecated.h"
17 #include "internal/cryptlib.h"
18 #ifndef OPENSSL_NO_DES
19 # include <openssl/evp.h>
20 # include <openssl/objects.h>
21 # include "crypto/evp.h"
22 # include <openssl/des.h>
23 # include <openssl/rand.h>
24 # include "evp_local.h"
29 DES_key_schedule ks[3];
32 void (*cbc) (const void *, void *, size_t,
33 const DES_key_schedule *, unsigned char *);
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 "sparc_arch.h"
45 extern unsigned int OPENSSL_sparcv9cap_P[];
47 # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES)
49 void des_t4_key_expand(const void *key, DES_key_schedule *ks);
50 void des_t4_ede3_cbc_encrypt(const void *inp, void *out, size_t len,
51 const DES_key_schedule ks[3], unsigned char iv[8]);
52 void des_t4_ede3_cbc_decrypt(const void *inp, void *out, size_t len,
53 const DES_key_schedule ks[3], unsigned char iv[8]);
56 static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
57 const unsigned char *iv, int enc);
59 static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
60 const unsigned char *iv, int enc);
62 static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
64 # define data(ctx) EVP_C_DATA(DES_EDE_KEY,ctx)
67 * Because of various casts and different args can't use
68 * IMPLEMENT_BLOCK_CIPHER
71 static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
72 const unsigned char *in, size_t inl)
74 BLOCK_CIPHER_ecb_loop()
75 DES_ecb3_encrypt((const_DES_cblock *)(in + i),
76 (DES_cblock *)(out + i),
77 &data(ctx)->ks1, &data(ctx)->ks2,
78 &data(ctx)->ks3, EVP_CIPHER_CTX_encrypting(ctx));
82 static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
83 const unsigned char *in, size_t inl)
85 while (inl >= EVP_MAXCHUNK) {
86 int num = EVP_CIPHER_CTX_num(ctx);
87 DES_ede3_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK,
88 &data(ctx)->ks1, &data(ctx)->ks2,
90 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
92 EVP_CIPHER_CTX_set_num(ctx, num);
98 int num = EVP_CIPHER_CTX_num(ctx);
99 DES_ede3_ofb64_encrypt(in, out, (long)inl,
100 &data(ctx)->ks1, &data(ctx)->ks2,
102 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
104 EVP_CIPHER_CTX_set_num(ctx, num);
109 static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
110 const unsigned char *in, size_t inl)
112 DES_EDE_KEY *dat = data(ctx);
114 if (dat->stream.cbc != NULL) {
115 (*dat->stream.cbc) (in, out, inl, dat->ks.ks,
116 EVP_CIPHER_CTX_iv_noconst(ctx));
120 while (inl >= EVP_MAXCHUNK) {
121 DES_ede3_cbc_encrypt(in, out, (long)EVP_MAXCHUNK,
122 &dat->ks1, &dat->ks2, &dat->ks3,
123 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
124 EVP_CIPHER_CTX_encrypting(ctx));
130 DES_ede3_cbc_encrypt(in, out, (long)inl,
131 &dat->ks1, &dat->ks2, &dat->ks3,
132 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
133 EVP_CIPHER_CTX_encrypting(ctx));
137 static int des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
138 const unsigned char *in, size_t inl)
140 while (inl >= EVP_MAXCHUNK) {
141 int num = EVP_CIPHER_CTX_num(ctx);
142 DES_ede3_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK,
143 &data(ctx)->ks1, &data(ctx)->ks2,
145 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
146 &num, EVP_CIPHER_CTX_encrypting(ctx));
147 EVP_CIPHER_CTX_set_num(ctx, num);
153 int num = EVP_CIPHER_CTX_num(ctx);
154 DES_ede3_cfb64_encrypt(in, out, (long)inl,
155 &data(ctx)->ks1, &data(ctx)->ks2,
157 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
158 &num, EVP_CIPHER_CTX_encrypting(ctx));
159 EVP_CIPHER_CTX_set_num(ctx, num);
165 * Although we have a CFB-r implementation for 3-DES, it doesn't pack the
166 * right way, so wrap it here
168 static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
169 const unsigned char *in, size_t inl)
172 unsigned char c[1], d[1];
174 if (!EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS))
176 for (n = 0; n < inl; ++n) {
177 c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
178 DES_ede3_cfb_encrypt(c, d, 1, 1,
179 &data(ctx)->ks1, &data(ctx)->ks2,
181 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
182 EVP_CIPHER_CTX_encrypting(ctx));
183 out[n / 8] = (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8)))
184 | ((d[0] & 0x80) >> (unsigned int)(n % 8));
190 static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
191 const unsigned char *in, size_t inl)
193 while (inl >= EVP_MAXCHUNK) {
194 DES_ede3_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK,
195 &data(ctx)->ks1, &data(ctx)->ks2,
197 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
198 EVP_CIPHER_CTX_encrypting(ctx));
204 DES_ede3_cfb_encrypt(in, out, 8, (long)inl,
205 &data(ctx)->ks1, &data(ctx)->ks2,
207 (DES_cblock *)EVP_CIPHER_CTX_iv_noconst(ctx),
208 EVP_CIPHER_CTX_encrypting(ctx));
212 BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64,
213 EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1,
214 des_ede_init_key, NULL, NULL, NULL, des3_ctrl)
215 # define des_ede3_cfb64_cipher des_ede_cfb64_cipher
216 # define des_ede3_ofb_cipher des_ede_ofb_cipher
217 # define des_ede3_cbc_cipher des_ede_cbc_cipher
218 # define des_ede3_ecb_cipher des_ede_ecb_cipher
219 BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64,
220 EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1,
221 des_ede3_init_key, NULL, NULL, NULL, des3_ctrl)
223 BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 1,
224 EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1,
225 des_ede3_init_key, NULL, NULL, NULL, des3_ctrl)
227 BLOCK_CIPHER_def_cfb(des_ede3, DES_EDE_KEY, NID_des_ede3, 24, 8, 8,
228 EVP_CIPH_RAND_KEY | EVP_CIPH_FLAG_DEFAULT_ASN1,
229 des_ede3_init_key, NULL, NULL, NULL, des3_ctrl)
231 static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
232 const unsigned char *iv, int enc)
234 DES_cblock *deskey = (DES_cblock *)key;
235 DES_EDE_KEY *dat = data(ctx);
237 dat->stream.cbc = NULL;
238 # if defined(SPARC_DES_CAPABLE)
239 if (SPARC_DES_CAPABLE) {
240 int mode = EVP_CIPHER_CTX_mode(ctx);
242 if (mode == EVP_CIPH_CBC_MODE) {
243 des_t4_key_expand(&deskey[0], &dat->ks1);
244 des_t4_key_expand(&deskey[1], &dat->ks2);
245 memcpy(&dat->ks3, &dat->ks1, sizeof(dat->ks1));
246 dat->stream.cbc = enc ? des_t4_ede3_cbc_encrypt :
247 des_t4_ede3_cbc_decrypt;
252 DES_set_key_unchecked(&deskey[0], &dat->ks1);
253 DES_set_key_unchecked(&deskey[1], &dat->ks2);
254 memcpy(&dat->ks3, &dat->ks1, sizeof(dat->ks1));
258 static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
259 const unsigned char *iv, int enc)
261 DES_cblock *deskey = (DES_cblock *)key;
262 DES_EDE_KEY *dat = data(ctx);
264 dat->stream.cbc = NULL;
265 # if defined(SPARC_DES_CAPABLE)
266 if (SPARC_DES_CAPABLE) {
267 int mode = EVP_CIPHER_CTX_mode(ctx);
269 if (mode == EVP_CIPH_CBC_MODE) {
270 des_t4_key_expand(&deskey[0], &dat->ks1);
271 des_t4_key_expand(&deskey[1], &dat->ks2);
272 des_t4_key_expand(&deskey[2], &dat->ks3);
273 dat->stream.cbc = enc ? des_t4_ede3_cbc_encrypt :
274 des_t4_ede3_cbc_decrypt;
279 DES_set_key_unchecked(&deskey[0], &dat->ks1);
280 DES_set_key_unchecked(&deskey[1], &dat->ks2);
281 DES_set_key_unchecked(&deskey[2], &dat->ks3);
285 static int des3_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
288 DES_cblock *deskey = ptr;
292 case EVP_CTRL_RAND_KEY:
293 kl = EVP_CIPHER_CTX_key_length(ctx);
294 if (kl < 0 || RAND_priv_bytes(ptr, kl) <= 0)
296 DES_set_odd_parity(deskey);
298 DES_set_odd_parity(deskey + 1);
300 DES_set_odd_parity(deskey + 2);
308 const EVP_CIPHER *EVP_des_ede(void)
313 const EVP_CIPHER *EVP_des_ede3(void)
315 return &des_ede3_ecb;
319 # include <openssl/sha.h>
321 static const unsigned char wrap_iv[8] =
322 { 0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05 };
324 static int des_ede3_unwrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
325 const unsigned char *in, size_t inl)
327 unsigned char icv[8], iv[8], sha1tmp[SHA_DIGEST_LENGTH];
333 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), wrap_iv, 8);
334 /* Decrypt first block which will end up as icv */
335 des_ede_cbc_cipher(ctx, icv, in, 8);
336 /* Decrypt central blocks */
338 * If decrypting in place move whole output along a block so the next
339 * des_ede_cbc_cipher is in place.
342 memmove(out, out + 8, inl - 8);
345 des_ede_cbc_cipher(ctx, out, in + 8, inl - 16);
346 /* Decrypt final block which will be IV */
347 des_ede_cbc_cipher(ctx, iv, in + inl - 8, 8);
348 /* Reverse order of everything */
349 BUF_reverse(icv, NULL, 8);
350 BUF_reverse(out, NULL, inl - 16);
351 BUF_reverse(EVP_CIPHER_CTX_iv_noconst(ctx), iv, 8);
352 /* Decrypt again using new IV */
353 des_ede_cbc_cipher(ctx, out, out, inl - 16);
354 des_ede_cbc_cipher(ctx, icv, icv, 8);
355 /* Work out SHA1 hash of first portion */
356 SHA1(out, inl - 16, sha1tmp);
358 if (!CRYPTO_memcmp(sha1tmp, icv, 8))
360 OPENSSL_cleanse(icv, 8);
361 OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
362 OPENSSL_cleanse(iv, 8);
363 OPENSSL_cleanse(EVP_CIPHER_CTX_iv_noconst(ctx), 8);
365 OPENSSL_cleanse(out, inl - 16);
370 static int des_ede3_wrap(EVP_CIPHER_CTX *ctx, unsigned char *out,
371 const unsigned char *in, size_t inl)
373 unsigned char sha1tmp[SHA_DIGEST_LENGTH];
376 /* Copy input to output buffer + 8 so we have space for IV */
377 memmove(out + 8, in, inl);
379 SHA1(in, inl, sha1tmp);
380 memcpy(out + inl + 8, sha1tmp, 8);
381 OPENSSL_cleanse(sha1tmp, SHA_DIGEST_LENGTH);
382 /* Generate random IV */
383 if (RAND_bytes(EVP_CIPHER_CTX_iv_noconst(ctx), 8) <= 0)
385 memcpy(out, EVP_CIPHER_CTX_iv_noconst(ctx), 8);
386 /* Encrypt everything after IV in place */
387 des_ede_cbc_cipher(ctx, out + 8, out + 8, inl + 8);
388 BUF_reverse(out, NULL, inl + 16);
389 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), wrap_iv, 8);
390 des_ede_cbc_cipher(ctx, out, out, inl + 16);
394 static int des_ede3_wrap_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
395 const unsigned char *in, size_t inl)
398 * Sanity check input length: we typically only wrap keys so EVP_MAXCHUNK
399 * is more than will ever be needed. Also input length must be a multiple
402 if (inl >= EVP_MAXCHUNK || inl % 8)
405 if (is_partially_overlapping(out, in, inl)) {
406 EVPerr(EVP_F_DES_EDE3_WRAP_CIPHER, EVP_R_PARTIALLY_OVERLAPPING);
410 if (EVP_CIPHER_CTX_encrypting(ctx))
411 return des_ede3_wrap(ctx, out, in, inl);
413 return des_ede3_unwrap(ctx, out, in, inl);
416 static const EVP_CIPHER des3_wrap = {
417 NID_id_smime_alg_CMS3DESwrap,
419 EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER
420 | EVP_CIPH_FLAG_DEFAULT_ASN1,
421 des_ede3_init_key, des_ede3_wrap_cipher,
424 NULL, NULL, NULL, NULL
427 const EVP_CIPHER *EVP_des_ede3_wrap(void)