// File: /workspace/github_c_makefile_star2_O0/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
subq $0x40, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movl $0x3, (%rax)
movb $0x0, -0x11(%rbp)
movq $0x0, -0x20(%rbp)
movw $0x0, -0x22(%rbp)
movq $0x0, -0x30(%rbp)
movw $0x0, -0x32(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x10(%rbp)
movb %al, -0x35(%rbp)
je 0x2ba4
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
setne %al
movb %al, -0x35(%rbp)
movb -0x35(%rbp), %al
andb $0x1, %al
movb %al, -0x33(%rbp)
movb $0x0, -0x34(%rbp)
movzbl -0x34(%rbp), %eax
movq -0x8(%rbp), %rcx
movzbl 0x28(%rcx), %ecx
cmpl %ecx, %eax
jge 0x2cdb
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movl (%rax), %eax
movl %eax, -0x3c(%rbp)
subl $0x1, %eax
je 0x2bfb
jmp 0x2be0
movl -0x3c(%rbp), %eax
subl $0x2, %eax
je 0x2c32
jmp 0x2bea
movl -0x3c(%rbp), %eax
subl $0x3, %eax
je 0x2c7e
jmp 0x2cca
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movzwl 0x4(%rax), %eax
cmpl $0x1, %eax
jne 0x2c2d
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rax
movb (%rax), %al
movb %al, -0x11(%rbp)
jmp 0x2ccc
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movw 0x4(%rax), %ax
movw %ax, -0x22(%rbp)
movzwl -0x22(%rbp), %eax
movl %eax, %edi
callq 0x23e0
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rsi
movzwl -0x22(%rbp), %eax
movl %eax, %edx
callq 0x2270
jmp 0x2ccc
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movw 0x4(%rax), %ax
movw %ax, -0x32(%rbp)
movzwl -0x32(%rbp), %eax
movl %eax, %edi
callq 0x23e0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
movq -0x8(%rbp), %rax
movq 0x30(%rax), %rax
movzbl -0x34(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq 0x8(%rax), %rsi
movzwl -0x32(%rbp), %eax
movl %eax, %edx
callq 0x2270
jmp 0x2ccc
jmp 0x2ccc
jmp 0x2cce
movb -0x34(%rbp), %al
addb $0x1, %al
movb %al, -0x34(%rbp)
jmp 0x2bb0
movzbl -0x11(%rbp), %eax
cmpl $0x0, %eax
jne 0x2d07
testb $0x1, -0x33(%rbp)
je 0x2d02
movl $0x3, %edi
leaq 0x46f7(%rip), %rsi      # 0x73ed
movb $0x0, %al
callq 0x4d70
jmp 0x2dba
jmp 0x2dc6
testb $0x1, -0x33(%rbp)
jne 0x2d25
movl $0x3, %edi
leaq 0x470f(%rip), %rsi      # 0x7428
movb $0x0, %al
callq 0x4d70
jmp 0x2dba
cmpq $0x0, -0x20(%rbp)
je 0x2d35
movzwl -0x22(%rbp), %eax
cmpl $0x0, %eax
jne 0x2d4a
movl $0x3, %edi
leaq 0x4723(%rip), %rsi      # 0x7464
movb $0x0, %al
callq 0x4d70
jmp 0x2dba
cmpq $0x0, -0x30(%rbp)
je 0x2d5a
movzwl -0x32(%rbp), %eax
cmpl $0x0, %eax
jne 0x2d6f
movl $0x3, %edi
leaq 0x472a(%rip), %rsi      # 0x7490
movb $0x0, %al
callq 0x4d70
jmp 0x2dba
movb -0x11(%rbp), %al
subb $0x1, %al
jne 0x2da3
jmp 0x2d78
movq -0x8(%rbp), %rdi
movq -0x10(%rbp), %rsi
movq -0x20(%rbp), %rdx
movw -0x22(%rbp), %ax
movq -0x30(%rbp), %r8
movzwl %ax, %ecx
movzwl -0x32(%rbp), %r9d
callq 0x2e10
testb $0x1, %al
jne 0x2d9f
jmp 0x2da1
jmp 0x2dc6
jmp 0x2dba
movzbl -0x11(%rbp), %edx
movl $0x3, %edi
leaq 0x4715(%rip), %rsi      # 0x74c8
movb $0x0, %al
callq 0x4d70
movq -0x8(%rbp), %rax
movl $0x5, (%rax)
jmp 0x2dd0
movq -0x8(%rbp), %rax
movl $0x4, (%rax)
cmpq $0x0, -0x20(%rbp)
je 0x2dec
jmp 0x2dd9
movq -0x20(%rbp), %rdi
callq 0x2350
movq $0x0, -0x20(%rbp)
jmp 0x2dec
cmpq $0x0, -0x30(%rbp)
je 0x2e08
jmp 0x2df5
movq -0x30(%rbp), %rdi
callq 0x2350
movq $0x0, -0x30(%rbp)
jmp 0x2e08
addq $0x40, %rsp
popq %rbp
retq
nop