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/aes.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_aes128_available(void)
25 : 2192 : {
26 [ + - ]: 2192 : return (EVP_aes_128_cbc() ? true : false);
27 : 2192 : }
28 : :
29 : : static bool s2n_cbc_cipher_aes256_available(void)
30 : 3282 : {
31 [ + - ]: 3282 : return (EVP_aes_256_cbc() ? true : false);
32 : 3282 : }
33 : :
34 : : static int s2n_cbc_cipher_aes_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
35 : 16882 : {
36 [ # # ][ - + ]: 16882 : POSIX_ENSURE_GTE(out->size, in->size);
37 : :
38 [ - + ][ # # ]: 16882 : POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
39 : :
40 : : /* len is set by EVP_EncryptUpdate and checked post operation */
41 : 16882 : int len = 0;
42 [ # # ][ - + ]: 16882 : POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
43 [ # # ][ - + ]: 16882 : POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT);
44 : :
45 : 16882 : return 0;
46 : 16882 : }
47 : :
48 : : int s2n_cbc_cipher_aes_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
49 : 16878 : {
50 [ - + ][ # # ]: 16878 : POSIX_ENSURE_GTE(out->size, in->size);
51 : :
52 [ # # ][ - + ]: 16878 : POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
53 : :
54 : : /* len is set by EVP_DecryptUpdate. It is not checked here but padding is manually removed and therefore
55 : : * the decryption operation is validated. */
56 : 16878 : int len = 0;
57 [ # # ][ - + ]: 16878 : POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
58 : :
59 : 16878 : return 0;
60 : 16878 : }
61 : :
62 : : S2N_RESULT s2n_cbc_cipher_aes128_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
63 : 88 : {
64 [ - + ][ # # ]: 88 : RESULT_ENSURE_EQ(in->size, 128 / 8);
65 : :
66 : : /* Always returns 1 */
67 : 88 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
68 [ - + ][ # # ]: 88 : RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
69 : :
70 : 88 : return S2N_RESULT_OK;
71 : 88 : }
72 : :
73 : : static S2N_RESULT s2n_cbc_cipher_aes128_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
74 : 88 : {
75 [ - + ][ # # ]: 88 : RESULT_ENSURE_EQ(in->size, 128 / 8);
76 : :
77 : 88 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
78 [ - + ][ # # ]: 88 : RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
79 : :
80 : 88 : return S2N_RESULT_OK;
81 : 88 : }
82 : :
83 : : static S2N_RESULT s2n_cbc_cipher_aes256_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
84 : 164 : {
85 [ # # ][ - + ]: 164 : RESULT_ENSURE_EQ(in->size, 256 / 8);
86 : :
87 : 164 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
88 [ - + ][ # # ]: 164 : RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
89 : :
90 : 164 : return S2N_RESULT_OK;
91 : 164 : }
92 : :
93 : : S2N_RESULT s2n_cbc_cipher_aes256_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
94 : 164 : {
95 [ # # ][ - + ]: 164 : RESULT_ENSURE_EQ(in->size, 256 / 8);
96 : :
97 : 164 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
98 [ - + ][ # # ]: 164 : RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
99 : :
100 : 164 : return S2N_RESULT_OK;
101 : 164 : }
102 : :
103 : : static S2N_RESULT s2n_cbc_cipher_aes_init(struct s2n_session_key *key)
104 : 504 : {
105 [ - + ][ # # ]: 504 : RESULT_EVP_CTX_INIT(key->evp_cipher_ctx);
106 : :
107 : 504 : return S2N_RESULT_OK;
108 : 504 : }
109 : :
110 : : static S2N_RESULT s2n_cbc_cipher_aes_destroy_key(struct s2n_session_key *key)
111 : 4 : {
112 : 4 : EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
113 : :
114 : 4 : return S2N_RESULT_OK;
115 : 4 : }
116 : :
117 : : const struct s2n_cipher s2n_aes128 = {
118 : : .key_material_size = 16,
119 : : .type = S2N_CBC,
120 : : .io.cbc = {
121 : : .block_size = 16,
122 : : .record_iv_size = 16,
123 : : .decrypt = s2n_cbc_cipher_aes_decrypt,
124 : : .encrypt = s2n_cbc_cipher_aes_encrypt },
125 : : .is_available = s2n_cbc_cipher_aes128_available,
126 : : .init = s2n_cbc_cipher_aes_init,
127 : : .set_decryption_key = s2n_cbc_cipher_aes128_set_decryption_key,
128 : : .set_encryption_key = s2n_cbc_cipher_aes128_set_encryption_key,
129 : : .destroy_key = s2n_cbc_cipher_aes_destroy_key,
130 : : };
131 : :
132 : : const struct s2n_cipher s2n_aes256 = {
133 : : .key_material_size = 32,
134 : : .type = S2N_CBC,
135 : : .io.cbc = {
136 : : .block_size = 16,
137 : : .record_iv_size = 16,
138 : : .decrypt = s2n_cbc_cipher_aes_decrypt,
139 : : .encrypt = s2n_cbc_cipher_aes_encrypt },
140 : : .is_available = s2n_cbc_cipher_aes256_available,
141 : : .init = s2n_cbc_cipher_aes_init,
142 : : .set_decryption_key = s2n_cbc_cipher_aes256_set_decryption_key,
143 : : .set_encryption_key = s2n_cbc_cipher_aes256_set_encryption_key,
144 : : .destroy_key = s2n_cbc_cipher_aes_destroy_key,
145 : : };
|