2
0

c_enc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 <openssl/cast.h>
  10. #include "cast_local.h"
  11. void CAST_encrypt(CAST_LONG *data, const CAST_KEY *key)
  12. {
  13. CAST_LONG l, r, t;
  14. const CAST_LONG *k;
  15. k = &(key->data[0]);
  16. l = data[0];
  17. r = data[1];
  18. E_CAST(0, k, l, r, +, ^, -);
  19. E_CAST(1, k, r, l, ^, -, +);
  20. E_CAST(2, k, l, r, -, +, ^);
  21. E_CAST(3, k, r, l, +, ^, -);
  22. E_CAST(4, k, l, r, ^, -, +);
  23. E_CAST(5, k, r, l, -, +, ^);
  24. E_CAST(6, k, l, r, +, ^, -);
  25. E_CAST(7, k, r, l, ^, -, +);
  26. E_CAST(8, k, l, r, -, +, ^);
  27. E_CAST(9, k, r, l, +, ^, -);
  28. E_CAST(10, k, l, r, ^, -, +);
  29. E_CAST(11, k, r, l, -, +, ^);
  30. if (!key->short_key) {
  31. E_CAST(12, k, l, r, +, ^, -);
  32. E_CAST(13, k, r, l, ^, -, +);
  33. E_CAST(14, k, l, r, -, +, ^);
  34. E_CAST(15, k, r, l, +, ^, -);
  35. }
  36. data[1] = l & 0xffffffffL;
  37. data[0] = r & 0xffffffffL;
  38. }
  39. void CAST_decrypt(CAST_LONG *data, const CAST_KEY *key)
  40. {
  41. CAST_LONG l, r, t;
  42. const CAST_LONG *k;
  43. k = &(key->data[0]);
  44. l = data[0];
  45. r = data[1];
  46. if (!key->short_key) {
  47. E_CAST(15, k, l, r, +, ^, -);
  48. E_CAST(14, k, r, l, -, +, ^);
  49. E_CAST(13, k, l, r, ^, -, +);
  50. E_CAST(12, k, r, l, +, ^, -);
  51. }
  52. E_CAST(11, k, l, r, -, +, ^);
  53. E_CAST(10, k, r, l, ^, -, +);
  54. E_CAST(9, k, l, r, +, ^, -);
  55. E_CAST(8, k, r, l, -, +, ^);
  56. E_CAST(7, k, l, r, ^, -, +);
  57. E_CAST(6, k, r, l, +, ^, -);
  58. E_CAST(5, k, l, r, -, +, ^);
  59. E_CAST(4, k, r, l, ^, -, +);
  60. E_CAST(3, k, l, r, +, ^, -);
  61. E_CAST(2, k, r, l, -, +, ^);
  62. E_CAST(1, k, l, r, ^, -, +);
  63. E_CAST(0, k, r, l, +, ^, -);
  64. data[1] = l & 0xffffffffL;
  65. data[0] = r & 0xffffffffL;
  66. }
  67. void CAST_cbc_encrypt(const unsigned char *in, unsigned char *out,
  68. long length, const CAST_KEY *ks, unsigned char *iv,
  69. int enc)
  70. {
  71. register CAST_LONG tin0, tin1;
  72. register CAST_LONG tout0, tout1, xor0, xor1;
  73. register long l = length;
  74. CAST_LONG tin[2];
  75. if (enc) {
  76. n2l(iv, tout0);
  77. n2l(iv, tout1);
  78. iv -= 8;
  79. for (l -= 8; l >= 0; l -= 8) {
  80. n2l(in, tin0);
  81. n2l(in, tin1);
  82. tin0 ^= tout0;
  83. tin1 ^= tout1;
  84. tin[0] = tin0;
  85. tin[1] = tin1;
  86. CAST_encrypt(tin, ks);
  87. tout0 = tin[0];
  88. tout1 = tin[1];
  89. l2n(tout0, out);
  90. l2n(tout1, out);
  91. }
  92. if (l != -8) {
  93. n2ln(in, tin0, tin1, l + 8);
  94. tin0 ^= tout0;
  95. tin1 ^= tout1;
  96. tin[0] = tin0;
  97. tin[1] = tin1;
  98. CAST_encrypt(tin, ks);
  99. tout0 = tin[0];
  100. tout1 = tin[1];
  101. l2n(tout0, out);
  102. l2n(tout1, out);
  103. }
  104. l2n(tout0, iv);
  105. l2n(tout1, iv);
  106. } else {
  107. n2l(iv, xor0);
  108. n2l(iv, xor1);
  109. iv -= 8;
  110. for (l -= 8; l >= 0; l -= 8) {
  111. n2l(in, tin0);
  112. n2l(in, tin1);
  113. tin[0] = tin0;
  114. tin[1] = tin1;
  115. CAST_decrypt(tin, ks);
  116. tout0 = tin[0] ^ xor0;
  117. tout1 = tin[1] ^ xor1;
  118. l2n(tout0, out);
  119. l2n(tout1, out);
  120. xor0 = tin0;
  121. xor1 = tin1;
  122. }
  123. if (l != -8) {
  124. n2l(in, tin0);
  125. n2l(in, tin1);
  126. tin[0] = tin0;
  127. tin[1] = tin1;
  128. CAST_decrypt(tin, ks);
  129. tout0 = tin[0] ^ xor0;
  130. tout1 = tin[1] ^ xor1;
  131. l2nn(tout0, tout1, out, l + 8);
  132. xor0 = tin0;
  133. xor1 = tin1;
  134. }
  135. l2n(xor0, iv);
  136. l2n(xor1, iv);
  137. }
  138. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  139. tin[0] = tin[1] = 0;
  140. }