// File: 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
subq $0x1440, %rsp           # imm = 0x1440
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movl -0x14(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x20(%rbp)
leaq 0xf(,%rax,4), %rdx
andq $-0x10, %rdx
movq %rsp, %rcx
subq %rdx, %rcx
movq %rcx, -0x1428(%rbp)
movq %rcx, %rsp
movq %rax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x14(%rbp), %eax
shll $0x5, %eax
movslq %eax, %rdi
callq 0x3030
movq %rax, -0x38(%rbp)
leaq -0x13e1(%rbp), %rdi
movq %rdi, -0x1420(%rbp)
callq 0x34e0
movq -0x1420(%rbp), %rdx
leaq 0x6d8a(%rip), %rsi      # 0xf7b4
leaq -0x13e0(%rbp), %rdi
callq 0x4670
jmp 0x8a38
leaq -0x13c0(%rbp), %rdi
leaq -0x13e0(%rbp), %rsi
callq 0xb580
jmp 0x8a4d
leaq -0x13e0(%rbp), %rdi
callq 0x3180
leaq -0x13e1(%rbp), %rdi
callq 0x32c0
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %esi
shll $0x5, %esi
movl $0x0, -0x13f8(%rbp)
leaq -0x13f8(%rbp), %rdx
callq 0xb5b0
jmp 0x8a87
movl $0x0, -0x13fc(%rbp)
movl -0x13fc(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8b42
leaq -0x13c0(%rbp), %rdi
callq 0xb600
movl %eax, -0x142c(%rbp)
jmp 0x8ab4
movq -0x1428(%rbp), %rax
movl -0x142c(%rbp), %edx
movslq -0x13fc(%rbp), %rcx
movl %edx, (%rax,%rcx,4)
movl -0x13fc(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x13fc(%rbp)
jmp 0x8a91
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13f0(%rbp)
movl %eax, -0x13f4(%rbp)
jmp 0x8b0e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13f0(%rbp)
movl %eax, -0x13f4(%rbp)
leaq -0x13e0(%rbp), %rdi
callq 0x3180
leaq -0x13e1(%rbp), %rdi
callq 0x32c0
jmp 0x8d21
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x13f0(%rbp)
movl %eax, -0x13f4(%rbp)
leaq -0x13c0(%rbp), %rdi
callq 0xb670
jmp 0x8d21
movl $0x1f, -0x1400(%rbp)
cmpl $0x0, -0x1400(%rbp)
jl 0x8be9
movl $0x0, -0x1404(%rbp)
movl -0x1404(%rbp), %eax
cmpl -0x14(%rbp), %eax
jge 0x8bd3
movq -0x1428(%rbp), %rax
movslq -0x1404(%rbp), %rcx
movl (%rax,%rcx,4), %eax
movl -0x1400(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
andl %ecx, %eax
movl -0x1400(%rbp), %ecx
movl $0x1, %edx
shll %cl, %edx
movl %edx, %ecx
cmpl %ecx, %eax
jne 0x8bc0
movq -0x38(%rbp), %rax
movl -0x1404(%rbp), %ecx
shll $0x5, %ecx
addl $0x1f, %ecx
subl -0x1400(%rbp), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
jmp 0x8bc2
movl -0x1404(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1404(%rbp)
jmp 0x8b63
jmp 0x8bd5
movl -0x1400(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x1400(%rbp)
jmp 0x8b4c
movl $0x0, -0x1408(%rbp)
movl -0x1408(%rbp), %eax
movl -0x14(%rbp), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jge 0x8ca8
movb $0x0, -0x1409(%rbp)
movl $0x0, -0x1410(%rbp)
cmpl $0x8, -0x1410(%rbp)
jge 0x8c73
movq -0x38(%rbp), %rax
movl -0x1408(%rbp), %ecx
shll $0x3, %ecx
addl -0x1410(%rbp), %ecx
movslq %ecx, %rcx
testb $0x1, (%rax,%rcx)
je 0x8c60
movl $0x7, %ecx
subl -0x1410(%rbp), %ecx
movl $0x1, %eax
shll %cl, %eax
movl %eax, %ecx
movzbl -0x1409(%rbp), %eax
addl %ecx, %eax
movb %al, -0x1409(%rbp)
jmp 0x8c62
movl -0x1410(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1410(%rbp)
jmp 0x8c18
movzbl -0x1409(%rbp), %esi
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movl %ecx, %edx
addl $0x1, %edx
movl %edx, -0x2c(%rbp)
movl %ecx, %ecx
movzbl (%rax,%rcx), %edx
addl %esi, %edx
movb %dl, (%rax,%rcx)
movl -0x1408(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1408(%rbp)
jmp 0x8bf3
movq -0x38(%rbp), %rdi
movl -0x14(%rbp), %esi
shll $0x5, %esi
movl $0x0, -0x1414(%rbp)
leaq -0x1414(%rbp), %rdx
callq 0xb5b0
jmp 0x8cca
movq -0x1428(%rbp), %rdi
movl -0x14(%rbp), %esi
movl $0x0, -0x1418(%rbp)
leaq -0x1418(%rbp), %rdx
callq 0xb620
jmp 0x8cec
movq -0x38(%rbp), %rax
movq %rax, -0x1438(%rbp)
cmpq $0x0, %rax
je 0x8d09
movq -0x1438(%rbp), %rdi
callq 0x3350
leaq -0x13c0(%rbp), %rdi
callq 0xb670
movq -0x20(%rbp), %rax
movq %rax, %rsp
movq %rbp, %rsp
popq %rbp
retq
movq -0x13f0(%rbp), %rdi
callq 0x34d0
nopl (%rax)