Debian Codegraph @

crt/s2n/crypto/s2n_composite_cipher_aes_sha.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 #include <openssl/evp.h>
18 #include <openssl/sha.h>
19
20 #include "crypto/s2n_cipher.h"
21 #include "crypto/s2n_fips.h"
22 #include "crypto/s2n_openssl.h"
23 #include "tls/s2n_crypto.h"
24 #include "utils/s2n_blob.h"
25 #include "utils/s2n_safety.h"
26
27 /* LibreSSL and BoringSSL support the cipher, but the interface is different from Openssl's. We
28 * should define a separate s2n_cipher struct for LibreSSL and BoringSSL.
29 */
30 #if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL)
31 /* Symbols for AES-SHA1-CBC composite ciphers were added in Openssl 1.0.1
32 * These composite ciphers exhibit erratic behavior in LibreSSL releases.
33 */
34 #if S2N_OPENSSL_VERSION_AT_LEAST(1, 0, 1)
35 #define S2N_AES_SHA1_COMPOSITE_AVAILABLE
36 #endif
37 #if defined(AWSLC_API_VERSION) && (AWSLC_API_VERSION <= 17)
38 #undef S2N_AES_SHA1_COMPOSITE_AVAILABLE
39 #endif
40 /* Symbols for AES-SHA256-CBC composite ciphers were added in Openssl 1.0.2
41 * See https://www.openssl.org/news/cl102.txt
42 * These composite ciphers exhibit erratic behavior in LibreSSL releases.
43 */
44 #if S2N_OPENSSL_VERSION_AT_LEAST(1, 0, 2)
45 #define S2N_AES_SHA256_COMPOSITE_AVAILABLE
46 #endif
47 #if defined(AWSLC_API_VERSION) && (AWSLC_API_VERSION <= 17)
48 #undef S2N_AES_SHA256_COMPOSITE_AVAILABLE
49 #endif
50 #endif
51
52 /* Silly accessors, but we avoid using version macro guards in multiple places */
53 static const EVP_CIPHER *s2n_evp_aes_128_cbc_hmac_sha1(void)
54 {
55 #if defined(S2N_AES_SHA1_COMPOSITE_AVAILABLE)
56 return EVP_aes_128_cbc_hmac_sha1();
57 #else
58 return NULL;
59 #endif
60 }
61
62 static const EVP_CIPHER *s2n_evp_aes_256_cbc_hmac_sha1(void)
63 {
64 #if defined(S2N_AES_SHA1_COMPOSITE_AVAILABLE)
65 return EVP_aes_256_cbc_hmac_sha1();
66 #else
67 return NULL;
68 #endif
69 }
70
71 static const EVP_CIPHER *s2n_evp_aes_128_cbc_hmac_sha256(void)
72 {
73 #if defined(S2N_AES_SHA256_COMPOSITE_AVAILABLE)
74 return EVP_aes_128_cbc_hmac_sha256();
75 #else
76 return NULL;
77 #endif
78 }
79
80 static const EVP_CIPHER *s2n_evp_aes_256_cbc_hmac_sha256(void)
81 {
82 #if defined(S2N_AES_SHA256_COMPOSITE_AVAILABLE)
83 return EVP_aes_256_cbc_hmac_sha256();
84 #else
85 return NULL;
86 #endif
87 }
88
89 static bool s2n_composite_cipher_aes128_sha_available(void)
90 {
91 /* EVP_aes_128_cbc_hmac_sha1() returns NULL if the implementations aren't available.
92 * See https://github.com/openssl/openssl/blob/master/crypto/evp/e_aes_cbc_hmac_sha1.c#L952
93 *
94 * Composite ciphers cannot be used when FIPS mode is set. Ciphers require the
95 * EVP_CIPH_FLAG_FIPS OpenSSL flag to be set for use when in FIPS mode, and composite
96 * ciphers cause OpenSSL errors due to the lack of the flag.
97 */
98 return (!s2n_is_in_fips_mode() && s2n_evp_aes_128_cbc_hmac_sha1() ? true : false);
99 }
100
101 static bool s2n_composite_cipher_aes256_sha_available(void)
102 {
103 /* Composite ciphers cannot be used when FIPS mode is set. Ciphers require the
104 * EVP_CIPH_FLAG_FIPS OpenSSL flag to be set for use when in FIPS mode, and composite
105 * ciphers cause OpenSSL errors due to the lack of the flag.
106 */
107 return (!s2n_is_in_fips_mode() && s2n_evp_aes_256_cbc_hmac_sha1() ? true : false);
108 }
109
110 static bool s2n_composite_cipher_aes128_sha256_available(void)
111 {
112 /* Composite ciphers cannot be used when FIPS mode is set. Ciphers require the
113 * EVP_CIPH_FLAG_FIPS OpenSSL flag to be set for use when in FIPS mode, and composite
114 * ciphers cause OpenSSL errors due to the lack of the flag.
115 */
116 return (!s2n_is_in_fips_mode() && s2n_evp_aes_128_cbc_hmac_sha256() ? true : false);
117 }
118
119 static bool s2n_composite_cipher_aes256_sha256_available(void)
120 {
121 /* Composite ciphers cannot be used when FIPS mode is set. Ciphers require the
122 * EVP_CIPH_FLAG_FIPS OpenSSL flag to be set for use when in FIPS mode, and composite
123 * ciphers cause OpenSSL errors due to the lack of the flag.
124 */
125 return (!s2n_is_in_fips_mode() && s2n_evp_aes_256_cbc_hmac_sha256() ? true : false);
126 }
127
128 static int s2n_composite_cipher_aes_sha_initial_hmac(struct s2n_session_key *key, uint8_t *sequence_number, uint8_t content_type,
129 uint16_t protocol_version, uint16_t payload_and_eiv_len, int *extra)
130 {
131 /* BoringSSL and AWS-LC(AWSLC_API_VERSION <= 17) do not support these composite ciphers with the existing EVP API, and they took out the
132 * constants used below. This method should never be called with BoringSSL or AWS-LC(AWSLC_API_VERSION <= 17) because the isAvaliable checked
133 * will fail. Instead of defining a possibly dangerous default or hard coding this to 0x16 error out with BoringSSL and AWS-LC(AWSLC_API_VERSION <= 17).
134 */
135 #if defined(OPENSSL_IS_BORINGSSL) || (defined(AWSLC_API_VERSION) && (AWSLC_API_VERSION <= 17))
136 POSIX_BAIL(S2N_ERR_UNIMPLEMENTED);
137 #else
138 uint8_t ctrl_buf[S2N_TLS12_AAD_LEN];
139 struct s2n_blob ctrl_blob = { 0 };
140 POSIX_GUARD(s2n_blob_init(&ctrl_blob, ctrl_buf, S2N_TLS12_AAD_LEN));
141 struct s2n_stuffer ctrl_stuffer = { 0 };
142 POSIX_GUARD(s2n_stuffer_init(&ctrl_stuffer, &ctrl_blob));
143
144 POSIX_GUARD(s2n_stuffer_write_bytes(&ctrl_stuffer, sequence_number, S2N_TLS_SEQUENCE_NUM_LEN));
145 POSIX_GUARD(s2n_stuffer_write_uint8(&ctrl_stuffer, content_type));
146 POSIX_GUARD(s2n_stuffer_write_uint8(&ctrl_stuffer, protocol_version / 10));
147 POSIX_GUARD(s2n_stuffer_write_uint8(&ctrl_stuffer, protocol_version % 10));
148 POSIX_GUARD(s2n_stuffer_write_uint16(&ctrl_stuffer, payload_and_eiv_len));
149
150 /* This will unnecessarily mangle the input buffer, which is fine since it's temporary
151 * Return value will be length of digest, padding, and padding length byte.
152 * See https://github.com/openssl/openssl/blob/master/crypto/evp/e_aes_cbc_hmac_sha1.c#L814
153 * and https://github.com/openssl/openssl/blob/4f0c475719defd7c051964ef9964cc6e5b3a63bf/ssl/record/ssl3_record.c#L743
154 */
155 int ctrl_ret = EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_AEAD_TLS1_AAD, S2N_TLS12_AAD_LEN, ctrl_buf);
156
157 S2N_ERROR_IF(ctrl_ret <= 0, S2N_ERR_INITIAL_HMAC);
158
159 *extra = ctrl_ret;
160 return 0;
161 #endif
162 }
163
164 static int s2n_composite_cipher_aes_sha_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *in, struct s2n_blob *out)
165 {
166 POSIX_ENSURE_EQ(out->size, in->size);
167
168 POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
169
170 /* len is set by EVP_EncryptUpdate and checked post operation */
171 int len = 0;
172 POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_ENCRYPT);
173
174 POSIX_ENSURE((int64_t) len == (int64_t) in->size, S2N_ERR_ENCRYPT);
175
176 return 0;
177 }
178
179 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)
180 {
181 POSIX_ENSURE_EQ(out->size, in->size);
182 POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT);
183
184 /* len is set by EVP_DecryptUpdate. It is not checked here but padding is manually removed and therefore
185 * the decryption operation is validated. */
186 int len = 0;
187 POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &len, in->data, in->size), S2N_ERR_DECRYPT);
188
189 return 0;
190 }
191
192 static int s2n_composite_cipher_aes_sha_set_mac_write_key(struct s2n_session_key *key, uint8_t *mac_key, uint32_t mac_size)
193 {
194 POSIX_ENSURE_EQ(mac_size, SHA_DIGEST_LENGTH);
195
196 EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_AEAD_SET_MAC_KEY, mac_size, mac_key);
197
198 return 0;
199 }
200
201 static int s2n_composite_cipher_aes_sha256_set_mac_write_key(struct s2n_session_key *key, uint8_t *mac_key, uint32_t mac_size)
202 {
203 POSIX_ENSURE_EQ(mac_size, SHA256_DIGEST_LENGTH);
204
205 EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_AEAD_SET_MAC_KEY, mac_size, mac_key);
206
207 return 0;
208 }
209
210 static S2N_RESULT s2n_composite_cipher_aes128_sha_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
211 {
212 RESULT_ENSURE_EQ(in->size, 16);
213
214 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
215 EVP_EncryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_128_cbc_hmac_sha1(), NULL, in->data, NULL);
216
217 return S2N_RESULT_OK;
218 }
219
220 static S2N_RESULT s2n_composite_cipher_aes128_sha_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
221 {
222 RESULT_ENSURE_EQ(in->size, 16);
223
224 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
225 EVP_DecryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_128_cbc_hmac_sha1(), NULL, in->data, NULL);
226
227 return S2N_RESULT_OK;
228 }
229
230 static S2N_RESULT s2n_composite_cipher_aes256_sha_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
231 {
232 RESULT_ENSURE_EQ(in->size, 32);
233
234 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
235 EVP_EncryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_256_cbc_hmac_sha1(), NULL, in->data, NULL);
236
237 return S2N_RESULT_OK;
238 }
239
240 static S2N_RESULT s2n_composite_cipher_aes256_sha_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
241 {
242 RESULT_ENSURE_EQ(in->size, 32);
243
244 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
245 EVP_DecryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_256_cbc_hmac_sha1(), NULL, in->data, NULL);
246
247 return S2N_RESULT_OK;
248 }
249
250 static S2N_RESULT s2n_composite_cipher_aes128_sha256_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
251 {
252 RESULT_ENSURE_EQ(in->size, 16);
253
254 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
255 EVP_EncryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_128_cbc_hmac_sha256(), NULL, in->data, NULL);
256
257 return S2N_RESULT_OK;
258 }
259
260 static S2N_RESULT s2n_composite_cipher_aes128_sha256_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
261 {
262 RESULT_ENSURE_EQ(in->size, 16);
263
264 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
265 EVP_DecryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_128_cbc_hmac_sha256(), NULL, in->data, NULL);
266
267 return S2N_RESULT_OK;
268 }
269
270 static S2N_RESULT s2n_composite_cipher_aes256_sha256_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in)
271 {
272 RESULT_ENSURE_EQ(in->size, 32);
273
274 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
275 EVP_EncryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_256_cbc_hmac_sha256(), NULL, in->data, NULL);
276
277 return S2N_RESULT_OK;
278 }
279
280 static S2N_RESULT s2n_composite_cipher_aes256_sha256_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in)
281 {
282 RESULT_ENSURE_EQ(in->size, 32);
283
284 EVP_CIPHER_CTX_set_padding(key->evp_cipher_ctx, 0);
285 EVP_DecryptInit_ex(key->evp_cipher_ctx, s2n_evp_aes_256_cbc_hmac_sha256(), NULL, in->data, NULL);
286
287 return S2N_RESULT_OK;
288 }
289
290 static S2N_RESULT s2n_composite_cipher_aes_sha_init(struct s2n_session_key *key)
291 {
292 RESULT_EVP_CTX_INIT(key->evp_cipher_ctx);
293
294 return S2N_RESULT_OK;
295 }
296
297 static S2N_RESULT s2n_composite_cipher_aes_sha_destroy_key(struct s2n_session_key *key)
298 {
299 EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx);
300
301 return S2N_RESULT_OK;
302 }
303
304 const struct s2n_cipher s2n_aes128_sha = {
305 .key_material_size = 16,
306 .type = S2N_COMPOSITE,
307 .io.comp = {
308 .block_size = 16,
309 .record_iv_size = 16,
310 .mac_key_size = SHA_DIGEST_LENGTH,
311 .decrypt = s2n_composite_cipher_aes_sha_decrypt,
312 .encrypt = s2n_composite_cipher_aes_sha_encrypt,
313 .set_mac_write_key = s2n_composite_cipher_aes_sha_set_mac_write_key,
314 .initial_hmac = s2n_composite_cipher_aes_sha_initial_hmac },
315 .is_available = s2n_composite_cipher_aes128_sha_available,
316 .init = s2n_composite_cipher_aes_sha_init,
317 .set_encryption_key = s2n_composite_cipher_aes128_sha_set_encryption_key,
318 .set_decryption_key = s2n_composite_cipher_aes128_sha_set_decryption_key,
319 .destroy_key = s2n_composite_cipher_aes_sha_destroy_key,
320 };
321
322 const struct s2n_cipher s2n_aes256_sha = {
323 .key_material_size = 32,
324 .type = S2N_COMPOSITE,
325 .io.comp = {
326 .block_size = 16,
327 .record_iv_size = 16,
328 .mac_key_size = SHA_DIGEST_LENGTH,
329 .decrypt = s2n_composite_cipher_aes_sha_decrypt,
330 .encrypt = s2n_composite_cipher_aes_sha_encrypt,
331 .set_mac_write_key = s2n_composite_cipher_aes_sha_set_mac_write_key,
332 .initial_hmac = s2n_composite_cipher_aes_sha_initial_hmac },
333 .is_available = s2n_composite_cipher_aes256_sha_available,
334 .init = s2n_composite_cipher_aes_sha_init,
335 .set_encryption_key = s2n_composite_cipher_aes256_sha_set_encryption_key,
336 .set_decryption_key = s2n_composite_cipher_aes256_sha_set_decryption_key,
337 .destroy_key = s2n_composite_cipher_aes_sha_destroy_key,
338 };
339
340 const struct s2n_cipher s2n_aes128_sha256 = {
341 .key_material_size = 16,
342 .type = S2N_COMPOSITE,
343 .io.comp = {
344 .block_size = 16,
345 .record_iv_size = 16,
346 .mac_key_size = SHA256_DIGEST_LENGTH,
347 .decrypt = s2n_composite_cipher_aes_sha_decrypt,
348 .encrypt = s2n_composite_cipher_aes_sha_encrypt,
349 .set_mac_write_key = s2n_composite_cipher_aes_sha256_set_mac_write_key,
350 .initial_hmac = s2n_composite_cipher_aes_sha_initial_hmac },
351 .is_available = s2n_composite_cipher_aes128_sha256_available,
352 .init = s2n_composite_cipher_aes_sha_init,
353 .set_encryption_key = s2n_composite_cipher_aes128_sha256_set_encryption_key,
354 .set_decryption_key = s2n_composite_cipher_aes128_sha256_set_decryption_key,
355 .destroy_key = s2n_composite_cipher_aes_sha_destroy_key,
356 };
357
358 const struct s2n_cipher s2n_aes256_sha256 = {
359 .key_material_size = 32,
360 .type = S2N_COMPOSITE,
361 .io.comp = {
362 .block_size = 16,
363 .record_iv_size = 16,
364 .mac_key_size = SHA256_DIGEST_LENGTH,
365 .decrypt = s2n_composite_cipher_aes_sha_decrypt,
366 .encrypt = s2n_composite_cipher_aes_sha_encrypt,
367 .set_mac_write_key = s2n_composite_cipher_aes_sha256_set_mac_write_key,
368 .initial_hmac = s2n_composite_cipher_aes_sha_initial_hmac },
369 .is_available = s2n_composite_cipher_aes256_sha256_available,
370 .init = s2n_composite_cipher_aes_sha_init,
371 .set_encryption_key = s2n_composite_cipher_aes256_sha256_set_encryption_key,
372 .set_decryption_key = s2n_composite_cipher_aes256_sha256_set_decryption_key,
373 .destroy_key = s2n_composite_cipher_aes_sha_destroy_key,
374 };