int ssl3_cbc_copy_mac(unsigned char *out,
                       const SSL3_RECORD *rec, size_t md_size)
{
#if defined(CBC_MAC_ROTATE_IN_PLACE)
    unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];
    unsigned char *rotated_mac;
#else
    unsigned char rotated_mac[EVP_MAX_MD_SIZE];
#endif
    size_t mac_end = rec->length;
    size_t mac_start = mac_end - md_size;
    size_t in_mac;
    size_t scan_start = 0;
    size_t i, j;
    size_t rotate_offset;
    if (!ossl_assert(rec->orig_len >= md_size
                     && md_size <= EVP_MAX_MD_SIZE))
        return 0;
#if defined(CBC_MAC_ROTATE_IN_PLACE)
    rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);
#endif
    if (rec->orig_len > md_size + 255 + 1)
        scan_start = rec->orig_len - (md_size + 255 + 1);
    in_mac = 0;
    rotate_offset = 0;
    memset(rotated_mac, 0, md_size);
    for (i = scan_start, j = 0; i < rec->orig_len; i++) {
        size_t mac_started = constant_time_eq_s(i, mac_start);
        size_t mac_ended = constant_time_lt_s(i, mac_end);
        unsigned char b = rec->data[i];
        in_mac |= mac_started;
        in_mac &= mac_ended;
        rotate_offset |= j & mac_started;
        rotated_mac[j++] |= b & in_mac;
        j &= constant_time_lt_s(j, md_size);
    }
#if defined(CBC_MAC_ROTATE_IN_PLACE)
    j = 0;
    for (i = 0; i < md_size; i++) {
        ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];
        out[j++] = rotated_mac[rotate_offset++];
        rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
    }
#else
    memset(out, 0, md_size);
    rotate_offset = md_size - rotate_offset;
    rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
    for (i = 0; i < md_size; i++) {
        for (j = 0; j < md_size; j++)
            out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);
        rotate_offset++;
        rotate_offset &= constant_time_lt_s(rotate_offset, md_size);
    }
