e_des.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright 1995-2021 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;
  27. } ks;
  28. union {
  29. void (*cbc) (const void *, void *, size_t,
  30. const DES_key_schedule *, unsigned char *);
  31. } stream;
  32. } EVP_DES_KEY;
  33. # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
  34. /* ----------^^^ this is not a typo, just a way to detect that
  35. * assembler support was in general requested... */
  36. # include "crypto/sparc_arch.h"
  37. # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES)
  38. void des_t4_key_expand(const void *key, DES_key_schedule *ks);
  39. void des_t4_cbc_encrypt(const void *inp, void *out, size_t len,
  40. const DES_key_schedule *ks, unsigned char iv[8]);
  41. void des_t4_cbc_decrypt(const void *inp, void *out, size_t len,
  42. const DES_key_schedule *ks, unsigned char iv[8]);
  43. # endif
  44. static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  45. const unsigned char *iv, int enc);
  46. static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
  47. /*
  48. * Because of various casts and different names can't use
  49. * IMPLEMENT_BLOCK_CIPHER
  50. */
  51. static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  52. const unsigned char *in, size_t inl)
  53. {
  54. BLOCK_CIPHER_ecb_loop()
  55. DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i),
  56. EVP_CIPHER_CTX_get_cipher_data(ctx),
  57. EVP_CIPHER_CTX_is_encrypting(ctx));
  58. return 1;
  59. }
  60. static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  61. const unsigned char *in, size_t inl)
  62. {
  63. while (inl >= EVP_MAXCHUNK) {
  64. int num = EVP_CIPHER_CTX_get_num(ctx);
  65. DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK,
  66. EVP_CIPHER_CTX_get_cipher_data(ctx),
  67. (DES_cblock *)ctx->iv, &num);
  68. EVP_CIPHER_CTX_set_num(ctx, num);
  69. inl -= EVP_MAXCHUNK;
  70. in += EVP_MAXCHUNK;
  71. out += EVP_MAXCHUNK;
  72. }
  73. if (inl) {
  74. int num = EVP_CIPHER_CTX_get_num(ctx);
  75. DES_ofb64_encrypt(in, out, (long)inl,
  76. EVP_CIPHER_CTX_get_cipher_data(ctx),
  77. (DES_cblock *)ctx->iv, &num);
  78. EVP_CIPHER_CTX_set_num(ctx, num);
  79. }
  80. return 1;
  81. }
  82. static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  83. const unsigned char *in, size_t inl)
  84. {
  85. EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx);
  86. if (dat->stream.cbc != NULL) {
  87. (*dat->stream.cbc) (in, out, inl, &dat->ks.ks, ctx->iv);
  88. return 1;
  89. }
  90. while (inl >= EVP_MAXCHUNK) {
  91. DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK,
  92. EVP_CIPHER_CTX_get_cipher_data(ctx),
  93. (DES_cblock *)ctx->iv,
  94. EVP_CIPHER_CTX_is_encrypting(ctx));
  95. inl -= EVP_MAXCHUNK;
  96. in += EVP_MAXCHUNK;
  97. out += EVP_MAXCHUNK;
  98. }
  99. if (inl)
  100. DES_ncbc_encrypt(in, out, (long)inl,
  101. EVP_CIPHER_CTX_get_cipher_data(ctx),
  102. (DES_cblock *)ctx->iv,
  103. EVP_CIPHER_CTX_is_encrypting(ctx));
  104. return 1;
  105. }
  106. static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  107. const unsigned char *in, size_t inl)
  108. {
  109. while (inl >= EVP_MAXCHUNK) {
  110. int num = EVP_CIPHER_CTX_get_num(ctx);
  111. DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK,
  112. EVP_CIPHER_CTX_get_cipher_data(ctx),
  113. (DES_cblock *)ctx->iv, &num,
  114. EVP_CIPHER_CTX_is_encrypting(ctx));
  115. EVP_CIPHER_CTX_set_num(ctx, num);
  116. inl -= EVP_MAXCHUNK;
  117. in += EVP_MAXCHUNK;
  118. out += EVP_MAXCHUNK;
  119. }
  120. if (inl) {
  121. int num = EVP_CIPHER_CTX_get_num(ctx);
  122. DES_cfb64_encrypt(in, out, (long)inl,
  123. EVP_CIPHER_CTX_get_cipher_data(ctx),
  124. (DES_cblock *)ctx->iv, &num,
  125. EVP_CIPHER_CTX_is_encrypting(ctx));
  126. EVP_CIPHER_CTX_set_num(ctx, num);
  127. }
  128. return 1;
  129. }
  130. /*
  131. * Although we have a CFB-r implementation for DES, it doesn't pack the right
  132. * way, so wrap it here
  133. */
  134. static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  135. const unsigned char *in, size_t inl)
  136. {
  137. size_t n, chunk = EVP_MAXCHUNK / 8;
  138. unsigned char c[1];
  139. unsigned char d[1] = { 0 }; /* Appease Coverity */
  140. if (inl < chunk)
  141. chunk = inl;
  142. while (inl && inl >= chunk) {
  143. for (n = 0; n < chunk * 8; ++n) {
  144. c[0] = (in[n / 8] & (1 << (7 - n % 8))) ? 0x80 : 0;
  145. DES_cfb_encrypt(c, d, 1, 1, EVP_CIPHER_CTX_get_cipher_data(ctx),
  146. (DES_cblock *)ctx->iv,
  147. EVP_CIPHER_CTX_is_encrypting(ctx));
  148. out[n / 8] =
  149. (out[n / 8] & ~(0x80 >> (unsigned int)(n % 8))) |
  150. ((d[0] & 0x80) >> (unsigned int)(n % 8));
  151. }
  152. inl -= chunk;
  153. in += chunk;
  154. out += chunk;
  155. if (inl < chunk)
  156. chunk = inl;
  157. }
  158. return 1;
  159. }
  160. static int des_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  161. const unsigned char *in, size_t inl)
  162. {
  163. while (inl >= EVP_MAXCHUNK) {
  164. DES_cfb_encrypt(in, out, 8, (long)EVP_MAXCHUNK,
  165. EVP_CIPHER_CTX_get_cipher_data(ctx),
  166. (DES_cblock *)ctx->iv,
  167. EVP_CIPHER_CTX_is_encrypting(ctx));
  168. inl -= EVP_MAXCHUNK;
  169. in += EVP_MAXCHUNK;
  170. out += EVP_MAXCHUNK;
  171. }
  172. if (inl)
  173. DES_cfb_encrypt(in, out, 8, (long)inl,
  174. EVP_CIPHER_CTX_get_cipher_data(ctx),
  175. (DES_cblock *)ctx->iv,
  176. EVP_CIPHER_CTX_is_encrypting(ctx));
  177. return 1;
  178. }
  179. BLOCK_CIPHER_defs(des, EVP_DES_KEY, NID_des, 8, 8, 8, 64,
  180. EVP_CIPH_RAND_KEY, des_init_key, NULL,
  181. EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl)
  182. BLOCK_CIPHER_def_cfb(des, EVP_DES_KEY, NID_des, 8, 8, 1,
  183. EVP_CIPH_RAND_KEY, des_init_key, NULL,
  184. EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl)
  185. BLOCK_CIPHER_def_cfb(des, EVP_DES_KEY, NID_des, 8, 8, 8,
  186. EVP_CIPH_RAND_KEY, des_init_key, NULL,
  187. EVP_CIPHER_set_asn1_iv, EVP_CIPHER_get_asn1_iv, des_ctrl)
  188. static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  189. const unsigned char *iv, int enc)
  190. {
  191. DES_cblock *deskey = (DES_cblock *)key;
  192. EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx);
  193. dat->stream.cbc = NULL;
  194. # if defined(SPARC_DES_CAPABLE)
  195. if (SPARC_DES_CAPABLE) {
  196. int mode = EVP_CIPHER_CTX_get_mode(ctx);
  197. if (mode == EVP_CIPH_CBC_MODE) {
  198. des_t4_key_expand(key, &dat->ks.ks);
  199. dat->stream.cbc = enc ? des_t4_cbc_encrypt : des_t4_cbc_decrypt;
  200. return 1;
  201. }
  202. }
  203. # endif
  204. DES_set_key_unchecked(deskey, EVP_CIPHER_CTX_get_cipher_data(ctx));
  205. return 1;
  206. }
  207. static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
  208. {
  209. switch (type) {
  210. case EVP_CTRL_RAND_KEY:
  211. if (RAND_priv_bytes(ptr, 8) <= 0)
  212. return 0;
  213. DES_set_odd_parity((DES_cblock *)ptr);
  214. return 1;
  215. default:
  216. return -1;
  217. }
  218. }
  219. #endif