// File: /workspace/github_c_makefile_star2_O3/TimeToogo[P]ff-proxy/src/crypto.c
// Function: ff_decrypt_request_aes_256_gcm
bool ff_decrypt_request_aes_256_gcm(
    struct ff_request *request,
    struct ff_encryption_config *config,
    uint8_t *iv,
    uint16_t iv_len,
    uint8_t *tag,
    uint16_t tag_len)
{
    EVP_CIPHER_CTX *ctx = NULL;
    int len;
    bool ret_val;

    uint8_t *plaintext_buff = malloc(request->payload_length);
    int plaintext_len = 0;
    int ret;
    struct ff_request_payload_node *payload_chunk = request->payload;

    struct ff_derived_key *derived_key = ff_derived_key_alloc(32);

    if (!ff_derive_key(request, config, derived_key))
    {
        goto error;
    }

    if (!(ctx = EVP_CIPHER_CTX_new()))
    {
        ff_log(FF_ERROR, "Failed to create new OpenSSL cipher");
        goto error;
    }

    if (!EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL))
    {
        ff_log(FF_ERROR, "Failed to init OpenSSL cipher");
        goto error;
    }

    if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, iv_len, NULL))
    {
        ff_log(FF_ERROR, "Failed to update OpenSSL cipher - IV length");
        goto error;
    }

    if (!EVP_DecryptInit_ex(ctx, NULL, NULL, derived_key->key, iv))
    {
        ff_log(FF_ERROR, "Failed to init OpenSSL cipher with key and IV");
        goto error;
    }

    do
    {
        if (!EVP_DecryptUpdate(ctx, plaintext_buff + plaintext_len, &len, payload_chunk->value, payload_chunk->length))
        {
            ff_log(FF_ERROR, "Failed decrypt request payload");
            goto error;
        }

        plaintext_len += len;

    } while ((payload_chunk = payload_chunk->next) != NULL);

    if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, tag_len, tag))
    {
        ff_log(FF_ERROR, "Failed to set cipher tag");
        goto error;
    }

    ret = EVP_DecryptFinal_ex(ctx, plaintext_buff + plaintext_len, &len);

    if (ret > 0)
    {
        plaintext_len += len;
        request->payload_length = plaintext_len;

        payload_chunk = request->payload;
        struct ff_request_payload_node *tmp_payload;

        while (payload_chunk != NULL)
        {
            tmp_payload = payload_chunk;
            payload_chunk = payload_chunk->next;
            ff_request_payload_node_free(tmp_payload);
        }

        request->payload = ff_request_payload_node_alloc();
        request->payload->length = plaintext_len;
        request->payload->offset = 0;
        request->payload->next = NULL;
        request->payload->value = plaintext_buff;
        goto done;
    }
    else
    {
        ff_log(FF_ERROR, "Failed decrypt and finalize request payload");
        goto error;
    }

error:
    FREE(plaintext_buff);
    ret_val = false;
    goto cleanup;

done:
    ff_log(FF_DEBUG, "Successfully decrypted incoming request");
    ret_val = true;
    goto cleanup;

cleanup:
    EVP_CIPHER_CTX_free(ctx);
    ff_derived_key_free(derived_key);

    return ret_val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movl %r9d, -0x44(%rbp)
movq %r8, -0x50(%rbp)
movl %ecx, -0x30(%rbp)
movq %rdx, -0x58(%rbp)
movq %rsi, %r15
movq %rdi, %r12
movq 0x40(%rdi), %rdi
callq 0x23c0
movq %rax, -0x40(%rbp)
movq 0x50(%r12), %r13
movl $0x10, %edi
callq 0x23c0
movq %rax, %rbx
movl $0x1, %edi
movl $0x20, %esi
callq 0x21f0
movq %rax, %r14
movq %rax, (%rbx)
movw $0x20, 0x8(%rbx)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2e6e
testb %al, %al
je 0x2dad
callq 0x21c0
testq %rax, %rax
je 0x2db2
movq %rax, %r15
callq 0x2430
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x2080
testl %eax, %eax
je 0x2dbe
movzwl -0x30(%rbp), %edx
movq %r15, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x2410
testl %eax, %eax
je 0x2dc7
movq %rbx, -0x30(%rbp)
xorl %ebx, %ebx
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r14, %rcx
movq -0x58(%rbp), %r8
callq 0x2080
testl %eax, %eax
je 0x2ddc
movslq %ebx, %r14
movq -0x40(%rbp), %rax
leaq (%rax,%r14), %rsi
movq 0x8(%r13), %rcx
movzwl 0x2(%r13), %r8d
movq %r15, %rdi
leaq -0x34(%rbp), %rdx
callq 0x21b0
testl %eax, %eax
je 0x2de5
movslq -0x34(%rbp), %rbx
addq %r14, %rbx
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x2cd4
movzwl -0x44(%rbp), %edx
movq %r15, %rdi
movl $0x11, %esi
movq -0x50(%rbp), %rcx
callq 0x2410
testl %eax, %eax
je 0x2dee
movq -0x40(%rbp), %rsi
addq %rbx, %rsi
leaq -0x34(%rbp), %rdx
movq %r15, %rdi
callq 0x20d0
testl %eax, %eax
jle 0x2df7
movslq -0x34(%rbp), %rax
movslq %ebx, %rbx
addq %rax, %rbx
movq %rbx, 0x40(%r12)
movq 0x50(%r12), %rdi
testq %rdi, %rdi
je 0x2d6e
movq 0x10(%rdi), %r14
callq 0x497d
movq %r14, %rdi
testq %r14, %r14
jne 0x2d5d
callq 0x4921
movq %rax, 0x50(%r12)
movw %bx, 0x2(%rax)
movw $0x0, (%rax)
movq $0x0, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x3835(%rip), %rsi      # 0x65cd
movl $0x1, %edi
xorl %eax, %eax
callq 0x3fa4
movb $0x1, %r14b
movq -0x30(%rbp), %rbx
jmp 0x2e1a
xorl %r15d, %r15d
jmp 0x2e0e
leaq 0x3714(%rip), %rsi      # 0x64cd
xorl %r15d, %r15d
jmp 0x2dce
leaq 0x372c(%rip), %rsi      # 0x64f1
jmp 0x2dce
leaq 0x3741(%rip), %rsi      # 0x650f
movl $0x4, %edi
xorl %eax, %eax
callq 0x3fa4
jmp 0x2e0e
leaq 0x3758(%rip), %rsi      # 0x653b
jmp 0x2dfe
leaq 0x377d(%rip), %rsi      # 0x6569
jmp 0x2dfe
leaq 0x3793(%rip), %rsi      # 0x6588
jmp 0x2dfe
leaq 0x37a3(%rip), %rsi      # 0x65a1
movl $0x4, %edi
xorl %eax, %eax
callq 0x3fa4
movq -0x30(%rbp), %rbx
movq -0x40(%rbp), %rdi
callq 0x2340
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x22d0
movq %rbx, %rdi
callq 0x2fcf
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq