xcbc_enc.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "des_locl.h"
  10. /* RSA's DESX */
  11. void DES_xcbc_encrypt(const unsigned char *in, unsigned char *out,
  12. long length, DES_key_schedule *schedule,
  13. DES_cblock *ivec, const_DES_cblock *inw,
  14. const_DES_cblock *outw, int enc)
  15. {
  16. register DES_LONG tin0, tin1;
  17. register DES_LONG tout0, tout1, xor0, xor1;
  18. register DES_LONG inW0, inW1, outW0, outW1;
  19. register const unsigned char *in2;
  20. register long l = length;
  21. DES_LONG tin[2];
  22. unsigned char *iv;
  23. in2 = &(*inw)[0];
  24. c2l(in2, inW0);
  25. c2l(in2, inW1);
  26. in2 = &(*outw)[0];
  27. c2l(in2, outW0);
  28. c2l(in2, outW1);
  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 ^ inW0;
  37. tin[0] = tin0;
  38. tin1 ^= tout1 ^ inW1;
  39. tin[1] = tin1;
  40. DES_encrypt1(tin, schedule, DES_ENCRYPT);
  41. tout0 = tin[0] ^ outW0;
  42. l2c(tout0, out);
  43. tout1 = tin[1] ^ outW1;
  44. l2c(tout1, out);
  45. }
  46. if (l != -8) {
  47. c2ln(in, tin0, tin1, l + 8);
  48. tin0 ^= tout0 ^ inW0;
  49. tin[0] = tin0;
  50. tin1 ^= tout1 ^ inW1;
  51. tin[1] = tin1;
  52. DES_encrypt1(tin, schedule, DES_ENCRYPT);
  53. tout0 = tin[0] ^ outW0;
  54. l2c(tout0, out);
  55. tout1 = tin[1] ^ outW1;
  56. l2c(tout1, out);
  57. }
  58. iv = &(*ivec)[0];
  59. l2c(tout0, iv);
  60. l2c(tout1, iv);
  61. } else {
  62. c2l(iv, xor0);
  63. c2l(iv, xor1);
  64. for (l -= 8; l > 0; l -= 8) {
  65. c2l(in, tin0);
  66. tin[0] = tin0 ^ outW0;
  67. c2l(in, tin1);
  68. tin[1] = tin1 ^ outW1;
  69. DES_encrypt1(tin, schedule, DES_DECRYPT);
  70. tout0 = tin[0] ^ xor0 ^ inW0;
  71. tout1 = tin[1] ^ xor1 ^ inW1;
  72. l2c(tout0, out);
  73. l2c(tout1, out);
  74. xor0 = tin0;
  75. xor1 = tin1;
  76. }
  77. if (l != -8) {
  78. c2l(in, tin0);
  79. tin[0] = tin0 ^ outW0;
  80. c2l(in, tin1);
  81. tin[1] = tin1 ^ outW1;
  82. DES_encrypt1(tin, schedule, DES_DECRYPT);
  83. tout0 = tin[0] ^ xor0 ^ inW0;
  84. tout1 = tin[1] ^ xor1 ^ inW1;
  85. l2cn(tout0, tout1, out, l + 8);
  86. xor0 = tin0;
  87. xor1 = tin1;
  88. }
  89. iv = &(*ivec)[0];
  90. l2c(xor0, iv);
  91. l2c(xor1, iv);
  92. }
  93. tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0;
  94. inW0 = inW1 = outW0 = outW1 = 0;
  95. tin[0] = tin[1] = 0;
  96. }