rc2_cbc.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * RC2 low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/rc2.h>
  15. #include "rc2_local.h"
  16. void RC2_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
  17. RC2_KEY *ks, unsigned char *iv, int encrypt)
  18. {
  19. register unsigned long tin0, tin1;
  20. register unsigned long tout0, tout1, xor0, xor1;
  21. register long l = length;
  22. unsigned long tin[2];
  23. if (encrypt) {
  24. c2l(iv, tout0);
  25. c2l(iv, tout1);
  26. iv -= 8;
  27. for (l -= 8; l >= 0; l -= 8) {
  28. c2l(in, tin0);
  29. c2l(in, tin1);
  30. tin0 ^= tout0;
  31. tin1 ^= tout1;
  32. tin[0] = tin0;
  33. tin[1] = tin1;
  34. RC2_encrypt(tin, ks);
  35. tout0 = tin[0];
  36. l2c(tout0, out);
  37. tout1 = tin[1];
  38. l2c(tout1, out);
  39. }
  40. if (l != -8) {
  41. c2ln(in, tin0, tin1, l + 8);
  42. tin0 ^= tout0;
  43. tin1 ^= tout1;
  44. tin[0] = tin0;
  45. tin[1] = tin1;
  46. RC2_encrypt(tin, ks);
  47. tout0 = tin[0];
  48. l2c(tout0, out);
  49. tout1 = tin[1];
  50. l2c(tout1, out);
  51. }
  52. l2c(tout0, iv);
  53. l2c(tout1, iv);
  54. } else {
  55. c2l(iv, xor0);
  56. c2l(iv, xor1);
  57. iv -= 8;
  58. for (l -= 8; l >= 0; l -= 8) {
  59. c2l(in, tin0);
  60. tin[0] = tin0;
  61. c2l(in, tin1);
  62. tin[1] = tin1;
  63. RC2_decrypt(tin, ks);
  64. tout0 = tin[0] ^ xor0;
  65. tout1 = tin[1] ^ xor1;
  66. l2c(tout0, out);
  67. l2c(tout1, out);
  68. xor0 = tin0;
  69. xor1 = tin1;
  70. }
  71. if (l != -8) {
  72. c2l(in, tin0);
  73. tin[0] = tin0;
  74. c2l(in, tin1);
  75. tin[1] = tin1;
  76. RC2_decrypt(tin, ks);
  77. tout0 = tin[0] ^ xor0;
  78. tout1 = tin[1] ^ xor1;
  79. l2cn(tout0, tout1, out, l + 8);
  80. xor0 = tin0;
  81. xor1 = tin1;
  82. }
  83. l2c(xor0, iv);
  84. l2c(xor1, iv);
  85. }
  86. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  87. tin[0] = tin[1] = 0;
  88. }
  89. void RC2_encrypt(unsigned long *d, RC2_KEY *key)
  90. {
  91. int i, n;
  92. register RC2_INT *p0, *p1;
  93. register RC2_INT x0, x1, x2, x3, t;
  94. unsigned long l;
  95. l = d[0];
  96. x0 = (RC2_INT) l & 0xffff;
  97. x1 = (RC2_INT) (l >> 16L);
  98. l = d[1];
  99. x2 = (RC2_INT) l & 0xffff;
  100. x3 = (RC2_INT) (l >> 16L);
  101. n = 3;
  102. i = 5;
  103. p0 = p1 = &(key->data[0]);
  104. for (;;) {
  105. t = (x0 + (x1 & ~x3) + (x2 & x3) + *(p0++)) & 0xffff;
  106. x0 = (t << 1) | (t >> 15);
  107. t = (x1 + (x2 & ~x0) + (x3 & x0) + *(p0++)) & 0xffff;
  108. x1 = (t << 2) | (t >> 14);
  109. t = (x2 + (x3 & ~x1) + (x0 & x1) + *(p0++)) & 0xffff;
  110. x2 = (t << 3) | (t >> 13);
  111. t = (x3 + (x0 & ~x2) + (x1 & x2) + *(p0++)) & 0xffff;
  112. x3 = (t << 5) | (t >> 11);
  113. if (--i == 0) {
  114. if (--n == 0)
  115. break;
  116. i = (n == 2) ? 6 : 5;
  117. x0 += p1[x3 & 0x3f];
  118. x1 += p1[x0 & 0x3f];
  119. x2 += p1[x1 & 0x3f];
  120. x3 += p1[x2 & 0x3f];
  121. }
  122. }
  123. d[0] =
  124. (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
  125. d[1] =
  126. (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);
  127. }
  128. void RC2_decrypt(unsigned long *d, RC2_KEY *key)
  129. {
  130. int i, n;
  131. register RC2_INT *p0, *p1;
  132. register RC2_INT x0, x1, x2, x3, t;
  133. unsigned long l;
  134. l = d[0];
  135. x0 = (RC2_INT) l & 0xffff;
  136. x1 = (RC2_INT) (l >> 16L);
  137. l = d[1];
  138. x2 = (RC2_INT) l & 0xffff;
  139. x3 = (RC2_INT) (l >> 16L);
  140. n = 3;
  141. i = 5;
  142. p0 = &(key->data[63]);
  143. p1 = &(key->data[0]);
  144. for (;;) {
  145. t = ((x3 << 11) | (x3 >> 5)) & 0xffff;
  146. x3 = (t - (x0 & ~x2) - (x1 & x2) - *(p0--)) & 0xffff;
  147. t = ((x2 << 13) | (x2 >> 3)) & 0xffff;
  148. x2 = (t - (x3 & ~x1) - (x0 & x1) - *(p0--)) & 0xffff;
  149. t = ((x1 << 14) | (x1 >> 2)) & 0xffff;
  150. x1 = (t - (x2 & ~x0) - (x3 & x0) - *(p0--)) & 0xffff;
  151. t = ((x0 << 15) | (x0 >> 1)) & 0xffff;
  152. x0 = (t - (x1 & ~x3) - (x2 & x3) - *(p0--)) & 0xffff;
  153. if (--i == 0) {
  154. if (--n == 0)
  155. break;
  156. i = (n == 2) ? 6 : 5;
  157. x3 = (x3 - p1[x2 & 0x3f]) & 0xffff;
  158. x2 = (x2 - p1[x1 & 0x3f]) & 0xffff;
  159. x1 = (x1 - p1[x0 & 0x3f]) & 0xffff;
  160. x0 = (x0 - p1[x3 & 0x3f]) & 0xffff;
  161. }
  162. }
  163. d[0] =
  164. (unsigned long)(x0 & 0xffff) | ((unsigned long)(x1 & 0xffff) << 16L);
  165. d[1] =
  166. (unsigned long)(x2 & 0xffff) | ((unsigned long)(x3 & 0xffff) << 16L);
  167. }