2
0

rc2_cbc.c 4.8 KB

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