Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 15 | #include <openssl/ssl.h> |
| 16 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 25 | #include "../crypto/internal.h" |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 26 | #include "internal.h" |
| 27 | |
| 28 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [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) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 41 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 50 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 51 | return 0; |
| 52 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [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); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 57 | enc_key = merged_key; |
| 58 | enc_key_len += mac_key_len; |
| 59 | enc_key_len += fixed_iv_len; |
| 60 | } |
| 61 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 62 | SSL_AEAD_CTX *aead_ctx = OPENSSL_malloc(sizeof(SSL_AEAD_CTX)); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 63 | if (aead_ctx == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 64 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 65 | return NULL; |
| 66 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 67 | OPENSSL_memset(aead_ctx, 0, sizeof(SSL_AEAD_CTX)); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 68 | aead_ctx->cipher = cipher; |
Robert Sloan | 1c9db53 | 2017-03-13 08:03:59 -0700 | [diff] [blame] | 69 | aead_ctx->version = version; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 70 | |
| 71 | if (!EVP_AEAD_CTX_init_with_direction( |
| 72 | &aead_ctx->ctx, aead, enc_key, enc_key_len, |
| 73 | EVP_AEAD_DEFAULT_TAG_LENGTH, direction)) { |
| 74 | OPENSSL_free(aead_ctx); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | assert(EVP_AEAD_nonce_length(aead) <= EVP_AEAD_MAX_NONCE_LENGTH); |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 79 | OPENSSL_COMPILE_ASSERT(EVP_AEAD_MAX_NONCE_LENGTH < 256, |
| 80 | variable_nonce_len_doesnt_fit_in_uint8_t); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 81 | aead_ctx->variable_nonce_len = (uint8_t)EVP_AEAD_nonce_length(aead); |
| 82 | if (mac_key_len == 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 83 | assert(fixed_iv_len <= sizeof(aead_ctx->fixed_nonce)); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 84 | OPENSSL_memcpy(aead_ctx->fixed_nonce, fixed_iv, fixed_iv_len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 85 | aead_ctx->fixed_nonce_len = fixed_iv_len; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [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 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -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; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 109 | assert(fixed_iv_len >= aead_ctx->variable_nonce_len); |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 110 | } |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 111 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 112 | assert(version < TLS1_3_VERSION); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 130 | size_t SSL_AEAD_CTX_explicit_nonce_len(const SSL_AEAD_CTX *aead) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 131 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 132 | aead = NULL; |
| 133 | #endif |
| 134 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 135 | if (aead != NULL && aead->variable_nonce_included_in_record) { |
| 136 | return aead->variable_nonce_len; |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 141 | size_t SSL_AEAD_CTX_max_overhead(const SSL_AEAD_CTX *aead) { |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 142 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 143 | aead = NULL; |
| 144 | #endif |
| 145 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 146 | if (aead == NULL) { |
| 147 | return 0; |
| 148 | } |
| 149 | return EVP_AEAD_max_overhead(aead->ctx.aead) + |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 150 | SSL_AEAD_CTX_explicit_nonce_len(aead); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 159 | if (aead->omit_ad) { |
| 160 | return 0; |
| 161 | } |
| 162 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 163 | OPENSSL_memcpy(out, seqnum, 8); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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 | 6e899c7 | 2016-06-09 18:02:18 -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 | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 180 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 181 | aead = NULL; |
| 182 | #endif |
| 183 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 184 | if (aead == NULL) { |
| 185 | /* Handle the initial NULL cipher. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 186 | CBS_init(out, in, in_len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 197 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [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; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 213 | OPENSSL_memset(nonce, 0, nonce_len); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 214 | } else { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 215 | OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 216 | nonce_len += aead->fixed_nonce_len; |
| 217 | } |
| 218 | |
| 219 | /* Add the variable nonce. */ |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 220 | if (aead->variable_nonce_included_in_record) { |
| 221 | if (in_len < aead->variable_nonce_len) { |
| 222 | /* Publicly invalid. */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 223 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 224 | return 0; |
| 225 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 226 | OPENSSL_memcpy(nonce + nonce_len, in, aead->variable_nonce_len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 227 | in += aead->variable_nonce_len; |
| 228 | in_len -= aead->variable_nonce_len; |
| 229 | } else { |
| 230 | assert(aead->variable_nonce_len == 8); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 231 | OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 232 | } |
| 233 | nonce_len += aead->variable_nonce_len; |
| 234 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [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 | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 238 | for (size_t i = 0; i < aead->fixed_nonce_len; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 239 | nonce[i] ^= aead->fixed_nonce[i]; |
| 240 | } |
| 241 | } |
| 242 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -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; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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 | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 257 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 258 | aead = NULL; |
| 259 | #endif |
| 260 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 261 | if (aead == NULL) { |
| 262 | /* Handle the initial NULL cipher. */ |
| 263 | if (in_len > max_out) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 264 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 265 | return 0; |
| 266 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 267 | OPENSSL_memmove(out, in, in_len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [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; |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 283 | OPENSSL_memset(nonce, 0, nonce_len); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 284 | } else { |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 285 | OPENSSL_memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 286 | nonce_len += aead->fixed_nonce_len; |
| 287 | } |
| 288 | |
| 289 | /* Select the variable nonce. */ |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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); |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 299 | OPENSSL_memcpy(nonce + nonce_len, seqnum, aead->variable_nonce_len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 306 | assert(!aead->xor_fixed_nonce); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 307 | if (max_out < aead->variable_nonce_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 308 | OPENSSL_PUT_ERROR(SSL, SSL_R_BUFFER_TOO_SMALL); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 309 | return 0; |
| 310 | } |
| 311 | if (out < in + in_len && in < out + aead->variable_nonce_len) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 312 | OPENSSL_PUT_ERROR(SSL, SSL_R_OUTPUT_ALIASES_INPUT); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 313 | return 0; |
| 314 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 315 | OPENSSL_memcpy(out, nonce + aead->fixed_nonce_len, |
| 316 | aead->variable_nonce_len); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 317 | extra_len = aead->variable_nonce_len; |
| 318 | out += aead->variable_nonce_len; |
| 319 | max_out -= aead->variable_nonce_len; |
| 320 | } |
| 321 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [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 | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 325 | for (size_t i = 0; i < aead->fixed_nonce_len; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 326 | nonce[i] ^= aead->fixed_nonce[i]; |
| 327 | } |
| 328 | } |
| 329 | |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [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 | } |