// File: /workspace/github_c_makefile_star2_O3/TimeToogo[P]ff-proxy/src/crypto.c
// Function: ff_decrypt_request
void ff_decrypt_request(struct ff_request *request, struct ff_encryption_config *config)
{
    request->state = FF_REQUEST_STATE_DECRYPTING;

    uint8_t encryption_mode = 0;
    uint8_t *iv = NULL;
    uint16_t iv_len = 0;
    uint8_t *tag = NULL;
    uint16_t tag_len = 0;

    bool has_key = config != NULL && config->key != NULL;

    for (uint8_t i = 0; i < request->options_length; i++)
    {
        switch (request->options[i]->type)
        {
        case FF_REQUEST_OPTION_TYPE_ENCRYPTION_MODE:
            if (request->options[i]->length == 1)
            {
                encryption_mode = (uint8_t)*request->options[i]->value;
            };
            break;

        case FF_REQUEST_OPTION_TYPE_ENCRYPTION_IV:
            iv_len = request->options[i]->length;
            iv = malloc(iv_len);
            memcpy(iv, request->options[i]->value, iv_len);
            break;

        case FF_REQUEST_OPTION_TYPE_ENCRYPTION_TAG:
            tag_len = request->options[i]->length;
            tag = malloc(tag_len);
            memcpy(tag, request->options[i]->value, tag_len);
            break;

        default:
            break;
        }
    }

    if (encryption_mode == 0)
    {
        if (has_key)
        {
            ff_log(FF_WARNING, "Encountered unencrypted request and pre-shared-key was set");
            goto error;
        }
        else
        {
            goto done;
        }
    }

    if (!has_key)
    {
        ff_log(FF_WARNING, "Encountered encrypted request and no pre-shared-key was set");
        goto error;
    }

    if (iv == NULL || iv_len == 0)
    {
        ff_log(FF_WARNING, "Encountered encrypted request with empty IV");
        goto error;
    }

    if (tag == NULL || tag_len == 0)
    {
        ff_log(FF_WARNING, "Encountered encrypted request with empty encryption tag");
        goto error;
    }

    switch (encryption_mode)
    {
    case FF_CRYPTO_MODE_AES_256_GCM:
        if (ff_decrypt_request_aes_256_gcm(request, config, iv, iv_len, tag, tag_len))
        {
            goto done;
        }
        else
        {
            goto error;
        }

    default:
        ff_log(FF_WARNING, "Encountered request with unknown encryption mode: %u", encryption_mode);
        goto error;
    }

error:
    request->state = FF_REQUEST_STATE_DECRYPTING_FAILED;
    goto cleanup;

done:
    request->state = FF_REQUEST_STATE_DECRYPTED;
    goto cleanup;

cleanup:
    if (iv != NULL)
    {
        FREE(iv);
    }

    if (tag != NULL)
    {
        FREE(tag);
    }

    return;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %rdi, %rbx
movl $0x3, (%rdi)
testq %rsi, %rsi
je 0x2a04
cmpq $0x0, (%rsi)
setne %cl
jmp 0x2a06
xorl %ecx, %ecx
movzbl 0x28(%rbx), %r14d
testq %r14, %r14
je 0x2aff
movl %ecx, -0x2c(%rbp)
movq %rsi, -0x68(%rbp)
movq %rbx, -0x48(%rbp)
movq 0x30(%rbx), %r15
xorl %r12d, %r12d
movq $0x0, -0x50(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x60(%rbp)
movq $0x0, -0x38(%rbp)
movq (%r15,%r12,8), %rbx
movl (%rbx), %eax
cmpl $0x3, %eax
je 0x2a94
cmpl $0x2, %eax
je 0x2a76
cmpl $0x1, %eax
jne 0x2abb
cmpw $0x1, 0x4(%rbx)
jne 0x2abb
movq 0x8(%rbx), %rax
movb (%rax), %al
movq %rax, -0x50(%rbp)
jmp 0x2abb
movzwl 0x4(%rbx), %eax
movq %rax, -0x60(%rbp)
movzwl %ax, %r13d
movq %r13, %rdi
callq 0x23c0
movq 0x8(%rbx), %rsi
movq %rax, -0x38(%rbp)
jmp 0x2ab0
movzwl 0x4(%rbx), %eax
movq %rax, -0x58(%rbp)
movzwl %ax, %r13d
movq %r13, %rdi
callq 0x23c0
movq 0x8(%rbx), %rsi
movq %rax, -0x40(%rbp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x2260
incq %r12
cmpq %r12, %r14
jne 0x2a4e
movq -0x50(%rbp), %rsi
testb %sil, %sil
je 0x2b07
cmpb $0x0, -0x2c(%rbp)
movq -0x48(%rbp), %rbx
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r15
je 0x2b28
testq %r15, %r15
sete %al
movq -0x60(%rbp), %rdi
testw %di, %di
sete %cl
orb %al, %cl
je 0x2b79
leaq 0x3937(%rip), %rsi      # 0x6434
jmp 0x2b2f
xorl %r14d, %r14d
xorl %r15d, %r15d
jmp 0x2b16
movq -0x48(%rbp), %rbx
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r15
movl -0x2c(%rbp), %ecx
movl $0x4, %eax
testb %cl, %cl
je 0x2b40
leaq 0x3897(%rip), %rsi      # 0x63bd
jmp 0x2b2f
leaq 0x38c9(%rip), %rsi      # 0x63f8
movl $0x3, %edi
xorl %eax, %eax
callq 0x3fa4
movl $0x5, %eax
movl %eax, (%rbx)
testq %r15, %r15
je 0x2b4f
movq %r15, %rdi
callq 0x2340
testq %r14, %r14
je 0x2b6a
movq %r14, %rdi
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2340
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
testq %r14, %r14
sete %al
movq -0x58(%rbp), %rdx
testw %dx, %dx
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x2ba5
leaq 0x38c9(%rip), %rsi      # 0x6460
movl $0x3, %edi
xorl %eax, %eax
callq 0x3fa4
jmp 0x2be6
cmpb $0x1, %sil
jne 0x2bcf
movzwl %di, %ecx
movzwl %dx, %r9d
movq %rbx, %rdi
movq -0x68(%rbp), %rsi
movq %r15, %rdx
movq %r14, %r8
callq 0x2bf2
movl $0x4, %ecx
testb %al, %al
je 0x2be6
jmp 0x2beb
movzbl %sil, %edx
leaq 0x38be(%rip), %rsi      # 0x6498
movl $0x3, %edi
xorl %eax, %eax
callq 0x3fa4
movl $0x5, %ecx
movl %ecx, (%rbx)
jmp 0x2b47