// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movw %r9w, %ax
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movw %cx, -0x1a(%rbp)
movq %r8, -0x28(%rbp)
movw %ax, -0x2a(%rbp)
movq $0x0, -0x38(%rbp)
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdi
callq 0x23e0
movq %rax, -0x48(%rbp)
movl $0x0, -0x4c(%rbp)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x20, %edi
callq 0x3110
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x60(%rbp), %rdx
callq 0x3160
testb $0x1, %al
jne 0x2e88
jmp 0x30c1
callq 0x21d0
movq %rax, -0x38(%rbp)
cmpq $0x0, %rax
jne 0x2eaf
movl $0x4, %edi
leaq 0x465a(%rip), %rsi      # 0x74fd
movb $0x0, %al
callq 0x4d70
jmp 0x30c1
movq -0x38(%rbp), %rax
movq %rax, -0x70(%rbp)
callq 0x2450
movq -0x70(%rbp), %rdi
movq %rax, %rsi
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rdx
movq %r8, %rcx
callq 0x2090
cmpl $0x0, %eax
jne 0x2ef0
movl $0x4, %edi
leaq 0x463d(%rip), %rsi      # 0x7521
movb $0x0, %al
callq 0x4d70
jmp 0x30c1
movq -0x38(%rbp), %rdi
movzwl -0x1a(%rbp), %edx
movl $0x9, %esi
xorl %eax, %eax
movl %eax, %ecx
callq 0x2430
cmpl $0x0, %eax
jne 0x2f23
movl $0x4, %edi
leaq 0x4628(%rip), %rsi      # 0x753f
movb $0x0, %al
callq 0x4d70
jmp 0x30c1
movq -0x38(%rbp), %rdi
movq -0x60(%rbp), %rax
movq (%rax), %rcx
movq -0x18(%rbp), %r8
xorl %eax, %eax
movl %eax, %edx
movq %rdx, %rsi
callq 0x2090
cmpl $0x0, %eax
jne 0x2f5b
movl $0x4, %edi
leaq 0x461c(%rip), %rsi      # 0x756b
movb $0x0, %al
callq 0x4d70
jmp 0x30c1
jmp 0x2f5d
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movslq -0x4c(%rbp), %rax
addq %rax, %rsi
movq -0x58(%rbp), %rax
movq 0x8(%rax), %rcx
movq -0x58(%rbp), %rax
movzwl 0x2(%rax), %r8d
leaq -0x3c(%rbp), %rdx
callq 0x21c0
cmpl $0x0, %eax
jne 0x2fa3
movl $0x4, %edi
leaq 0x4602(%rip), %rsi      # 0x7599
movb $0x0, %al
callq 0x4d70
jmp 0x30c1
movl -0x3c(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, %rax
jne 0x2f5d
movq -0x38(%rbp), %rdi
movzwl -0x2a(%rbp), %edx
movq -0x28(%rbp), %rcx
movl $0x11, %esi
callq 0x2430
cmpl $0x0, %eax
jne 0x2ff1
movl $0x4, %edi
leaq 0x45d3(%rip), %rsi      # 0x75b8
movb $0x0, %al
callq 0x4d70
jmp 0x30c1
movq -0x38(%rbp), %rdi
movq -0x48(%rbp), %rsi
movslq -0x4c(%rbp), %rax
addq %rax, %rsi
leaq -0x3c(%rbp), %rdx
callq 0x20e0
movl %eax, -0x50(%rbp)
cmpl $0x0, -0x50(%rbp)
jle 0x30ae
movl -0x3c(%rbp), %eax
addl -0x4c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movslq -0x4c(%rbp), %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x40(%rax)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
je 0x305d
movq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x58(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x58(%rbp)
movq -0x68(%rbp), %rdi
callq 0x5f40
jmp 0x3037
callq 0x5ea0
movq %rax, %rcx
movq -0x8(%rbp), %rax
movq %rcx, 0x50(%rax)
movl -0x4c(%rbp), %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movw %cx, 0x2(%rax)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movw $0x0, (%rax)
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq $0x0, 0x10(%rax)
movq -0x48(%rbp), %rcx
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq %rcx, 0x8(%rax)
jmp 0x30da
movl $0x4, %edi
leaq 0x4517(%rip), %rsi      # 0x75d1
movb $0x0, %al
callq 0x4d70
jmp 0x30c3
movq -0x48(%rbp), %rdi
callq 0x2350
movq $0x0, -0x48(%rbp)
movb $0x0, -0x3d(%rbp)
jmp 0x30f1
movl $0x1, %edi
leaq 0x4517(%rip), %rsi      # 0x75fd
movb $0x0, %al
callq 0x4d70
movb $0x1, -0x3d(%rbp)
movq -0x38(%rbp), %rdi
callq 0x22e0
movq -0x60(%rbp), %rdi
callq 0x3330
movb -0x3d(%rbp), %al
andb $0x1, %al
addq $0x70, %rsp
popq %rbp
retq
nop