e_des.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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;
  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 "sparc_arch.h"
  37. extern unsigned int OPENSSL_sparcv9cap_P[];
  38. # define SPARC_DES_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_DES)
  39. void des_t4_key_expand(const void *key, DES_key_schedule *ks);
  40. void des_t4_cbc_encrypt(const void *inp, void *out, size_t len,
  41. const DES_key_schedule *ks, unsigned char iv[8]);
  42. void des_t4_cbc_decrypt(const void *inp, void *out, size_t len,
  43. const DES_key_schedule *ks, unsigned char iv[8]);
  44. # endif
  45. static int des_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  46. const unsigned char *iv, int enc);
  47. static int des_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
  48. /*
  49. * Because of various casts and different names can't use
  50. * IMPLEMENT_BLOCK_CIPHER
  51. */
  52. static int des_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  53. const unsigned char *in, size_t inl)
  54. {
  55. BLOCK_CIPHER_ecb_loop()
  56. DES_ecb_encrypt((DES_cblock *)(in + i), (DES_cblock *)(out + i),
  57. EVP_CIPHER_CTX_get_cipher_data(ctx),
  58. EVP_CIPHER_CTX_encrypting(ctx));
  59. return 1;
  60. }
  61. static int des_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  62. const unsigned char *in, size_t inl)
  63. {
  64. while (inl >= EVP_MAXCHUNK) {
  65. int num = EVP_CIPHER_CTX_num(ctx);
  66. DES_ofb64_encrypt(in, out, (long)EVP_MAXCHUNK,
  67. EVP_CIPHER_CTX_get_cipher_data(ctx),
  68. (DES_cblock *)ctx->iv, &num);
  69. EVP_CIPHER_CTX_set_num(ctx, num);
  70. inl -= EVP_MAXCHUNK;
  71. in += EVP_MAXCHUNK;
  72. out += EVP_MAXCHUNK;
  73. }
  74. if (inl) {
  75. int num = EVP_CIPHER_CTX_num(ctx);
  76. DES_ofb64_encrypt(in, out, (long)inl,
  77. EVP_CIPHER_CTX_get_cipher_data(ctx),
  78. (DES_cblock *)ctx->iv, &num);
  79. EVP_CIPHER_CTX_set_num(ctx, num);
  80. }
  81. return 1;
  82. }
  83. static int des_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  84. const unsigned char *in, size_t inl)
  85. {
  86. EVP_DES_KEY *dat = (EVP_DES_KEY *) EVP_CIPHER_CTX_get_cipher_data(ctx);
  87. if (dat->stream.cbc != NULL) {
  88. (*dat->stream.cbc) (in, out, inl, &dat->ks.ks, ctx->iv);
  89. return 1;
  90. }
  91. while (inl >= EVP_MAXCHUNK) {
  92. DES_ncbc_encrypt(in, out, (long)EVP_MAXCHUNK,
  93. EVP_CIPHER_CTX_get_cipher_data(ctx),
  94. (DES_cblock *)ctx->iv,
  95. EVP_CIPHER_CTX_encrypting(ctx));
  96. inl -= EVP_MAXCHUNK;
  97. in += EVP_MAXCHUNK;
  98. out += EVP_MAXCHUNK;
  99. }
  100. if (inl)
  101. DES_ncbc_encrypt(in, out, (long)inl,
  102. EVP_CIPHER_CTX_get_cipher_data(ctx),
  103. (DES_cblock *)ctx->iv,
  104. EVP_CIPHER_CTX_encrypting(ctx));
  105. return 1;
  106. }
  107. static int des_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  108. const unsigned char *in, size_t inl)
  109. {
  110. while (inl >= EVP_MAXCHUNK) {
  111. int num = EVP_CIPHER_CTX_num(ctx);
  112. DES_cfb64_encrypt(in, out, (long)EVP_MAXCHUNK,
  113. EVP_CIPHER_CTX_get_cipher_data(ctx),
  114. (DES_cblock *)ctx->iv, &num,
  115. EVP_CIPHER_CTX_encrypting(ctx));
  116. EVP_CIPHER_CTX_set_num(ctx, num);
  117. inl -= EVP_MAXCHUNK;
  118. in += EVP_MAXCHUNK;
  119. out += EVP_MAXCHUNK;
  120. }
  121. if (inl) {
  122. int num = EVP_CIPHER_CTX_num(ctx);
  123. DES_cfb64_encrypt(in, out, (long)inl,
  124. EVP_CIPHER_CTX_get_cipher_data(ctx),
  125. (DES_cblock *)ctx->iv, &num,
  126. EVP_CIPHER_CTX_encrypting(ctx));
  127. EVP_CIPHER_CTX_set_num(ctx, num);
  128. }
  129. return 1;
  130. }
  131. /*
  132. * Although we have a CFB-r implementation for DES, it doesn't pack the right
  133. * way, so wrap it here
  134. */
  135. static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  136. const unsigned char *in, size_t inl)
  137. {
  138. size_t n, chunk = EVP_MAXCHUNK / 8;
  139. unsigned char c[1], d[1];
  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_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_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_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_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