rc4_skey.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. #include <openssl/opensslv.h>
  12. const char *RC4_options(void)
  13. {
  14. if (sizeof(RC4_INT) == 1)
  15. return ("rc4(char)");
  16. else
  17. return ("rc4(int)");
  18. }
  19. /*-
  20. * RC4 as implemented from a posting from
  21. * Newsgroups: sci.crypt
  22. * From: sterndark@netcom.com (David Sterndark)
  23. * Subject: RC4 Algorithm revealed.
  24. * Message-ID: <sternCvKL4B.Hyy@netcom.com>
  25. * Date: Wed, 14 Sep 1994 06:35:31 GMT
  26. */
  27. void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
  28. {
  29. register RC4_INT tmp;
  30. register int id1, id2;
  31. register RC4_INT *d;
  32. unsigned int i;
  33. d = &(key->data[0]);
  34. key->x = 0;
  35. key->y = 0;
  36. id1 = id2 = 0;
  37. #define SK_LOOP(d,n) { \
  38. tmp=d[(n)]; \
  39. id2 = (data[id1] + tmp + id2) & 0xff; \
  40. if (++id1 == len) id1=0; \
  41. d[(n)]=d[id2]; \
  42. d[id2]=tmp; }
  43. for (i = 0; i < 256; i++)
  44. d[i] = i;
  45. for (i = 0; i < 256; i += 4) {
  46. SK_LOOP(d, i + 0);
  47. SK_LOOP(d, i + 1);
  48. SK_LOOP(d, i + 2);
  49. SK_LOOP(d, i + 3);
  50. }
  51. }