cfb64ede.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 1995-2017 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. #include "des_local.h"
  10. /*
  11. * The input and output encrypted as though 64bit cfb mode is being used.
  12. * The extra state information to record how much of the 64bit block we have
  13. * used is contained in *num;
  14. */
  15. void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  16. long length, DES_key_schedule *ks1,
  17. DES_key_schedule *ks2, DES_key_schedule *ks3,
  18. DES_cblock *ivec, int *num, int enc)
  19. {
  20. register DES_LONG v0, v1;
  21. register long l = length;
  22. register int n = *num;
  23. DES_LONG ti[2];
  24. unsigned char *iv, c, cc;
  25. iv = &(*ivec)[0];
  26. if (enc) {
  27. while (l--) {
  28. if (n == 0) {
  29. c2l(iv, v0);
  30. c2l(iv, v1);
  31. ti[0] = v0;
  32. ti[1] = v1;
  33. DES_encrypt3(ti, ks1, ks2, ks3);
  34. v0 = ti[0];
  35. v1 = ti[1];
  36. iv = &(*ivec)[0];
  37. l2c(v0, iv);
  38. l2c(v1, iv);
  39. iv = &(*ivec)[0];
  40. }
  41. c = *(in++) ^ iv[n];
  42. *(out++) = c;
  43. iv[n] = c;
  44. n = (n + 1) & 0x07;
  45. }
  46. } else {
  47. while (l--) {
  48. if (n == 0) {
  49. c2l(iv, v0);
  50. c2l(iv, v1);
  51. ti[0] = v0;
  52. ti[1] = v1;
  53. DES_encrypt3(ti, ks1, ks2, ks3);
  54. v0 = ti[0];
  55. v1 = ti[1];
  56. iv = &(*ivec)[0];
  57. l2c(v0, iv);
  58. l2c(v1, iv);
  59. iv = &(*ivec)[0];
  60. }
  61. cc = *(in++);
  62. c = iv[n];
  63. iv[n] = cc;
  64. *(out++) = c ^ cc;
  65. n = (n + 1) & 0x07;
  66. }
  67. }
  68. v0 = v1 = ti[0] = ti[1] = c = cc = 0;
  69. *num = n;
  70. }
  71. /*
  72. * This is compatible with the single key CFB-r for DES, even thought that's
  73. * not what EVP needs.
  74. */
  75. void DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out,
  76. int numbits, long length, DES_key_schedule *ks1,
  77. DES_key_schedule *ks2, DES_key_schedule *ks3,
  78. DES_cblock *ivec, int enc)
  79. {
  80. register DES_LONG d0, d1, v0, v1;
  81. register unsigned long l = length, n = ((unsigned int)numbits + 7) / 8;
  82. register int num = numbits, i;
  83. DES_LONG ti[2];
  84. unsigned char *iv;
  85. unsigned char ovec[16];
  86. if (num > 64)
  87. return;
  88. iv = &(*ivec)[0];
  89. c2l(iv, v0);
  90. c2l(iv, v1);
  91. if (enc) {
  92. while (l >= n) {
  93. l -= n;
  94. ti[0] = v0;
  95. ti[1] = v1;
  96. DES_encrypt3(ti, ks1, ks2, ks3);
  97. c2ln(in, d0, d1, n);
  98. in += n;
  99. d0 ^= ti[0];
  100. d1 ^= ti[1];
  101. l2cn(d0, d1, out, n);
  102. out += n;
  103. /*
  104. * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  105. * gcc :-(
  106. */
  107. if (num == 32) {
  108. v0 = v1;
  109. v1 = d0;
  110. } else if (num == 64) {
  111. v0 = d0;
  112. v1 = d1;
  113. } else {
  114. iv = &ovec[0];
  115. l2c(v0, iv);
  116. l2c(v1, iv);
  117. l2c(d0, iv);
  118. l2c(d1, iv);
  119. /* shift ovec left most of the bits... */
  120. memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0));
  121. /* now the remaining bits */
  122. if (num % 8 != 0)
  123. for (i = 0; i < 8; ++i) {
  124. ovec[i] <<= num % 8;
  125. ovec[i] |= ovec[i + 1] >> (8 - num % 8);
  126. }
  127. iv = &ovec[0];
  128. c2l(iv, v0);
  129. c2l(iv, v1);
  130. }
  131. }
  132. } else {
  133. while (l >= n) {
  134. l -= n;
  135. ti[0] = v0;
  136. ti[1] = v1;
  137. DES_encrypt3(ti, ks1, ks2, ks3);
  138. c2ln(in, d0, d1, n);
  139. in += n;
  140. /*
  141. * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  142. * gcc :-(
  143. */
  144. if (num == 32) {
  145. v0 = v1;
  146. v1 = d0;
  147. } else if (num == 64) {
  148. v0 = d0;
  149. v1 = d1;
  150. } else {
  151. iv = &ovec[0];
  152. l2c(v0, iv);
  153. l2c(v1, iv);
  154. l2c(d0, iv);
  155. l2c(d1, iv);
  156. /* shift ovec left most of the bits... */
  157. memmove(ovec, ovec + num / 8, 8 + (num % 8 ? 1 : 0));
  158. /* now the remaining bits */
  159. if (num % 8 != 0)
  160. for (i = 0; i < 8; ++i) {
  161. ovec[i] <<= num % 8;
  162. ovec[i] |= ovec[i + 1] >> (8 - num % 8);
  163. }
  164. iv = &ovec[0];
  165. c2l(iv, v0);
  166. c2l(iv, v1);
  167. }
  168. d0 ^= ti[0];
  169. d1 ^= ti[1];
  170. l2cn(d0, d1, out, n);
  171. out += n;
  172. }
  173. }
  174. iv = &(*ivec)[0];
  175. l2c(v0, iv);
  176. l2c(v1, iv);
  177. v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0;
  178. }