// File: /workspace/github_c_makefile_star2_O1/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 $0x28, %rsp
movl %r9d, -0x44(%rbp)
movq %r8, -0x50(%rbp)
movl %ecx, %r13d
movq %rdx, -0x38(%rbp)
movq %rsi, %r15
movq %rdi, %r12
movq 0x40(%rdi), %rdi
callq 0x23c0
movq %rax, -0x40(%rbp)
movq 0x50(%r12), %r14
movl $0x10, %edi
callq 0x23c0
movq %rax, %rbx
movl $0x1, %edi
movl $0x20, %esi
callq 0x21f0
movq %rax, (%rbx)
movw $0x20, 0x8(%rbx)
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x2fb9
testb %al, %al
je 0x2f00
callq 0x21c0
testq %rax, %rax
je 0x2f05
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 0x2f11
movzwl %r13w, %edx
movq %r15, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x2410
testl %eax, %eax
je 0x2f1a
movq (%rbx), %rcx
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq -0x38(%rbp), %r8
callq 0x2080
testl %eax, %eax
je 0x2f23
movq %rbx, -0x38(%rbp)
movslq %r13d, %rbx
movq -0x40(%rbp), %rax
leaq (%rax,%rbx), %rsi
movq 0x8(%r14), %rcx
movzwl 0x2(%r14), %r8d
movq %r15, %rdi
leaq -0x2c(%rbp), %rdx
callq 0x21b0
testl %eax, %eax
je 0x2f2c
movslq -0x2c(%rbp), %r13
addq %rbx, %r13
movq 0x10(%r14), %r14
testq %r14, %r14
jne 0x2e26
movzwl -0x44(%rbp), %edx
movq %r15, %rdi
movl $0x11, %esi
movq -0x50(%rbp), %rcx
callq 0x2410
testl %eax, %eax
je 0x2f39
movq -0x40(%rbp), %rsi
addq %r13, %rsi
leaq -0x2c(%rbp), %rdx
movq %r15, %rdi
callq 0x20d0
testl %eax, %eax
jle 0x2f42
movslq -0x2c(%rbp), %rax
movslq %r13d, %r14
addq %rax, %r14
movq %r14, 0x40(%r12)
movq 0x50(%r12), %rdi
testq %rdi, %rdi
je 0x2ec0
movq 0x10(%rdi), %rbx
callq 0x4b8d
movq %rbx, %rdi
testq %rbx, %rbx
jne 0x2eaf
callq 0x4b31
movq %rax, 0x50(%r12)
movw %r14w, 0x2(%rax)
movw $0x0, (%rax)
movq $0x0, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x36e2(%rip), %rsi      # 0x65cd
movl $0x1, %edi
xorl %eax, %eax
callq 0x4100
movb $0x1, %r14b
movq -0x38(%rbp), %rbx
jmp 0x2f65
xorl %r15d, %r15d
jmp 0x2f59
leaq 0x35c1(%rip), %rsi      # 0x64cd
xorl %r15d, %r15d
jmp 0x2f4d
leaq 0x35d9(%rip), %rsi      # 0x64f1
jmp 0x2f4d
leaq 0x35ee(%rip), %rsi      # 0x650f
jmp 0x2f4d
leaq 0x3611(%rip), %rsi      # 0x653b
jmp 0x2f4d
movq -0x38(%rbp), %rbx
leaq 0x3632(%rip), %rsi      # 0x6569
jmp 0x2f4d
leaq 0x3648(%rip), %rsi      # 0x6588
jmp 0x2f49
leaq 0x3658(%rip), %rsi      # 0x65a1
movq -0x38(%rbp), %rbx
movl $0x4, %edi
xorl %eax, %eax
callq 0x4100
movq -0x40(%rbp), %rdi
callq 0x2340
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x22d0
movq %rbx, %rdi
callq 0x3132
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq