pcbc_enc.c 2.0 KB

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