2
0

cfb64enc.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_local.h"
  10. /*
  11. * The input and output encrypted as though 64bit cfb mode is being used.
  12. * The extra state information to record how much of the 64bit block we have
  13. * used is contained in *num;
  14. */
  15. void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  16. long length, DES_key_schedule *schedule,
  17. DES_cblock *ivec, int *num, int enc)
  18. {
  19. register DES_LONG v0, v1;
  20. register long l = length;
  21. register int n = *num;
  22. DES_LONG ti[2];
  23. unsigned char *iv, c, cc;
  24. iv = &(*ivec)[0];
  25. if (enc) {
  26. while (l--) {
  27. if (n == 0) {
  28. c2l(iv, v0);
  29. ti[0] = v0;
  30. c2l(iv, v1);
  31. ti[1] = v1;
  32. DES_encrypt1(ti, schedule, DES_ENCRYPT);
  33. iv = &(*ivec)[0];
  34. v0 = ti[0];
  35. l2c(v0, iv);
  36. v0 = ti[1];
  37. l2c(v0, iv);
  38. iv = &(*ivec)[0];
  39. }
  40. c = *(in++) ^ iv[n];
  41. *(out++) = c;
  42. iv[n] = c;
  43. n = (n + 1) & 0x07;
  44. }
  45. } else {
  46. while (l--) {
  47. if (n == 0) {
  48. c2l(iv, v0);
  49. ti[0] = v0;
  50. c2l(iv, v1);
  51. ti[1] = v1;
  52. DES_encrypt1(ti, schedule, DES_ENCRYPT);
  53. iv = &(*ivec)[0];
  54. v0 = ti[0];
  55. l2c(v0, iv);
  56. v0 = ti[1];
  57. l2c(v0, iv);
  58. iv = &(*ivec)[0];
  59. }
  60. cc = *(in++);
  61. c = iv[n];
  62. iv[n] = cc;
  63. *(out++) = c ^ cc;
  64. n = (n + 1) & 0x07;
  65. }
  66. }
  67. v0 = v1 = ti[0] = ti[1] = c = cc = 0;
  68. *num = n;
  69. }