You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

238 line
7.3 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <string.h>
  57. #include <openssl/des.h>
  58. #include "../../crypto/fipsmodule/des/internal.h"
  59. #include "../../crypto/internal.h"
  60. // The input and output encrypted as though 64bit cfb mode is being used. The
  61. // extra state information to record how much of the 64bit block we have used
  62. // is contained in *num;
  63. void DES_ede3_cfb64_encrypt(const uint8_t *in, uint8_t *out,
  64. long length, DES_key_schedule *ks1,
  65. DES_key_schedule *ks2, DES_key_schedule *ks3,
  66. DES_cblock *ivec, int *num, int enc) {
  67. uint32_t v0, v1;
  68. long l = length;
  69. int n = *num;
  70. uint32_t ti[2];
  71. uint8_t *iv, c, cc;
  72. iv = ivec->bytes;
  73. if (enc) {
  74. while (l--) {
  75. if (n == 0) {
  76. c2l(iv, v0);
  77. c2l(iv, v1);
  78. ti[0] = v0;
  79. ti[1] = v1;
  80. DES_encrypt3(ti, ks1, ks2, ks3);
  81. v0 = ti[0];
  82. v1 = ti[1];
  83. iv = ivec->bytes;
  84. l2c(v0, iv);
  85. l2c(v1, iv);
  86. iv = ivec->bytes;
  87. }
  88. c = *(in++) ^ iv[n];
  89. *(out++) = c;
  90. iv[n] = c;
  91. n = (n + 1) & 0x07;
  92. }
  93. } else {
  94. while (l--) {
  95. if (n == 0) {
  96. c2l(iv, v0);
  97. c2l(iv, v1);
  98. ti[0] = v0;
  99. ti[1] = v1;
  100. DES_encrypt3(ti, ks1, ks2, ks3);
  101. v0 = ti[0];
  102. v1 = ti[1];
  103. iv = ivec->bytes;
  104. l2c(v0, iv);
  105. l2c(v1, iv);
  106. iv = ivec->bytes;
  107. }
  108. cc = *(in++);
  109. c = iv[n];
  110. iv[n] = cc;
  111. *(out++) = c ^ cc;
  112. n = (n + 1) & 0x07;
  113. }
  114. }
  115. v0 = v1 = ti[0] = ti[1] = c = cc = 0;
  116. *num = n;
  117. }
  118. // This is compatible with the single key CFB-r for DES, even thought that's
  119. // not what EVP needs.
  120. void DES_ede3_cfb_encrypt(const uint8_t *in, uint8_t *out, int numbits,
  121. long length, DES_key_schedule *ks1,
  122. DES_key_schedule *ks2, DES_key_schedule *ks3,
  123. DES_cblock *ivec, int enc) {
  124. uint32_t d0, d1, v0, v1;
  125. unsigned long l = length, n = ((unsigned int)numbits + 7) / 8;
  126. int num = numbits, i;
  127. uint32_t ti[2];
  128. uint8_t *iv;
  129. uint8_t ovec[16];
  130. if (num > 64) {
  131. return;
  132. };
  133. iv = ivec->bytes;
  134. c2l(iv, v0);
  135. c2l(iv, v1);
  136. if (enc) {
  137. while (l >= n) {
  138. l -= n;
  139. ti[0] = v0;
  140. ti[1] = v1;
  141. DES_encrypt3(ti, ks1, ks2, ks3);
  142. c2ln(in, d0, d1, n);
  143. in += n;
  144. d0 ^= ti[0];
  145. d1 ^= ti[1];
  146. l2cn(d0, d1, out, n);
  147. out += n;
  148. // 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  149. // gcc :-(
  150. if (num == 32) {
  151. v0 = v1;
  152. v1 = d0;
  153. } else if (num == 64) {
  154. v0 = d0;
  155. v1 = d1;
  156. } else {
  157. iv = &ovec[0];
  158. l2c(v0, iv);
  159. l2c(v1, iv);
  160. l2c(d0, iv);
  161. l2c(d1, iv);
  162. // shift ovec left most of the bits...
  163. OPENSSL_memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0));
  164. // now the remaining bits
  165. if (num % 8 != 0) {
  166. for (i = 0; i < 8; ++i) {
  167. ovec[i] <<= num % 8;
  168. ovec[i] |= ovec[i + 1] >> (8 - num % 8);
  169. }
  170. }
  171. iv = &ovec[0];
  172. c2l(iv, v0);
  173. c2l(iv, v1);
  174. }
  175. }
  176. } else {
  177. while (l >= n) {
  178. l -= n;
  179. ti[0] = v0;
  180. ti[1] = v1;
  181. DES_encrypt3(ti, ks1, ks2, ks3);
  182. c2ln(in, d0, d1, n);
  183. in += n;
  184. // 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  185. // gcc :-(
  186. if (num == 32) {
  187. v0 = v1;
  188. v1 = d0;
  189. } else if (num == 64) {
  190. v0 = d0;
  191. v1 = d1;
  192. } else {
  193. iv = &ovec[0];
  194. l2c(v0, iv);
  195. l2c(v1, iv);
  196. l2c(d0, iv);
  197. l2c(d1, iv);
  198. // shift ovec left most of the bits...
  199. OPENSSL_memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0));
  200. // now the remaining bits
  201. if (num % 8 != 0) {
  202. for (i = 0; i < 8; ++i) {
  203. ovec[i] <<= num % 8;
  204. ovec[i] |= ovec[i + 1] >> (8 - num % 8);
  205. }
  206. }
  207. iv = &ovec[0];
  208. c2l(iv, v0);
  209. c2l(iv, v1);
  210. }
  211. d0 ^= ti[0];
  212. d1 ^= ti[1];
  213. l2cn(d0, d1, out, n);
  214. out += n;
  215. }
  216. }
  217. iv = ivec->bytes;
  218. l2c(v0, iv);
  219. l2c(v1, iv);
  220. v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0;
  221. }