cfb64ede.c 5.5 KB

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