| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <openssl/rc4.h> |
| 17 |
|
| 18 |
#include "crypto/s2n_cipher.h" |
| 19 |
#include "crypto/s2n_fips.h" |
| 20 |
#include "crypto/s2n_openssl.h" |
| 21 |
#include "utils/s2n_blob.h" |
| 22 |
#include "utils/s2n_safety.h" |
| 23 |
|
| 24 |
static const EVP_CIPHER *s2n_evp_rc4() |
| 25 |
{ |
| 26 |
#ifdef S2N_LIBCRYPTO_SUPPORTS_EVP_RC4 |
| 27 |
return EVP_rc4(); |
| 28 |
#else |
| 29 |
return NULL; |
| 30 |
#endif |
| 31 |
} |
| 32 |
|
| 33 |
static bool s2n_stream_cipher_rc4_available(void) |
| 34 |
{ |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
if (S2N_OPENSSL_VERSION_AT_LEAST(3, 0, 0)) { |
| 40 |
return false; |
| 41 |
} |
| 42 |
return (s2n_evp_rc4() ? true : false); |
| 43 |
} |
| 44 |
|
| 45 |
static int s2n_stream_cipher_rc4_encrypt(struct s2n_session_key *key, struct s2n_blob *in, struct s2n_blob *out) |
| 46 |
{ |
| 47 |
POSIX_ENSURE_GTE(out->size, in->size); |
| 48 |
|
| 49 |
|
| 50 |
int len = 0; |
| 51 |
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT); |
| 52 |
|
| 53 |
POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_DECRYPT); |
| 54 |
|
| 55 |
return 0; |
| 56 |
} |
| 57 |
|
| 58 |
static int s2n_stream_cipher_rc4_decrypt(struct s2n_session_key *key, struct s2n_blob *in, struct s2n_blob *out) |
| 59 |
{ |
| 60 |
POSIX_ENSURE_GTE(out->size, in->size); |
| 61 |
|
| 62 |
|
| 63 |
int len = 0; |
| 64 |
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT); |
| 65 |
|
| 66 |
POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_DECRYPT); |
| 67 |
|
| 68 |
return 0; |
| 69 |
} |
| 70 |
|
| 71 |
static S2N_RESULT s2n_stream_cipher_rc4_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 72 |
{ |
| 73 |
RESULT_ENSURE_EQ(in->size, 16); |
| 74 |
RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, s2n_evp_rc4(), NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 75 |
|
| 76 |
return S2N_RESULT_OK; |
| 77 |
} |
| 78 |
|
| 79 |
static S2N_RESULT s2n_stream_cipher_rc4_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 80 |
{ |
| 81 |
RESULT_ENSURE_EQ(in->size, 16); |
| 82 |
RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, s2n_evp_rc4(), NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 83 |
|
| 84 |
return S2N_RESULT_OK; |
| 85 |
} |
| 86 |
|
| 87 |
static S2N_RESULT s2n_stream_cipher_rc4_init(struct s2n_session_key *key) |
| 88 |
{ |
| 89 |
RESULT_EVP_CTX_INIT(key->evp_cipher_ctx); |
| 90 |
|
| 91 |
return S2N_RESULT_OK; |
| 92 |
} |
| 93 |
|
| 94 |
static S2N_RESULT s2n_stream_cipher_rc4_destroy_key(struct s2n_session_key *key) |
| 95 |
{ |
| 96 |
EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx); |
| 97 |
|
| 98 |
return S2N_RESULT_OK; |
| 99 |
} |
| 100 |
|
| 101 |
const struct s2n_cipher s2n_rc4 = { |
| 102 |
.type = S2N_STREAM, |
| 103 |
.key_material_size = 16, |
| 104 |
.io.stream = { |
| 105 |
.decrypt = s2n_stream_cipher_rc4_decrypt, |
| 106 |
.encrypt = s2n_stream_cipher_rc4_encrypt }, |
| 107 |
.is_available = s2n_stream_cipher_rc4_available, |
| 108 |
.init = s2n_stream_cipher_rc4_init, |
| 109 |
.set_decryption_key = s2n_stream_cipher_rc4_set_decryption_key, |
| 110 |
.set_encryption_key = s2n_stream_cipher_rc4_set_encryption_key, |
| 111 |
.destroy_key = s2n_stream_cipher_rc4_destroy_key, |
| 112 |
}; |