rc4_skey.c 1.5 KB

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