David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 1 | /* Copyright (c) 2015, Google Inc. |
| 2 | * |
| 3 | * Permission to use, copy, modify, and/or distribute this software for any |
| 4 | * purpose with or without fee is hereby granted, provided that the above |
| 5 | * copyright notice and this permission notice appear in all copies. |
| 6 | * |
| 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
David Benjamin | 9e4e01e | 2015-09-15 01:48:04 -0400 | [diff] [blame] | 15 | #include <openssl/ssl.h> |
| 16 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 17 | #include <assert.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include <openssl/aead.h> |
| 21 | #include <openssl/err.h> |
| 22 | #include <openssl/rand.h> |
| 23 | #include <openssl/type_check.h> |
| 24 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 25 | #include "../crypto/internal.h" |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 26 | #include "internal.h" |
| 27 | |
| 28 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 29 | SSL_AEAD_CTX *SSL_AEAD_CTX_new(enum evp_aead_direction_t direction, |
| 30 | uint16_t version, const SSL_CIPHER *cipher, |
| 31 | const uint8_t *enc_key, size_t enc_key_len, |
| 32 | const uint8_t *mac_key, size_t mac_key_len, |
| 33 | const uint8_t *fixed_iv, size_t fixed_iv_len) { |
| 34 | const EVP_AEAD *aead; |
David Benjamin | 4b0d0e4 | 2016-10-28 17:17:14 -0400 | [diff] [blame] | 35 | size_t expected_mac_key_len, expected_fixed_iv_len; |
| 36 | if (!ssl_cipher_get_evp_aead(&aead, &expected_mac_key_len, |
| 37 | &expected_fixed_iv_len, cipher, version) || |
| 38 | /* Ensure the caller returned correct key sizes. */ |
| 39 | expected_fixed_iv_len != fixed_iv_len || |
| 40 | expected_mac_key_len != mac_key_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 41 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | uint8_t merged_key[EVP_AEAD_MAX_KEY_LENGTH]; |
| 46 | if (mac_key_len > 0) { |
| 47 | /* This is a "stateful" AEAD (for compatibility with pre-AEAD cipher |
| 48 | * suites). */ |
| 49 | if (mac_key_len + enc_key_len + fixed_iv_len > sizeof(merged_key)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 50 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 51 | return 0; |
| 52 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 53 | OPENSSL_memcpy(merged_key, mac_key, mac_key_len); |
| 54 | OPENSSL_memcpy(merged_key + mac_key_len, enc_key, enc_key_len); |
| 55 | OPENSSL_memcpy(merged_key + mac_key_len + enc_key_len, fixed_iv, |
| 56 | fixed_iv_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 57 | enc_key = merged_key; |
| 58 | enc_key_len += mac_key_len; |
| 59 | enc_key_len += fixed_iv_len; |
| 60 | } |
| 61 | |
Brian Smith | 5ba0689 | 2016-02-07 09:36:04 -1000 | [diff] [blame] | 62 | SSL_AEAD_CTX *aead_ctx = OPENSSL_malloc(sizeof(SSL_AEAD_CTX)); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 63 | if (aead_ctx == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 64 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 65 | return NULL; |
| 66 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 67 | OPENSSL_memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX)); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 68 | aead_ctx->cipher = cipher; |
| 69 | |
| 70 | if (!EVP_AEAD_CTX_init_with_direction( |
| 71 | &aead_ctx->ctx, aead, enc_key, enc_key_len, |
| 72 | EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) { |
| 73 | OPENSSL_free(aead_ctx); |
| 74 | return NULL; |
| 75 | } |
| 76 | |
| 77 | assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH); |
David Benjamin | 5db7c9b | 2017-01-24 16:17:03 -0500 | [diff] [blame] | 78 | OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256, |
| 79 | variable_nonce_len_doesnt_fit_in_uint8_t); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 80 | aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead); |
David Benjamin | 51a01a5 | 2015-10-29 13:19:56 -0400 | [diff] [blame] | 81 | if (mac_key_len == 0) { |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 82 | assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce)); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 83 | OPENSSL_memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 84 | aead_ctx->fixed_nonce_len = fixed_iv_len; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 85 | |
| 86 | if (cipher->algorithm_enc & SSL_CHACHA20POLY1305) { |
| 87 | /* The fixed nonce into the actual nonce (the sequence number). */ |
| 88 | aead_ctx->xor_fixed_nonce = 1; |
| 89 | aead_ctx->variable_nonce_len = 8; |
| 90 | } else { |
| 91 | /* The fixed IV is prepended to the nonce. */ |
| 92 | assert(fixed_iv_len <= aead_ctx->variable_nonce_len); |
| 93 | aead_ctx->variable_nonce_len -= fixed_iv_len; |
| 94 | } |
| 95 | |
David Benjamin | b2a985b | 2015-06-21 15:13:57 -0400 | [diff] [blame] | 96 | /* AES-GCM uses an explicit nonce. */ |
| 97 | if (cipher->algorithm_enc & (SSL_AES128GCM | SSL_AES256GCM)) { |
| 98 | aead_ctx->variable_nonce_included_in_record = 1; |
| 99 | } |
Steven Valdez | 494650c | 2016-05-24 12:43:04 -0400 | [diff] [blame] | 100 | |
| 101 | /* The TLS 1.3 construction XORs the fixed nonce into the sequence number |
| 102 | * and omits the additional data. */ |
| 103 | if (version >= TLS1_3_VERSION) { |
| 104 | aead_ctx->xor_fixed_nonce = 1; |
| 105 | aead_ctx->variable_nonce_len = 8; |
| 106 | aead_ctx->variable_nonce_included_in_record = 0; |
| 107 | aead_ctx->omit_ad = 1; |
Steven Valdez | 7975056 | 2016-06-16 06:38:04 -0400 | [diff] [blame] | 108 | assert(fixed_iv_len >= aead_ctx->variable_nonce_len); |
Steven Valdez | 494650c | 2016-05-24 12:43:04 -0400 | [diff] [blame] | 109 | } |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 110 | } else { |
Steven Valdez | 7975056 | 2016-06-16 06:38:04 -0400 | [diff] [blame] | 111 | assert(version < TLS1_3_VERSION); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 112 | aead_ctx->variable_nonce_included_in_record = 1; |
| 113 | aead_ctx->random_variable_nonce = 1; |
| 114 | aead_ctx->omit_length_in_ad = 1; |
| 115 | aead_ctx->omit_version_in_ad = (version == SSL3_VERSION); |
| 116 | } |
| 117 | |
| 118 | return aead_ctx; |
| 119 | } |
| 120 | |
| 121 | void SSL_AEAD_CTX_free(SSL_AEAD_CTX *aead) { |
| 122 | if (aead == NULL) { |
| 123 | return; |
| 124 | } |
| 125 | EVP_AEAD_CTX_cleanup(&aead->ctx); |
| 126 | OPENSSL_free(aead); |
| 127 | } |
| 128 | |
David Benjamin | a772b16 | 2017-01-24 17:51:33 -0500 | [diff] [blame^] | 129 | size_t SSL_AEAD_CTX_explicit_nonce_len(const SSL_AEAD_CTX *aead) { |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 130 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 131 | aead = NULL; |
| 132 | #endif |
| 133 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 134 | if (aead != NULL && aead->variable_nonce_included_in_record) { |
| 135 | return aead->variable_nonce_len; |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
David Benjamin | a772b16 | 2017-01-24 17:51:33 -0500 | [diff] [blame^] | 140 | size_t SSL_AEAD_CTX_max_overhead(const SSL_AEAD_CTX *aead) { |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 141 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 142 | aead = NULL; |
| 143 | #endif |
| 144 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 145 | if (aead == NULL) { |
| 146 | return 0; |
| 147 | } |
| 148 | return EVP_AEAD_max_overhead(aead->ctx.aead) + |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 149 | SSL_AEAD_CTX_explicit_nonce_len(aead); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /* ssl_aead_ctx_get_ad writes the additional data for |aead| into |out| and |
| 153 | * returns the number of bytes written. */ |
| 154 | static size_t ssl_aead_ctx_get_ad(SSL_AEAD_CTX *aead, uint8_t out[13], |
| 155 | uint8_t type, uint16_t wire_version, |
| 156 | const uint8_t seqnum[8], |
| 157 | size_t plaintext_len) { |
Steven Valdez | 494650c | 2016-05-24 12:43:04 -0400 | [diff] [blame] | 158 | if (aead->omit_ad) { |
| 159 | return 0; |
| 160 | } |
| 161 | |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 162 | OPENSSL_memcpy(out, seqnum, 8); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 163 | size_t len = 8; |
| 164 | out[len++] = type; |
| 165 | if (!aead->omit_version_in_ad) { |
| 166 | out[len++] = (uint8_t)(wire_version >> 8); |
| 167 | out[len++] = (uint8_t)wire_version; |
| 168 | } |
| 169 | if (!aead->omit_length_in_ad) { |
| 170 | out[len++] = (uint8_t)(plaintext_len >> 8); |
| 171 | out[len++] = (uint8_t)plaintext_len; |
| 172 | } |
| 173 | return len; |
| 174 | } |
| 175 | |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 176 | int SSL_AEAD_CTX_open(SSL_AEAD_CTX *aead, CBS *out, uint8_t type, |
| 177 | uint16_t wire_version, const uint8_t seqnum[8], |
| 178 | uint8_t *in, size_t in_len) { |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 179 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 180 | aead = NULL; |
| 181 | #endif |
| 182 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 183 | if (aead == NULL) { |
| 184 | /* Handle the initial NULL cipher. */ |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 185 | CBS_init(out, in, in_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | /* TLS 1.2 AEADs include the length in the AD and are assumed to have fixed |
| 190 | * overhead. Otherwise the parameter is unused. */ |
| 191 | size_t plaintext_len = 0; |
| 192 | if (!aead->omit_length_in_ad) { |
| 193 | size_t overhead = SSL_AEAD_CTX_max_overhead(aead); |
| 194 | if (in_len < overhead) { |
| 195 | /* Publicly invalid. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 196 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 197 | return 0; |
| 198 | } |
| 199 | plaintext_len = in_len - overhead; |
| 200 | } |
| 201 | uint8_t ad[13]; |
| 202 | size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum, |
| 203 | plaintext_len); |
| 204 | |
| 205 | /* Assemble the nonce. */ |
| 206 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 207 | size_t nonce_len = 0; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 208 | |
| 209 | /* Prepend the fixed nonce, or left-pad with zeros if XORing. */ |
| 210 | if (aead->xor_fixed_nonce) { |
| 211 | nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 212 | OPENSSL_memset(nonce, 0, nonce_len); |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 213 | } else { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 214 | OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len); |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 215 | nonce_len += aead->fixed_nonce_len; |
| 216 | } |
| 217 | |
| 218 | /* Add the variable nonce. */ |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 219 | if (aead->variable_nonce_included_in_record) { |
| 220 | if (in_len < aead->variable_nonce_len) { |
| 221 | /* Publicly invalid. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 222 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 223 | return 0; |
| 224 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 225 | OPENSSL_memcpy(nonce + nonce_len, in, aead->variable_nonce_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 226 | in += aead->variable_nonce_len; |
| 227 | in_len -= aead->variable_nonce_len; |
| 228 | } else { |
| 229 | assert(aead->variable_nonce_len == 8); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 230 | OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 231 | } |
| 232 | nonce_len += aead->variable_nonce_len; |
| 233 | |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 234 | /* XOR the fixed nonce, if necessary. */ |
| 235 | if (aead->xor_fixed_nonce) { |
| 236 | assert(nonce_len == aead->fixed_nonce_len); |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 237 | for (size_t i = 0; i < aead->fixed_nonce_len; i++) { |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 238 | nonce[i] ^= aead->fixed_nonce[i]; |
| 239 | } |
| 240 | } |
| 241 | |
David Benjamin | a7810c1 | 2016-06-06 18:54:51 -0400 | [diff] [blame] | 242 | /* Decrypt in-place. */ |
| 243 | size_t len; |
| 244 | if (!EVP_AEAD_CTX_open(&aead->ctx, in, &len, in_len, nonce, nonce_len, |
| 245 | in, in_len, ad, ad_len)) { |
| 246 | return 0; |
| 247 | } |
| 248 | CBS_init(out, in, len); |
| 249 | return 1; |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | int SSL_AEAD_CTX_seal(SSL_AEAD_CTX *aead, uint8_t *out, size_t *out_len, |
| 253 | size_t max_out, uint8_t type, uint16_t wire_version, |
| 254 | const uint8_t seqnum[8], const uint8_t *in, |
| 255 | size_t in_len) { |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 256 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 257 | aead = NULL; |
| 258 | #endif |
| 259 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 260 | if (aead == NULL) { |
| 261 | /* Handle the initial NULL cipher. */ |
| 262 | if (in_len > max_out) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 263 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 264 | return 0; |
| 265 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 266 | OPENSSL_memmove(out, in, in_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 267 | *out_len = in_len; |
| 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | uint8_t ad[13]; |
| 272 | size_t ad_len = ssl_aead_ctx_get_ad(aead, ad, type, wire_version, seqnum, |
| 273 | in_len); |
| 274 | |
| 275 | /* Assemble the nonce. */ |
| 276 | uint8_t nonce[EVP_AEAD_MAX_NONCE_LENGTH]; |
| 277 | size_t nonce_len = 0; |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 278 | |
| 279 | /* Prepend the fixed nonce, or left-pad with zeros if XORing. */ |
| 280 | if (aead->xor_fixed_nonce) { |
| 281 | nonce_len = aead->fixed_nonce_len - aead->variable_nonce_len; |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 282 | OPENSSL_memset(nonce, 0, nonce_len); |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 283 | } else { |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 284 | OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len); |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 285 | nonce_len += aead->fixed_nonce_len; |
| 286 | } |
| 287 | |
| 288 | /* Select the variable nonce. */ |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 289 | if (aead->random_variable_nonce) { |
| 290 | assert(aead->variable_nonce_included_in_record); |
| 291 | if (!RAND_bytes(nonce + nonce_len, aead->variable_nonce_len)) { |
| 292 | return 0; |
| 293 | } |
| 294 | } else { |
| 295 | /* When sending we use the sequence number as the variable part of the |
| 296 | * nonce. */ |
| 297 | assert(aead->variable_nonce_len == 8); |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 298 | OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 299 | } |
| 300 | nonce_len += aead->variable_nonce_len; |
| 301 | |
| 302 | /* Emit the variable nonce if included in the record. */ |
| 303 | size_t extra_len = 0; |
| 304 | if (aead->variable_nonce_included_in_record) { |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 305 | assert(!aead->xor_fixed_nonce); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 306 | if (max_out < aead->variable_nonce_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 307 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | if (out < in + in_len && in < out + aead->variable_nonce_len) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 311 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 312 | return 0; |
| 313 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 314 | OPENSSL_memcpy(out, nonce + aead->fixed_nonce_len, |
| 315 | aead->variable_nonce_len); |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 316 | extra_len = aead->variable_nonce_len; |
| 317 | out += aead->variable_nonce_len; |
| 318 | max_out -= aead->variable_nonce_len; |
| 319 | } |
| 320 | |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 321 | /* XOR the fixed nonce, if necessary. */ |
| 322 | if (aead->xor_fixed_nonce) { |
| 323 | assert(nonce_len == aead->fixed_nonce_len); |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 324 | for (size_t i = 0; i < aead->fixed_nonce_len; i++) { |
David Benjamin | 13414b3 | 2015-12-09 23:02:39 -0500 | [diff] [blame] | 325 | nonce[i] ^= aead->fixed_nonce[i]; |
| 326 | } |
| 327 | } |
| 328 | |
David Benjamin | 31a0779 | 2015-03-03 14:20:26 -0500 | [diff] [blame] | 329 | if (!EVP_AEAD_CTX_seal(&aead->ctx, out, out_len, max_out, nonce, nonce_len, |
| 330 | in, in_len, ad, ad_len)) { |
| 331 | return 0; |
| 332 | } |
| 333 | *out_len += extra_len; |
| 334 | return 1; |
| 335 | } |