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 : 1404 : {
26 [ + - ]: 1404 : return (EVP_aes_128_cbc() ? true : false);
27 : 1404 : }
28 : :
29 : : static bool s2n_cbc_cipher_aes256_available(void)
30 : 2100 : {
31 [ + - ]: 2100 : return (EVP_aes_256_cbc() ? true : false);
32 : 2100 : }
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 : 16923 : {
36 [ # # ][ - + ]: 16923 : POSIX_ENSURE_GTE(out->size, in->size);
37 : :
38 [ - + ][ # # ]: 16923 : 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 : 16923 : int len = 0;
42 [ - + ][ # # ]: 16923 : POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
43 [ - + ][ # # ]: 16923 : POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT);
44 : :
45 : 16923 : return 0;
46 : 16923 : }
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 : 16919 : {
50 [ # # ][ - + ]: 16919 : POSIX_ENSURE_GTE(out->size, in->size);
51 : :
52 [ - + ][ # # ]: 16919 : POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
53 : :
54 : 16919 : int len = 0;
55 [ - + ][ # # ]: 16919 : POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
56 [ - + ][ # # ]: 16919 : POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_DECRYPT);
57 : :
58 : 16919 : return 0;
59 : 16919 : }
60 : :
61 : : S2N_RESULT s2n_cbc_cipher_aes128_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
62 : 91 : {
63 [ # # ][ - + ]: 91 : RESULT_ENSURE_EQ(in->size, 128 / 8);
64 : :
65 [ - + ][ # # ]: 91 : RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
66 : : /* Padding must be disabled after key init to take effect. */
67 : 91 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
68 : :
69 : 91 : return S2N_RESULT_OK;
70 : 91 : }
71 : :
72 : : static S2N_RESULT s2n_cbc_cipher_aes128_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
73 : 91 : {
74 [ - + ][ # # ]: 91 : RESULT_ENSURE_EQ(in->size, 128 / 8);
75 : :
76 [ # # ][ - + ]: 91 : RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
77 : : /* Padding must be disabled after key init to take effect. */
78 : 91 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
79 : :
80 : 91 : return S2N_RESULT_OK;
81 : 91 : }
82 : :
83 : : static S2N_RESULT s2n_cbc_cipher_aes256_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
84 : 166 : {
85 [ # # ][ - + ]: 166 : RESULT_ENSURE_EQ(in->size, 256 / 8);
86 : :
87 [ # # ][ - + ]: 166 : RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
88 : : /* Padding must be disabled after key init to take effect. */
89 : 166 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
90 : :
91 : 166 : return S2N_RESULT_OK;
92 : 166 : }
93 : :
94 : : S2N_RESULT s2n_cbc_cipher_aes256_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
95 : 166 : {
96 [ - + ][ # # ]: 166 : RESULT_ENSURE_EQ(in->size, 256 / 8);
97 : :
98 [ # # ][ - + ]: 166 : RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
99 : : /* Padding must be disabled after key init to take effect. */
100 : 166 : EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
101 : :
102 : 166 : return S2N_RESULT_OK;
103 : 166 : }
104 : :
105 : : static S2N_RESULT s2n_cbc_cipher_aes_init(struct s2n_session_key *key)
106 : 514 : {
107 [ - + ][ # # ]: 514 : RESULT_EVP_CTX_INIT(key->evp_cipher_ctx);
108 : :
109 : 514 : return S2N_RESULT_OK;
110 : 514 : }
111 : :
112 : : static S2N_RESULT s2n_cbc_cipher_aes_destroy_key(struct s2n_session_key *key)
113 : 6 : {
114 : 6 : EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
115 : :
116 : 6 : return S2N_RESULT_OK;
117 : 6 : }
118 : :
119 : : const struct s2n_cipher s2n_aes128 = {
120 : : .key_material_size = 16,
121 : : .type = S2N_CBC,
122 : : .io.cbc = {
123 : : .block_size = 16,
124 : : .record_iv_size = 16,
125 : : .decrypt = s2n_cbc_cipher_aes_decrypt,
126 : : .encrypt = s2n_cbc_cipher_aes_encrypt },
127 : : .is_available = s2n_cbc_cipher_aes128_available,
128 : : .init = s2n_cbc_cipher_aes_init,
129 : : .set_decryption_key = s2n_cbc_cipher_aes128_set_decryption_key,
130 : : .set_encryption_key = s2n_cbc_cipher_aes128_set_encryption_key,
131 : : .destroy_key = s2n_cbc_cipher_aes_destroy_key,
132 : : };
133 : :
134 : : const struct s2n_cipher s2n_aes256 = {
135 : : .key_material_size = 32,
136 : : .type = S2N_CBC,
137 : : .io.cbc = {
138 : : .block_size = 16,
139 : : .record_iv_size = 16,
140 : : .decrypt = s2n_cbc_cipher_aes_decrypt,
141 : : .encrypt = s2n_cbc_cipher_aes_encrypt },
142 : : .is_available = s2n_cbc_cipher_aes256_available,
143 : : .init = s2n_cbc_cipher_aes_init,
144 : : .set_decryption_key = s2n_cbc_cipher_aes256_set_decryption_key,
145 : : .set_encryption_key = s2n_cbc_cipher_aes256_set_encryption_key,
146 : : .destroy_key = s2n_cbc_cipher_aes_destroy_key,
147 : : };
|