| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
#if !defined(LIBRESSL_VERSION_NUMBER) && !defined(OPENSSL_IS_BORINGSSL) |
| 31 |
|
| 32 |
|
| 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 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 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 |
|
| 113 |
|
| 114 |
|
| 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 |
|
| 122 |
|
| 123 |
|
| 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 |
|
| 132 |
|
| 133 |
|
| 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 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 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 |
|
| 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 |
|
| 185 |
|
| 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 |
}; |