A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

179 lines
4.5 KiB

23 years ago
23 years ago
23 years ago
  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/blowfish.h>
  10. #include "bf_locl.h"
  11. /*
  12. * Blowfish as implemented from 'Blowfish: Springer-Verlag paper' (From
  13. * LECTURE NOTES IN COMPUTER SCIENCE 809, FAST SOFTWARE ENCRYPTION, CAMBRIDGE
  14. * SECURITY WORKSHOP, CAMBRIDGE, U.K., DECEMBER 9-11, 1993)
  15. */
  16. #if (BF_ROUNDS != 16) && (BF_ROUNDS != 20)
  17. # error If you set BF_ROUNDS to some value other than 16 or 20, you will have \
  18. to modify the code.
  19. #endif
  20. void BF_encrypt(BF_LONG *data, const BF_KEY *key)
  21. {
  22. register BF_LONG l, r;
  23. register const BF_LONG *p, *s;
  24. p = key->P;
  25. s = &(key->S[0]);
  26. l = data[0];
  27. r = data[1];
  28. l ^= p[0];
  29. BF_ENC(r, l, s, p[1]);
  30. BF_ENC(l, r, s, p[2]);
  31. BF_ENC(r, l, s, p[3]);
  32. BF_ENC(l, r, s, p[4]);
  33. BF_ENC(r, l, s, p[5]);
  34. BF_ENC(l, r, s, p[6]);
  35. BF_ENC(r, l, s, p[7]);
  36. BF_ENC(l, r, s, p[8]);
  37. BF_ENC(r, l, s, p[9]);
  38. BF_ENC(l, r, s, p[10]);
  39. BF_ENC(r, l, s, p[11]);
  40. BF_ENC(l, r, s, p[12]);
  41. BF_ENC(r, l, s, p[13]);
  42. BF_ENC(l, r, s, p[14]);
  43. BF_ENC(r, l, s, p[15]);
  44. BF_ENC(l, r, s, p[16]);
  45. # if BF_ROUNDS == 20
  46. BF_ENC(r, l, s, p[17]);
  47. BF_ENC(l, r, s, p[18]);
  48. BF_ENC(r, l, s, p[19]);
  49. BF_ENC(l, r, s, p[20]);
  50. # endif
  51. r ^= p[BF_ROUNDS + 1];
  52. data[1] = l & 0xffffffffU;
  53. data[0] = r & 0xffffffffU;
  54. }
  55. #ifndef BF_DEFAULT_OPTIONS
  56. void BF_decrypt(BF_LONG *data, const BF_KEY *key)
  57. {
  58. register BF_LONG l, r;
  59. register const BF_LONG *p, *s;
  60. p = key->P;
  61. s = &(key->S[0]);
  62. l = data[0];
  63. r = data[1];
  64. l ^= p[BF_ROUNDS + 1];
  65. # if BF_ROUNDS == 20
  66. BF_ENC(r, l, s, p[20]);
  67. BF_ENC(l, r, s, p[19]);
  68. BF_ENC(r, l, s, p[18]);
  69. BF_ENC(l, r, s, p[17]);
  70. # endif
  71. BF_ENC(r, l, s, p[16]);
  72. BF_ENC(l, r, s, p[15]);
  73. BF_ENC(r, l, s, p[14]);
  74. BF_ENC(l, r, s, p[13]);
  75. BF_ENC(r, l, s, p[12]);
  76. BF_ENC(l, r, s, p[11]);
  77. BF_ENC(r, l, s, p[10]);
  78. BF_ENC(l, r, s, p[9]);
  79. BF_ENC(r, l, s, p[8]);
  80. BF_ENC(l, r, s, p[7]);
  81. BF_ENC(r, l, s, p[6]);
  82. BF_ENC(l, r, s, p[5]);
  83. BF_ENC(r, l, s, p[4]);
  84. BF_ENC(l, r, s, p[3]);
  85. BF_ENC(r, l, s, p[2]);
  86. BF_ENC(l, r, s, p[1]);
  87. r ^= p[0];
  88. data[1] = l & 0xffffffffU;
  89. data[0] = r & 0xffffffffU;
  90. }
  91. void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
  92. const BF_KEY *schedule, unsigned char *ivec, int encrypt)
  93. {
  94. register BF_LONG tin0, tin1;
  95. register BF_LONG tout0, tout1, xor0, xor1;
  96. register long l = length;
  97. BF_LONG tin[2];
  98. if (encrypt) {
  99. n2l(ivec, tout0);
  100. n2l(ivec, tout1);
  101. ivec -= 8;
  102. for (l -= 8; l >= 0; l -= 8) {
  103. n2l(in, tin0);
  104. n2l(in, tin1);
  105. tin0 ^= tout0;
  106. tin1 ^= tout1;
  107. tin[0] = tin0;
  108. tin[1] = tin1;
  109. BF_encrypt(tin, schedule);
  110. tout0 = tin[0];
  111. tout1 = tin[1];
  112. l2n(tout0, out);
  113. l2n(tout1, out);
  114. }
  115. if (l != -8) {
  116. n2ln(in, tin0, tin1, l + 8);
  117. tin0 ^= tout0;
  118. tin1 ^= tout1;
  119. tin[0] = tin0;
  120. tin[1] = tin1;
  121. BF_encrypt(tin, schedule);
  122. tout0 = tin[0];
  123. tout1 = tin[1];
  124. l2n(tout0, out);
  125. l2n(tout1, out);
  126. }
  127. l2n(tout0, ivec);
  128. l2n(tout1, ivec);
  129. } else {
  130. n2l(ivec, xor0);
  131. n2l(ivec, xor1);
  132. ivec -= 8;
  133. for (l -= 8; l >= 0; l -= 8) {
  134. n2l(in, tin0);
  135. n2l(in, tin1);
  136. tin[0] = tin0;
  137. tin[1] = tin1;
  138. BF_decrypt(tin, schedule);
  139. tout0 = tin[0] ^ xor0;
  140. tout1 = tin[1] ^ xor1;
  141. l2n(tout0, out);
  142. l2n(tout1, out);
  143. xor0 = tin0;
  144. xor1 = tin1;
  145. }
  146. if (l != -8) {
  147. n2l(in, tin0);
  148. n2l(in, tin1);
  149. tin[0] = tin0;
  150. tin[1] = tin1;
  151. BF_decrypt(tin, schedule);
  152. tout0 = tin[0] ^ xor0;
  153. tout1 = tin[1] ^ xor1;
  154. l2nn(tout0, tout1, out, l + 8);
  155. xor0 = tin0;
  156. xor1 = tin1;
  157. }
  158. l2n(xor0, ivec);
  159. l2n(xor1, ivec);
  160. }
  161. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  162. tin[0] = tin[1] = 0;
  163. }
  164. #endif