Debian Codegraph @

crt/s2n/crypto/s2n_cbc_cipher_aes.c

raw symbols SCIP
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 {
26 return (EVP_aes_128_cbc() ? true : false);
27 }
28
29 static bool s2n_cbc_cipher_aes256_available(void)
30 {
31 return (EVP_aes_256_cbc() ? true : false);
32 }
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 {
36 POSIX_ENSURE_GTE(out->size, in->size);
37
38 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 int len = 0;
42 POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
43 POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT);
44
45 return 0;
46 }
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 {
50 POSIX_ENSURE_GTE(out->size, in->size);
51
52 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 int len = 0;
57 POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
58
59 return 0;
60 }
61
62 S2N_RESULT s2n_cbc_cipher_aes128_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
63 {
64 RESULT_ENSURE_EQ(in->size, 128 / 8);
65
66 /* Always returns 1 */
67 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
68 RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
69
70 return S2N_RESULT_OK;
71 }
72
73 static S2N_RESULT s2n_cbc_cipher_aes128_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
74 {
75 RESULT_ENSURE_EQ(in->size, 128 / 8);
76
77 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
78 RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
79
80 return S2N_RESULT_OK;
81 }
82
83 static S2N_RESULT s2n_cbc_cipher_aes256_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
84 {
85 RESULT_ENSURE_EQ(in->size, 256 / 8);
86
87 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
88 RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
89
90 return S2N_RESULT_OK;
91 }
92
93 S2N_RESULT s2n_cbc_cipher_aes256_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
94 {
95 RESULT_ENSURE_EQ(in->size, 256 / 8);
96
97 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
98 RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_cbc(), NULL, in->data, NULL), S2N_ERR_KEY_INIT);
99
100 return S2N_RESULT_OK;
101 }
102
103 static S2N_RESULT s2n_cbc_cipher_aes_init(struct s2n_session_key *key)
104 {
105 RESULT_EVP_CTX_INIT(key->evp_cipher_ctx);
106
107 return S2N_RESULT_OK;
108 }
109
110 static S2N_RESULT s2n_cbc_cipher_aes_destroy_key(struct s2n_session_key *key)
111 {
112 EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
113
114 return S2N_RESULT_OK;
115 }
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 };