rc5cfb64.c 2.3 KB

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