e_rc4.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright 1995-2021 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. EVP_ORIG_GLOBAL,
  34. rc4_init_key,
  35. rc4_cipher,
  36. NULL,
  37. sizeof(EVP_RC4_KEY),
  38. NULL,
  39. NULL,
  40. NULL,
  41. NULL
  42. };
  43. static const EVP_CIPHER r4_40_cipher = {
  44. NID_rc4_40,
  45. 1, 5 /* 40 bit */ , 0,
  46. EVP_CIPH_VARIABLE_LENGTH,
  47. EVP_ORIG_GLOBAL,
  48. rc4_init_key,
  49. rc4_cipher,
  50. NULL,
  51. sizeof(EVP_RC4_KEY),
  52. NULL,
  53. NULL,
  54. NULL,
  55. NULL
  56. };
  57. const EVP_CIPHER *EVP_rc4(void)
  58. {
  59. return &r4_cipher;
  60. }
  61. const EVP_CIPHER *EVP_rc4_40(void)
  62. {
  63. return &r4_40_cipher;
  64. }
  65. static int rc4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  66. const unsigned char *iv, int enc)
  67. {
  68. int keylen;
  69. if ((keylen = EVP_CIPHER_CTX_get_key_length(ctx)) <= 0)
  70. return 0;
  71. RC4_set_key(&data(ctx)->ks, keylen, key);
  72. return 1;
  73. }
  74. static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  75. const unsigned char *in, size_t inl)
  76. {
  77. RC4(&data(ctx)->ks, inl, in, out);
  78. return 1;
  79. }
  80. #endif