s2n_stream_cipher_rc4.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #include <openssl/rc4.h>
  16. #include "crypto/s2n_cipher.h"
  17. #include "crypto/s2n_fips.h"
  18. #include "crypto/s2n_openssl.h"
  19. #include "utils/s2n_blob.h"
  20. #include "utils/s2n_safety.h"
  21. static const EVP_CIPHER *s2n_evp_rc4()
  22. {
  23. #ifdef S2N_LIBCRYPTO_SUPPORTS_EVP_RC4
  24. return EVP_rc4();
  25. #else
  26. return NULL;
  27. #endif
  28. }
  29. static uint8_t s2n_stream_cipher_rc4_available()
  30. {
  31. if (s2n_is_in_fips_mode()) {
  32. return 0;
  33. }
  34. /* RC4 MIGHT be available in Openssl-3.0, depending on whether or not the
  35. * "legacy" provider is loaded. However, for simplicity, assume that RC4
  36. * is unavailable.
  37. */
  38. if (S2N_OPENSSL_VERSION_AT_LEAST(3, 0, 0)) {
  39. return 0;
  40. }
  41. return (s2n_evp_rc4() ? 1 : 0);
  42. }
  43. static int s2n_stream_cipher_rc4_encrypt(struct s2n_session_key *key, struct s2n_blob *in, struct s2n_blob *out)
  44. {
  45. POSIX_ENSURE_GTE(out->size, in->size);
  46. /* len is set by EVP_EncryptUpdate and checked post operation */
  47. int len = 0;
  48. POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
  49. POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_DECRYPT);
  50. return 0;
  51. }
  52. static int s2n_stream_cipher_rc4_decrypt(struct s2n_session_key *key, struct s2n_blob *in, struct s2n_blob *out)
  53. {
  54. POSIX_ENSURE_GTE(out->size, in->size);
  55. /* len is set by EVP_DecryptUpdate and checked post operation */
  56. int len = 0;
  57. POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
  58. POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_DECRYPT);
  59. return 0;
  60. }
  61. static int s2n_stream_cipher_rc4_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  62. {
  63. POSIX_ENSURE_EQ(in->size, 16);
  64. POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, s2n_evp_rc4(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  65. return S2N_SUCCESS;
  66. }
  67. static int s2n_stream_cipher_rc4_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
  68. {
  69. POSIX_ENSURE_EQ(in->size, 16);
  70. POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, s2n_evp_rc4(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
  71. return S2N_SUCCESS;
  72. }
  73. static int s2n_stream_cipher_rc4_init(struct s2n_session_key *key)
  74. {
  75. s2n_evp_ctx_init(key->evp_cipher_ctx);
  76. return 0;
  77. }
  78. static int s2n_stream_cipher_rc4_destroy_key(struct s2n_session_key *key)
  79. {
  80. EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
  81. return 0;
  82. }
  83. const struct s2n_cipher s2n_rc4 = {
  84. .type = S2N_STREAM,
  85. .key_material_size = 16,
  86. .io.stream = {
  87. .decrypt = s2n_stream_cipher_rc4_decrypt,
  88. .encrypt = s2n_stream_cipher_rc4_encrypt },
  89. .is_available = s2n_stream_cipher_rc4_available,
  90. .init = s2n_stream_cipher_rc4_init,
  91. .set_decryption_key = s2n_stream_cipher_rc4_set_decryption_key,
  92. .set_encryption_key = s2n_stream_cipher_rc4_set_encryption_key,
  93. .destroy_key = s2n_stream_cipher_rc4_destroy_key,
  94. };