| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <openssl/evp.h> |
| 17 |
|
| 18 |
#include "crypto/s2n_cipher.h" |
| 19 |
#include "crypto/s2n_openssl.h" |
| 20 |
#include "error/s2n_errno.h" |
| 21 |
#include "utils/s2n_blob.h" |
| 22 |
#include "utils/s2n_safety.h" |
| 23 |
|
| 24 |
static bool s2n_cbc_cipher_3des_available(void) |
| 25 |
{ |
| 26 |
return (EVP_des_ede3_cbc() ? true : false); |
| 27 |
} |
| 28 |
|
| 29 |
static int s2n_cbc_cipher_3des_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out) |
| 30 |
{ |
| 31 |
POSIX_ENSURE_GTE(out->size, in->size); |
| 32 |
|
| 33 |
POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT); |
| 34 |
|
| 35 |
|
| 36 |
int len = 0; |
| 37 |
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT); |
| 38 |
POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT); |
| 39 |
|
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
static int s2n_cbc_cipher_3des_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out) |
| 44 |
{ |
| 45 |
POSIX_ENSURE_GTE(out->size, in->size); |
| 46 |
|
| 47 |
POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT); |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
int len = 0; |
| 52 |
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT); |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
|
| 57 |
static S2N_RESULT s2n_cbc_cipher_3des_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 58 |
{ |
| 59 |
RESULT_ENSURE_EQ(in->size, 192 / 8); |
| 60 |
|
| 61 |
EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0); |
| 62 |
RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_des_ede3_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 63 |
|
| 64 |
return S2N_RESULT_OK; |
| 65 |
} |
| 66 |
|
| 67 |
static S2N_RESULT s2n_cbc_cipher_3des_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 68 |
{ |
| 69 |
RESULT_ENSURE_EQ(in->size, 192 / 8); |
| 70 |
|
| 71 |
EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0); |
| 72 |
RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_des_ede3_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 73 |
|
| 74 |
return S2N_RESULT_OK; |
| 75 |
} |
| 76 |
|
| 77 |
static S2N_RESULT s2n_cbc_cipher_3des_init(struct s2n_session_key *key) |
| 78 |
{ |
| 79 |
RESULT_EVP_CTX_INIT(key->evp_cipher_ctx); |
| 80 |
|
| 81 |
return S2N_RESULT_OK; |
| 82 |
} |
| 83 |
|
| 84 |
static S2N_RESULT s2n_cbc_cipher_3des_destroy_key(struct s2n_session_key *key) |
| 85 |
{ |
| 86 |
EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx); |
| 87 |
|
| 88 |
return S2N_RESULT_OK; |
| 89 |
} |
| 90 |
|
| 91 |
const struct s2n_cipher s2n_3des = { |
| 92 |
.key_material_size = 24, |
| 93 |
.type = S2N_CBC, |
| 94 |
.io.cbc = { |
| 95 |
.block_size = 8, |
| 96 |
.record_iv_size = 8, |
| 97 |
.decrypt = s2n_cbc_cipher_3des_decrypt, |
| 98 |
.encrypt = s2n_cbc_cipher_3des_encrypt }, |
| 99 |
.is_available = s2n_cbc_cipher_3des_available, |
| 100 |
.init = s2n_cbc_cipher_3des_init, |
| 101 |
.set_decryption_key = s2n_cbc_cipher_3des_set_decryption_key, |
| 102 |
.set_encryption_key = s2n_cbc_cipher_3des_set_encryption_key, |
| 103 |
.destroy_key = s2n_cbc_cipher_3des_destroy_key, |
| 104 |
}; |