pcbc_enc.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output,
  16. long length, DES_key_schedule *schedule,
  17. DES_cblock *ivec, int enc)
  18. {
  19. register DES_LONG sin0, sin1, xor0, xor1, tout0, tout1;
  20. DES_LONG tin[2];
  21. const unsigned char *in;
  22. unsigned char *out, *iv;
  23. in = input;
  24. out = output;
  25. iv = &(*ivec)[0];
  26. if (enc) {
  27. c2l(iv, xor0);
  28. c2l(iv, xor1);
  29. for (; length > 0; length -= 8) {
  30. if (length >= 8) {
  31. c2l(in, sin0);
  32. c2l(in, sin1);
  33. } else
  34. c2ln(in, sin0, sin1, length);
  35. tin[0] = sin0 ^ xor0;
  36. tin[1] = sin1 ^ xor1;
  37. DES_encrypt1((DES_LONG *)tin, schedule, DES_ENCRYPT);
  38. tout0 = tin[0];
  39. tout1 = tin[1];
  40. xor0 = sin0 ^ tout0;
  41. xor1 = sin1 ^ tout1;
  42. l2c(tout0, out);
  43. l2c(tout1, out);
  44. }
  45. } else {
  46. c2l(iv, xor0);
  47. c2l(iv, xor1);
  48. for (; length > 0; length -= 8) {
  49. c2l(in, sin0);
  50. c2l(in, sin1);
  51. tin[0] = sin0;
  52. tin[1] = sin1;
  53. DES_encrypt1((DES_LONG *)tin, schedule, DES_DECRYPT);
  54. tout0 = tin[0] ^ xor0;
  55. tout1 = tin[1] ^ xor1;
  56. if (length >= 8) {
  57. l2c(tout0, out);
  58. l2c(tout1, out);
  59. } else
  60. l2cn(tout0, tout1, out, length);
  61. xor0 = tout0 ^ sin0;
  62. xor1 = tout1 ^ sin1;
  63. }
  64. }
  65. tin[0] = tin[1] = 0;
  66. sin0 = sin1 = xor0 = xor1 = tout0 = tout1 = 0;
  67. }