// File: /workspace/github_c_makefile_star2_O2/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, %ebx
movq %rdx, -0x58(%rbp)
movq %rsi, %r15
movq %rdi, %r12
movq 0x40(%rdi), %rdi
callq 0x23c0
movq %rax, -0x40(%rbp)
movq 0x50(%r12), %r13
pushq $0x20
popq %rdi
callq 0x2d7d
movq %rax, %r14
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x2dab
testb %al, %al
je 0x2cb9
callq 0x21c0
testq %rax, %rax
je 0x2cc1
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 0x2ccd
movzwl %bx, %edx
pushq $0x9
popq %rsi
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x2410
testl %eax, %eax
je 0x2cd6
movq %r14, -0x38(%rbp)
movq (%r14), %rcx
xorl %r14d, %r14d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
movq -0x58(%rbp), %r8
callq 0x2080
testl %eax, %eax
je 0x2ce9
movslq %r14d, %rbx
movq -0x40(%rbp), %rax
leaq (%rax,%rbx), %rsi
movq 0x8(%r13), %rcx
movzwl 0x2(%r13), %r8d
movq %r15, %rdi
leaq -0x2c(%rbp), %rdx
callq 0x21b0
testl %eax, %eax
je 0x2cf2
movslq -0x2c(%rbp), %r14
addq %rbx, %r14
movq 0x10(%r13), %r13
testq %r13, %r13
jne 0x2c24
movzwl -0x44(%rbp), %edx
pushq $0x11
popq %rsi
movq %r15, %rdi
movq -0x50(%rbp), %rcx
callq 0x2410
testl %eax, %eax
je 0x2d33
movq -0x40(%rbp), %rsi
addq %r14, %rsi
leaq -0x2c(%rbp), %rdx
movq %r15, %rdi
callq 0x20d0
testl %eax, %eax
jle 0x2d3c
movslq -0x2c(%rbp), %rax
movslq %r14d, %rbx
addq %rax, %rbx
movq %rbx, 0x40(%r12)
movq 0x50(%r12), %rdi
testq %rdi, %rdi
je 0x2cfb
movq 0x10(%rdi), %r14
callq 0x46ff
movq %r14, %rdi
jmp 0x2ca6
xorl %r15d, %r15d
jmp 0x2d51
leaq 0x2805(%rip), %rsi      # 0x54cd
xorl %r15d, %r15d
jmp 0x2cdd
leaq 0x281d(%rip), %rsi      # 0x54f1
jmp 0x2cdd
leaq 0x2832(%rip), %rsi      # 0x550f
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x3db4
jmp 0x2d51
leaq 0x284b(%rip), %rsi      # 0x553b
jmp 0x2d43
leaq 0x2870(%rip), %rsi      # 0x5569
jmp 0x2d43
callq 0x46a8
movq %rax, 0x50(%r12)
movw %bx, 0x2(%rax)
andw $0x0, (%rax)
andq $0x0, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x8(%rax)
leaq 0x28ac(%rip), %rsi      # 0x55cd
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x3db4
movb $0x1, %bl
movq -0x38(%rbp), %r14
jmp 0x2d5c
leaq 0x284e(%rip), %rsi      # 0x5588
jmp 0x2d43
leaq 0x285e(%rip), %rsi      # 0x55a1
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x3db4
movq -0x38(%rbp), %r14
movq -0x40(%rbp), %rdi
callq 0x2340
xorl %ebx, %ebx
movq %r15, %rdi
callq 0x22d0
movq %r14, %rdi
callq 0x2efd
movl %ebx, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq