// File: ethanmick[P]Blowfish482/Blowfish.cpp
// Function: Blowfish::setKey(unsigned char*)
void Blowfish::setKey(uint8_t* key){
    
    for (int i = 0; i < 18; i++) {
        pArray[i] = computeHexPi();
    }
    
    for (int i=0; i < 256; i++) {
    	s1[i] = computeHexPi();
    }
    
    for (int i=0; i < 256; i++) {
    	s2[i] = computeHexPi();
    }
    
    for (int i=0; i < 256; i++) {
    	s3[i] = computeHexPi();
    }
   
    for (int i=0; i < 256; i++) {
    	s4[i] = computeHexPi();
    }

    uint32_t key_size = keySize();
   
    uint32_t subkeyCounter = 0; 
    for (int pI=0; pI < 18; pI++) {
        
        uint32_t subkey = 0; 

        for (int i=0; i < 4; i++, subkeyCounter++) {
            if (subkeyCounter >= key_size) {
                subkeyCounter = 0;
            }
    	    subkey <<= 8;
            subkey |= (uint32_t)key[subkeyCounter];
        }

    	pArray[pI] ^= subkey;
    } 
        
    uint8_t* zeros = new uint8_t[ 8 ];
    for( int i = 0; i < 8; i++ ){
        zeros[ i ] = 0;
    }
        
    for (int i=0; i < 18; i += 2) {
        encrypt(zeros);
        pArray[i] = pack32BitWord(zeros, 0);
        pArray[i+1] = pack32BitWord(zeros, 4);
    }

    for (int i=0; i < 256; i += 2) {
        encrypt(zeros);
        s1[i] = pack32BitWord(zeros, 0);
        s1[i+1] = pack32BitWord(zeros, 4);
    } 

    for (int i=0; i < 256; i += 2) {
        encrypt(zeros);
        s2[i] = pack32BitWord(zeros, 0);
        s2[i+1] = pack32BitWord(zeros, 4);
    } 

    for (int i=0; i < 256; i += 2) {
        encrypt(zeros);
        s3[i] = pack32BitWord(zeros, 0);
        s3[i+1] = pack32BitWord(zeros, 4);
    } 

    for (int i=0; i < 256; i += 2) {
        encrypt(zeros);
        s4[i] = pack32BitWord(zeros, 0);
        s4[i+1] = pack32BitWord(zeros, 4);
    } 

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x60(%rbp)
movl $0x0, -0x14(%rbp)
cmpl $0x12, -0x14(%rbp)
jge 0x249a
movq -0x60(%rbp), %rdi
callq 0x2860
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq -0x14(%rbp), %rcx
movl %edx, 0x100c(%rax,%rcx,4)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x246f
movl $0x0, -0x18(%rbp)
cmpl $0x100, -0x18(%rbp)     # imm = 0x100
jge 0x24cc
movq -0x60(%rbp), %rdi
callq 0x2860
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq -0x18(%rbp), %rcx
movl %edx, 0xc(%rax,%rcx,4)
movl -0x18(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x18(%rbp)
jmp 0x24a1
movl $0x0, -0x1c(%rbp)
cmpl $0x100, -0x1c(%rbp)     # imm = 0x100
jge 0x2501
movq -0x60(%rbp), %rdi
callq 0x2860
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq -0x1c(%rbp), %rcx
movl %edx, 0x40c(%rax,%rcx,4)
movl -0x1c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x1c(%rbp)
jmp 0x24d3
movl $0x0, -0x20(%rbp)
cmpl $0x100, -0x20(%rbp)     # imm = 0x100
jge 0x2536
movq -0x60(%rbp), %rdi
callq 0x2860
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq -0x20(%rbp), %rcx
movl %edx, 0x80c(%rax,%rcx,4)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x2508
movl $0x0, -0x24(%rbp)
cmpl $0x100, -0x24(%rbp)     # imm = 0x100
jge 0x256b
movq -0x60(%rbp), %rdi
callq 0x2860
movl %eax, %edx
movq -0x60(%rbp), %rax
movslq -0x24(%rbp), %rcx
movl %edx, 0xc0c(%rax,%rcx,4)
movl -0x24(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
jmp 0x253d
movq -0x60(%rbp), %rdi
movq (%rdi), %rax
callq *0x8(%rax)
movl %eax, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
cmpl $0x12, -0x30(%rbp)
jge 0x2601
movl $0x0, -0x34(%rbp)
movl $0x0, -0x38(%rbp)
cmpl $0x4, -0x38(%rbp)
jge 0x25dd
movl -0x2c(%rbp), %eax
cmpl -0x28(%rbp), %eax
jb 0x25af
movl $0x0, -0x2c(%rbp)
movl -0x34(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x34(%rbp)
movq -0x10(%rbp), %rax
movl -0x2c(%rbp), %ecx
movzbl (%rax,%rcx), %eax
orl -0x34(%rbp), %eax
movl %eax, -0x34(%rbp)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x259a
movq -0x60(%rbp), %rax
movl -0x34(%rbp), %edx
movslq -0x30(%rbp), %rcx
xorl 0x100c(%rax,%rcx,4), %edx
movl %edx, 0x100c(%rax,%rcx,4)
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x2586
movl $0x8, %edi
callq 0x2030
movq %rax, -0x40(%rbp)
movl $0x0, -0x44(%rbp)
cmpl $0x8, -0x44(%rbp)
jge 0x2633
movq -0x40(%rbp), %rax
movslq -0x44(%rbp), %rcx
movb $0x0, (%rax,%rcx)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x2616
movl $0x0, -0x48(%rbp)
cmpl $0x12, -0x48(%rbp)
jge 0x269d
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x29c0
movq -0x60(%rbp), %rdi
movl %eax, %ecx
movslq -0x48(%rbp), %rax
movl %ecx, 0x100c(%rdi,%rax,4)
movq -0x40(%rbp), %rsi
movl $0x4, %edx
callq 0x29c0
movl %eax, %edx
movq -0x60(%rbp), %rax
movl -0x48(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, 0x100c(%rax,%rcx,4)
movl -0x48(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x48(%rbp)
jmp 0x263a
movl $0x0, -0x4c(%rbp)
cmpl $0x100, -0x4c(%rbp)     # imm = 0x100
jge 0x2704
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x29c0
movq -0x60(%rbp), %rdi
movl %eax, %ecx
movslq -0x4c(%rbp), %rax
movl %ecx, 0xc(%rdi,%rax,4)
movq -0x40(%rbp), %rsi
movl $0x4, %edx
callq 0x29c0
movl %eax, %edx
movq -0x60(%rbp), %rax
movl -0x4c(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, 0xc(%rax,%rcx,4)
movl -0x4c(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x4c(%rbp)
jmp 0x26a4
movl $0x0, -0x50(%rbp)
cmpl $0x100, -0x50(%rbp)     # imm = 0x100
jge 0x2771
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x29c0
movq -0x60(%rbp), %rdi
movl %eax, %ecx
movslq -0x50(%rbp), %rax
movl %ecx, 0x40c(%rdi,%rax,4)
movq -0x40(%rbp), %rsi
movl $0x4, %edx
callq 0x29c0
movl %eax, %edx
movq -0x60(%rbp), %rax
movl -0x50(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, 0x40c(%rax,%rcx,4)
movl -0x50(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x50(%rbp)
jmp 0x270b
movl $0x0, -0x54(%rbp)
cmpl $0x100, -0x54(%rbp)     # imm = 0x100
jge 0x27de
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x29c0
movq -0x60(%rbp), %rdi
movl %eax, %ecx
movslq -0x54(%rbp), %rax
movl %ecx, 0x80c(%rdi,%rax,4)
movq -0x40(%rbp), %rsi
movl $0x4, %edx
callq 0x29c0
movl %eax, %edx
movq -0x60(%rbp), %rax
movl -0x54(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, 0x80c(%rax,%rcx,4)
movl -0x54(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x54(%rbp)
jmp 0x2778
movl $0x0, -0x58(%rbp)
cmpl $0x100, -0x58(%rbp)     # imm = 0x100
jge 0x284b
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
movq (%rdi), %rax
callq *0x18(%rax)
movq -0x60(%rbp), %rdi
movq -0x40(%rbp), %rsi
xorl %edx, %edx
callq 0x29c0
movq -0x60(%rbp), %rdi
movl %eax, %ecx
movslq -0x58(%rbp), %rax
movl %ecx, 0xc0c(%rdi,%rax,4)
movq -0x40(%rbp), %rsi
movl $0x4, %edx
callq 0x29c0
movl %eax, %edx
movq -0x60(%rbp), %rax
movl -0x58(%rbp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, 0xc0c(%rax,%rcx,4)
movl -0x58(%rbp), %eax
addl $0x2, %eax
movl %eax, -0x58(%rbp)
jmp 0x27e5
addq $0x60, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)