#endif
    return 1;
}
['int dtls1_get_record(SSL *s)\n{\n    int ssl_major, ssl_minor;\n    int rret;\n    size_t more, n;\n    SSL3_RECORD *rr;\n    unsigned char *p = NULL;\n    unsigned short version;\n    DTLS1_BITMAP *bitmap;\n    unsigned int is_next_epoch;\n    rr = RECORD_LAYER_get_rrec(&s->rlayer);\n again:\n    if (!dtls1_process_buffered_records(s))\n        return -1;\n    if (dtls1_get_processed_record(s))\n        return 1;\n    if ((RECORD_LAYER_get_rstate(&s->rlayer) != SSL_ST_READ_BODY) ||\n        (RECORD_LAYER_get_packet_length(&s->rlayer) < DTLS1_RT_HEADER_LENGTH)) {\n        rret = ssl3_read_n(s, DTLS1_RT_HEADER_LENGTH,\n                           SSL3_BUFFER_get_len(&s->rlayer.rbuf), 0, 1, &n);\n        if (rret <= 0)\n            return rret;\n        if (RECORD_LAYER_get_packet_length(&s->rlayer) !=\n            DTLS1_RT_HEADER_LENGTH) {\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n        RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_BODY);\n        p = RECORD_LAYER_get_packet(&s->rlayer);\n        if (s->msg_callback)\n            s->msg_callback(0, 0, SSL3_RT_HEADER, p, DTLS1_RT_HEADER_LENGTH,\n                            s, s->msg_callback_arg);\n        rr->type = *(p++);\n        ssl_major = *(p++);\n        ssl_minor = *(p++);\n        version = (ssl_major << 8) | ssl_minor;\n        n2s(p, rr->epoch);\n        memcpy(&(RECORD_LAYER_get_read_sequence(&s->rlayer)[2]), p, 6);\n        p += 6;\n        n2s(p, rr->length);\n        if (!s->first_packet) {\n            if (version != s->version) {\n                rr->length = 0;\n                RECORD_LAYER_reset_packet_length(&s->rlayer);\n                goto again;\n            }\n        }\n        if ((version & 0xff00) != (s->version & 0xff00)) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n        if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n    }\n    if (rr->length >\n        RECORD_LAYER_get_packet_length(&s->rlayer) - DTLS1_RT_HEADER_LENGTH) {\n        more = rr->length;\n        rret = ssl3_read_n(s, more, more, 1, 1, &n);\n        if (rret <= 0 || n != more) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n    }\n    RECORD_LAYER_set_rstate(&s->rlayer, SSL_ST_READ_HEADER);\n    bitmap = dtls1_get_bitmap(s, rr, &is_next_epoch);\n    if (bitmap == NULL) {\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto again;\n    }\n#ifndef OPENSSL_NO_SCTP\n    if (!BIO_dgram_is_sctp(SSL_get_rbio(s))) {\n#endif\n        if (!dtls1_record_replay_check(s, bitmap)) {\n            rr->length = 0;\n            RECORD_LAYER_reset_packet_length(&s->rlayer);\n            goto again;\n        }\n#ifndef OPENSSL_NO_SCTP\n    }\n#endif\n    if (rr->length == 0)\n        goto again;\n    if (is_next_epoch) {\n        if ((SSL_in_init(s) || ossl_statem_get_in_handshake(s))) {\n            if (dtls1_buffer_record\n                (s, &(DTLS_RECORD_LAYER_get_unprocessed_rcds(&s->rlayer)),\n                 rr->seq_num) < 0)\n                return -1;\n        }\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto again;\n    }\n    if (!dtls1_process_record(s, bitmap)) {\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto again;\n    }\n    return (1);\n}', 'int dtls1_process_record(SSL *s, DTLS1_BITMAP *bitmap)\n{\n    int i, al;\n    int enc_err;\n    SSL_SESSION *sess;\n    SSL3_RECORD *rr;\n    int imac_size;\n    size_t mac_size;\n    unsigned char md[EVP_MAX_MD_SIZE];\n    rr = RECORD_LAYER_get_rrec(&s->rlayer);\n    sess = s->session;\n    rr->input = &(RECORD_LAYER_get_packet(&s->rlayer)[DTLS1_RT_HEADER_LENGTH]);\n    if (rr->length > SSL3_RT_MAX_ENCRYPTED_LENGTH) {\n        al = SSL_AD_RECORD_OVERFLOW;\n        SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_ENCRYPTED_LENGTH_TOO_LONG);\n        goto f_err;\n    }\n    rr->data = rr->input;\n    rr->orig_len = rr->length;\n    if (SSL_READ_ETM(s) && s->read_hash) {\n        unsigned char *mac;\n        mac_size = EVP_MD_CTX_size(s->read_hash);\n        if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        if (rr->orig_len < mac_size) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        rr->length -= mac_size;\n        mac = rr->data + rr->length;\n        i = s->method->ssl3_enc->mac(s, rr, md, 0   );\n        if (i == 0 || CRYPTO_memcmp(md, mac, (size_t)mac_size) != 0) {\n            al = SSL_AD_BAD_RECORD_MAC;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n                   SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC);\n            goto f_err;\n        }\n    }\n    enc_err = s->method->ssl3_enc->enc(s, rr, 1, 0);\n    if (enc_err == 0) {\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto err;\n    }\n#ifdef SSL_DEBUG\n    printf("dec %ld\\n", rr->length);\n    {\n        size_t z;\n        for (z = 0; z < rr->length; z++)\n            printf("%02X%c", rr->data[z], ((z + 1) % 16) ? \' \' : \'\\n\');\n    }\n    printf("\\n");\n#endif\n    if ((sess != NULL) && !SSL_READ_ETM(s) &&\n        (s->enc_read_ctx != NULL) && (EVP_MD_CTX_md(s->read_hash) != NULL)) {\n        unsigned char *mac = NULL;\n        unsigned char mac_tmp[EVP_MAX_MD_SIZE];\n        imac_size = EVP_MD_CTX_size(s->read_hash);\n        if (imac_size < 0) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_LIB_EVP);\n            goto f_err;\n        }\n        mac_size = (size_t)imac_size;\n        if (!ossl_assert(mac_size <= EVP_MAX_MD_SIZE)) {\n            al = SSL_AD_INTERNAL_ERROR;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_R_INTERNAL_ERROR);\n            goto f_err;\n        }\n        if (rr->orig_len < mac_size ||\n            (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE &&\n             rr->orig_len < mac_size + 1)) {\n            al = SSL_AD_DECODE_ERROR;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_LENGTH_TOO_SHORT);\n            goto f_err;\n        }\n        if (EVP_CIPHER_CTX_mode(s->enc_read_ctx) == EVP_CIPH_CBC_MODE) {\n            mac = mac_tmp;\n            if (!ssl3_cbc_copy_mac(mac_tmp, rr, mac_size)) {\n                al = SSL_AD_INTERNAL_ERROR;\n                SSLerr(SSL_F_DTLS1_PROCESS_RECORD, ERR_R_INTERNAL_ERROR);\n                goto f_err;\n            }\n            rr->length -= mac_size;\n        } else {\n            rr->length -= mac_size;\n            mac = &rr->data[rr->length];\n        }\n        i = s->method->ssl3_enc->mac(s, rr, md, 0   );\n        if (i == 0 || mac == NULL\n            || CRYPTO_memcmp(md, mac, mac_size) != 0)\n            enc_err = -1;\n        if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH + mac_size)\n            enc_err = -1;\n    }\n    if (enc_err < 0) {\n        rr->length = 0;\n        RECORD_LAYER_reset_packet_length(&s->rlayer);\n        goto err;\n    }\n    if (s->expand != NULL) {\n        if (rr->length > SSL3_RT_MAX_COMPRESSED_LENGTH) {\n            al = SSL_AD_RECORD_OVERFLOW;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD,\n                   SSL_R_COMPRESSED_LENGTH_TOO_LONG);\n            goto f_err;\n        }\n        if (!ssl3_do_uncompress(s, rr)) {\n            al = SSL_AD_DECOMPRESSION_FAILURE;\n            SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_BAD_DECOMPRESSION);\n            goto f_err;\n        }\n    }\n    if (rr->length > SSL3_RT_MAX_PLAIN_LENGTH) {\n        al = SSL_AD_RECORD_OVERFLOW;\n        SSLerr(SSL_F_DTLS1_PROCESS_RECORD, SSL_R_DATA_LENGTH_TOO_LONG);\n        goto f_err;\n    }\n    rr->off = 0;\n    RECORD_LAYER_reset_packet_length(&s->rlayer);\n    dtls1_record_bitmap_update(s, bitmap);\n    return (1);\n f_err:\n    ssl3_send_alert(s, SSL3_AL_FATAL, al);\n err:\n    return (0);\n}', 'int ssl3_cbc_copy_mac(unsigned char *out,\n                       const SSL3_RECORD *rec, size_t md_size)\n{\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n    unsigned char rotated_mac_buf[64 + EVP_MAX_MD_SIZE];\n    unsigned char *rotated_mac;\n#else\n    unsigned char rotated_mac[EVP_MAX_MD_SIZE];\n#endif\n    size_t mac_end = rec->length;\n    size_t mac_start = mac_end - md_size;\n    size_t in_mac;\n    size_t scan_start = 0;\n    size_t i, j;\n    size_t rotate_offset;\n    if (!ossl_assert(rec->orig_len >= md_size\n                     && md_size <= EVP_MAX_MD_SIZE))\n        return 0;\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n    rotated_mac = rotated_mac_buf + ((0 - (size_t)rotated_mac_buf) & 63);\n#endif\n    if (rec->orig_len > md_size + 255 + 1)\n        scan_start = rec->orig_len - (md_size + 255 + 1);\n    in_mac = 0;\n    rotate_offset = 0;\n    memset(rotated_mac, 0, md_size);\n    for (i = scan_start, j = 0; i < rec->orig_len; i++) {\n        size_t mac_started = constant_time_eq_s(i, mac_start);\n        size_t mac_ended = constant_time_lt_s(i, mac_end);\n        unsigned char b = rec->data[i];\n        in_mac |= mac_started;\n        in_mac &= mac_ended;\n        rotate_offset |= j & mac_started;\n        rotated_mac[j++] |= b & in_mac;\n        j &= constant_time_lt_s(j, md_size);\n    }\n#if defined(CBC_MAC_ROTATE_IN_PLACE)\n    j = 0;\n    for (i = 0; i < md_size; i++) {\n        ((volatile unsigned char *)rotated_mac)[rotate_offset ^ 32];\n        out[j++] = rotated_mac[rotate_offset++];\n        rotate_offset &= constant_time_lt_s(rotate_offset, md_size);\n    }\n#else\n    memset(out, 0, md_size);\n    rotate_offset = md_size - rotate_offset;\n    rotate_offset &= constant_time_lt_s(rotate_offset, md_size);\n    for (i = 0; i < md_size; i++) {\n        for (j = 0; j < md_size; j++)\n            out[j] |= rotated_mac[i] & constant_time_eq_8_s(j, rotate_offset);\n        rotate_offset++;\n        rotate_offset &= constant_time_lt_s(rotate_offset, md_size);\n    }\n#endif\n    return 1;\n}']