// File: /workspace/github_c_makefile_star2_O1/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
movl $0x3, (%rdi)
testq %rsi, %rsi
je 0x2b4c
cmpq $0x0, (%rsi)
setne %al
movl %eax, -0x2c(%rbp)
jmp 0x2b53
movl $0x0, -0x2c(%rbp)
movq %rsi, -0x68(%rbp)
movzbl 0x28(%rdi), %r14d
testq %r14, %r14
movq %rdi, -0x60(%rbp)
je 0x2c0f
movq 0x30(%rdi), %r15
xorl %r12d, %r12d
movq $0x0, -0x48(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x38(%rbp)
movq (%r15,%r12,8), %rbx
movl (%rbx), %eax
cmpl $0x3, %eax
je 0x2bde
cmpl $0x2, %eax
je 0x2bc0
cmpl $0x1, %eax
jne 0x2c05
cmpw $0x1, 0x4(%rbx)
jne 0x2c05
movq 0x8(%rbx), %rax
movb (%rax), %al
movq %rax, -0x48(%rbp)
jmp 0x2c05
movzwl 0x4(%rbx), %eax
movq %rax, -0x58(%rbp)
movzwl %ax, %r13d
movq %r13, %rdi
callq 0x23c0
movq 0x8(%rbx), %rsi
movq %rax, -0x38(%rbp)
jmp 0x2bfa
movzwl 0x4(%rbx), %eax
movq %rax, -0x50(%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 0x2b98
jmp 0x2c37
movq $0x0, -0x38(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq -0x48(%rbp), %rdx
testb %dl, %dl
je 0x2c71
cmpb $0x0, -0x2c(%rbp)
movq -0x60(%rbp), %rbx
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r15
je 0x2c8c
testq %r15, %r15
sete %al
movq -0x58(%rbp), %rdi
testw %di, %di
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x2ca6
leaq 0x37c5(%rip), %rsi      # 0x6434
jmp 0x2c93
cmpb $0x0, -0x2c(%rbp)
movq -0x60(%rbp), %rbx
movq -0x40(%rbp), %r14
movq -0x38(%rbp), %r15
je 0x2cf0
leaq 0x3733(%rip), %rsi      # 0x63bd
jmp 0x2c93
leaq 0x3765(%rip), %rsi      # 0x63f8
movl $0x3, %edi
xorl %eax, %eax
callq 0x4100
movl $0x5, %ecx
jmp 0x2cf5
testq %r14, %r14
sete %al
movq -0x50(%rbp), %rsi
testw %si, %si
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x2cc6
leaq 0x379c(%rip), %rsi      # 0x6460
jmp 0x2c93
movzbl %dl, %edx
cmpl $0x1, %edx
jne 0x2d2e
movzwl %di, %ecx
movzwl %si, %r9d
movq %rbx, %rdi
movq -0x68(%rbp), %rsi
movq %r15, %rdx
movq %r14, %r8
callq 0x2d46
movl $0x5, %ecx
testb %al, %al
je 0x2cf5
movl $0x4, %ecx
movl %ecx, (%rbx)
testq %r15, %r15
je 0x2d04
movq %r15, %rdi
callq 0x2340
testq %r14, %r14
je 0x2d1f
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
leaq 0x3763(%rip), %rsi      # 0x6498
movl $0x3, %edi
xorl %eax, %eax
callq 0x4100
jmp 0x2c9f