// File: justinpajak[P]Sum_SGX/aes.cpp
// Function: main
int main(int argc, char *argv[]) {

	/* 256 bit key */
    unsigned char *key = (unsigned char *)"01234567890123456789012345678901";

    /* 128 bit IV */
    unsigned char *iv = (unsigned char *)"0123456789012345";

    /* Plaintext and Ciphertext declaration */
    unsigned char *plaintext;
    unsigned char ciphertext[128];
    int ciphertext_len, decryptedtext_len;

    /* Loop through data.txt file and encrypt each float */
    FILE* data = fopen("data.txt", "r+");
    if (!data) {
        fprintf(stderr, "Unable to open file: %s\n", strerror(errno));
        return 1;
    }
    FILE* enc_data = fopen("enc.txt", "w+");
    if (!enc_data) {
        fprintf(stderr, "Unable to open file: %s\n", strerror(errno));
        return 1;
    }
    char buffer[BUFSIZ];
    while (fgets(buffer, BUFSIZ, data)) {
        buffer[16] = '\0';
        plaintext = (unsigned char*)buffer;
        ciphertext_len = encrypt(plaintext, strlen((char*)plaintext), key, iv, ciphertext);
        fwrite((const char*)ciphertext, 128, 1, enc_data);
    };


    // test protocol: Decrypt all of enc.txt to dec.txt 
    /*FILE* dec_data = fopen("dec.txt", "w");
    if (!dec_data) {
        fprintf(stderr, "Unable to open file: %s\n", strerror(errno));
        return 1;
    }
    fseek(enc_data, 0L, SEEK_SET);
    while(fread((char*)ciphertext, 128, 1, enc_data)) {
        unsigned char decryptedtext[128];
        decryptedtext_len = decrypt(ciphertext, ciphertext_len, key, iv, decryptedtext);
        decryptedtext[decryptedtext_len] = '\0';
        fputs((const char*)decryptedtext, dec_data);
        fputs("\n", dec_data);
    }

    fclose(dec_data);*/

    fclose(data);
    fclose(enc_data);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20f0, %rsp           # imm = 0x20F0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq 0xd44(%rip), %rax       # 0x2004
movq %rax, -0x18(%rbp)
leaq 0xd5a(%rip), %rax       # 0x2025
movq %rax, -0x20(%rbp)
leaq 0xd60(%rip), %rdi       # 0x2036
leaq 0xd62(%rip), %rsi       # 0x203f
callq 0x1100
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
jne 0x1334
movq 0x2cde(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq %rax, -0x20d8(%rbp)
callq 0x1030
movl (%rax), %edi
callq 0x1060
movq -0x20d8(%rbp), %rdi
movq %rax, %rdx
leaq 0xd21(%rip), %rsi       # 0x2042
movb $0x0, %al
callq 0x1110
movl $0x1, -0x4(%rbp)
jmp 0x1443
leaq 0xd20(%rip), %rdi       # 0x205b
leaq 0xd21(%rip), %rsi       # 0x2063
callq 0x1100
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
jne 0x1399
movq 0x2c79(%rip), %rax      # 0x3fd8
movq (%rax), %rax
movq %rax, -0x20e0(%rbp)
callq 0x1030
movl (%rax), %edi
callq 0x1060
movq -0x20e0(%rbp), %rdi
movq %rax, %rdx
leaq 0xcbc(%rip), %rsi       # 0x2042
movb $0x0, %al
callq 0x1110
movl $0x1, -0x4(%rbp)
jmp 0x1443
jmp 0x139b
leaq -0x20d0(%rbp), %rdi
movq -0xc0(%rbp), %rdx
movl $0x2000, %esi           # imm = 0x2000
callq 0x1130
cmpq $0x0, %rax
je 0x1424
movb $0x0, -0x20c0(%rbp)
leaq -0x20d0(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rax
movq %rax, -0x20e8(%rbp)
movq -0x28(%rbp), %rdi
callq 0x1080
movq -0x20e8(%rbp), %rdi
movl %eax, %esi
movq -0x18(%rbp), %rdx
movq -0x20(%rbp), %rcx
leaq -0xb0(%rbp), %r8
callq 0x1450
movl %eax, -0xb4(%rbp)
leaq -0xb0(%rbp), %rdi
movq -0xc8(%rbp), %rcx
movl $0x80, %esi
movl $0x1, %edx
callq 0x1150
jmp 0x139b
movq -0xc0(%rbp), %rdi
callq 0x10e0
movq -0xc8(%rbp), %rdi
callq 0x10e0
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x20f0, %rsp           # imm = 0x20F0
popq %rbp
retq
nop