// File: /workspace/github_c_makefile_star2_O2/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, -0x30(%rbp)
movq %rdx, -0x60(%rbp)
movq %rsi, %r15
movq %rdi, %rbx
movq 0x18(%rbp), %r12
movq 0x10(%rbp), %r14
movq 0x40(%rdi), %rdi
callq 0x2220
movq %rax, -0x40(%rbp)
movq %rbx, -0x48(%rbp)
movq 0x50(%rbx), %rbx
pushq $0x20
popq %rdi
callq 0x3eb5
movq %rax, %r13
movw $0x10, (%r12)
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x2120
movq %rax, (%r14)
pushq $0x10
popq %rsi
movq %rax, %rdi
callq 0x2240
testl %eax, %eax
je 0x3296
movq (%r14), %rsi
movzwl (%r12), %edx
movq %r15, %rdi
movq %r13, %rcx
callq 0x405a
testb %al, %al
je 0x329f
movq -0x30(%rbp), %rax
movw $0xc, (%rax)
pushq $0x1
popq %rdi
pushq $0xc
popq %rsi
callq 0x2120
movq -0x60(%rbp), %r14
movq %rax, (%r14)
pushq $0xc
popq %rsi
movq %rax, %rdi
callq 0x2240
testl %eax, %eax
movq -0x40(%rbp), %r12
je 0x32b9
callq 0x2110
testq %rax, %rax
je 0x32c2
movq %rax, %r15
callq 0x2280
movq %r15, %rdi
movq %rax, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x20f0
testl %eax, %eax
je 0x3303
movq -0x30(%rbp), %rax
movzwl (%rax), %edx
pushq $0x9
popq %rsi
movq %r15, %rdi
xorl %ecx, %ecx
callq 0x2250
testl %eax, %eax
je 0x330c
movq %r13, -0x30(%rbp)
movq (%r13), %rcx
movq (%r14), %r8
xorl %r13d, %r13d
movq %r15, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x20f0
testl %eax, %eax
je 0x3315
movslq %r13d, %r14
leaq (%r12,%r14), %rsi
movq 0x8(%rbx), %rcx
movzwl 0x2(%rbx), %r8d
movq %r15, %rdi
leaq -0x34(%rbp), %rdx
callq 0x2060
testl %eax, %eax
je 0x331e
movslq -0x34(%rbp), %r13
addq %r14, %r13
movq 0x10(%rbx), %rbx
testq %rbx, %rbx
jne 0x31e8
movq %r12, %rsi
addq %r13, %rsi
leaq -0x34(%rbp), %rdx
movq %r15, %rdi
callq 0x2290
testl %eax, %eax
je 0x334e
movslq -0x34(%rbp), %rbx
movq -0x50(%rbp), %rax
movw $0x10, (%rax)
pushq $0x1
popq %rdi
pushq $0x10
popq %rsi
callq 0x2120
movq -0x58(%rbp), %rcx
movq %rax, (%rcx)
pushq $0x10
popq %rdx
movq %r15, %rdi
movl %edx, %esi
movq %rax, %rcx
callq 0x2250
testl %eax, %eax
je 0x3357
movslq %r13d, %rax
addq %rax, %rbx
movq -0x48(%rbp), %rax
movq 0x50(%rax), %rdi
movq -0x30(%rbp), %r13
testq %rdi, %rdi
je 0x3327
movq 0x10(%rdi), %r14
callq 0x39a3
movq %r14, %rdi
jmp 0x327f
leaq 0x222f(%rip), %rsi      # 0x54cc
jmp 0x32a6
leaq 0x2257(%rip), %rsi      # 0x54fd
xorl %r15d, %r15d
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x37ac
movq -0x40(%rbp), %r12
jmp 0x32d6
leaq 0x2276(%rip), %rsi      # 0x5536
jmp 0x32c9
leaq 0x229c(%rip), %rsi      # 0x5565
xorl %r15d, %r15d
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x37ac
movq %r12, %rdi
callq 0x21d0
xorl %r14d, %r14d
movq %r15, %rdi
callq 0x21b0
movq %r13, %rdi
callq 0x4035
movl %r14d, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x227f(%rip), %rsi      # 0x5589
jmp 0x32cc
leaq 0x2294(%rip), %rsi      # 0x55a7
jmp 0x32cc
leaq 0x22b7(%rip), %rsi      # 0x55d3
jmp 0x335e
leaq 0x22dc(%rip), %rsi      # 0x5601
jmp 0x335e
callq 0x394c
movq -0x48(%rbp), %rcx
movq %rax, 0x50(%rcx)
movw %bx, 0x2(%rax)
andw $0x0, (%rax)
andq $0x0, 0x10(%rax)
movq %r12, 0x8(%rax)
movq %rbx, 0x40(%rcx)
movb $0x1, %r14b
jmp 0x32e1
leaq 0x22cb(%rip), %rsi      # 0x5620
jmp 0x335e
leaq 0x22ee(%rip), %rsi      # 0x564c
pushq $0x4
popq %rdi
xorl %eax, %eax
callq 0x37ac
movq -0x30(%rbp), %r13
jmp 0x32d6
nopl (%rax)