// File: /workspace/github_cpp_makefile_star2_O2/lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::generateKey(unsigned char*, int)
void Cipher::generateKey(unsigned char* buff, int _Nk) {
    unsigned int rand[_Nk], x=0;
    bool* key2 = new bool[32*_Nk];
    
    std::random_device rd("/dev/urandom");
    std::fill_n(key2, 32*_Nk, 0);

    /* Generate Nk 32 bits INTs */
    for (int i=0; i<_Nk; i++)
        rand[i] = rd();

    /* Saving the binary sequence */
    for (int i=31; i>=0; i--) {
        for (int j=0; j<_Nk; j++) {
            if((rand[j] & 1<<i) == 1<<i)
                key2[32*j+31-i]=1;
        }
    }
    for(int i=0; i<4*_Nk; i++) {
        unsigned char ch = 0;
        for (int j=0; j<8; j++)
            if(key2[8*i+j])
                ch += 1<<(7-j);
        buff[x++] += ch;
    }
    
    /* Local variables cleanup as it can leave traces in memory */
    std::fill_n(key2, 32*_Nk, 0);
    std::fill_n(rand, _Nk, 0);
    delete[] key2, rand;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x13c8, %rsp           # imm = 0x13C8
movq %rsi, -0x48(%rbp)
movl %edx, %eax
movq %rsp, %r14
leaq 0xf(,%rax,4), %rax
andq $-0x10, %rax
subq %rax, %r14
movq %r14, %rsp
movq %rdx, -0x38(%rbp)
movl %edx, %ebx
shll $0x5, %ebx
movslq %ebx, %rdi
callq 0x2030
movq %rax, %r15
leaq 0x33be(%rip), %rsi      # 0x7796
leaq -0x68(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x2dc2
leaq -0x13f0(%rbp), %rdi
leaq -0x68(%rbp), %rsi
callq 0x2380
leaq -0x68(%rbp), %r13
movq %r13, %rdi
callq 0x20c0
andl $0x0, (%r13)
leaq -0x68(%rbp), %rdx
movq %r15, %rdi
movl %ebx, -0x3c(%rbp)
movl %ebx, %esi
callq 0x5f6e
xorl %r12d, %r12d
movq -0x38(%rbp), %rax
testl %eax, %eax
movl $0x0, %ebx
cmovgl %eax, %ebx
leaq -0x13f0(%rbp), %r13
cmpq %r12, %rbx
je 0x4445
movq %r13, %rdi
callq 0x22c0
movl %eax, (%r14,%r12,4)
incq %r12
jmp 0x442f
pushq $0x1f
popq %rcx
xorl %edx, %edx
pushq $0x1
popq %rax
movabsq $0x100000000, %rsi      # imm = 0x100000000
movabsq $0x2000000000, %rdi     # imm = 0x2000000000
testq %rcx, %rcx
js 0x4499
movl %eax, %r8d
shll %cl, %r8d
movq %rdx, %r9
xorl %r10d, %r10d
cmpq %r10, %rbx
je 0x4491
testl %r8d, (%r14,%r10,4)
je 0x4489
movq %r9, %r11
sarq $0x20, %r11
movb $0x1, (%r15,%r11)
incq %r10
addq %rdi, %r9
jmp 0x4472
decq %rcx
addq %rsi, %rdx
jmp 0x4461
movq -0x38(%rbp), %rcx
leal (,%rcx,4), %edx
xorl %esi, %esi
testl %edx, %edx
cmovlel %esi, %edx
pushq $0x7
popq %rdi
xorl %r8d, %r8d
cmpq %rdx, %r8
je 0x44f5
leal (,%r8,8), %r9d
addq %r15, %r9
xorl %r10d, %r10d
movl %edi, %ecx
xorl %r11d, %r11d
cmpq $0x8, %r10
je 0x44e8
movl %eax, %ebx
shll %cl, %ebx
cmpb $0x0, (%r9,%r10)
cmovel %esi, %ebx
addb %r11b, %bl
incq %r10
decl %ecx
movl %ebx, %r11d
jmp 0x44c9
movq -0x48(%rbp), %rcx
addb %r11b, (%rcx,%r8)
incq %r8
jmp 0x44b1
leaq -0x68(%rbp), %rdx
andl $0x0, (%rdx)
movq %r15, %rdi
movl -0x3c(%rbp), %esi
callq 0x5f6e
leaq -0x68(%rbp), %rdx
andl $0x0, (%rdx)
movq %r14, %rdi
movq -0x38(%rbp), %rsi
callq 0x5f9b
movq %r15, %rdi
callq 0x2270
leaq -0x13f0(%rbp), %rdi
callq 0x5846
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4556
jmp 0x4556
jmp 0x4556
movq %rax, %rbx
leaq -0x68(%rbp), %rdi
callq 0x20c0
jmp 0x4565
movq %rax, %rbx
jmp 0x4565
movq %rax, %rbx
leaq -0x13f0(%rbp), %rdi
callq 0x5846
movq %rbx, %rdi
callq 0x23d0
nop