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.

86 lines
2.4 KiB

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. void BF_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
  12. const BF_KEY *schedule, unsigned char *ivec, int encrypt)
  13. {
  14. register BF_LONG tin0, tin1;
  15. register BF_LONG tout0, tout1, xor0, xor1;
  16. register long l = length;
  17. BF_LONG tin[2];
  18. if (encrypt) {
  19. n2l(ivec, tout0);
  20. n2l(ivec, tout1);
  21. ivec -= 8;
  22. for (l -= 8; l >= 0; l -= 8) {
  23. n2l(in, tin0);
  24. n2l(in, tin1);
  25. tin0 ^= tout0;
  26. tin1 ^= tout1;
  27. tin[0] = tin0;
  28. tin[1] = tin1;
  29. BF_encrypt(tin, schedule);
  30. tout0 = tin[0];
  31. tout1 = tin[1];
  32. l2n(tout0, out);
  33. l2n(tout1, out);
  34. }
  35. if (l != -8) {
  36. n2ln(in, tin0, tin1, l + 8);
  37. tin0 ^= tout0;
  38. tin1 ^= tout1;
  39. tin[0] = tin0;
  40. tin[1] = tin1;
  41. BF_encrypt(tin, schedule);
  42. tout0 = tin[0];
  43. tout1 = tin[1];
  44. l2n(tout0, out);
  45. l2n(tout1, out);
  46. }
  47. l2n(tout0, ivec);
  48. l2n(tout1, ivec);
  49. } else {
  50. n2l(ivec, xor0);
  51. n2l(ivec, xor1);
  52. ivec -= 8;
  53. for (l -= 8; l >= 0; l -= 8) {
  54. n2l(in, tin0);
  55. n2l(in, tin1);
  56. tin[0] = tin0;
  57. tin[1] = tin1;
  58. BF_decrypt(tin, schedule);
  59. tout0 = tin[0] ^ xor0;
  60. tout1 = tin[1] ^ xor1;
  61. l2n(tout0, out);
  62. l2n(tout1, out);
  63. xor0 = tin0;
  64. xor1 = tin1;
  65. }
  66. if (l != -8) {
  67. n2l(in, tin0);
  68. n2l(in, tin1);
  69. tin[0] = tin0;
  70. tin[1] = tin1;
  71. BF_decrypt(tin, schedule);
  72. tout0 = tin[0] ^ xor0;
  73. tout1 = tin[1] ^ xor1;
  74. l2nn(tout0, tout1, out, l + 8);
  75. xor0 = tin0;
  76. xor1 = tin1;
  77. }
  78. l2n(xor0, ivec);
  79. l2n(xor1, ivec);
  80. }
  81. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  82. tin[0] = tin[1] = 0;
  83. }