rc5_enc.c 4.3 KB

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