e_des.c 8.1 KB

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