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