e_des3.c 14 KB

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