int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)
{
    EVP_CIPHER_CTX *ctx;
    unsigned char iv[EVP_MAX_IV_LENGTH];
    size_t ivlen, taglen, offset, loop;
    unsigned char *staticiv;
    unsigned char *seq;
    int lenu, lenf;
    SSL3_RECORD *rec = &recs[0];
    uint32_t alg_enc;
    if (n_recs != 1) {
        return -1;
    }
    if (sending) {
        ctx = s->enc_write_ctx;
        staticiv = s->write_iv;
        seq = RECORD_LAYER_get_write_sequence(&s->rlayer);
    } else {
        ctx = s->enc_read_ctx;
        staticiv = s->read_iv;
        seq = RECORD_LAYER_get_read_sequence(&s->rlayer);
    }
    if (ctx == NULL) {
        memmove(rec->data, rec->input, rec->length);
        rec->input = rec->data;
        return 1;
    }
    ivlen = EVP_CIPHER_CTX_iv_length(ctx);
    if (s->early_data_state == SSL_EARLY_DATA_WRITING
            || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
        alg_enc = s->session->cipher->algorithm_enc;
    } else {
        assert(s->s3->tmp.new_cipher != NULL);
        if (s->s3->tmp.new_cipher == NULL)
            return -1;
        alg_enc = s->s3->tmp.new_cipher->algorithm_enc;
    }
    if (alg_enc & SSL_AESCCM) {
        if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
            taglen = EVP_CCM8_TLS_TAG_LEN;
         else
            taglen = EVP_CCM_TLS_TAG_LEN;
         if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,
                                         NULL) <= 0)
            return -1;
    } else if (alg_enc & SSL_AESGCM) {
        taglen = EVP_GCM_TLS_TAG_LEN;
    } else if (alg_enc & SSL_CHACHA20) {
        taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
    } else {
        return -1;
    }
    if (!sending) {
        if (rec->length < taglen + 1)
            return 0;
        rec->length -= taglen;
    }
    if (ivlen < SEQ_NUM_SIZE) {
        return -1;
    }
    offset = ivlen - SEQ_NUM_SIZE;
    memcpy(iv, staticiv, offset);
    for (loop = 0; loop < SEQ_NUM_SIZE; loop++)
        iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];
    for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {
        ++seq[loop - 1];
        if (seq[loop - 1] != 0)
            break;
    }
    if (loop == 0) {
        return -1;
    }
    if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0
            || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
                                             taglen,
                                             rec->data + rec->length) <= 0)
            || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,
                                (unsigned int)rec->length) <= 0
            || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0
            || (size_t)(lenu + lenf) != rec->length) {
        return -1;
    }
    if (sending) {
        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,
                                rec->data + rec->length) <= 0)
            return -1;
        rec->length += taglen;
    }
    return 1;
}
['int do_ssl3_write(SSL *s, int type, const unsigned char *buf,\n                  size_t *pipelens, size_t numpipes,\n                  int create_empty_fragment, size_t *written)\n{\n    WPACKET pkt[SSL_MAX_PIPELINES];\n    SSL3_RECORD wr[SSL_MAX_PIPELINES];\n    WPACKET *thispkt;\n    SSL3_RECORD *thiswr;\n    unsigned char *recordstart;\n    int i, mac_size, clear = 0;\n    size_t prefix_len = 0;\n    int eivlen = 0;\n    size_t align = 0;\n    SSL3_BUFFER *wb;\n    SSL_SESSION *sess;\n    size_t totlen = 0, len, wpinited = 0;\n    size_t j;\n    for (j = 0; j < numpipes; j++)\n        totlen += pipelens[j];\n    if (RECORD_LAYER_write_pending(&s->rlayer))\n        return ssl3_write_pending(s, type, buf, totlen, written);\n    if (s->s3->alert_dispatch) {\n        i = s->method->ssl_dispatch_alert(s);\n        if (i <= 0)\n            return (i);\n    }\n    if (s->rlayer.numwpipes < numpipes)\n        if (!ssl3_setup_write_buffer(s, numpipes, 0))\n            return -1;\n    if (totlen == 0 && !create_empty_fragment)\n        return 0;\n    sess = s->session;\n    if ((sess == NULL) ||\n        (s->enc_write_ctx == NULL) || (EVP_MD_CTX_md(s->write_hash) == NULL)) {\n        clear = s->enc_write_ctx ? 0 : 1;\n        mac_size = 0;\n    } else {\n        mac_size = EVP_MD_CTX_size(s->write_hash);\n        if (mac_size < 0)\n            goto err;\n    }\n    if (!clear && !create_empty_fragment && !s->s3->empty_fragment_done) {\n        if (s->s3->need_empty_fragments && type == SSL3_RT_APPLICATION_DATA) {\n            size_t tmppipelen = 0;\n            int ret;\n            ret = do_ssl3_write(s, type, buf, &tmppipelen, 1, 1, &prefix_len);\n            if (ret <= 0)\n                goto err;\n            if (prefix_len >\n                (SSL3_RT_HEADER_LENGTH + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n        }\n        s->s3->empty_fragment_done = 1;\n    }\n    if (create_empty_fragment) {\n        wb = &s->rlayer.wbuf[0];\n#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD!=0\n        align = (size_t)SSL3_BUFFER_get_buf(wb) + 2 * SSL3_RT_HEADER_LENGTH;\n        align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);\n#endif\n        SSL3_BUFFER_set_offset(wb, align);\n        if (!WPACKET_init_static_len(&pkt[0], SSL3_BUFFER_get_buf(wb),\n                                     SSL3_BUFFER_get_len(wb), 0)\n                || !WPACKET_allocate_bytes(&pkt[0], align, NULL)) {\n            SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        wpinited = 1;\n    } else if (prefix_len) {\n        wb = &s->rlayer.wbuf[0];\n        if (!WPACKET_init_static_len(&pkt[0],\n                                     SSL3_BUFFER_get_buf(wb),\n                                     SSL3_BUFFER_get_len(wb), 0)\n                || !WPACKET_allocate_bytes(&pkt[0], SSL3_BUFFER_get_offset(wb)\n                                                    + prefix_len, NULL)) {\n            SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        wpinited = 1;\n    } else {\n        for (j = 0; j < numpipes; j++) {\n            thispkt = &pkt[j];\n            wb = &s->rlayer.wbuf[j];\n#if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0\n            align = (size_t)SSL3_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;\n            align = SSL3_ALIGN_PAYLOAD - 1 - ((align - 1) % SSL3_ALIGN_PAYLOAD);\n#endif\n            SSL3_BUFFER_set_offset(wb, align);\n            if (!WPACKET_init_static_len(thispkt, SSL3_BUFFER_get_buf(wb),\n                                         SSL3_BUFFER_get_len(wb), 0)\n                    || !WPACKET_allocate_bytes(thispkt, align, NULL)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            wpinited++;\n        }\n    }\n    if (s->enc_write_ctx && SSL_USE_EXPLICIT_IV(s) && !SSL_TREAT_AS_TLS13(s)) {\n        int mode = EVP_CIPHER_CTX_mode(s->enc_write_ctx);\n        if (mode == EVP_CIPH_CBC_MODE) {\n            eivlen = EVP_CIPHER_CTX_iv_length(s->enc_write_ctx);\n            if (eivlen <= 1)\n                eivlen = 0;\n        } else if (mode == EVP_CIPH_GCM_MODE) {\n            eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;\n        } else if (mode == EVP_CIPH_CCM_MODE) {\n            eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;\n        }\n    }\n    totlen = 0;\n    memset(wr, 0, sizeof wr);\n    for (j = 0; j < numpipes; j++) {\n        unsigned int version = SSL_TREAT_AS_TLS13(s) ? TLS1_VERSION : s->version;\n        unsigned char *compressdata = NULL;\n        size_t maxcomplen;\n        unsigned int rectype;\n        thispkt = &pkt[j];\n        thiswr = &wr[j];\n        SSL3_RECORD_set_type(thiswr, type);\n        if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL)\n            rectype = SSL3_RT_APPLICATION_DATA;\n        else\n            rectype = type;\n        if (SSL_get_state(s) == TLS_ST_CW_CLNT_HELLO\n            && !s->renegotiate && TLS1_get_version(s) > TLS1_VERSION)\n            version = TLS1_VERSION;\n        maxcomplen = pipelens[j];\n        if (s->compress != NULL)\n            maxcomplen += SSL3_RT_MAX_COMPRESSED_OVERHEAD;\n        if (!WPACKET_put_bytes_u8(thispkt, rectype)\n                || !WPACKET_put_bytes_u16(thispkt, version)\n                || !WPACKET_start_sub_packet_u16(thispkt)\n                || (eivlen > 0\n                    && !WPACKET_allocate_bytes(thispkt, eivlen, NULL))\n                || (maxcomplen > 0\n                    && !WPACKET_reserve_bytes(thispkt, maxcomplen,\n                                              &compressdata))) {\n            SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        SSL3_RECORD_set_data(thiswr, compressdata);\n        SSL3_RECORD_set_length(thiswr, pipelens[j]);\n        SSL3_RECORD_set_input(thiswr, (unsigned char *)&buf[totlen]);\n        totlen += pipelens[j];\n        if (s->compress != NULL) {\n            if (!ssl3_do_compress(s, thiswr)\n                    || !WPACKET_allocate_bytes(thispkt, thiswr->length, NULL)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, SSL_R_COMPRESSION_FAILURE);\n                goto err;\n            }\n        } else {\n            if (!WPACKET_memcpy(thispkt, thiswr->input, thiswr->length)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            SSL3_RECORD_reset_input(&wr[j]);\n        }\n        if (SSL_TREAT_AS_TLS13(s) && s->enc_write_ctx != NULL) {\n            if (!WPACKET_put_bytes_u8(thispkt, type)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            SSL3_RECORD_add_length(thiswr, 1);\n        }\n        if (!SSL_WRITE_ETM(s) && mac_size != 0) {\n            unsigned char *mac;\n            if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)\n                    || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n        }\n        if(!WPACKET_reserve_bytes(thispkt, SSL_RT_MAX_CIPHER_BLOCK_SIZE,\n                                  NULL)\n                || !WPACKET_get_length(thispkt, &len)) {\n            SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        recordstart = WPACKET_get_curr(thispkt) - len;\n        SSL3_RECORD_set_data(thiswr, recordstart);\n        SSL3_RECORD_reset_input(thiswr);\n        SSL3_RECORD_set_length(thiswr, len);\n    }\n    if (s->early_data_state == SSL_EARLY_DATA_WRITING\n            || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {\n        if (tls13_enc(s, wr, numpipes, 1) < 1)\n            goto err;\n    } else {\n        if (s->method->ssl3_enc->enc(s, wr, numpipes, 1) < 1)\n            goto err;\n    }\n    for (j = 0; j < numpipes; j++) {\n        size_t origlen;\n        thispkt = &pkt[j];\n        thiswr = &wr[j];\n        if (!WPACKET_get_length(thispkt, &origlen)\n                || origlen > thiswr->length\n                || (thiswr->length > origlen\n                    && !WPACKET_allocate_bytes(thispkt,\n                                               thiswr->length - origlen, NULL))) {\n            SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (SSL_WRITE_ETM(s) && mac_size != 0) {\n            unsigned char *mac;\n            if (!WPACKET_allocate_bytes(thispkt, mac_size, &mac)\n                    || !s->method->ssl3_enc->mac(s, thiswr, mac, 1)) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            SSL3_RECORD_add_length(thiswr, mac_size);\n        }\n        if (!WPACKET_get_length(thispkt, &len)\n                || !WPACKET_close(thispkt)) {\n            SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        if (s->msg_callback) {\n            recordstart = WPACKET_get_curr(thispkt) - len\n                          - SSL3_RT_HEADER_LENGTH;\n            s->msg_callback(1, 0, SSL3_RT_HEADER, recordstart,\n                            SSL3_RT_HEADER_LENGTH, s,\n                            s->msg_callback_arg);\n        }\n        if (!WPACKET_finish(thispkt)) {\n            SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n            goto err;\n        }\n        SSL3_RECORD_set_type(thiswr, type);\n        SSL3_RECORD_add_length(thiswr, SSL3_RT_HEADER_LENGTH);\n        if (create_empty_fragment) {\n            if (j > 0) {\n                SSLerr(SSL_F_DO_SSL3_WRITE, ERR_R_INTERNAL_ERROR);\n                goto err;\n            }\n            *written = SSL3_RECORD_get_length(thiswr);\n            return 1;\n        }\n        SSL3_BUFFER_set_left(&s->rlayer.wbuf[j],\n                             prefix_len + SSL3_RECORD_get_length(thiswr));\n    }\n    s->rlayer.wpend_tot = totlen;\n    s->rlayer.wpend_buf = buf;\n    s->rlayer.wpend_type = type;\n    s->rlayer.wpend_ret = totlen;\n    return ssl3_write_pending(s, type, buf, totlen, written);\n err:\n    for (j = 0; j < wpinited; j++)\n        WPACKET_cleanup(&pkt[j]);\n    return -1;\n}', 'int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending)\n{\n    EVP_CIPHER_CTX *ctx;\n    unsigned char iv[EVP_MAX_IV_LENGTH];\n    size_t ivlen, taglen, offset, loop;\n    unsigned char *staticiv;\n    unsigned char *seq;\n    int lenu, lenf;\n    SSL3_RECORD *rec = &recs[0];\n    uint32_t alg_enc;\n    if (n_recs != 1) {\n        return -1;\n    }\n    if (sending) {\n        ctx = s->enc_write_ctx;\n        staticiv = s->write_iv;\n        seq = RECORD_LAYER_get_write_sequence(&s->rlayer);\n    } else {\n        ctx = s->enc_read_ctx;\n        staticiv = s->read_iv;\n        seq = RECORD_LAYER_get_read_sequence(&s->rlayer);\n    }\n    if (ctx == NULL) {\n        memmove(rec->data, rec->input, rec->length);\n        rec->input = rec->data;\n        return 1;\n    }\n    ivlen = EVP_CIPHER_CTX_iv_length(ctx);\n    if (s->early_data_state == SSL_EARLY_DATA_WRITING\n            || s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {\n        alg_enc = s->session->cipher->algorithm_enc;\n    } else {\n        assert(s->s3->tmp.new_cipher != NULL);\n        if (s->s3->tmp.new_cipher == NULL)\n            return -1;\n        alg_enc = s->s3->tmp.new_cipher->algorithm_enc;\n    }\n    if (alg_enc & SSL_AESCCM) {\n        if (alg_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))\n            taglen = EVP_CCM8_TLS_TAG_LEN;\n         else\n            taglen = EVP_CCM_TLS_TAG_LEN;\n         if (sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, taglen,\n                                         NULL) <= 0)\n            return -1;\n    } else if (alg_enc & SSL_AESGCM) {\n        taglen = EVP_GCM_TLS_TAG_LEN;\n    } else if (alg_enc & SSL_CHACHA20) {\n        taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;\n    } else {\n        return -1;\n    }\n    if (!sending) {\n        if (rec->length < taglen + 1)\n            return 0;\n        rec->length -= taglen;\n    }\n    if (ivlen < SEQ_NUM_SIZE) {\n        return -1;\n    }\n    offset = ivlen - SEQ_NUM_SIZE;\n    memcpy(iv, staticiv, offset);\n    for (loop = 0; loop < SEQ_NUM_SIZE; loop++)\n        iv[offset + loop] = staticiv[offset + loop] ^ seq[loop];\n    for (loop = SEQ_NUM_SIZE; loop > 0; loop--) {\n        ++seq[loop - 1];\n        if (seq[loop - 1] != 0)\n            break;\n    }\n    if (loop == 0) {\n        return -1;\n    }\n    if (EVP_CipherInit_ex(ctx, NULL, NULL, NULL, iv, sending) <= 0\n            || (!sending && EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,\n                                             taglen,\n                                             rec->data + rec->length) <= 0)\n            || EVP_CipherUpdate(ctx, rec->data, &lenu, rec->input,\n                                (unsigned int)rec->length) <= 0\n            || EVP_CipherFinal_ex(ctx, rec->data + lenu, &lenf) <= 0\n            || (size_t)(lenu + lenf) != rec->length) {\n        return -1;\n    }\n    if (sending) {\n        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, taglen,\n                                rec->data + rec->length) <= 0)\n            return -1;\n        rec->length += taglen;\n    }\n    return 1;\n}']