e_rc4.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #ifndef OPENSSL_NO_RC4
  17. # include <openssl/evp.h>
  18. # include <openssl/objects.h>
  19. # include <openssl/rc4.h>
  20. # include "crypto/evp.h"
  21. typedef struct {
  22. RC4_KEY ks; /* working key */
  23. } EVP_RC4_KEY;
  24. # define data(ctx) ((EVP_RC4_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx))
  25. static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  26. const unsigned char *iv, int enc);
  27. static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  28. const unsigned char *in, size_t inl);
  29. static const EVP_CIPHER r4_cipher = {
  30. NID_rc4,
  31. 1, EVP_RC4_KEY_SIZE, 0,
  32. EVP_CIPH_VARIABLE_LENGTH,
  33. rc4_init_key,
  34. rc4_cipher,
  35. NULL,
  36. sizeof(EVP_RC4_KEY),
  37. NULL,
  38. NULL,
  39. NULL,
  40. NULL
  41. };
  42. static const EVP_CIPHER r4_40_cipher = {
  43. NID_rc4_40,
  44. 1, 5 /* 40 bit */ , 0,
  45. EVP_CIPH_VARIABLE_LENGTH,
  46. rc4_init_key,
  47. rc4_cipher,
  48. NULL,
  49. sizeof(EVP_RC4_KEY),
  50. NULL,
  51. NULL,
  52. NULL,
  53. NULL
  54. };
  55. const EVP_CIPHER *EVP_rc4(void)
  56. {
  57. return &r4_cipher;
  58. }
  59. const EVP_CIPHER *EVP_rc4_40(void)
  60. {
  61. return &r4_40_cipher;
  62. }
  63. static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  64. const unsigned char *iv, int enc)
  65. {
  66. RC4_set_key(&data(ctx)->ks, EVP_CIPHER_CTX_key_length(ctx), key);
  67. return 1;
  68. }
  69. static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  70. const unsigned char *in, size_t inl)
  71. {
  72. RC4(&data(ctx)->ks, inl, in, out);
  73. return 1;
  74. }
  75. #endif