i_cfb64.c 2.2 KB

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