e_rc4.c 1.9 KB

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