rc4_enc.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. * RC4 low level APIs are deprecated for public use, but still ok for internal
  11. * use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/rc4.h>
  15. #include "rc4_local.h"
  16. /*-
  17. * RC4 as implemented from a posting from
  18. * Newsgroups: sci.crypt
  19. * Subject: RC4 Algorithm revealed.
  20. * Message-ID: <sternCvKL4B.Hyy@netcom.com>
  21. * Date: Wed, 14 Sep 1994 06:35:31 GMT
  22. */
  23. void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
  24. unsigned char *outdata)
  25. {
  26. register RC4_INT *d;
  27. register RC4_INT x, y, tx, ty;
  28. size_t i;
  29. x = key->x;
  30. y = key->y;
  31. d = key->data;
  32. #define LOOP(in,out) \
  33. x=((x+1)&0xff); \
  34. tx=d[x]; \
  35. y=(tx+y)&0xff; \
  36. d[x]=ty=d[y]; \
  37. d[y]=tx; \
  38. (out) = d[(tx+ty)&0xff]^ (in);
  39. i = len >> 3;
  40. if (i) {
  41. for (;;) {
  42. LOOP(indata[0], outdata[0]);
  43. LOOP(indata[1], outdata[1]);
  44. LOOP(indata[2], outdata[2]);
  45. LOOP(indata[3], outdata[3]);
  46. LOOP(indata[4], outdata[4]);
  47. LOOP(indata[5], outdata[5]);
  48. LOOP(indata[6], outdata[6]);
  49. LOOP(indata[7], outdata[7]);
  50. indata += 8;
  51. outdata += 8;
  52. if (--i == 0)
  53. break;
  54. }
  55. }
  56. i = len & 0x07;
  57. if (i) {
  58. for (;;) {
  59. LOOP(indata[0], outdata[0]);
  60. if (--i == 0)
  61. break;
  62. LOOP(indata[1], outdata[1]);
  63. if (--i == 0)
  64. break;
  65. LOOP(indata[2], outdata[2]);
  66. if (--i == 0)
  67. break;
  68. LOOP(indata[3], outdata[3]);
  69. if (--i == 0)
  70. break;
  71. LOOP(indata[4], outdata[4]);
  72. if (--i == 0)
  73. break;
  74. LOOP(indata[5], outdata[5]);
  75. if (--i == 0)
  76. break;
  77. LOOP(indata[6], outdata[6]);
  78. if (--i == 0)
  79. break;
  80. }
  81. }
  82. key->x = x;
  83. key->y = y;
  84. }