2
0

rc5_enc.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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 <stdio.h>
  10. #include <openssl/rc5.h>
  11. #include "rc5_local.h"
  12. void RC5_32_cbc_encrypt(const unsigned char *in, unsigned char *out,
  13. long length, RC5_32_KEY *ks, unsigned char *iv,
  14. int encrypt)
  15. {
  16. register unsigned long tin0, tin1;
  17. register unsigned long tout0, tout1, xor0, xor1;
  18. register long l = length;
  19. unsigned long tin[2];
  20. if (encrypt) {
  21. c2l(iv, tout0);
  22. c2l(iv, tout1);
  23. iv -= 8;
  24. for (l -= 8; l >= 0; l -= 8) {
  25. c2l(in, tin0);
  26. c2l(in, tin1);
  27. tin0 ^= tout0;
  28. tin1 ^= tout1;
  29. tin[0] = tin0;
  30. tin[1] = tin1;
  31. RC5_32_encrypt(tin, ks);
  32. tout0 = tin[0];
  33. l2c(tout0, out);
  34. tout1 = tin[1];
  35. l2c(tout1, out);
  36. }
  37. if (l != -8) {
  38. c2ln(in, tin0, tin1, l + 8);
  39. tin0 ^= tout0;
  40. tin1 ^= tout1;
  41. tin[0] = tin0;
  42. tin[1] = tin1;
  43. RC5_32_encrypt(tin, ks);
  44. tout0 = tin[0];
  45. l2c(tout0, out);
  46. tout1 = tin[1];
  47. l2c(tout1, out);
  48. }
  49. l2c(tout0, iv);
  50. l2c(tout1, iv);
  51. } else {
  52. c2l(iv, xor0);
  53. c2l(iv, xor1);
  54. iv -= 8;
  55. for (l -= 8; l >= 0; l -= 8) {
  56. c2l(in, tin0);
  57. tin[0] = tin0;
  58. c2l(in, tin1);
  59. tin[1] = tin1;
  60. RC5_32_decrypt(tin, ks);
  61. tout0 = tin[0] ^ xor0;
  62. tout1 = tin[1] ^ xor1;
  63. l2c(tout0, out);
  64. l2c(tout1, out);
  65. xor0 = tin0;
  66. xor1 = tin1;
  67. }
  68. if (l != -8) {
  69. c2l(in, tin0);
  70. tin[0] = tin0;
  71. c2l(in, tin1);
  72. tin[1] = tin1;
  73. RC5_32_decrypt(tin, ks);
  74. tout0 = tin[0] ^ xor0;
  75. tout1 = tin[1] ^ xor1;
  76. l2cn(tout0, tout1, out, l + 8);
  77. xor0 = tin0;
  78. xor1 = tin1;
  79. }
  80. l2c(xor0, iv);
  81. l2c(xor1, iv);
  82. }
  83. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  84. tin[0] = tin[1] = 0;
  85. }
  86. void RC5_32_encrypt(unsigned long *d, RC5_32_KEY *key)
  87. {
  88. RC5_32_INT a, b, *s;
  89. s = key->data;
  90. a = d[0] + s[0];
  91. b = d[1] + s[1];
  92. E_RC5_32(a, b, s, 2);
  93. E_RC5_32(a, b, s, 4);
  94. E_RC5_32(a, b, s, 6);
  95. E_RC5_32(a, b, s, 8);
  96. E_RC5_32(a, b, s, 10);
  97. E_RC5_32(a, b, s, 12);
  98. E_RC5_32(a, b, s, 14);
  99. E_RC5_32(a, b, s, 16);
  100. if (key->rounds == 12) {
  101. E_RC5_32(a, b, s, 18);
  102. E_RC5_32(a, b, s, 20);
  103. E_RC5_32(a, b, s, 22);
  104. E_RC5_32(a, b, s, 24);
  105. } else if (key->rounds == 16) {
  106. /* Do a full expansion to avoid a jump */
  107. E_RC5_32(a, b, s, 18);
  108. E_RC5_32(a, b, s, 20);
  109. E_RC5_32(a, b, s, 22);
  110. E_RC5_32(a, b, s, 24);
  111. E_RC5_32(a, b, s, 26);
  112. E_RC5_32(a, b, s, 28);
  113. E_RC5_32(a, b, s, 30);
  114. E_RC5_32(a, b, s, 32);
  115. }
  116. d[0] = a;
  117. d[1] = b;
  118. }
  119. void RC5_32_decrypt(unsigned long *d, RC5_32_KEY *key)
  120. {
  121. RC5_32_INT a, b, *s;
  122. s = key->data;
  123. a = d[0];
  124. b = d[1];
  125. if (key->rounds == 16) {
  126. D_RC5_32(a, b, s, 32);
  127. D_RC5_32(a, b, s, 30);
  128. D_RC5_32(a, b, s, 28);
  129. D_RC5_32(a, b, s, 26);
  130. /* Do a full expansion to avoid a jump */
  131. D_RC5_32(a, b, s, 24);
  132. D_RC5_32(a, b, s, 22);
  133. D_RC5_32(a, b, s, 20);
  134. D_RC5_32(a, b, s, 18);
  135. } else if (key->rounds == 12) {
  136. D_RC5_32(a, b, s, 24);
  137. D_RC5_32(a, b, s, 22);
  138. D_RC5_32(a, b, s, 20);
  139. D_RC5_32(a, b, s, 18);
  140. }
  141. D_RC5_32(a, b, s, 16);
  142. D_RC5_32(a, b, s, 14);
  143. D_RC5_32(a, b, s, 12);
  144. D_RC5_32(a, b, s, 10);
  145. D_RC5_32(a, b, s, 8);
  146. D_RC5_32(a, b, s, 6);
  147. D_RC5_32(a, b, s, 4);
  148. D_RC5_32(a, b, s, 2);
  149. d[0] = a - s[0];
  150. d[1] = b - s[1];
  151. }