LCOV - code coverage report
Current view: top level - crypto - s2n_cbc_cipher_3des.c (source / functions) Hit Total Coverage
Test: unit_test_coverage.info Lines: 39 39 100.0 %
Date: 2026-08-22 07:27:53 Functions: 7 7 100.0 %
Branches: 14 54 25.9 %

           Branch data     Line data    Source code
       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                 :            : 
      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                 :       2094 : {
      26         [ +  - ]:       2094 :     return (EVP_des_ede3_cbc() ? true : false);
      27                 :       2094 : }
      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                 :       8716 : {
      31 [ -  + ][ #  # ]:       8716 :     POSIX_ENSURE_GTE(out->size, in->size);
      32                 :            : 
      33 [ #  # ][ -  + ]:       8716 :     POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
      34                 :            : 
      35                 :            :     /* len is set by EVP_EncryptUpdate and checked post operation */
      36                 :       8716 :     int len = 0;
      37 [ #  # ][ -  + ]:       8716 :     POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
      38 [ -  + ][ #  # ]:       8716 :     POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT);
      39                 :            : 
      40                 :       8716 :     return 0;
      41                 :       8716 : }
      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                 :       8715 : {
      45 [ -  + ][ #  # ]:       8715 :     POSIX_ENSURE_GTE(out->size, in->size);
      46                 :            : 
      47 [ -  + ][ #  # ]:       8715 :     POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
      48                 :            : 
      49                 :       8715 :     int len = 0;
      50 [ -  + ][ #  # ]:       8715 :     POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
      51 [ -  + ][ #  # ]:       8715 :     POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_DECRYPT);
      52                 :            : 
      53                 :       8715 :     return 0;
      54                 :       8715 : }
      55                 :            : 
      56                 :            : static S2N_RESULT s2n_cbc_cipher_3des_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
      57                 :        198 : {
      58 [ -  + ][ #  # ]:        198 :     RESULT_ENSURE_EQ(in->size, 192 / 8);
      59                 :            : 
      60 [ #  # ][ -  + ]:        198 :     RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_des_ede3_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
      61                 :            :     /* Padding must be disabled after key init to take effect. */
      62                 :        198 :     EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
      63                 :            : 
      64                 :        198 :     return S2N_RESULT_OK;
      65                 :        198 : }
      66                 :            : 
      67                 :            : static S2N_RESULT s2n_cbc_cipher_3des_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
      68                 :        198 : {
      69 [ -  + ][ #  # ]:        198 :     RESULT_ENSURE_EQ(in->size, 192 / 8);
      70                 :            : 
      71 [ -  + ][ #  # ]:        198 :     RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_des_ede3_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
      72                 :            :     /* Padding must be disabled after key init to take effect. */
      73                 :        198 :     EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
      74                 :            : 
      75                 :        198 :     return S2N_RESULT_OK;
      76                 :        198 : }
      77                 :            : 
      78                 :            : static S2N_RESULT s2n_cbc_cipher_3des_init(struct s2n_session_key *key)
      79                 :        396 : {
      80 [ #  # ][ -  + ]:        396 :     RESULT_EVP_CTX_INIT(key->evp_cipher_ctx);
      81                 :            : 
      82                 :        396 :     return S2N_RESULT_OK;
      83                 :        396 : }
      84                 :            : 
      85                 :            : static S2N_RESULT s2n_cbc_cipher_3des_destroy_key(struct s2n_session_key *key)
      86                 :         22 : {
      87                 :         22 :     EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
      88                 :            : 
      89                 :         22 :     return S2N_RESULT_OK;
      90                 :         22 : }
      91                 :            : 
      92                 :            : const struct s2n_cipher s2n_3des = {
      93                 :            :     .key_material_size = 24,
      94                 :            :     .type = S2N_CBC,
      95                 :            :     .io.cbc = {
      96                 :            :             .block_size = 8,
      97                 :            :             .record_iv_size = 8,
      98                 :            :             .decrypt = s2n_cbc_cipher_3des_decrypt,
      99                 :            :             .encrypt = s2n_cbc_cipher_3des_encrypt },
     100                 :            :     .is_available = s2n_cbc_cipher_3des_available,
     101                 :            :     .init = s2n_cbc_cipher_3des_init,
     102                 :            :     .set_decryption_key = s2n_cbc_cipher_3des_set_decryption_key,
     103                 :            :     .set_encryption_key = s2n_cbc_cipher_3des_set_encryption_key,
     104                 :            :     .destroy_key = s2n_cbc_cipher_3des_destroy_key,
     105                 :            : };

Generated by: LCOV version 1.14