Add CCM mode support for TLS 1.3
[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 send)
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
33     if (n_recs != 1) {
34         /* Should not happen */
35         /* TODO(TLS1.3): Support pipelining */
36         return -1;
37     }
38
39     if (send) {
40         ctx = s->enc_write_ctx;
41         staticiv = s->write_iv;
42         seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
43     } else {
44         ctx = s->enc_read_ctx;
45         staticiv = s->read_iv;
46         seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
47     }
48
49     if (ctx == NULL) {
50         memmove(rec->data, rec->input, rec->length);
51         rec->input = rec->data;
52         return 1;
53     }
54     ivlen = EVP_CIPHER_CTX_iv_length(ctx);
55
56     if (EVP_CIPHER_CTX_mode(ctx) == EVP_CIPH_CCM_MODE) {
57         if (s->s3->tmp.new_cipher->algorithm_enc
58                 & (SSL_AES128CCM8 | SSL_AES256CCM8))
59             taglen = EVP_CCM8_TLS_TAG_LEN;
60          else
61             taglen = EVP_CCM_TLS_TAG_LEN;
62          if (send && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
63                                          NULL) <= 0)
64             return -1;
65     } else {
66         taglen = EVP_GCM_TLS_TAG_LEN;
67     }
68
69     if (!send) {
70         /*
71          * Take off tag. There must be at least one byte of content type as
72          * well as the tag
73          */
74         if (rec->length < taglen + 1)
75             return 0;
76         rec->length -= taglen;
77     }
78
79     /* Set up IV */
80     if (ivlen < SEQ_NUM_SIZE) {
81         /* Should not happen */
82         return -1;
83     }
84     offset = ivlen - SEQ_NUM_SIZE;
85     memcpy(iv, staticiv, offset);
86     for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
87         iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
88
89     /* Increment the sequence counter */
90     for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
91         ++seq[loop - 1];
92         if (seq[loop - 1] != 0)
93             break;
94     }
95     if (loop == 0) {
96         /* Sequence has wrapped */
97         return -1;
98     }
99
100     /* TODO(size_t): lenu/lenf should be a size_t but EVP doesn't support it */
101     if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, send) <= 0
102             || (!send && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
103                                              taglen,
104                                              rec->data + rec->length) <= 0)
105             || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
106                                 (unsigned int)rec->length) <= 0
107             || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
108             || (size_t)(lenu + lenf) != rec->length) {
109         return -1;
110     }
111     if (send) {
112         /* Add the tag */
113         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
114                                 rec->data + rec->length) <= 0)
115             return -1;
116         rec->length += taglen;
117     }
118
119     return 1;
120 }