| 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 |
|
| 19 |
#include "crypto/s2n_cipher.h" |
| 20 |
#include "crypto/s2n_ktls_crypto.h" |
| 21 |
#include "tls/s2n_crypto.h" |
| 22 |
#include "utils/s2n_blob.h" |
| 23 |
#include "utils/s2n_safety.h" |
| 24 |
|
| 25 |
static bool s2n_aead_cipher_aes128_gcm_available(void) |
| 26 |
{ |
| 27 |
#if defined(S2N_LIBCRYPTO_SUPPORTS_EVP_AEAD_TLS) |
| 28 |
return (EVP_aead_aes_128_gcm() ? true : false); |
| 29 |
#else |
| 30 |
return (EVP_aes_128_gcm() ? true : false); |
| 31 |
#endif |
| 32 |
} |
| 33 |
|
| 34 |
static bool s2n_aead_cipher_aes256_gcm_available(void) |
| 35 |
{ |
| 36 |
#if defined(S2N_LIBCRYPTO_SUPPORTS_EVP_AEAD_TLS) |
| 37 |
return (EVP_aead_aes_256_gcm() ? true : false); |
| 38 |
#else |
| 39 |
return (EVP_aes_256_gcm() ? true : false); |
| 40 |
#endif |
| 41 |
} |
| 42 |
|
| 43 |
#if defined(S2N_LIBCRYPTO_SUPPORTS_EVP_AEAD_TLS) |
| 44 |
|
| 45 |
static int s2n_aead_cipher_aes_gcm_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out) |
| 46 |
{ |
| 47 |
POSIX_ENSURE_REF(in); |
| 48 |
POSIX_ENSURE_REF(out); |
| 49 |
POSIX_ENSURE_REF(iv); |
| 50 |
POSIX_ENSURE_REF(key); |
| 51 |
POSIX_ENSURE_REF(aad); |
| 52 |
|
| 53 |
|
| 54 |
POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN); |
| 55 |
POSIX_ENSURE_GTE(out->size, in->size); |
| 56 |
POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN); |
| 57 |
|
| 58 |
|
| 59 |
size_t in_len = in->size - S2N_TLS_GCM_TAG_LEN; |
| 60 |
|
| 61 |
size_t out_len = 0; |
| 62 |
|
| 63 |
POSIX_GUARD_OSSL(EVP_AEAD_CTX_seal(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in_len, aad->data, aad->size), S2N_ERR_ENCRYPT); |
| 64 |
|
| 65 |
S2N_ERROR_IF((in_len + S2N_TLS_GCM_TAG_LEN) != out_len, S2N_ERR_ENCRYPT); |
| 66 |
|
| 67 |
return S2N_SUCCESS; |
| 68 |
} |
| 69 |
|
| 70 |
static int s2n_aead_cipher_aes_gcm_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out) |
| 71 |
{ |
| 72 |
POSIX_ENSURE_REF(in); |
| 73 |
POSIX_ENSURE_REF(out); |
| 74 |
POSIX_ENSURE_REF(iv); |
| 75 |
POSIX_ENSURE_REF(key); |
| 76 |
POSIX_ENSURE_REF(aad); |
| 77 |
|
| 78 |
POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN); |
| 79 |
POSIX_ENSURE_GTE(out->size, in->size - S2N_TLS_GCM_TAG_LEN); |
| 80 |
POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN); |
| 81 |
|
| 82 |
|
| 83 |
size_t out_len = 0; |
| 84 |
|
| 85 |
POSIX_GUARD_OSSL(EVP_AEAD_CTX_open(key->evp_aead_ctx, out->data, &out_len, out->size, iv->data, iv->size, in->data, in->size, aad->data, aad->size), S2N_ERR_DECRYPT); |
| 86 |
|
| 87 |
S2N_ERROR_IF((in->size - S2N_TLS_GCM_TAG_LEN) != out_len, S2N_ERR_ENCRYPT); |
| 88 |
|
| 89 |
return S2N_SUCCESS; |
| 90 |
} |
| 91 |
|
| 92 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 93 |
{ |
| 94 |
RESULT_ENSURE_REF(key); |
| 95 |
RESULT_ENSURE_REF(in); |
| 96 |
|
| 97 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN); |
| 98 |
|
| 99 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 100 |
|
| 101 |
return S2N_RESULT_OK; |
| 102 |
} |
| 103 |
|
| 104 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 105 |
{ |
| 106 |
RESULT_ENSURE_REF(key); |
| 107 |
RESULT_ENSURE_REF(in); |
| 108 |
|
| 109 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN); |
| 110 |
|
| 111 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 112 |
|
| 113 |
return S2N_RESULT_OK; |
| 114 |
} |
| 115 |
|
| 116 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 117 |
{ |
| 118 |
RESULT_ENSURE_REF(key); |
| 119 |
RESULT_ENSURE_REF(in); |
| 120 |
|
| 121 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN); |
| 122 |
|
| 123 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 124 |
|
| 125 |
return S2N_RESULT_OK; |
| 126 |
} |
| 127 |
|
| 128 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 129 |
{ |
| 130 |
RESULT_ENSURE_REF(key); |
| 131 |
RESULT_ENSURE_REF(in); |
| 132 |
|
| 133 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN); |
| 134 |
|
| 135 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls12(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 136 |
|
| 137 |
return S2N_RESULT_OK; |
| 138 |
} |
| 139 |
|
| 140 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 141 |
{ |
| 142 |
RESULT_ENSURE_REF(key); |
| 143 |
RESULT_ENSURE_REF(in); |
| 144 |
|
| 145 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN); |
| 146 |
|
| 147 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 148 |
|
| 149 |
return S2N_RESULT_OK; |
| 150 |
} |
| 151 |
|
| 152 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 153 |
{ |
| 154 |
RESULT_ENSURE_REF(key); |
| 155 |
RESULT_ENSURE_REF(in); |
| 156 |
|
| 157 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN); |
| 158 |
|
| 159 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 160 |
|
| 161 |
return S2N_RESULT_OK; |
| 162 |
} |
| 163 |
|
| 164 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 165 |
{ |
| 166 |
RESULT_ENSURE_REF(key); |
| 167 |
RESULT_ENSURE_REF(in); |
| 168 |
|
| 169 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN); |
| 170 |
|
| 171 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_128_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 172 |
|
| 173 |
return S2N_RESULT_OK; |
| 174 |
} |
| 175 |
|
| 176 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 177 |
{ |
| 178 |
RESULT_ENSURE_REF(key); |
| 179 |
RESULT_ENSURE_REF(in); |
| 180 |
|
| 181 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN); |
| 182 |
|
| 183 |
RESULT_GUARD_OSSL(EVP_AEAD_CTX_init(key->evp_aead_ctx, EVP_aead_aes_256_gcm_tls13(), in->data, in->size, S2N_TLS_GCM_TAG_LEN, NULL), S2N_ERR_KEY_INIT); |
| 184 |
|
| 185 |
return S2N_RESULT_OK; |
| 186 |
} |
| 187 |
|
| 188 |
static S2N_RESULT s2n_aead_cipher_aes_gcm_init(struct s2n_session_key *key) |
| 189 |
{ |
| 190 |
RESULT_ENSURE_REF(key); |
| 191 |
|
| 192 |
EVP_AEAD_CTX_zero(key->evp_aead_ctx); |
| 193 |
|
| 194 |
return S2N_RESULT_OK; |
| 195 |
} |
| 196 |
|
| 197 |
static S2N_RESULT s2n_aead_cipher_aes_gcm_destroy_key(struct s2n_session_key *key) |
| 198 |
{ |
| 199 |
RESULT_ENSURE_REF(key); |
| 200 |
|
| 201 |
EVP_AEAD_CTX_cleanup(key->evp_aead_ctx); |
| 202 |
|
| 203 |
return S2N_RESULT_OK; |
| 204 |
} |
| 205 |
|
| 206 |
#else |
| 207 |
|
| 208 |
static int s2n_aead_cipher_aes_gcm_encrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out) |
| 209 |
{ |
| 210 |
|
| 211 |
POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN); |
| 212 |
POSIX_ENSURE_GTE(out->size, in->size); |
| 213 |
POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN); |
| 214 |
|
| 215 |
|
| 216 |
POSIX_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT); |
| 217 |
|
| 218 |
|
| 219 |
int in_len = in->size - S2N_TLS_GCM_TAG_LEN; |
| 220 |
uint8_t *tag_data = out->data + out->size - S2N_TLS_GCM_TAG_LEN; |
| 221 |
|
| 222 |
|
| 223 |
int out_len = 0; |
| 224 |
|
| 225 |
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_ENCRYPT); |
| 226 |
|
| 227 |
|
| 228 |
POSIX_GUARD_OSSL(EVP_EncryptUpdate(key->evp_cipher_ctx, out->data, &out_len, in->data, in_len), S2N_ERR_ENCRYPT); |
| 229 |
|
| 230 |
|
| 231 |
S2N_ERROR_IF(in_len != out_len, S2N_ERR_ENCRYPT); |
| 232 |
|
| 233 |
|
| 234 |
POSIX_GUARD_OSSL(EVP_EncryptFinal_ex(key->evp_cipher_ctx, out->data, &out_len), S2N_ERR_ENCRYPT); |
| 235 |
|
| 236 |
|
| 237 |
POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_GET_TAG, S2N_TLS_GCM_TAG_LEN, tag_data), S2N_ERR_ENCRYPT); |
| 238 |
|
| 239 |
|
| 240 |
S2N_ERROR_IF(0 != out_len, S2N_ERR_ENCRYPT); |
| 241 |
|
| 242 |
return S2N_SUCCESS; |
| 243 |
} |
| 244 |
|
| 245 |
static int s2n_aead_cipher_aes_gcm_decrypt(struct s2n_session_key *key, struct s2n_blob *iv, struct s2n_blob *aad, struct s2n_blob *in, struct s2n_blob *out) |
| 246 |
{ |
| 247 |
POSIX_ENSURE_GTE(in->size, S2N_TLS_GCM_TAG_LEN); |
| 248 |
POSIX_ENSURE_GTE(out->size, in->size); |
| 249 |
POSIX_ENSURE_EQ(iv->size, S2N_TLS_GCM_IV_LEN); |
| 250 |
|
| 251 |
|
| 252 |
POSIX_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, NULL, iv->data), S2N_ERR_KEY_INIT); |
| 253 |
|
| 254 |
|
| 255 |
int in_len = in->size - S2N_TLS_GCM_TAG_LEN; |
| 256 |
uint8_t *tag_data = in->data + in->size - S2N_TLS_GCM_TAG_LEN; |
| 257 |
|
| 258 |
|
| 259 |
POSIX_GUARD_OSSL(EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_TAG, S2N_TLS_GCM_TAG_LEN, tag_data), S2N_ERR_DECRYPT); |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
int out_len = 0; |
| 265 |
|
| 266 |
POSIX_GUARD_OSSL(EVP_DecryptUpdate(key->evp_cipher_ctx, NULL, &out_len, aad->data, aad->size), S2N_ERR_DECRYPT); |
| 267 |
|
| 268 |
int evp_decrypt_rc = 1; |
| 269 |
|
| 270 |
evp_decrypt_rc &= EVP_DecryptUpdate(key->evp_cipher_ctx, out->data, &out_len, in->data, in_len); |
| 271 |
|
| 272 |
|
| 273 |
evp_decrypt_rc &= EVP_DecryptFinal_ex(key->evp_cipher_ctx, out->data, &out_len); |
| 274 |
|
| 275 |
S2N_ERROR_IF(evp_decrypt_rc != 1, S2N_ERR_DECRYPT); |
| 276 |
|
| 277 |
return S2N_SUCCESS; |
| 278 |
} |
| 279 |
|
| 280 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 281 |
{ |
| 282 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN); |
| 283 |
|
| 284 |
RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT); |
| 285 |
|
| 286 |
EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL); |
| 287 |
|
| 288 |
RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 289 |
|
| 290 |
return S2N_RESULT_OK; |
| 291 |
} |
| 292 |
|
| 293 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_encryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 294 |
{ |
| 295 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN); |
| 296 |
|
| 297 |
RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT); |
| 298 |
|
| 299 |
EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL); |
| 300 |
|
| 301 |
RESULT_GUARD_OSSL(EVP_EncryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 302 |
|
| 303 |
return S2N_RESULT_OK; |
| 304 |
} |
| 305 |
|
| 306 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 307 |
{ |
| 308 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_128_GCM_KEY_LEN); |
| 309 |
|
| 310 |
RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_128_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT); |
| 311 |
|
| 312 |
EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL); |
| 313 |
|
| 314 |
RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 315 |
|
| 316 |
return S2N_RESULT_OK; |
| 317 |
} |
| 318 |
|
| 319 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_decryption_key(struct s2n_session_key *key, struct s2n_blob *in) |
| 320 |
{ |
| 321 |
RESULT_ENSURE_EQ(in->size, S2N_TLS_AES_256_GCM_KEY_LEN); |
| 322 |
|
| 323 |
RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL), S2N_ERR_KEY_INIT); |
| 324 |
|
| 325 |
EVP_CIPHER_CTX_ctrl(key->evp_cipher_ctx, EVP_CTRL_GCM_SET_IVLEN, S2N_TLS_GCM_IV_LEN, NULL); |
| 326 |
|
| 327 |
RESULT_GUARD_OSSL(EVP_DecryptInit_ex(key->evp_cipher_ctx, NULL, NULL, in->data, NULL), S2N_ERR_KEY_INIT); |
| 328 |
|
| 329 |
return S2N_RESULT_OK; |
| 330 |
} |
| 331 |
|
| 332 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 333 |
{ |
| 334 |
RESULT_GUARD(s2n_aead_cipher_aes128_gcm_set_encryption_key(key, in)); |
| 335 |
|
| 336 |
return S2N_RESULT_OK; |
| 337 |
} |
| 338 |
|
| 339 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_encryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 340 |
{ |
| 341 |
RESULT_GUARD(s2n_aead_cipher_aes256_gcm_set_encryption_key(key, in)); |
| 342 |
|
| 343 |
return S2N_RESULT_OK; |
| 344 |
} |
| 345 |
|
| 346 |
static S2N_RESULT s2n_aead_cipher_aes128_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 347 |
{ |
| 348 |
RESULT_GUARD(s2n_aead_cipher_aes128_gcm_set_decryption_key(key, in)); |
| 349 |
|
| 350 |
return S2N_RESULT_OK; |
| 351 |
} |
| 352 |
|
| 353 |
static S2N_RESULT s2n_aead_cipher_aes256_gcm_set_decryption_key_tls13(struct s2n_session_key *key, struct s2n_blob *in) |
| 354 |
{ |
| 355 |
RESULT_GUARD(s2n_aead_cipher_aes256_gcm_set_decryption_key(key, in)); |
| 356 |
|
| 357 |
return S2N_RESULT_OK; |
| 358 |
} |
| 359 |
|
| 360 |
static S2N_RESULT s2n_aead_cipher_aes_gcm_init(struct s2n_session_key *key) |
| 361 |
{ |
| 362 |
RESULT_EVP_CTX_INIT(key->evp_cipher_ctx); |
| 363 |
|
| 364 |
return S2N_RESULT_OK; |
| 365 |
} |
| 366 |
|
| 367 |
static S2N_RESULT s2n_aead_cipher_aes_gcm_destroy_key(struct s2n_session_key *key) |
| 368 |
{ |
| 369 |
EVP_CIPHER_CTX_cleanup(key->evp_cipher_ctx); |
| 370 |
|
| 371 |
return S2N_RESULT_OK; |
| 372 |
} |
| 373 |
|
| 374 |
#endif |
| 375 |
|
| 376 |
static S2N_RESULT s2n_tls12_aead_cipher_aes128_gcm_set_ktls_info( |
| 377 |
struct s2n_ktls_crypto_info_inputs *in, struct s2n_ktls_crypto_info *out) |
| 378 |
{ |
| 379 |
RESULT_ENSURE_REF(in); |
| 380 |
RESULT_ENSURE_REF(out); |
| 381 |
|
| 382 |
s2n_ktls_crypto_info_tls12_aes_gcm_128 *crypto_info = &out->ciphers.aes_gcm_128; |
| 383 |
crypto_info->info.version = TLS_1_2_VERSION; |
| 384 |
crypto_info->info.cipher_type = TLS_CIPHER_AES_GCM_128; |
| 385 |
|
| 386 |
RESULT_ENSURE_LTE(sizeof(crypto_info->key), in->key.size); |
| 387 |
RESULT_CHECKED_MEMCPY(crypto_info->key, in->key.data, sizeof(crypto_info->key)); |
| 388 |
RESULT_ENSURE_LTE(sizeof(crypto_info->rec_seq), in->seq.size); |
| 389 |
RESULT_CHECKED_MEMCPY(crypto_info->rec_seq, in->seq.data, sizeof(crypto_info->rec_seq)); |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
RESULT_ENSURE_LTE(sizeof(crypto_info->salt), in->iv.size); |
| 405 |
RESULT_CHECKED_MEMCPY(crypto_info->salt, in->iv.data, sizeof(crypto_info->salt)); |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
RESULT_ENSURE_LTE(sizeof(crypto_info->iv), in->seq.size); |
| 419 |
RESULT_CHECKED_MEMCPY(crypto_info->iv, in->seq.data, sizeof(crypto_info->iv)); |
| 420 |
|
| 421 |
RESULT_GUARD_POSIX(s2n_blob_init(&out->value, (uint8_t *) (void *) crypto_info, |
| 422 |
sizeof(s2n_ktls_crypto_info_tls12_aes_gcm_128))); |
| 423 |
return S2N_RESULT_OK; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
static S2N_RESULT s2n_tls12_aead_cipher_aes256_gcm_set_ktls_info( |
| 430 |
struct s2n_ktls_crypto_info_inputs *in, struct s2n_ktls_crypto_info *out) |
| 431 |
{ |
| 432 |
RESULT_ENSURE_REF(in); |
| 433 |
RESULT_ENSURE_REF(out); |
| 434 |
|
| 435 |
s2n_ktls_crypto_info_tls12_aes_gcm_256 *crypto_info = &out->ciphers.aes_gcm_256; |
| 436 |
crypto_info->info.version = TLS_1_2_VERSION; |
| 437 |
crypto_info->info.cipher_type = TLS_CIPHER_AES_GCM_256; |
| 438 |
|
| 439 |
RESULT_ENSURE_LTE(sizeof(crypto_info->key), in->key.size); |
| 440 |
RESULT_CHECKED_MEMCPY(crypto_info->key, in->key.data, sizeof(crypto_info->key)); |
| 441 |
RESULT_ENSURE_LTE(sizeof(crypto_info->rec_seq), in->seq.size); |
| 442 |
RESULT_CHECKED_MEMCPY(crypto_info->rec_seq, in->seq.data, sizeof(crypto_info->rec_seq)); |
| 443 |
RESULT_ENSURE_LTE(sizeof(crypto_info->salt), in->iv.size); |
| 444 |
RESULT_CHECKED_MEMCPY(crypto_info->salt, in->iv.data, sizeof(crypto_info->salt)); |
| 445 |
RESULT_ENSURE_LTE(sizeof(crypto_info->iv), in->seq.size); |
| 446 |
RESULT_CHECKED_MEMCPY(crypto_info->iv, in->seq.data, sizeof(crypto_info->iv)); |
| 447 |
|
| 448 |
RESULT_GUARD_POSIX(s2n_blob_init(&out->value, (uint8_t *) (void *) crypto_info, |
| 449 |
sizeof(s2n_ktls_crypto_info_tls12_aes_gcm_256))); |
| 450 |
return S2N_RESULT_OK; |
| 451 |
} |
| 452 |
|
| 453 |
static S2N_RESULT s2n_tls13_aead_cipher_aes128_gcm_set_ktls_info( |
| 454 |
struct s2n_ktls_crypto_info_inputs *in, struct s2n_ktls_crypto_info *out) |
| 455 |
{ |
| 456 |
RESULT_ENSURE_REF(in); |
| 457 |
RESULT_ENSURE_REF(out); |
| 458 |
|
| 459 |
s2n_ktls_crypto_info_tls12_aes_gcm_128 *crypto_info = &out->ciphers.aes_gcm_128; |
| 460 |
crypto_info->info.version = TLS_1_3_VERSION; |
| 461 |
crypto_info->info.cipher_type = TLS_CIPHER_AES_GCM_128; |
| 462 |
|
| 463 |
RESULT_ENSURE_LTE(sizeof(crypto_info->key), in->key.size); |
| 464 |
RESULT_CHECKED_MEMCPY(crypto_info->key, in->key.data, sizeof(crypto_info->key)); |
| 465 |
RESULT_ENSURE_LTE(sizeof(crypto_info->rec_seq), in->seq.size); |
| 466 |
RESULT_CHECKED_MEMCPY(crypto_info->rec_seq, in->seq.data, sizeof(crypto_info->rec_seq)); |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
size_t salt_size = sizeof(crypto_info->salt); |
| 478 |
RESULT_ENSURE_LTE(salt_size, in->iv.size); |
| 479 |
RESULT_CHECKED_MEMCPY(crypto_info->salt, in->iv.data, salt_size); |
| 480 |
size_t iv_remainder = in->iv.size - salt_size; |
| 481 |
RESULT_ENSURE_LTE(sizeof(crypto_info->iv), iv_remainder); |
| 482 |
RESULT_CHECKED_MEMCPY(crypto_info->iv, in->iv.data + salt_size, sizeof(crypto_info->iv)); |
| 483 |
|
| 484 |
RESULT_GUARD_POSIX(s2n_blob_init(&out->value, (uint8_t *) (void *) crypto_info, |
| 485 |
sizeof(s2n_ktls_crypto_info_tls12_aes_gcm_128))); |
| 486 |
return S2N_RESULT_OK; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
static S2N_RESULT s2n_tls13_aead_cipher_aes256_gcm_set_ktls_info( |
| 493 |
struct s2n_ktls_crypto_info_inputs *in, struct s2n_ktls_crypto_info *out) |
| 494 |
{ |
| 495 |
RESULT_ENSURE_REF(in); |
| 496 |
RESULT_ENSURE_REF(out); |
| 497 |
|
| 498 |
s2n_ktls_crypto_info_tls12_aes_gcm_256 *crypto_info = &out->ciphers.aes_gcm_256; |
| 499 |
crypto_info->info.version = TLS_1_3_VERSION; |
| 500 |
crypto_info->info.cipher_type = TLS_CIPHER_AES_GCM_256; |
| 501 |
|
| 502 |
RESULT_ENSURE_LTE(sizeof(crypto_info->key), in->key.size); |
| 503 |
RESULT_CHECKED_MEMCPY(crypto_info->key, in->key.data, sizeof(crypto_info->key)); |
| 504 |
RESULT_ENSURE_LTE(sizeof(crypto_info->rec_seq), in->seq.size); |
| 505 |
RESULT_CHECKED_MEMCPY(crypto_info->rec_seq, in->seq.data, sizeof(crypto_info->rec_seq)); |
| 506 |
|
| 507 |
size_t salt_size = sizeof(crypto_info->salt); |
| 508 |
RESULT_ENSURE_LTE(salt_size, in->iv.size); |
| 509 |
RESULT_CHECKED_MEMCPY(crypto_info->salt, in->iv.data, salt_size); |
| 510 |
size_t iv_remainder = in->iv.size - salt_size; |
| 511 |
RESULT_ENSURE_LTE(sizeof(crypto_info->iv), iv_remainder); |
| 512 |
RESULT_CHECKED_MEMCPY(crypto_info->iv, in->iv.data + salt_size, sizeof(crypto_info->iv)); |
| 513 |
|
| 514 |
RESULT_GUARD_POSIX(s2n_blob_init(&out->value, (uint8_t *) (void *) crypto_info, |
| 515 |
sizeof(s2n_ktls_crypto_info_tls12_aes_gcm_256))); |
| 516 |
return S2N_RESULT_OK; |
| 517 |
} |
| 518 |
|
| 519 |
const struct s2n_cipher s2n_aes128_gcm = { |
| 520 |
.key_material_size = S2N_TLS_AES_128_GCM_KEY_LEN, |
| 521 |
.type = S2N_AEAD, |
| 522 |
.io.aead = { |
| 523 |
.record_iv_size = S2N_TLS_GCM_EXPLICIT_IV_LEN, |
| 524 |
.fixed_iv_size = S2N_TLS_GCM_FIXED_IV_LEN, |
| 525 |
.tag_size = S2N_TLS_GCM_TAG_LEN, |
| 526 |
.decrypt = s2n_aead_cipher_aes_gcm_decrypt, |
| 527 |
.encrypt = s2n_aead_cipher_aes_gcm_encrypt }, |
| 528 |
.is_available = s2n_aead_cipher_aes128_gcm_available, |
| 529 |
.init = s2n_aead_cipher_aes_gcm_init, |
| 530 |
.set_encryption_key = s2n_aead_cipher_aes128_gcm_set_encryption_key, |
| 531 |
.set_decryption_key = s2n_aead_cipher_aes128_gcm_set_decryption_key, |
| 532 |
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key, |
| 533 |
.set_ktls_info = s2n_tls12_aead_cipher_aes128_gcm_set_ktls_info, |
| 534 |
}; |
| 535 |
|
| 536 |
const struct s2n_cipher s2n_aes256_gcm = { |
| 537 |
.key_material_size = S2N_TLS_AES_256_GCM_KEY_LEN, |
| 538 |
.type = S2N_AEAD, |
| 539 |
.io.aead = { |
| 540 |
.record_iv_size = S2N_TLS_GCM_EXPLICIT_IV_LEN, |
| 541 |
.fixed_iv_size = S2N_TLS_GCM_FIXED_IV_LEN, |
| 542 |
.tag_size = S2N_TLS_GCM_TAG_LEN, |
| 543 |
.decrypt = s2n_aead_cipher_aes_gcm_decrypt, |
| 544 |
.encrypt = s2n_aead_cipher_aes_gcm_encrypt }, |
| 545 |
.is_available = s2n_aead_cipher_aes256_gcm_available, |
| 546 |
.init = s2n_aead_cipher_aes_gcm_init, |
| 547 |
.set_encryption_key = s2n_aead_cipher_aes256_gcm_set_encryption_key, |
| 548 |
.set_decryption_key = s2n_aead_cipher_aes256_gcm_set_decryption_key, |
| 549 |
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key, |
| 550 |
.set_ktls_info = s2n_tls12_aead_cipher_aes256_gcm_set_ktls_info, |
| 551 |
}; |
| 552 |
|
| 553 |
|
| 554 |
const struct s2n_cipher s2n_tls13_aes128_gcm = { |
| 555 |
.key_material_size = S2N_TLS_AES_128_GCM_KEY_LEN, |
| 556 |
.type = S2N_AEAD, |
| 557 |
.io.aead = { |
| 558 |
.record_iv_size = S2N_TLS13_RECORD_IV_LEN, |
| 559 |
.fixed_iv_size = S2N_TLS13_FIXED_IV_LEN, |
| 560 |
.tag_size = S2N_TLS_GCM_TAG_LEN, |
| 561 |
.decrypt = s2n_aead_cipher_aes_gcm_decrypt, |
| 562 |
.encrypt = s2n_aead_cipher_aes_gcm_encrypt }, |
| 563 |
.is_available = s2n_aead_cipher_aes128_gcm_available, |
| 564 |
.init = s2n_aead_cipher_aes_gcm_init, |
| 565 |
.set_encryption_key = s2n_aead_cipher_aes128_gcm_set_encryption_key_tls13, |
| 566 |
.set_decryption_key = s2n_aead_cipher_aes128_gcm_set_decryption_key_tls13, |
| 567 |
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key, |
| 568 |
.set_ktls_info = s2n_tls13_aead_cipher_aes128_gcm_set_ktls_info, |
| 569 |
}; |
| 570 |
|
| 571 |
const struct s2n_cipher s2n_tls13_aes256_gcm = { |
| 572 |
.key_material_size = S2N_TLS_AES_256_GCM_KEY_LEN, |
| 573 |
.type = S2N_AEAD, |
| 574 |
.io.aead = { |
| 575 |
.record_iv_size = S2N_TLS13_RECORD_IV_LEN, |
| 576 |
.fixed_iv_size = S2N_TLS13_FIXED_IV_LEN, |
| 577 |
.tag_size = S2N_TLS_GCM_TAG_LEN, |
| 578 |
.decrypt = s2n_aead_cipher_aes_gcm_decrypt, |
| 579 |
.encrypt = s2n_aead_cipher_aes_gcm_encrypt }, |
| 580 |
.is_available = s2n_aead_cipher_aes256_gcm_available, |
| 581 |
.init = s2n_aead_cipher_aes_gcm_init, |
| 582 |
.set_encryption_key = s2n_aead_cipher_aes256_gcm_set_encryption_key_tls13, |
| 583 |
.set_decryption_key = s2n_aead_cipher_aes256_gcm_set_decryption_key_tls13, |
| 584 |
.destroy_key = s2n_aead_cipher_aes_gcm_destroy_key, |
| 585 |
.set_ktls_info = s2n_tls13_aead_cipher_aes256_gcm_set_ktls_info, |
| 586 |
}; |