2
0

rc4_enc.c 2.2 KB

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