61e209254ebf0c2c26bd20e0e1aac8609237380c
[openssl.git] / ssl / record / ssl3_record_tls13.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
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"
12
13 /*-
14  * tls13_enc encrypts/decrypts |n_recs| in |recs|.
15  *
16  * Returns:
17  *    0: (in non-constant time) if the record is publically invalid (i.e. too
18  *        short etc).
19  *    1: if the record encryption was successful.
20  *   -1: if the record's AEAD-authenticator is invalid or, if sending,
21  *       an internal error occurred.
22  */
23 int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
24 {
25     EVP_CIPHER_CTX *ctx;
26     unsigned char iv[EVP_MAX_IV_LENGTH];
27     size_t ivlen, taglen, offset, loop;
28     unsigned char *staticiv;
29     unsigned char *seq;
30     int lenu, lenf;
31     SSL3_RECORD *rec = &recs[0];
32     uint32_t alg_enc;
33
34     if (n_recs != 1) {
35         /* Should not happen */
36         /* TODO(TLS1.3): Support pipelining */
37         return -1;
38     }
39
40     if (sending) {
41         ctx = s->enc_write_ctx;
42         staticiv = s->write_iv;
43         seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
44     } else {
45         ctx = s->enc_read_ctx;
46         staticiv = s->read_iv;
47         seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
48     }
49
50     if (ctx == NULL) {
51         memmove(rec->data, rec->input, rec->length);
52         rec->input = rec->data;
53         return 1;
54     }
55
56     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
57
58     if (s->early_data_state == SSL_EARLY_DATA_WRITING
59             || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
60         alg_enc = s->session->cipher->algorithm_enc;
61     } else {
62         /*
63          * To get here we must have selected a ciphersuite - otherwise ctx would
64          * be NULL
65          */
66         if (!ossl_assert(s->s3->tmp.new_cipher != NULL))
67             return -1;
68         alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
69     }
70
71     if (alg_enc & SSL_AESCCM) {
72         if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
73             taglen = EVP_CCM8_TLS_TAG_LEN;
74          else
75             taglen = EVP_CCM_TLS_TAG_LEN;
76          if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
77                                          NULL) <= 0)
78             return -1;
79     } else if (alg_enc & SSL_AESGCM) {
80         taglen = EVP_GCM_TLS_TAG_LEN;
81     } else if (alg_enc & SSL_CHACHA20) {
82         taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
83     } else {
84         return -1;
85     }
86
87     if (!sending) {
88         /*
89          * Take off tag. There must be at least one byte of content type as
90          * well as the tag
91          */
92         if (rec->length < taglen + 1)
93             return 0;
94         rec->length -= taglen;
95     }
96
97     /* Set up IV */
98     if (ivlen < SEQ_NUM_SIZE) {
99         /* Should not happen */
100         return -1;
101     }
102     offset = ivlen - SEQ_NUM_SIZE;
103     memcpy(iv, staticiv, offset);
104     for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
105         iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
106
107     /* Increment the sequence counter */
108     for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
109         ++seq[loop - 1];
110         if (seq[loop - 1] != 0)
111             break;
112     }
113     if (loop == 0) {
114         /* Sequence has wrapped */
115         return -1;
116     }
117
118     /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
119     if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
120             || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
121                                              taglen,
122                                              rec->data + rec->length) <= 0)
123             || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
124                                 (unsigned int)rec->length) <= 0
125             || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
126             || (size_t)(lenu + lenf) != rec->length) {
127         return -1;
128     }
129     if (sending) {
130         /* Add the tag */
131         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
132                                 rec->data + rec->length) <= 0)
133             return -1;
134         rec->length += taglen;
135     }
136
137     return 1;
138 }