2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "../ssl_locl.h"
12 #include "record_locl.h"
15 * tls13_enc encrypts/decrypts |n_recs| in |recs|.
18 * 0: (in non-constant time) if the record is publically invalid (i.e. too
20 * 1: if the record encryption was successful.
21 * -1: if the record's AEAD-authenticator is invalid or, if sending,
22 * an internal error occurred.
24 int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send)
27 unsigned char iv[EVP_MAX_IV_LENGTH];
28 size_t ivlen, taglen, offset, loop;
29 unsigned char *staticiv;
32 SSL3_RECORD *rec = &recs[0];
36 /* Should not happen */
37 /* TODO(TLS1.3): Support pipelining */
42 ctx = s->enc_write_ctx;
43 staticiv = s->write_iv;
44 seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
46 ctx = s->enc_read_ctx;
47 staticiv = s->read_iv;
48 seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
52 memmove(rec->data, rec->input, rec->length);
53 rec->input = rec->data;
57 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
59 if (s->early_data_state == SSL_EARLY_DATA_WRITING) {
60 alg_enc = s->session->cipher->algorithm_enc;
63 * To get here we must have selected a ciphersuite - otherwise ctx would
66 assert(s->s3->tmp.new_cipher != NULL);
67 if (s->s3->tmp.new_cipher == NULL)
69 alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
72 if (alg_enc & SSL_AESCCM) {
73 if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
74 taglen = EVP_CCM8_TLS_TAG_LEN;
76 taglen = EVP_CCM_TLS_TAG_LEN;
77 if (send && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
80 } else if (alg_enc & SSL_AESGCM) {
81 taglen = EVP_GCM_TLS_TAG_LEN;
82 } else if (alg_enc & SSL_CHACHA20) {
83 taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
90 * Take off tag. There must be at least one byte of content type as
93 if (rec->length < taglen + 1)
95 rec->length -= taglen;
99 if (ivlen < SEQ_NUM_SIZE) {
100 /* Should not happen */
103 offset = ivlen - SEQ_NUM_SIZE;
104 memcpy(iv, staticiv, offset);
105 for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
106 iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
108 /* Increment the sequence counter */
109 for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
111 if (seq[loop - 1] != 0)
115 /* Sequence has wrapped */
119 /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
120 if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, send) <= 0
121 || (!send && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
123 rec->data + rec->length) <= 0)
124 || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
125 (unsigned int)rec->length) <= 0
126 || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
127 || (size_t)(lenu + lenf) != rec->length) {
132 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
133 rec->data + rec->length) <= 0)
135 rec->length += taglen;