// File: /workspace/github_c_makefile_star2_O1/TimeToogo[P]ff-proxy/client/c/crypto.c
// Function: ff_client_encrypt_request_aes_256_gcm_pbkdf2
bool ff_client_encrypt_request_aes_256_gcm_pbkdf2(
    struct ff_request *request,
    struct ff_encryption_config *config,
    uint8_t **iv,
    uint16_t *iv_len,
    uint8_t **tag,
    uint16_t *tag_len,
    uint8_t **salt,
    uint16_t *salt_len)
{
    EVP_CIPHER_CTX *ctx = NULL;
    int len;
    bool ret_val;

    uint8_t *ciphertext_buff = malloc(request->payload_length * sizeof(uint8_t));
    int ciphertext_len = 0;
    struct ff_request_payload_node *payload_chunk = request->payload;
    struct ff_derived_key *key = ff_derived_key_alloc(32);

    *salt_len = 16;
    *salt = (uint8_t *)calloc(1, *salt_len);

    if (!RAND_bytes(*salt, *salt_len))
    {
        ff_log(FF_ERROR, "Failed to generate salt while encrypting request");
        goto error;
    }

    if (!ff_derive_key_pbkdf2(config, *salt, *salt_len, key))
    {
        ff_log(FF_ERROR, "Failed to derive encryption key while encrypting request");
        goto error;
    }

    *iv_len = 12;
    *iv = calloc(1, *iv_len);

    if (!RAND_bytes(*iv, *iv_len))
    {
        ff_log(FF_ERROR, "Failed to generate IV while encrypting request");
        goto error;
    }

    if (!(ctx = EVP_CIPHER_CTX_new()))
    {
        ff_log(FF_ERROR, "Failed to create new OpenSSL cipher");
        goto error;
    }

    if (!EVP_EncryptInit_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_EncryptInit_ex(ctx, NULL, NULL, key->key, *iv))
    {
        ff_log(FF_ERROR, "Failed to init OpenSSL cipher with key and IV");
        goto error;
    }

    do
    {
        if (!EVP_EncryptUpdate(ctx, ciphertext_buff + ciphertext_len, &len, payload_chunk->value, payload_chunk->length))
        {
            ff_log(FF_ERROR, "Failed encrypt request payload");
            goto error;
        }

        ciphertext_len += len;

    } while ((payload_chunk = payload_chunk->next) != NULL);

    if (!EVP_EncryptFinal_ex(ctx, ciphertext_buff + ciphertext_len, &len))
    {
        ff_log(FF_ERROR, "Failed encrypt and finalize request payload");
        goto error;
    }

    ciphertext_len += len;
    *tag_len = 16;
    *tag = calloc(1, 16);

    if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, *tag_len, *tag))
    {
        ff_log(FF_ERROR, "Failed to get cipher tag");
        goto error;
    }

    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 = ciphertext_len;
    request->payload->offset = 0;
    request->payload->next = NULL;
    request->payload->value = ciphertext_buff;
    request->payload_length = ciphertext_len;
    goto done;

error:
    FREE(ciphertext_buff);
    ret_val = false;
    goto cleanup;

done:
    ret_val = true;
    goto cleanup;

cleanup:
    EVP_CIPHER_CTX_free(ctx);
    ff_derived_key_free(key);

    return ret_val;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x38, %rsp
