e_des3.c 14 KB

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