2
0

bf_enc.c 4.6 KB

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