movq %r9, -0x50(%rbp)
movq %r8, -0x58(%rbp)
movq %rcx, -0x60(%rbp)
movq %rdx, -0x30(%rbp)
movq %rsi, %r12
movq %rdi, %rbx
movq 0x18(%rbp), %r13
movq 0x10(%rbp), %r14
movq 0x40(%rdi), %rdi
callq 0x2220
movq %rax, -0x40(%rbp)
movq %rbx, -0x48(%rbp)
movq 0x50(%rbx), %rbx
movl $0x20, %edi
callq 0x444f
movq %rax, %r15
movw $0x10, (%r13)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2120
movq %rax, (%r14)
movq %rax, %rdi
movl $0x10, %esi
callq 0x2240
testl %eax, %eax
je 0x3610
movq (%r14), %rsi
movzwl (%r13), %edx
movq %r12, %rdi
movq %r15, %rcx
callq 0x462b
testb %al, %al
je 0x3619
movq -0x60(%rbp), %r12
movw $0xc, (%r12)
movl $0x1, %edi
movl $0xc, %esi
callq 0x2120
movq -0x30(%rbp), %r14
movq %rax, (%r14)
movq %rax, %rdi
movl $0xc, %esi
callq 0x2240
testl %eax, %eax
je 0x3622
callq 0x2110
testq %rax, %rax
je 0x362b
movq %rax, %r13
callq 0x2280
movq %r13, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20f0
testl %eax, %eax
je 0x3672
movzwl (%r12), %edx
movq %r13, %rdi
movl $0x9, %esi
xorl %ecx, %ecx
callq 0x2250
testl %eax, %eax
je 0x367b
movq (%r15), %rcx
movq (%r14), %r8
movq %r13, %rdi
xorl %r13d, %r13d
movq %rdi, -0x30(%rbp)
xorl %esi, %esi
xorl %edx, %edx
callq 0x20f0
testl %eax, %eax
je 0x3684
movq %r15, %r12
leaq 0x20e6(%rip), %r14      # 0x5601
movslq %r13d, %r15
movq -0x40(%rbp), %rax
leaq (%rax,%r15), %rsi
movq 0x8(%rbx), %rcx
movzwl 0x2(%rbx), %r8d
movq -0x30(%rbp), %r13
movq %r13, %rdi
leaq -0x34(%rbp), %rdx
callq 0x2060
testl %eax, %eax
je 0x3691
movslq -0x34(%rbp), %r13
addq %r15, %r13
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x351b
movq -0x40(%rbp), %rsi
addq %r13, %rsi
leaq -0x34(%rbp), %rdx
movq -0x30(%rbp), %rbx
movq %rbx, %rdi
callq 0x2290
testl %eax, %eax
je 0x3696
movslq -0x34(%rbp), %r14
movq -0x50(%rbp), %rax
movw $0x10, (%rax)
movl $0x1, %edi
movl $0x10, %esi
callq 0x2120
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
movq %rbx, %rdi
movl $0x10, %esi
movl $0x10, %edx
movq %rax, %rcx
callq 0x2250
testl %eax, %eax
je 0x36a5
movslq %r13d, %rax
addq %rax, %r14
movq %r14, %r13
movq -0x48(%rbp), %rbx
movq 0x50(%rbx), %rdi
testq %rdi, %rdi
movq %r12, %r15
je 0x35e0
movq 0x10(%rdi), %r14
callq 0x3e71
movq %r14, %rdi
testq %r14, %r14
jne 0x35cf
callq 0x3e15
movq %rax, 0x50(%rbx)
movw %r13w, 0x2(%rax)
movw $0x0, (%rax)
movq $0x0, 0x10(%rax)
movq -0x40(%rbp), %rcx
movq %rcx, 0x8(%rax)
movq %r13, 0x40(%rbx)
movb $0x1, %r12b
movq -0x30(%rbp), %r13
jmp 0x3650
leaq 0x1eb5(%rip), %r14      # 0x54cc
jmp 0x3632
leaq 0x1edd(%rip), %r14      # 0x54fd
jmp 0x3632
leaq 0x1f0d(%rip), %r14      # 0x5536
jmp 0x3632
leaq 0x1f33(%rip), %r14      # 0x5565
xorl %r13d, %r13d
xorl %r12d, %r12d
movl $0x4, %edi
movq %r14, %rsi
xorl %eax, %eax
callq 0x3c64
movq -0x40(%rbp), %rdi
callq 0x21d0
movq %r13, %rdi
callq 0x21b0
movq %r15, %rdi
callq 0x45fa
movl %r12d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1f10(%rip), %r14      # 0x5589
jmp 0x3635
leaq 0x1f25(%rip), %r14      # 0x55a7
jmp 0x3635
leaq 0x1f48(%rip), %r14      # 0x55d3
movq -0x30(%rbp), %r13
jmp 0x3635
movq %r12, %r15
jmp 0x3635
leaq 0x1f83(%rip), %r14      # 0x5620
movq %r12, %r15
movq %rbx, %r13
jmp 0x3635
leaq 0x1fa0(%rip), %r14      # 0x564c
movq %r12, %r15
jmp 0x368b
nopl (%rax)