bf_enc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. void BF_decrypt(BF_LONG *data, const BF_KEY *key)
  56. {
  57. register BF_LONG l, r;
  58. register const BF_LONG *p, *s;
  59. p = key->P;
  60. s = &(key->S[0]);
  61. l = data[0];
  62. r = data[1];
  63. l ^= p[BF_ROUNDS + 1];
  64. # if BF_ROUNDS == 20
  65. BF_ENC(r, l, s, p[20]);
  66. BF_ENC(l, r, s, p[19]);
  67. BF_ENC(r, l, s, p[18]);
  68. BF_ENC(l, r, s, p[17]);
  69. # endif
  70. BF_ENC(r, l, s, p[16]);
  71. BF_ENC(l, r, s, p[15]);
  72. BF_ENC(r, l, s, p[14]);
  73. BF_ENC(l, r, s, p[13]);
  74. BF_ENC(r, l, s, p[12]);
  75. BF_ENC(l, r, s, p[11]);
  76. BF_ENC(r, l, s, p[10]);
  77. BF_ENC(l, r, s, p[9]);
  78. BF_ENC(r, l, s, p[8]);
  79. BF_ENC(l, r, s, p[7]);
  80. BF_ENC(r, l, s, p[6]);
  81. BF_ENC(l, r, s, p[5]);
  82. BF_ENC(r, l, s, p[4]);
  83. BF_ENC(l, r, s, p[3]);
  84. BF_ENC(r, l, s, p[2]);
  85. BF_ENC(l, r, s, p[1]);
  86. r ^= p[0];
  87. data[1] = l & 0xffffffffU;
  88. data[0] = r & 0xffffffffU;
  89. }
  90. void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
  91. const BF_KEY *schedule, unsigned char *ivec, int encrypt)
  92. {
  93. register BF_LONG tin0, tin1;
  94. register BF_LONG tout0, tout1, xor0, xor1;
  95. register long l = length;
  96. BF_LONG tin[2];
  97. if (encrypt) {
  98. n2l(ivec, tout0);
  99. n2l(ivec, tout1);
  100. ivec -= 8;
  101. for (l -= 8; l >= 0; l -= 8) {
  102. n2l(in, tin0);
  103. n2l(in, tin1);
  104. tin0 ^= tout0;
  105. tin1 ^= tout1;
  106. tin[0] = tin0;
  107. tin[1] = tin1;
  108. BF_encrypt(tin, schedule);
  109. tout0 = tin[0];
  110. tout1 = tin[1];
  111. l2n(tout0, out);
  112. l2n(tout1, out);
  113. }
  114. if (l != -8) {
  115. n2ln(in, tin0, tin1, l + 8);
  116. tin0 ^= tout0;
  117. tin1 ^= tout1;
  118. tin[0] = tin0;
  119. tin[1] = tin1;
  120. BF_encrypt(tin, schedule);
  121. tout0 = tin[0];
  122. tout1 = tin[1];
  123. l2n(tout0, out);
  124. l2n(tout1, out);
  125. }
  126. l2n(tout0, ivec);
  127. l2n(tout1, ivec);
  128. } else {
  129. n2l(ivec, xor0);
  130. n2l(ivec, xor1);
  131. ivec -= 8;
  132. for (l -= 8; l >= 0; l -= 8) {
  133. n2l(in, tin0);
  134. n2l(in, tin1);
  135. tin[0] = tin0;
  136. tin[1] = tin1;
  137. BF_decrypt(tin, schedule);
  138. tout0 = tin[0] ^ xor0;
  139. tout1 = tin[1] ^ xor1;
  140. l2n(tout0, out);
  141. l2n(tout1, out);
  142. xor0 = tin0;
  143. xor1 = tin1;
  144. }
  145. if (l != -8) {
  146. n2l(in, tin0);
  147. n2l(in, tin1);
  148. tin[0] = tin0;
  149. tin[1] = tin1;
  150. BF_decrypt(tin, schedule);
  151. tout0 = tin[0] ^ xor0;
  152. tout1 = tin[1] ^ xor1;
  153. l2nn(tout0, tout1, out, l + 8);
  154. xor0 = tin0;
  155. xor1 = tin1;
  156. }
  157. l2n(xor0, ivec);
  158. l2n(xor1, ivec);
  159. }
  160. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  161. tin[0] = tin[1] = 0;
  162. }