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.

cfb64ede.c 7.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/des/internal.h"
  59. /* The input and output encrypted as though 64bit cfb mode is being used. The
  60. * extra state information to record how much of the 64bit block we have used
  61. * is contained in *num; */
  62. void DES_ede3_cfb64_encrypt(const uint8_t *in, uint8_t *out,
  63. long length, DES_key_schedule *ks1,
  64. DES_key_schedule *ks2, DES_key_schedule *ks3,
  65. DES_cblock *ivec, int *num, int enc) {
  66. uint32_t v0, v1;
  67. long l = length;
  68. int n = *num;
  69. uint32_t ti[2];
  70. uint8_t *iv, c, cc;
  71. iv = ivec->bytes;
  72. if (enc) {
  73. while (l--) {
  74. if (n == 0) {
  75. c2l(iv, v0);
  76. c2l(iv, v1);
  77. ti[0] = v0;
  78. ti[1] = v1;
  79. DES_encrypt3(ti, ks1, ks2, ks3);
  80. v0 = ti[0];
  81. v1 = ti[1];
  82. iv = ivec->bytes;
  83. l2c(v0, iv);
  84. l2c(v1, iv);
  85. iv = ivec->bytes;
  86. }
  87. c = *(in++) ^ iv[n];
  88. *(out++) = c;
  89. iv[n] = c;
  90. n = (n + 1) & 0x07;
  91. }
  92. } else {
  93. while (l--) {
  94. if (n == 0) {
  95. c2l(iv, v0);
  96. c2l(iv, v1);
  97. ti[0] = v0;
  98. ti[1] = v1;
  99. DES_encrypt3(ti, ks1, ks2, ks3);
  100. v0 = ti[0];
  101. v1 = ti[1];
  102. iv = ivec->bytes;
  103. l2c(v0, iv);
  104. l2c(v1, iv);
  105. iv = ivec->bytes;
  106. }
  107. cc = *(in++);
  108. c = iv[n];
  109. iv[n] = cc;
  110. *(out++) = c ^ cc;
  111. n = (n + 1) & 0x07;
  112. }
  113. }
  114. v0 = v1 = ti[0] = ti[1] = c = cc = 0;
  115. *num = n;
  116. }
  117. /* This is compatible with the single key CFB-r for DES, even thought that's
  118. * not what EVP needs. */
  119. void DES_ede3_cfb_encrypt(const uint8_t *in, uint8_t *out, int numbits,
  120. long length, DES_key_schedule *ks1,
  121. DES_key_schedule *ks2, DES_key_schedule *ks3,
  122. DES_cblock *ivec, int enc) {
  123. uint32_t d0, d1, v0, v1;
  124. unsigned long l = length, n = ((unsigned int)numbits + 7) / 8;
  125. int num = numbits, i;
  126. uint32_t ti[2];
  127. uint8_t *iv;
  128. uint8_t ovec[16];
  129. if (num > 64) {
  130. return;
  131. };
  132. iv = ivec->bytes;
  133. c2l(iv, v0);
  134. c2l(iv, v1);
  135. if (enc) {
  136. while (l >= n) {
  137. l -= n;
  138. ti[0] = v0;
  139. ti[1] = v1;
  140. DES_encrypt3(ti, ks1, ks2, ks3);
  141. c2ln(in, d0, d1, n);
  142. in += n;
  143. d0 ^= ti[0];
  144. d1 ^= ti[1];
  145. l2cn(d0, d1, out, n);
  146. out += n;
  147. /* 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  148. * gcc :-( */
  149. if (num == 32) {
  150. v0 = v1;
  151. v1 = d0;
  152. } else if (num == 64) {
  153. v0 = d0;
  154. v1 = d1;
  155. } else {
  156. iv = &ovec[0];
  157. l2c(v0, iv);
  158. l2c(v1, iv);
  159. l2c(d0, iv);
  160. l2c(d1, iv);
  161. /* shift ovec left most of the bits... */
  162. memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0));
  163. /* now the remaining bits */
  164. if (num % 8 != 0) {
  165. for (i = 0; i < 8; ++i) {
  166. ovec[i] <<= num % 8;
  167. ovec[i] |= ovec[i + 1] >> (8 - num % 8);
  168. }
  169. }
  170. iv = &ovec[0];
  171. c2l(iv, v0);
  172. c2l(iv, v1);
  173. }
  174. }
  175. } else {
  176. while (l >= n) {
  177. l -= n;
  178. ti[0] = v0;
  179. ti[1] = v1;
  180. DES_encrypt3(ti, ks1, ks2, ks3);
  181. c2ln(in, d0, d1, n);
  182. in += n;
  183. /* 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  184. * gcc :-( */
  185. if (num == 32) {
  186. v0 = v1;
  187. v1 = d0;
  188. } else if (num == 64) {
  189. v0 = d0;
  190. v1 = d1;
  191. } else {
  192. iv = &ovec[0];
  193. l2c(v0, iv);
  194. l2c(v1, iv);
  195. l2c(d0, iv);
  196. l2c(d1, iv);
  197. /* shift ovec left most of the bits... */
  198. memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0));
  199. /* now the remaining bits */
  200. if (num % 8 != 0) {
  201. for (i = 0; i < 8; ++i) {
  202. ovec[i] <<= num % 8;
  203. ovec[i] |= ovec[i + 1] >> (8 - num % 8);
  204. }
  205. }
  206. iv = &ovec[0];
  207. c2l(iv, v0);
  208. c2l(iv, v1);
  209. }
  210. d0 ^= ti[0];
  211. d1 ^= ti[1];
  212. l2cn(d0, d1, out, n);
  213. out += n;
  214. }
  215. }
  216. iv = ivec->bytes;
  217. l2c(v0, iv);
  218. l2c(v1, iv);
  219. v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0;
  220. }