cfb_enc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 1995-2022 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 "internal/e_os.h"
  15. #include "des_local.h"
  16. #include <assert.h>
  17. /*
  18. * The input and output are loaded in multiples of 8 bits. What this means is
  19. * that if you hame numbits=12 and length=2 the first 12 bits will be
  20. * retrieved from the first byte and half the second. The second 12 bits
  21. * will come from the 3rd and half the 4th byte.
  22. */
  23. /*
  24. * Until Aug 1 2003 this function did not correctly implement CFB-r, so it
  25. * will not be compatible with any encryption prior to that date. Ben.
  26. */
  27. void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
  28. long length, DES_key_schedule *schedule,
  29. DES_cblock *ivec, int enc)
  30. {
  31. register DES_LONG d0, d1, v0, v1;
  32. register unsigned long l = length;
  33. register int num = numbits / 8, n = (numbits + 7) / 8, i, rem =
  34. numbits % 8;
  35. DES_LONG ti[2];
  36. unsigned char *iv;
  37. #ifndef L_ENDIAN
  38. unsigned char ovec[16];
  39. #else
  40. unsigned int sh[4];
  41. unsigned char *ovec = (unsigned char *)sh;
  42. /* I kind of count that compiler optimizes away this assertion, */
  43. assert(sizeof(sh[0]) == 4); /* as this holds true for all, */
  44. /* but 16-bit platforms... */
  45. #endif
  46. if (numbits <= 0 || numbits > 64)
  47. return;
  48. iv = &(*ivec)[0];
  49. c2l(iv, v0);
  50. c2l(iv, v1);
  51. if (enc) {
  52. while (l >= (unsigned long)n) {
  53. l -= n;
  54. ti[0] = v0;
  55. ti[1] = v1;
  56. DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT);
  57. c2ln(in, d0, d1, n);
  58. in += n;
  59. d0 ^= ti[0];
  60. d1 ^= ti[1];
  61. l2cn(d0, d1, out, n);
  62. out += n;
  63. /*
  64. * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  65. * gcc :-(
  66. */
  67. if (numbits == 32) {
  68. v0 = v1;
  69. v1 = d0;
  70. } else if (numbits == 64) {
  71. v0 = d0;
  72. v1 = d1;
  73. } else {
  74. #ifndef L_ENDIAN
  75. iv = &ovec[0];
  76. l2c(v0, iv);
  77. l2c(v1, iv);
  78. l2c(d0, iv);
  79. l2c(d1, iv);
  80. #else
  81. sh[0] = v0, sh[1] = v1, sh[2] = d0, sh[3] = d1;
  82. #endif
  83. if (rem == 0)
  84. memmove(ovec, ovec + num, 8);
  85. else
  86. for (i = 0; i < 8; ++i)
  87. ovec[i] = ovec[i + num] << rem |
  88. ovec[i + num + 1] >> (8 - rem);
  89. #ifdef L_ENDIAN
  90. v0 = sh[0], v1 = sh[1];
  91. #else
  92. iv = &ovec[0];
  93. c2l(iv, v0);
  94. c2l(iv, v1);
  95. #endif
  96. }
  97. }
  98. } else {
  99. while (l >= (unsigned long)n) {
  100. l -= n;
  101. ti[0] = v0;
  102. ti[1] = v1;
  103. DES_encrypt1((DES_LONG *)ti, schedule, DES_ENCRYPT);
  104. c2ln(in, d0, d1, n);
  105. in += n;
  106. /*
  107. * 30-08-94 - eay - changed because l>>32 and l<<32 are bad under
  108. * gcc :-(
  109. */
  110. if (numbits == 32) {
  111. v0 = v1;
  112. v1 = d0;
  113. } else if (numbits == 64) {
  114. v0 = d0;
  115. v1 = d1;
  116. } else {
  117. #ifndef L_ENDIAN
  118. iv = &ovec[0];
  119. l2c(v0, iv);
  120. l2c(v1, iv);
  121. l2c(d0, iv);
  122. l2c(d1, iv);
  123. #else
  124. sh[0] = v0, sh[1] = v1, sh[2] = d0, sh[3] = d1;
  125. #endif
  126. if (rem == 0)
  127. memmove(ovec, ovec + num, 8);
  128. else
  129. for (i = 0; i < 8; ++i)
  130. ovec[i] = ovec[i + num] << rem |
  131. ovec[i + num + 1] >> (8 - rem);
  132. #ifdef L_ENDIAN
  133. v0 = sh[0], v1 = sh[1];
  134. #else
  135. iv = &ovec[0];
  136. c2l(iv, v0);
  137. c2l(iv, v1);
  138. #endif
  139. }
  140. d0 ^= ti[0];
  141. d1 ^= ti[1];
  142. l2cn(d0, d1, out, n);
  143. out += n;
  144. }
  145. }
  146. iv = &(*ivec)[0];
  147. l2c(v0, iv);
  148. l2c(v1, iv);
  149. v0 = v1 = d0 = d1 = ti[0] = ti[1] = 0;
  150. }