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