]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/record/ssl3_record_tls13.c
Update copyright year
[thirdparty/openssl.git] / ssl / record / ssl3_record_tls13.c
CommitLineData
bebc0c7d 1/*
b0edda11 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
bebc0c7d
MC
3 *
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
8 */
9
10#include "../ssl_locl.h"
11#include "record_locl.h"
67dc995e 12#include "internal/cryptlib.h"
bebc0c7d
MC
13
14/*-
921d84a0
MC
15 * tls13_enc encrypts/decrypts |n_recs| in |recs|. Will call SSLfatal() for
16 * internal errors, but not otherwise.
bebc0c7d
MC
17 *
18 * Returns:
19 * 0: (in non-constant time) if the record is publically invalid (i.e. too
20 * short etc).
21 * 1: if the record encryption was successful.
22 * -1: if the record's AEAD-authenticator is invalid or, if sending,
23 * an internal error occurred.
24 */
d1186c30 25int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
bebc0c7d
MC
26{
27 EVP_CIPHER_CTX *ctx;
3295d242
MC
28 unsigned char iv[EVP_MAX_IV_LENGTH], recheader[SSL3_RT_HEADER_LENGTH];
29 size_t ivlen, taglen, offset, loop, hdrlen;
bebc0c7d
MC
30 unsigned char *staticiv;
31 unsigned char *seq;
32 int lenu, lenf;
33 SSL3_RECORD *rec = &recs[0];
18b3a80a 34 uint32_t alg_enc;
3295d242 35 WPACKET wpkt;
bebc0c7d
MC
36
37 if (n_recs != 1) {
38 /* Should not happen */
39 /* TODO(TLS1.3): Support pipelining */
921d84a0
MC
40 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
41 ERR_R_INTERNAL_ERROR);
bebc0c7d
MC
42 return -1;
43 }
44
d1186c30 45 if (sending) {
bebc0c7d
MC
46 ctx = s->enc_write_ctx;
47 staticiv = s->write_iv;
48 seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
49 } else {
50 ctx = s->enc_read_ctx;
51 staticiv = s->read_iv;
52 seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
53 }
54
55 if (ctx == NULL) {
56 memmove(rec->data, rec->input, rec->length);
57 rec->input = rec->data;
58 return 1;
59 }
18b3a80a 60
bebc0c7d
MC
61 ivlen = EVP_CIPHER_CTX_iv_length(ctx);
62
ef6c191b
MC
63 if (s->early_data_state == SSL_EARLY_DATA_WRITING
64 || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
0ef28021 65 if (s->session != NULL && s->session->ext.max_early_data > 0) {
add8d0e9 66 alg_enc = s->session->cipher->algorithm_enc;
0ef28021
MC
67 } else {
68 if (!ossl_assert(s->psksession != NULL
921d84a0
MC
69 && s->psksession->ext.max_early_data > 0)) {
70 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
71 ERR_R_INTERNAL_ERROR);
0ef28021 72 return -1;
921d84a0 73 }
add8d0e9 74 alg_enc = s->psksession->cipher->algorithm_enc;
0ef28021 75 }
49e7fe12
MC
76 } else {
77 /*
78 * To get here we must have selected a ciphersuite - otherwise ctx would
79 * be NULL
80 */
921d84a0
MC
81 if (!ossl_assert(s->s3->tmp.new_cipher != NULL)) {
82 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
83 ERR_R_INTERNAL_ERROR);
49e7fe12 84 return -1;
921d84a0 85 }
49e7fe12
MC
86 alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
87 }
18b3a80a 88
20fc2051
DSH
89 if (alg_enc & SSL_AESCCM) {
90 if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
ec07b1d8
DSH
91 taglen = EVP_CCM8_TLS_TAG_LEN;
92 else
93 taglen = EVP_CCM_TLS_TAG_LEN;
d1186c30 94 if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
921d84a0
MC
95 NULL) <= 0) {
96 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
97 ERR_R_INTERNAL_ERROR);
ec07b1d8 98 return -1;
921d84a0 99 }
20fc2051 100 } else if (alg_enc & SSL_AESGCM) {
ec07b1d8 101 taglen = EVP_GCM_TLS_TAG_LEN;
20fc2051
DSH
102 } else if (alg_enc & SSL_CHACHA20) {
103 taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
104 } else {
921d84a0
MC
105 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
106 ERR_R_INTERNAL_ERROR);
20fc2051 107 return -1;
ec07b1d8
DSH
108 }
109
d1186c30 110 if (!sending) {
bebc0c7d
MC
111 /*
112 * Take off tag. There must be at least one byte of content type as
113 * well as the tag
114 */
ec07b1d8 115 if (rec->length < taglen + 1)
bebc0c7d 116 return 0;
ec07b1d8 117 rec->length -= taglen;
bebc0c7d
MC
118 }
119
120 /* Set up IV */
121 if (ivlen < SEQ_NUM_SIZE) {
122 /* Should not happen */
921d84a0
MC
123 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
124 ERR_R_INTERNAL_ERROR);
bebc0c7d
MC
125 return -1;
126 }
127 offset = ivlen - SEQ_NUM_SIZE;
128 memcpy(iv, staticiv, offset);
129 for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
130 iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
131
ac77aa9a
MC
132 /* Increment the sequence counter */
133 for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
134 ++seq[loop - 1];
135 if (seq[loop - 1] != 0)
136 break;
137 }
138 if (loop == 0) {
139 /* Sequence has wrapped */
140 return -1;
141 }
142
6606d600 143 /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
d1186c30
TS
144 if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
145 || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
ec07b1d8 146 taglen,
3295d242
MC
147 rec->data + rec->length) <= 0)) {
148 return -1;
149 }
150
151 /* Set up the AAD */
152 if (!WPACKET_init_static_len(&wpkt, recheader, sizeof(recheader), 0)
153 || !WPACKET_put_bytes_u8(&wpkt, rec->type)
154 || !WPACKET_put_bytes_u16(&wpkt, rec->rec_version)
155 || !WPACKET_put_bytes_u16(&wpkt, rec->length + taglen)
156 || !WPACKET_get_total_written(&wpkt, &hdrlen)
157 || hdrlen != SSL3_RT_HEADER_LENGTH
158 || !WPACKET_finish(&wpkt)) {
159 WPACKET_cleanup(&wpkt);
160 return -1;
161 }
162
163 /*
164 * For CCM we must explicitly set the total plaintext length before we add
165 * any AAD.
166 */
167 if (((alg_enc & SSL_AESCCM) != 0
168 && EVP_CipherUpdate(ctx, NULL, &lenu, NULL,
169 (unsigned int)rec->length) <= 0)
170 || EVP_CipherUpdate(ctx, NULL, &lenu, recheader,
171 sizeof(recheader)) <= 0
ec07b1d8
DSH
172 || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
173 (unsigned int)rec->length) <= 0
bebc0c7d
MC
174 || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
175 || (size_t)(lenu + lenf) != rec->length) {
176 return -1;
177 }
d1186c30 178 if (sending) {
bebc0c7d 179 /* Add the tag */
ec07b1d8 180 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
921d84a0
MC
181 rec->data + rec->length) <= 0) {
182 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_ENC,
183 ERR_R_INTERNAL_ERROR);
bebc0c7d 184 return -1;
921d84a0 185 }
ec07b1d8 186 rec->length += taglen;
bebc0c7d
MC
187 }
188
189 return 1;
190}