Skip to content

Commit

Permalink
openssl3 integration: workaround for new EVP_Cipher return code (#3466)
Browse files Browse the repository at this point in the history
  • Loading branch information
toidiu authored Aug 31, 2022
1 parent 1d9538b commit 1209208
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
12 changes: 8 additions & 4 deletions crypto/s2n_aead_cipher_aes_gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ static int s2n_aead_cipher_aes_gcm_encrypt(struct s2n_session_key *key, struct s

/* Adjust input length to account for the Tag length */
size_t in_len = in->size - S2N_TLS_GCM_TAG_LEN;
/* out_len is set by EVP_AEAD_CTX_seal and checked post operation */
size_t out_len = 0;

POSIX_GUARD_OSSL(EVP_AEAD_CTX_seal(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in_len, aad->data, aad->size), S2N_ERR_ENCRYPT);
Expand All @@ -83,6 +84,7 @@ static int s2n_aead_cipher_aes_gcm_decrypt(struct s2n_session_key *key, struct s
POSIX_ENSURE_GTE(out->size, in->size - S2N_TLS_GCM_TAG_LEN);
POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN);

/* out_len is set by EVP_AEAD_CTX_open and checked post operation */
size_t out_len = 0;

POSIX_GUARD_OSSL(EVP_AEAD_CTX_open(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in->size, aad->data, aad->size), S2N_ERR_DECRYPT);
Expand Down Expand Up @@ -222,7 +224,8 @@ static int s2n_aead_cipher_aes_gcm_encrypt(struct s2n_session_key *key, struct s
int in_len = in->size - S2N_TLS_GCM_TAG_LEN;
uint8_t *tag_data = out->data + out->size - S2N_TLS_GCM_TAG_LEN;

int out_len;
/* out_len is set by EVP_EncryptUpdate and checked post operation */
int out_len = 0;
/* Specify the AAD */
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_ENCRYPT);

Expand Down Expand Up @@ -260,7 +263,10 @@ static int s2n_aead_cipher_aes_gcm_decrypt(struct s2n_session_key *key, struct s
/* Set the TAG */
POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_TAG, S2N_TLS_GCM_TAG_LEN, tag_data), S2N_ERR_DECRYPT);

int out_len;
/* out_len is set by EVP_DecryptUpdate. While we verify the content of out_len in
* s2n_aead_chacha20_poly1305_encrypt, we refrain from this here. This is to avoid
* doing any branching before the ciphertext is verified. */
int out_len = 0;
/* Specify the AAD */
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_DECRYPT);

Expand All @@ -273,8 +279,6 @@ static int s2n_aead_cipher_aes_gcm_decrypt(struct s2n_session_key *key, struct s

S2N_ERROR_IF(evp_decrypt_rc != 1, S2N_ERR_DECRYPT);

/* While we verify the content of out_len in s2n_aead_cipher_aes_gcm_encrypt, we refrain from this here. This is to avoid doing any branching before the ciphertext is verified. */

return S2N_SUCCESS;
}

Expand Down
12 changes: 8 additions & 4 deletions crypto/s2n_aead_cipher_chacha20_poly1305.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ static int s2n_aead_chacha20_poly1305_encrypt(struct s2n_session_key *key, struc
int in_len = in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;
uint8_t *tag_data = out->data + out->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;

int out_len;
/* out_len is set by EVP_EncryptUpdate and checked post operation */
int out_len = 0;
/* Specify the AAD */
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_ENCRYPT);

Expand Down Expand Up @@ -99,7 +100,10 @@ static int s2n_aead_chacha20_poly1305_decrypt(struct s2n_session_key *key, struc
/* Set the TAG */
POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_TAG, S2N_TLS_CHACHA20_POLY1305_TAG_LEN, tag_data), S2N_ERR_DECRYPT);

int out_len;
/* out_len is set by EVP_DecryptUpdate. While we verify the content of out_len in
* s2n_aead_chacha20_poly1305_encrypt, we refrain from this here. This is to avoid
* doing any branching before the ciphertext is verified. */
int out_len = 0;
/* Specify the AAD */
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_DECRYPT);

Expand All @@ -112,8 +116,6 @@ static int s2n_aead_chacha20_poly1305_decrypt(struct s2n_session_key *key, struc

S2N_ERROR_IF(evp_decrypt_rc != 1, S2N_ERR_DECRYPT);

/* While we verify the content of out_len in s2n_aead_chacha20_poly1305_encrypt, we refrain from this here. This is to avoid doing any branching before the ciphertext is verified. */

return 0;
}

