xcbc_enc.c 3.1 KB

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