// File: lmclaramunt[P]AES-Crypto/Cipher.cpp
// Function: Cipher::getKey(bool, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>*, unsigned char***)
void Cipher::getKey(bool encrypt, string* keyPath, unsigned char*** keyExpanded){
    unsigned char* key = new unsigned char[4*Nk];
    ifstream input(*keyPath, ios::binary);
    if(input.is_open()){        //Read key, given path to it
        for(int i=0; i < 4*Nk; i++)
            key[i] = input.get();
        input.close(); 
    } else if(encrypt) {          //Create new key if we are encrypting
        generateKey(key, Nk);
        ofstream keyFile;
        keyFile.open(*keyPath, ios::binary);
        keyFile.write((const char*)key, 4*Nk);
        keyFile.close();       
    } else {
        throw "Missing key for decryption\n";   //User should have key to decrypt
    }

    /* MEM53-CPP. Explicitly construct and destruct objects when 
       manually managing object lifetime */
    *keyExpanded = new unsigned char*[4*(Nr+1)];    //Expand the key   
    for(int j=0; j<4*(Nr+1); j++)
        (*keyExpanded)[j] = new unsigned char[4];
    keyExpansion(key, *keyExpanded);
    
    delete[] key;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x480, %rsp            # imm = 0x480
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x458(%rbp)
movl 0x18(%rax), %eax
shll $0x2, %eax
movslq %eax, %rdi
callq 0x3030
movq %rax, -0x28(%rbp)
movq -0x18(%rbp), %rsi
leaq -0x230(%rbp), %rdi
movq %rdi, -0x450(%rbp)
movl $0x4, %edx
callq 0x3430
movq -0x450(%rbp), %rdi
callq 0x3420
movb %al, -0x445(%rbp)
jmp 0x7bec
movb -0x445(%rbp), %al
testb $0x1, %al
jne 0x7bfb
jmp 0x7c81
movl $0x0, -0x240(%rbp)
movq -0x458(%rbp), %rcx
movl -0x240(%rbp), %eax
movl 0x18(%rcx), %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jge 0x7c6e
leaq -0x230(%rbp), %rdi
callq 0x3310
movl %eax, -0x45c(%rbp)
jmp 0x7c30
movl -0x45c(%rbp), %eax
movb %al, %dl
movq -0x28(%rbp), %rax
movslq -0x240(%rbp), %rcx
movb %dl, (%rax,%rcx)
movl -0x240(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x240(%rbp)
jmp 0x7c05
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
jmp 0x7e47
leaq -0x230(%rbp), %rdi
callq 0x3050
jmp 0x7c7c
jmp 0x7d54
testb $0x1, -0x9(%rbp)
je 0x7d26
movq -0x458(%rbp), %rdi
movq -0x28(%rbp), %rsi
movl 0x18(%rdi), %edx
callq 0x89b0
jmp 0x7ca0
leaq -0x440(%rbp), %rdi
callq 0x3480
jmp 0x7cae
movq -0x18(%rbp), %rsi
leaq -0x440(%rbp), %rdi
movl $0x4, %edx
callq 0x3110
jmp 0x7cc5
movq -0x458(%rbp), %rax
movq -0x28(%rbp), %rsi
movl 0x18(%rax), %eax
shll $0x2, %eax
movslq %eax, %rdx
leaq -0x440(%rbp), %rdi
callq 0x3360
jmp 0x7ce7
leaq -0x440(%rbp), %rdi
callq 0x33b0
jmp 0x7cf5
leaq -0x440(%rbp), %rdi
callq 0x33d0
jmp 0x7d52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x238(%rbp)
movl %eax, -0x23c(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x33d0
jmp 0x7e47
movl $0x8, %edi
callq 0x3120
movq %rax, %rdi
leaq 0x7a5e(%rip), %rax      # 0xf798
movq %rax, (%rdi)
movq 0xe27c(%rip), %rsi      # 0x15fc0
xorl %eax, %eax
movl %eax, %edx
callq 0x3490
jmp 0x7e5f
jmp 0x7d54
movq -0x458(%rbp), %rax
movl 0x1c(%rax), %ecx
movl %ecx, %eax
leal 0x4(,%rax,4), %eax
cltq
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
movq $-0x1, %rax
cmovoq %rax, %rdi
callq 0x3030
movq %rax, -0x468(%rbp)
jmp 0x7d8d
movq -0x468(%rbp), %rcx
movq -0x20(%rbp), %rax
movq %rcx, (%rax)
movl $0x0, -0x444(%rbp)
movq -0x458(%rbp), %rcx
movl -0x444(%rbp), %eax
movl 0x1c(%rcx), %ecx
addl $0x1, %ecx
shll $0x2, %ecx
cmpl %ecx, %eax
jge 0x7dfc
movl $0x4, %edi
callq 0x3030
movq %rax, -0x470(%rbp)
jmp 0x7dd2
movq -0x470(%rbp), %rdx
movq -0x20(%rbp), %rax
movq (%rax), %rax
movslq -0x444(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movl -0x444(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x444(%rbp)
jmp 0x7da5
movq -0x458(%rbp), %rdi
movq -0x28(%rbp), %rsi
movq -0x20(%rbp), %rax
movq (%rax), %rdx
callq 0x8d30
jmp 0x7e15
movq -0x28(%rbp), %rax
movq %rax, -0x478(%rbp)
cmpq $0x0, %rax
je 0x7e32
movq -0x478(%rbp), %rdi
callq 0x3350
leaq -0x230(%rbp), %rdi
callq 0x3060
addq $0x480, %rsp            # imm = 0x480
popq %rbp
retq
leaq -0x230(%rbp), %rdi
callq 0x3060
movq -0x238(%rbp), %rdi
callq 0x34d0
nop