c_enc.c 4.1 KB

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