162306a36Sopenharmony_ci/* 262306a36Sopenharmony_ci * Copyright (c) 2016 Tom Herbert <tom@herbertland.com> 362306a36Sopenharmony_ci * Copyright (c) 2016-2017, Mellanox Technologies. All rights reserved. 462306a36Sopenharmony_ci * Copyright (c) 2016-2017, Dave Watson <davejwatson@fb.com>. All rights reserved. 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * This software is available to you under a choice of one of two 762306a36Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 862306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 962306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the 1062306a36Sopenharmony_ci * OpenIB.org BSD license below: 1162306a36Sopenharmony_ci * 1262306a36Sopenharmony_ci * Redistribution and use in source and binary forms, with or 1362306a36Sopenharmony_ci * without modification, are permitted provided that the following 1462306a36Sopenharmony_ci * conditions are met: 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * - Redistributions of source code must retain the above 1762306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 1862306a36Sopenharmony_ci * disclaimer. 1962306a36Sopenharmony_ci * 2062306a36Sopenharmony_ci * - Redistributions in binary form must reproduce the above 2162306a36Sopenharmony_ci * copyright notice, this list of conditions and the following 2262306a36Sopenharmony_ci * disclaimer in the documentation and/or other materials 2362306a36Sopenharmony_ci * provided with the distribution. 2462306a36Sopenharmony_ci * 2562306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 2662306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 2762306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 2862306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 2962306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 3062306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 3162306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 3262306a36Sopenharmony_ci * SOFTWARE. 3362306a36Sopenharmony_ci */ 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#ifndef _TLS_INT_H 3662306a36Sopenharmony_ci#define _TLS_INT_H 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci#include <asm/byteorder.h> 3962306a36Sopenharmony_ci#include <linux/types.h> 4062306a36Sopenharmony_ci#include <linux/skmsg.h> 4162306a36Sopenharmony_ci#include <net/tls.h> 4262306a36Sopenharmony_ci#include <net/tls_prot.h> 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci#define TLS_PAGE_ORDER (min_t(unsigned int, PAGE_ALLOC_COSTLY_ORDER, \ 4562306a36Sopenharmony_ci TLS_MAX_PAYLOAD_SIZE >> PAGE_SHIFT)) 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci#define __TLS_INC_STATS(net, field) \ 4862306a36Sopenharmony_ci __SNMP_INC_STATS((net)->mib.tls_statistics, field) 4962306a36Sopenharmony_ci#define TLS_INC_STATS(net, field) \ 5062306a36Sopenharmony_ci SNMP_INC_STATS((net)->mib.tls_statistics, field) 5162306a36Sopenharmony_ci#define TLS_DEC_STATS(net, field) \ 5262306a36Sopenharmony_ci SNMP_DEC_STATS((net)->mib.tls_statistics, field) 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_cistruct tls_cipher_desc { 5562306a36Sopenharmony_ci unsigned int nonce; 5662306a36Sopenharmony_ci unsigned int iv; 5762306a36Sopenharmony_ci unsigned int key; 5862306a36Sopenharmony_ci unsigned int salt; 5962306a36Sopenharmony_ci unsigned int tag; 6062306a36Sopenharmony_ci unsigned int rec_seq; 6162306a36Sopenharmony_ci unsigned int iv_offset; 6262306a36Sopenharmony_ci unsigned int key_offset; 6362306a36Sopenharmony_ci unsigned int salt_offset; 6462306a36Sopenharmony_ci unsigned int rec_seq_offset; 6562306a36Sopenharmony_ci char *cipher_name; 6662306a36Sopenharmony_ci bool offloadable; 6762306a36Sopenharmony_ci size_t crypto_info; 6862306a36Sopenharmony_ci}; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci#define TLS_CIPHER_MIN TLS_CIPHER_AES_GCM_128 7162306a36Sopenharmony_ci#define TLS_CIPHER_MAX TLS_CIPHER_ARIA_GCM_256 7262306a36Sopenharmony_ciextern const struct tls_cipher_desc tls_cipher_desc[TLS_CIPHER_MAX + 1 - TLS_CIPHER_MIN]; 7362306a36Sopenharmony_ci 7462306a36Sopenharmony_cistatic inline const struct tls_cipher_desc *get_cipher_desc(u16 cipher_type) 7562306a36Sopenharmony_ci{ 7662306a36Sopenharmony_ci if (cipher_type < TLS_CIPHER_MIN || cipher_type > TLS_CIPHER_MAX) 7762306a36Sopenharmony_ci return NULL; 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_ci return &tls_cipher_desc[cipher_type - TLS_CIPHER_MIN]; 8062306a36Sopenharmony_ci} 8162306a36Sopenharmony_ci 8262306a36Sopenharmony_cistatic inline char *crypto_info_iv(struct tls_crypto_info *crypto_info, 8362306a36Sopenharmony_ci const struct tls_cipher_desc *cipher_desc) 8462306a36Sopenharmony_ci{ 8562306a36Sopenharmony_ci return (char *)crypto_info + cipher_desc->iv_offset; 8662306a36Sopenharmony_ci} 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_cistatic inline char *crypto_info_key(struct tls_crypto_info *crypto_info, 8962306a36Sopenharmony_ci const struct tls_cipher_desc *cipher_desc) 9062306a36Sopenharmony_ci{ 9162306a36Sopenharmony_ci return (char *)crypto_info + cipher_desc->key_offset; 9262306a36Sopenharmony_ci} 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_cistatic inline char *crypto_info_salt(struct tls_crypto_info *crypto_info, 9562306a36Sopenharmony_ci const struct tls_cipher_desc *cipher_desc) 9662306a36Sopenharmony_ci{ 9762306a36Sopenharmony_ci return (char *)crypto_info + cipher_desc->salt_offset; 9862306a36Sopenharmony_ci} 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_cistatic inline char *crypto_info_rec_seq(struct tls_crypto_info *crypto_info, 10162306a36Sopenharmony_ci const struct tls_cipher_desc *cipher_desc) 10262306a36Sopenharmony_ci{ 10362306a36Sopenharmony_ci return (char *)crypto_info + cipher_desc->rec_seq_offset; 10462306a36Sopenharmony_ci} 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci/* TLS records are maintained in 'struct tls_rec'. It stores the memory pages 10862306a36Sopenharmony_ci * allocated or mapped for each TLS record. After encryption, the records are 10962306a36Sopenharmony_ci * stores in a linked list. 11062306a36Sopenharmony_ci */ 11162306a36Sopenharmony_cistruct tls_rec { 11262306a36Sopenharmony_ci struct list_head list; 11362306a36Sopenharmony_ci int tx_ready; 11462306a36Sopenharmony_ci int tx_flags; 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci struct sk_msg msg_plaintext; 11762306a36Sopenharmony_ci struct sk_msg msg_encrypted; 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci /* AAD | msg_plaintext.sg.data | sg_tag */ 12062306a36Sopenharmony_ci struct scatterlist sg_aead_in[2]; 12162306a36Sopenharmony_ci /* AAD | msg_encrypted.sg.data (data contains overhead for hdr & iv & tag) */ 12262306a36Sopenharmony_ci struct scatterlist sg_aead_out[2]; 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci char content_type; 12562306a36Sopenharmony_ci struct scatterlist sg_content_type; 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci struct sock *sk; 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci char aad_space[TLS_AAD_SPACE_SIZE]; 13062306a36Sopenharmony_ci u8 iv_data[MAX_IV_SIZE]; 13162306a36Sopenharmony_ci struct aead_request aead_req; 13262306a36Sopenharmony_ci u8 aead_req_ctx[]; 13362306a36Sopenharmony_ci}; 13462306a36Sopenharmony_ci 13562306a36Sopenharmony_ciint __net_init tls_proc_init(struct net *net); 13662306a36Sopenharmony_civoid __net_exit tls_proc_fini(struct net *net); 13762306a36Sopenharmony_ci 13862306a36Sopenharmony_cistruct tls_context *tls_ctx_create(struct sock *sk); 13962306a36Sopenharmony_civoid tls_ctx_free(struct sock *sk, struct tls_context *ctx); 14062306a36Sopenharmony_civoid update_sk_prot(struct sock *sk, struct tls_context *ctx); 14162306a36Sopenharmony_ci 14262306a36Sopenharmony_ciint wait_on_pending_writer(struct sock *sk, long *timeo); 14362306a36Sopenharmony_civoid tls_err_abort(struct sock *sk, int err); 14462306a36Sopenharmony_ci 14562306a36Sopenharmony_ciint tls_set_sw_offload(struct sock *sk, struct tls_context *ctx, int tx); 14662306a36Sopenharmony_civoid tls_update_rx_zc_capable(struct tls_context *tls_ctx); 14762306a36Sopenharmony_civoid tls_sw_strparser_arm(struct sock *sk, struct tls_context *ctx); 14862306a36Sopenharmony_civoid tls_sw_strparser_done(struct tls_context *tls_ctx); 14962306a36Sopenharmony_ciint tls_sw_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); 15062306a36Sopenharmony_civoid tls_sw_splice_eof(struct socket *sock); 15162306a36Sopenharmony_civoid tls_sw_cancel_work_tx(struct tls_context *tls_ctx); 15262306a36Sopenharmony_civoid tls_sw_release_resources_tx(struct sock *sk); 15362306a36Sopenharmony_civoid tls_sw_free_ctx_tx(struct tls_context *tls_ctx); 15462306a36Sopenharmony_civoid tls_sw_free_resources_rx(struct sock *sk); 15562306a36Sopenharmony_civoid tls_sw_release_resources_rx(struct sock *sk); 15662306a36Sopenharmony_civoid tls_sw_free_ctx_rx(struct tls_context *tls_ctx); 15762306a36Sopenharmony_ciint tls_sw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len, 15862306a36Sopenharmony_ci int flags, int *addr_len); 15962306a36Sopenharmony_cibool tls_sw_sock_is_readable(struct sock *sk); 16062306a36Sopenharmony_cissize_t tls_sw_splice_read(struct socket *sock, loff_t *ppos, 16162306a36Sopenharmony_ci struct pipe_inode_info *pipe, 16262306a36Sopenharmony_ci size_t len, unsigned int flags); 16362306a36Sopenharmony_ciint tls_sw_read_sock(struct sock *sk, read_descriptor_t *desc, 16462306a36Sopenharmony_ci sk_read_actor_t read_actor); 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ciint tls_device_sendmsg(struct sock *sk, struct msghdr *msg, size_t size); 16762306a36Sopenharmony_civoid tls_device_splice_eof(struct socket *sock); 16862306a36Sopenharmony_ciint tls_tx_records(struct sock *sk, int flags); 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_civoid tls_sw_write_space(struct sock *sk, struct tls_context *ctx); 17162306a36Sopenharmony_civoid tls_device_write_space(struct sock *sk, struct tls_context *ctx); 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ciint tls_process_cmsg(struct sock *sk, struct msghdr *msg, 17462306a36Sopenharmony_ci unsigned char *record_type); 17562306a36Sopenharmony_ciint decrypt_skb(struct sock *sk, struct scatterlist *sgout); 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ciint tls_sw_fallback_init(struct sock *sk, 17862306a36Sopenharmony_ci struct tls_offload_context_tx *offload_ctx, 17962306a36Sopenharmony_ci struct tls_crypto_info *crypto_info); 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ciint tls_strp_dev_init(void); 18262306a36Sopenharmony_civoid tls_strp_dev_exit(void); 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_civoid tls_strp_done(struct tls_strparser *strp); 18562306a36Sopenharmony_civoid tls_strp_stop(struct tls_strparser *strp); 18662306a36Sopenharmony_ciint tls_strp_init(struct tls_strparser *strp, struct sock *sk); 18762306a36Sopenharmony_civoid tls_strp_data_ready(struct tls_strparser *strp); 18862306a36Sopenharmony_ci 18962306a36Sopenharmony_civoid tls_strp_check_rcv(struct tls_strparser *strp); 19062306a36Sopenharmony_civoid tls_strp_msg_done(struct tls_strparser *strp); 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ciint tls_rx_msg_size(struct tls_strparser *strp, struct sk_buff *skb); 19362306a36Sopenharmony_civoid tls_rx_msg_ready(struct tls_strparser *strp); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_civoid tls_strp_msg_load(struct tls_strparser *strp, bool force_refresh); 19662306a36Sopenharmony_ciint tls_strp_msg_cow(struct tls_sw_context_rx *ctx); 19762306a36Sopenharmony_cistruct sk_buff *tls_strp_msg_detach(struct tls_sw_context_rx *ctx); 19862306a36Sopenharmony_ciint tls_strp_msg_hold(struct tls_strparser *strp, struct sk_buff_head *dst); 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_cistatic inline struct tls_msg *tls_msg(struct sk_buff *skb) 20162306a36Sopenharmony_ci{ 20262306a36Sopenharmony_ci struct sk_skb_cb *scb = (struct sk_skb_cb *)skb->cb; 20362306a36Sopenharmony_ci 20462306a36Sopenharmony_ci return &scb->tls; 20562306a36Sopenharmony_ci} 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_cistatic inline struct sk_buff *tls_strp_msg(struct tls_sw_context_rx *ctx) 20862306a36Sopenharmony_ci{ 20962306a36Sopenharmony_ci DEBUG_NET_WARN_ON_ONCE(!ctx->strp.msg_ready || !ctx->strp.anchor->len); 21062306a36Sopenharmony_ci return ctx->strp.anchor; 21162306a36Sopenharmony_ci} 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_cistatic inline bool tls_strp_msg_ready(struct tls_sw_context_rx *ctx) 21462306a36Sopenharmony_ci{ 21562306a36Sopenharmony_ci return ctx->strp.msg_ready; 21662306a36Sopenharmony_ci} 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_cistatic inline bool tls_strp_msg_mixed_decrypted(struct tls_sw_context_rx *ctx) 21962306a36Sopenharmony_ci{ 22062306a36Sopenharmony_ci return ctx->strp.mixed_decrypted; 22162306a36Sopenharmony_ci} 22262306a36Sopenharmony_ci 22362306a36Sopenharmony_ci#ifdef CONFIG_TLS_DEVICE 22462306a36Sopenharmony_ciint tls_device_init(void); 22562306a36Sopenharmony_civoid tls_device_cleanup(void); 22662306a36Sopenharmony_ciint tls_set_device_offload(struct sock *sk, struct tls_context *ctx); 22762306a36Sopenharmony_civoid tls_device_free_resources_tx(struct sock *sk); 22862306a36Sopenharmony_ciint tls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx); 22962306a36Sopenharmony_civoid tls_device_offload_cleanup_rx(struct sock *sk); 23062306a36Sopenharmony_civoid tls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq); 23162306a36Sopenharmony_ciint tls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx); 23262306a36Sopenharmony_ci#else 23362306a36Sopenharmony_cistatic inline int tls_device_init(void) { return 0; } 23462306a36Sopenharmony_cistatic inline void tls_device_cleanup(void) {} 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_cistatic inline int 23762306a36Sopenharmony_citls_set_device_offload(struct sock *sk, struct tls_context *ctx) 23862306a36Sopenharmony_ci{ 23962306a36Sopenharmony_ci return -EOPNOTSUPP; 24062306a36Sopenharmony_ci} 24162306a36Sopenharmony_ci 24262306a36Sopenharmony_cistatic inline void tls_device_free_resources_tx(struct sock *sk) {} 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_cistatic inline int 24562306a36Sopenharmony_citls_set_device_offload_rx(struct sock *sk, struct tls_context *ctx) 24662306a36Sopenharmony_ci{ 24762306a36Sopenharmony_ci return -EOPNOTSUPP; 24862306a36Sopenharmony_ci} 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_cistatic inline void tls_device_offload_cleanup_rx(struct sock *sk) {} 25162306a36Sopenharmony_cistatic inline void 25262306a36Sopenharmony_citls_device_rx_resync_new_rec(struct sock *sk, u32 rcd_len, u32 seq) {} 25362306a36Sopenharmony_ci 25462306a36Sopenharmony_cistatic inline int 25562306a36Sopenharmony_citls_device_decrypted(struct sock *sk, struct tls_context *tls_ctx) 25662306a36Sopenharmony_ci{ 25762306a36Sopenharmony_ci return 0; 25862306a36Sopenharmony_ci} 25962306a36Sopenharmony_ci#endif 26062306a36Sopenharmony_ci 26162306a36Sopenharmony_ciint tls_push_sg(struct sock *sk, struct tls_context *ctx, 26262306a36Sopenharmony_ci struct scatterlist *sg, u16 first_offset, 26362306a36Sopenharmony_ci int flags); 26462306a36Sopenharmony_ciint tls_push_partial_record(struct sock *sk, struct tls_context *ctx, 26562306a36Sopenharmony_ci int flags); 26662306a36Sopenharmony_civoid tls_free_partial_record(struct sock *sk, struct tls_context *ctx); 26762306a36Sopenharmony_ci 26862306a36Sopenharmony_cistatic inline bool tls_is_partially_sent_record(struct tls_context *ctx) 26962306a36Sopenharmony_ci{ 27062306a36Sopenharmony_ci return !!ctx->partially_sent_record; 27162306a36Sopenharmony_ci} 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_cistatic inline bool tls_is_pending_open_record(struct tls_context *tls_ctx) 27462306a36Sopenharmony_ci{ 27562306a36Sopenharmony_ci return tls_ctx->pending_open_record_frags; 27662306a36Sopenharmony_ci} 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_cistatic inline bool tls_bigint_increment(unsigned char *seq, int len) 27962306a36Sopenharmony_ci{ 28062306a36Sopenharmony_ci int i; 28162306a36Sopenharmony_ci 28262306a36Sopenharmony_ci for (i = len - 1; i >= 0; i--) { 28362306a36Sopenharmony_ci ++seq[i]; 28462306a36Sopenharmony_ci if (seq[i] != 0) 28562306a36Sopenharmony_ci break; 28662306a36Sopenharmony_ci } 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci return (i == -1); 28962306a36Sopenharmony_ci} 29062306a36Sopenharmony_ci 29162306a36Sopenharmony_cistatic inline void tls_bigint_subtract(unsigned char *seq, int n) 29262306a36Sopenharmony_ci{ 29362306a36Sopenharmony_ci u64 rcd_sn; 29462306a36Sopenharmony_ci __be64 *p; 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci BUILD_BUG_ON(TLS_MAX_REC_SEQ_SIZE != 8); 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci p = (__be64 *)seq; 29962306a36Sopenharmony_ci rcd_sn = be64_to_cpu(*p); 30062306a36Sopenharmony_ci *p = cpu_to_be64(rcd_sn - n); 30162306a36Sopenharmony_ci} 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_cistatic inline void 30462306a36Sopenharmony_citls_advance_record_sn(struct sock *sk, struct tls_prot_info *prot, 30562306a36Sopenharmony_ci struct cipher_context *ctx) 30662306a36Sopenharmony_ci{ 30762306a36Sopenharmony_ci if (tls_bigint_increment(ctx->rec_seq, prot->rec_seq_size)) 30862306a36Sopenharmony_ci tls_err_abort(sk, -EBADMSG); 30962306a36Sopenharmony_ci 31062306a36Sopenharmony_ci if (prot->version != TLS_1_3_VERSION && 31162306a36Sopenharmony_ci prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) 31262306a36Sopenharmony_ci tls_bigint_increment(ctx->iv + prot->salt_size, 31362306a36Sopenharmony_ci prot->iv_size); 31462306a36Sopenharmony_ci} 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_cistatic inline void 31762306a36Sopenharmony_citls_xor_iv_with_seq(struct tls_prot_info *prot, char *iv, char *seq) 31862306a36Sopenharmony_ci{ 31962306a36Sopenharmony_ci int i; 32062306a36Sopenharmony_ci 32162306a36Sopenharmony_ci if (prot->version == TLS_1_3_VERSION || 32262306a36Sopenharmony_ci prot->cipher_type == TLS_CIPHER_CHACHA20_POLY1305) { 32362306a36Sopenharmony_ci for (i = 0; i < 8; i++) 32462306a36Sopenharmony_ci iv[i + 4] ^= seq[i]; 32562306a36Sopenharmony_ci } 32662306a36Sopenharmony_ci} 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_cistatic inline void 32962306a36Sopenharmony_citls_fill_prepend(struct tls_context *ctx, char *buf, size_t plaintext_len, 33062306a36Sopenharmony_ci unsigned char record_type) 33162306a36Sopenharmony_ci{ 33262306a36Sopenharmony_ci struct tls_prot_info *prot = &ctx->prot_info; 33362306a36Sopenharmony_ci size_t pkt_len, iv_size = prot->iv_size; 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ci pkt_len = plaintext_len + prot->tag_size; 33662306a36Sopenharmony_ci if (prot->version != TLS_1_3_VERSION && 33762306a36Sopenharmony_ci prot->cipher_type != TLS_CIPHER_CHACHA20_POLY1305) { 33862306a36Sopenharmony_ci pkt_len += iv_size; 33962306a36Sopenharmony_ci 34062306a36Sopenharmony_ci memcpy(buf + TLS_NONCE_OFFSET, 34162306a36Sopenharmony_ci ctx->tx.iv + prot->salt_size, iv_size); 34262306a36Sopenharmony_ci } 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ci /* we cover nonce explicit here as well, so buf should be of 34562306a36Sopenharmony_ci * size KTLS_DTLS_HEADER_SIZE + KTLS_DTLS_NONCE_EXPLICIT_SIZE 34662306a36Sopenharmony_ci */ 34762306a36Sopenharmony_ci buf[0] = prot->version == TLS_1_3_VERSION ? 34862306a36Sopenharmony_ci TLS_RECORD_TYPE_DATA : record_type; 34962306a36Sopenharmony_ci /* Note that VERSION must be TLS_1_2 for both TLS1.2 and TLS1.3 */ 35062306a36Sopenharmony_ci buf[1] = TLS_1_2_VERSION_MINOR; 35162306a36Sopenharmony_ci buf[2] = TLS_1_2_VERSION_MAJOR; 35262306a36Sopenharmony_ci /* we can use IV for nonce explicit according to spec */ 35362306a36Sopenharmony_ci buf[3] = pkt_len >> 8; 35462306a36Sopenharmony_ci buf[4] = pkt_len & 0xFF; 35562306a36Sopenharmony_ci} 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_cistatic inline 35862306a36Sopenharmony_civoid tls_make_aad(char *buf, size_t size, char *record_sequence, 35962306a36Sopenharmony_ci unsigned char record_type, struct tls_prot_info *prot) 36062306a36Sopenharmony_ci{ 36162306a36Sopenharmony_ci if (prot->version != TLS_1_3_VERSION) { 36262306a36Sopenharmony_ci memcpy(buf, record_sequence, prot->rec_seq_size); 36362306a36Sopenharmony_ci buf += 8; 36462306a36Sopenharmony_ci } else { 36562306a36Sopenharmony_ci size += prot->tag_size; 36662306a36Sopenharmony_ci } 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci buf[0] = prot->version == TLS_1_3_VERSION ? 36962306a36Sopenharmony_ci TLS_RECORD_TYPE_DATA : record_type; 37062306a36Sopenharmony_ci buf[1] = TLS_1_2_VERSION_MAJOR; 37162306a36Sopenharmony_ci buf[2] = TLS_1_2_VERSION_MINOR; 37262306a36Sopenharmony_ci buf[3] = size >> 8; 37362306a36Sopenharmony_ci buf[4] = size & 0xFF; 37462306a36Sopenharmony_ci} 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_ci#endif 377