// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rdi, %rbx
movl $0x3, (%rdi)
testq %rsi, %rsi
movq %rsi, -0x60(%rbp)
je 0x29bf
cmpq $0x0, (%rsi)
setne %al
movl %eax, -0x2c(%rbp)
jmp 0x29c6
movl $0x0, -0x2c(%rbp)
movzbl 0x28(%rbx), %r15d
xorl %r14d, %r14d
movq $0x0, -0x50(%rbp)
movq $0x0, -0x48(%rbp)
movq $0x0, -0x58(%rbp)
movq $0x0, -0x40(%rbp)
movq $0x0, -0x38(%rbp)
cmpq %r14, %r15
je 0x2a83
movq 0x30(%rbx), %rax
movq (%rax,%r14,8), %r12
movl (%r12), %eax
cmpl $0x3, %eax
je 0x2a51
cmpl $0x2, %eax
je 0x2a30
cmpl $0x1, %eax
jne 0x2a7b
cmpw $0x1, 0x4(%r12)
jne 0x2a7b
movq 0x8(%r12), %rax
movb (%rax), %al
movq %rax, -0x38(%rbp)
jmp 0x2a7b
movzwl 0x4(%r12), %eax
movq %rax, -0x48(%rbp)
movzwl %ax, %r13d
movq %r13, %rdi
callq 0x23c0
movq 0x8(%r12), %rsi
movq %rax, -0x50(%rbp)
jmp 0x2a70
movzwl 0x4(%r12), %eax
movq %rax, -0x40(%rbp)
movzwl %ax, %r13d
movq %r13, %rdi
callq 0x23c0
movq 0x8(%r12), %rsi
movq %rax, -0x58(%rbp)
movq %rax, %rdi
movq %r13, %rdx
callq 0x2260
incq %r14
jmp 0x29f6
cmpb $0x0, -0x38(%rbp)
je 0x2ab5
cmpb $0x0, -0x2c(%rbp)
movq -0x58(%rbp), %r14
movq -0x50(%rbp), %r15
je 0x2acc
testq %r15, %r15
sete %al
cmpw $0x0, -0x48(%rbp)
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x2ae1
leaq 0x2981(%rip), %rsi      # 0x5434
jmp 0x2ad3
cmpb $0x0, -0x2c(%rbp)
movq -0x58(%rbp), %r14
movq -0x50(%rbp), %r15
je 0x2b26
leaq 0x28f3(%rip), %rsi      # 0x53bd
jmp 0x2ad3
leaq 0x2925(%rip), %rsi      # 0x53f8
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x3db4
pushq $0x5
jmp 0x2b28
testq %r14, %r14
sete %al
cmpw $0x0, -0x40(%rbp)
sete %cl
orb %al, %cl
cmpb $0x1, %cl
jne 0x2aff
leaq 0x2963(%rip), %rsi      # 0x5460
jmp 0x2ad3
movq -0x38(%rbp), %rax
cmpb $0x1, %al
jne 0x2b49
movzwl -0x48(%rbp), %ecx
movzwl -0x40(%rbp), %r9d
movq %rbx, %rdi
movq -0x60(%rbp), %rsi
movq %r15, %rdx
movq %r14, %r8
callq 0x2b62
testb %al, %al
je 0x2add
pushq $0x4
popq %rax
movl %eax, (%rbx)
movq %r15, %rdi
callq 0x2340
movq %r14, %rdi
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2340
movzbl %al, %edx
leaq 0x2945(%rip), %rsi      # 0x5498
pushq $0x3
popq %rdi
xorl %eax, %eax
callq 0x3db4
jmp 0x2add