Expand Down Expand Up @@ -168,6 +170,7 @@ static int s2n_aead_chacha20_poly1305_encrypt(struct s2n_session_key *key, struc

/* Adjust input length to account for the Tag length */
size_t in_len = in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN;
/* out_len is set by EVP_AEAD_CTX_seal and checked post operation */
size_t out_len = 0;

POSIX_GUARD_OSSL(EVP_AEAD_CTX_seal(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in_len, aad->data, aad->size), S2N_ERR_ENCRYPT);
Expand All @@ -183,6 +186,7 @@ static int s2n_aead_chacha20_poly1305_decrypt(struct s2n_session_key *key, struc
POSIX_ENSURE_GTE(out->size, in->size - S2N_TLS_CHACHA20_POLY1305_TAG_LEN);
POSIX_ENSURE_EQ(iv->size, S2N_TLS_CHACHA20_POLY1305_IV_LEN);

/* out_len is set by EVP_AEAD_CTX_open and checked post operation */
size_t out_len = 0;

POSIX_GUARD_OSSL(EVP_AEAD_CTX_open(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in->size, aad->data, aad->size), S2N_ERR_DECRYPT);
Expand Down
7 changes: 5 additions & 2 deletions crypto/s2n_cbc_cipher_3des.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ static int s2n_cbc_cipher_3des_encrypt(struct s2n_session_key *key, struct s2n_b

POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);

int len = out->size;
/* len is set by EVP_EncryptUpdate and checked post operation */
int len = 0;
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
S2N_ERROR_IF(len != in->size, S2N_ERR_ENCRYPT);

Expand All @@ -47,7 +48,9 @@ static int s2n_cbc_cipher_3des_decrypt(struct s2n_session_key *key, struct s2n_b

POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);

int len = out->size;
/* len is set by EVP_DecryptUpdate. It is not checked here but padding is manually removed and therefore
* the decryption operation is validated. */
int len = 0;
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);

return 0;
Expand Down
8 changes: 6 additions & 2 deletions crypto/s2n_cbc_cipher_aes.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ static int s2n_cbc_cipher_aes_encrypt(struct s2n_session_key *key, struct s2n_bl

POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);

int len = out->size;
/* len is set by EVP_EncryptUpdate and checked post operation */
int len = 0;
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
S2N_ERROR_IF(len != in->size, S2N_ERR_ENCRYPT);

Expand All @@ -51,7 +52,10 @@ int s2n_cbc_cipher_aes_decrypt(struct s2n_session_key *key, struct s2n_blob *iv,
POSIX_ENSURE_GTE(out->size, in->size);

POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
int len = out->size;

/* len is set by EVP_DecryptUpdate. It is not checked here but padding is manually removed and therefore
* the decryption operation is validated. */
int len = 0;
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);

return 0;
Expand Down
14 changes: 11 additions & 3 deletions crypto/s2n_composite_cipher_aes_sha.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,25 @@ static int s2n_composite_cipher_aes_sha_encrypt(struct s2n_session_key *key, str
POSIX_ENSURE_EQ(out->size, in->size);

POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
POSIX_GUARD_OSSL(EVP_Cipher(key->evp_cipher_ctx, out->data, in->data, in->size), S2N_ERR_ENCRYPT);

/* len is set by EVP_EncryptUpdate and checked post operation */
int len = 0;
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);

S2N_ERROR_IF(len != in->size, S2N_ERR_ENCRYPT);

return 0;
}

static int s2n_composite_cipher_aes_sha_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
{
POSIX_ENSURE_EQ(out->size, in->size);

POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
POSIX_GUARD_OSSL(EVP_Cipher(key->evp_cipher_ctx, out->data, in->data, in->size), S2N_ERR_DECRYPT);

/* len is set by EVP_DecryptUpdate. It is not checked here but padding is manually removed and therefore
* the decryption operation is validated. */
int len = 0;
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);

return 0;
}
Expand Down
1 change: 1 addition & 0 deletions crypto/s2n_drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static S2N_RESULT s2n_drbg_block_encrypt(EVP_CIPHER_CTX *ctx, uint8_t in[S2N_DRB
{
RESULT_ENSURE_REF(ctx);

/* len is set by EVP_EncryptUpdate and checked post operation */
int len = S2N_DRBG_BLOCK_SIZE;
RESULT_GUARD_OSSL(EVP_EncryptUpdate(ctx, out, &len, in, S2N_DRBG_BLOCK_SIZE), S2N_ERR_DRBG);
RESULT_ENSURE_EQ(len, S2N_DRBG_BLOCK_SIZE);
Expand Down
10 changes: 6 additions & 4 deletions crypto/s2n_stream_cipher_rc4.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ static int s2n_stream_cipher_rc4_encrypt(struct s2n_session_key *key, struct s2n
{
POSIX_ENSURE_GTE(out->size, in->size);

int len = out->size;
/* len is set by EVP_EncryptUpdate and checked post operation */
int len = 0;
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);

S2N_ERROR_IF(len != in->size, S2N_ERR_ENCRYPT);
Expand All @@ -52,10 +53,11 @@ static int s2n_stream_cipher_rc4_decrypt(struct s2n_session_key *key, struct s2n
{
POSIX_ENSURE_GTE(out->size, in->size);

int len = out->size;
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
/* len is set by EVP_DecryptUpdate and checked post operation */
int len = 0;
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);

S2N_ERROR_IF(len != in->size, S2N_ERR_ENCRYPT);
S2N_ERROR_IF(len != in->size, S2N_ERR_DECRYPT);

return 0;
}
Expand Down

0 comments on commit 1209208

Please sign in to comment.