e_rc4.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. 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. int keylen;
  67. if ((keylen = EVP_CIPHER_CTX_key_length(ctx)) <= 0)
  68. return 0;
  69. RC4_set_key(&data(ctx)->ks, keylen, key);
  70. return 1;
  71. }
  72. static int rc4_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  73. const unsigned char *in, size_t inl)
  74. {
  75. RC4(&data(ctx)->ks, inl, in, out);
  76. return 1;
  77. }
  78. #endif