2 * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
10 #include "ssl_local.h"
11 #include "internal/ktls.h"
13 #ifndef OPENSSL_NO_KTLS_RX
15 * Count the number of records that were not processed yet from record boundary.
17 * This function assumes that there are only fully formed records read in the
18 * record layer. If read_ahead is enabled, then this might be false and this
21 static int count_unprocessed_records(SSL *s)
23 SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
27 if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left))
30 while (PACKET_remaining(&pkt) > 0) {
31 /* Skip record type and version */
32 if (!PACKET_forward(&pkt, 3))
35 /* Read until next record */
36 if (!PACKET_get_length_prefixed_2(&pkt, &subpkt))
46 * The kernel cannot offload receive if a partial TLS record has been read.
47 * Check the read buffer for unprocessed records. If the buffer contains a
48 * partial record, fail and return 0. Otherwise, update the sequence
49 * number at *rec_seq for the count of unprocessed records and return 1.
51 static int check_rx_read_ahead(SSL *s, unsigned char *rec_seq)
53 int bit, count_unprocessed;
55 count_unprocessed = count_unprocessed_records(s);
56 if (count_unprocessed < 0)
59 /* increment the crypto_info record sequence */
60 while (count_unprocessed) {
61 for (bit = 7; bit >= 0; bit--) { /* increment */
63 if (rec_seq[bit] != 0)
74 #if defined(__FreeBSD__)
75 # include "crypto/cryptodev.h"
78 * Check if a given cipher is supported by the KTLS interface.
79 * The kernel might still fail the setsockopt() if no suitable
80 * provider is found, but this checks if the socket option
81 * supports the cipher suite used at all.
83 int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
84 const EVP_CIPHER_CTX *dd)
97 switch (s->s3.tmp.new_cipher->algorithm_enc) {
101 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
102 case SSL_CHACHA20POLY1305:
109 switch (s->s3.tmp.new_cipher->algorithm_mac) {
122 /* Function to configure kernel TLS structure */
123 int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
124 void *rl_sequence, ktls_crypto_info_t *crypto_info,
125 int is_tx, unsigned char *iv,
126 unsigned char *key, unsigned char *mac_key,
127 size_t mac_secret_size)
129 memset(crypto_info, 0, sizeof(*crypto_info));
130 switch (s->s3.tmp.new_cipher->algorithm_enc) {
133 crypto_info->cipher_algorithm = CRYPTO_AES_NIST_GCM_16;
134 if (s->version == TLS1_3_VERSION)
135 crypto_info->iv_len = EVP_CIPHER_CTX_get_iv_length(dd);
137 crypto_info->iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
139 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
140 case SSL_CHACHA20POLY1305:
141 crypto_info->cipher_algorithm = CRYPTO_CHACHA20_POLY1305;
142 crypto_info->iv_len = EVP_CIPHER_CTX_get_iv_length(dd);
147 switch (s->s3.tmp.new_cipher->algorithm_mac) {
149 crypto_info->auth_algorithm = CRYPTO_SHA1_HMAC;
152 crypto_info->auth_algorithm = CRYPTO_SHA2_256_HMAC;
155 crypto_info->auth_algorithm = CRYPTO_SHA2_384_HMAC;
160 crypto_info->cipher_algorithm = CRYPTO_AES_CBC;
161 crypto_info->iv_len = EVP_CIPHER_get_iv_length(c);
162 crypto_info->auth_key = mac_key;
163 crypto_info->auth_key_len = mac_secret_size;
168 crypto_info->cipher_key = key;
169 crypto_info->cipher_key_len = EVP_CIPHER_get_key_length(c);
170 crypto_info->iv = iv;
171 crypto_info->tls_vmajor = (s->version >> 8) & 0x000000ff;
172 crypto_info->tls_vminor = (s->version & 0x000000ff);
173 # ifdef TCP_RXTLS_ENABLE
174 memcpy(crypto_info->rec_seq, rl_sequence, sizeof(crypto_info->rec_seq));
175 if (!is_tx && !check_rx_read_ahead(s, crypto_info->rec_seq))
184 #endif /* __FreeBSD__ */
186 #if defined(OPENSSL_SYS_LINUX)
188 /* Function to check supported ciphers in Linux */
189 int ktls_check_supported_cipher(const SSL *s, const EVP_CIPHER *c,
190 const EVP_CIPHER_CTX *dd)
192 switch (s->version) {
200 /* check that cipher is AES_GCM_128, AES_GCM_256, AES_CCM_128
201 * or Chacha20-Poly1305
203 # ifdef OPENSSL_KTLS_AES_CCM_128
204 if (EVP_CIPHER_is_a(c, "AES-128-CCM")) {
205 if (s->version == TLS_1_3_VERSION /* broken on 5.x kernels */
206 || EVP_CIPHER_CTX_get_tag_length(dd) != EVP_CCM_TLS_TAG_LEN)
212 # ifdef OPENSSL_KTLS_AES_GCM_128
213 || EVP_CIPHER_is_a(c, "AES-128-GCM")
215 # ifdef OPENSSL_KTLS_AES_GCM_256
216 || EVP_CIPHER_is_a(c, "AES-256-GCM")
218 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
219 || EVP_CIPHER_is_a(c, "ChaCha20-Poly1305")
227 /* Function to configure kernel TLS structure */
228 int ktls_configure_crypto(SSL *s, const EVP_CIPHER *c, EVP_CIPHER_CTX *dd,
229 void *rl_sequence, ktls_crypto_info_t *crypto_info,
230 int is_tx, unsigned char *iv,
231 unsigned char *key, unsigned char *mac_key,
232 size_t mac_secret_size)
234 unsigned char geniv[12];
235 unsigned char *iiv = iv;
237 # ifdef OPENSSL_NO_KTLS_RX
242 if (s->version == TLS1_2_VERSION &&
243 EVP_CIPHER_get_mode(c) == EVP_CIPH_GCM_MODE) {
244 if (!EVP_CIPHER_CTX_get_updated_iv(dd, geniv,
245 EVP_GCM_TLS_FIXED_IV_LEN
246 + EVP_GCM_TLS_EXPLICIT_IV_LEN))
251 memset(crypto_info, 0, sizeof(*crypto_info));
252 switch (EVP_CIPHER_get_nid(c))
254 # ifdef OPENSSL_KTLS_AES_GCM_128
255 case NID_aes_128_gcm:
256 crypto_info->gcm128.info.cipher_type = TLS_CIPHER_AES_GCM_128;
257 crypto_info->gcm128.info.version = s->version;
258 crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm128);
259 memcpy(crypto_info->gcm128.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
260 TLS_CIPHER_AES_GCM_128_IV_SIZE);
261 memcpy(crypto_info->gcm128.salt, iiv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
262 memcpy(crypto_info->gcm128.key, key, EVP_CIPHER_get_key_length(c));
263 memcpy(crypto_info->gcm128.rec_seq, rl_sequence,
264 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
265 if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm128.rec_seq))
269 # ifdef OPENSSL_KTLS_AES_GCM_256
270 case NID_aes_256_gcm:
271 crypto_info->gcm256.info.cipher_type = TLS_CIPHER_AES_GCM_256;
272 crypto_info->gcm256.info.version = s->version;
273 crypto_info->tls_crypto_info_len = sizeof(crypto_info->gcm256);
274 memcpy(crypto_info->gcm256.iv, iiv + EVP_GCM_TLS_FIXED_IV_LEN,
275 TLS_CIPHER_AES_GCM_256_IV_SIZE);
276 memcpy(crypto_info->gcm256.salt, iiv, TLS_CIPHER_AES_GCM_256_SALT_SIZE);
277 memcpy(crypto_info->gcm256.key, key, EVP_CIPHER_get_key_length(c));
278 memcpy(crypto_info->gcm256.rec_seq, rl_sequence,
279 TLS_CIPHER_AES_GCM_256_REC_SEQ_SIZE);
280 if (!is_tx && !check_rx_read_ahead(s, crypto_info->gcm256.rec_seq))
284 # ifdef OPENSSL_KTLS_AES_CCM_128
285 case NID_aes_128_ccm:
286 crypto_info->ccm128.info.cipher_type = TLS_CIPHER_AES_CCM_128;
287 crypto_info->ccm128.info.version = s->version;
288 crypto_info->tls_crypto_info_len = sizeof(crypto_info->ccm128);
289 memcpy(crypto_info->ccm128.iv, iiv + EVP_CCM_TLS_FIXED_IV_LEN,
290 TLS_CIPHER_AES_CCM_128_IV_SIZE);
291 memcpy(crypto_info->ccm128.salt, iiv, TLS_CIPHER_AES_CCM_128_SALT_SIZE);
292 memcpy(crypto_info->ccm128.key, key, EVP_CIPHER_get_key_length(c));
293 memcpy(crypto_info->ccm128.rec_seq, rl_sequence,
294 TLS_CIPHER_AES_CCM_128_REC_SEQ_SIZE);
295 if (!is_tx && !check_rx_read_ahead(s, crypto_info->ccm128.rec_seq))
299 # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
300 case NID_chacha20_poly1305:
301 crypto_info->chacha20poly1305.info.cipher_type = TLS_CIPHER_CHACHA20_POLY1305;
302 crypto_info->chacha20poly1305.info.version = s->version;
303 crypto_info->tls_crypto_info_len = sizeof(crypto_info->chacha20poly1305);
304 memcpy(crypto_info->chacha20poly1305.iv, iiv,
305 TLS_CIPHER_CHACHA20_POLY1305_IV_SIZE);
306 memcpy(crypto_info->chacha20poly1305.key, key,
307 EVP_CIPHER_get_key_length(c));
308 memcpy(crypto_info->chacha20poly1305.rec_seq, rl_sequence,
309 TLS_CIPHER_CHACHA20_POLY1305_REC_SEQ_SIZE);
311 && !check_rx_read_ahead(s,
312 crypto_info->chacha20poly1305.rec_seq))
322 #endif /* OPENSSL_SYS_LINUX */