rc4_skey.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. #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. * Subject: RC4 Algorithm revealed.
  23. * Message-ID: <sternCvKL4B.Hyy@netcom.com>
  24. * Date: Wed, 14 Sep 1994 06:35:31 GMT
  25. */
  26. void RC4_set_key(RC4_KEY *key, int len, const unsigned char *data)
  27. {
  28. register RC4_INT tmp;
  29. register int id1, id2;
  30. register RC4_INT *d;
  31. unsigned int i;
  32. d = &(key->data[0]);
  33. key->x = 0;
  34. key->y = 0;
  35. id1 = id2 = 0;
  36. #define SK_LOOP(d,n) { \
  37. tmp=d[(n)]; \
  38. id2 = (data[id1] + tmp + id2) & 0xff; \
  39. if (++id1 == len) id1=0; \
  40. d[(n)]=d[id2]; \
  41. d[id2]=tmp; }
  42. for (i = 0; i < 256; i++)
  43. d[i] = i;
  44. for (i = 0; i < 256; i += 4) {
  45. SK_LOOP(d, i + 0);
  46. SK_LOOP(d, i + 1);
  47. SK_LOOP(d, i + 2);
  48. SK_LOOP(d, i + 3);
  49. }
  50. }