ncbc_enc.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Copyright 1998-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. * #included by:
  11. * cbc_enc.c (DES_cbc_encrypt)
  12. * des_enc.c (DES_ncbc_encrypt)
  13. */
  14. #include "des_local.h"
  15. #ifdef CBC_ENC_C__DONT_UPDATE_IV
  16. void DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
  17. DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
  18. #else
  19. void DES_ncbc_encrypt(const unsigned char *in, unsigned char *out,
  20. long length, DES_key_schedule *_schedule,
  21. DES_cblock *ivec, int enc)
  22. #endif
  23. {
  24. register DES_LONG tin0, tin1;
  25. register DES_LONG tout0, tout1, xor0, xor1;
  26. register long l = length;
  27. DES_LONG tin[2];
  28. unsigned char *iv;
  29. iv = &(*ivec)[0];
  30. if (enc) {
  31. c2l(iv, tout0);
  32. c2l(iv, tout1);
  33. for (l -= 8; l >= 0; l -= 8) {
  34. c2l(in, tin0);
  35. c2l(in, tin1);
  36. tin0 ^= tout0;
  37. tin[0] = tin0;
  38. tin1 ^= tout1;
  39. tin[1] = tin1;
  40. DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT);
  41. tout0 = tin[0];
  42. l2c(tout0, out);
  43. tout1 = tin[1];
  44. l2c(tout1, out);
  45. }
  46. if (l != -8) {
  47. c2ln(in, tin0, tin1, l + 8);
  48. tin0 ^= tout0;
  49. tin[0] = tin0;
  50. tin1 ^= tout1;
  51. tin[1] = tin1;
  52. DES_encrypt1((DES_LONG *)tin, _schedule, DES_ENCRYPT);
  53. tout0 = tin[0];
  54. l2c(tout0, out);
  55. tout1 = tin[1];
  56. l2c(tout1, out);
  57. }
  58. #ifndef CBC_ENC_C__DONT_UPDATE_IV
  59. iv = &(*ivec)[0];
  60. l2c(tout0, iv);
  61. l2c(tout1, iv);
  62. #endif
  63. } else {
  64. c2l(iv, xor0);
  65. c2l(iv, xor1);
  66. for (l -= 8; l >= 0; l -= 8) {
  67. c2l(in, tin0);
  68. tin[0] = tin0;
  69. c2l(in, tin1);
  70. tin[1] = tin1;
  71. DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT);
  72. tout0 = tin[0] ^ xor0;
  73. tout1 = tin[1] ^ xor1;
  74. l2c(tout0, out);
  75. l2c(tout1, out);
  76. xor0 = tin0;
  77. xor1 = tin1;
  78. }
  79. if (l != -8) {
  80. c2l(in, tin0);
  81. tin[0] = tin0;
  82. c2l(in, tin1);
  83. tin[1] = tin1;
  84. DES_encrypt1((DES_LONG *)tin, _schedule, DES_DECRYPT);
  85. tout0 = tin[0] ^ xor0;
  86. tout1 = tin[1] ^ xor1;
  87. l2cn(tout0, tout1, out, l + 8);
  88. #ifndef CBC_ENC_C__DONT_UPDATE_IV
  89. xor0 = tin0;
  90. xor1 = tin1;
  91. #endif
  92. }
  93. #ifndef CBC_ENC_C__DONT_UPDATE_IV
  94. iv = &(*ivec)[0];
  95. l2c(xor0, iv);
  96. l2c(xor1, iv);
  97. #endif
  98. }
  99. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  100. tin[0] = tin[1] = 0;
  101. }