This is a mini-AES program I have been working on partly as an independent project and partly for my internship over the past few months. I'm not sure if it works or not, when I try to run it it takes forever, I waited about 30 min and got impatient. Also, I just started programming C about 6 months ago.
I am looking for critique and *corrections* of this program. It will be much appreciated. I also realize some of my methods may not be the most efficient, I was just trying to get it done, and will also welcome any critique in this category.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
FILE *fp;
char *buffer;
char *outputBuffer;
unsigned long fileLen;
int c;
//user inputted key - tester
char* KEY = "hi";
//round keys
unsigned int w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11;
//text nibbles
unsigned int Nibble1, Nibble2, Nibble3, Nibble4;
int currentBufferPosition = 0;
unsigned int t[2][16];
void readFile(char *name) {
fp = fopen(name, "rb");
fseek(fp, 0, SEEK_END);
fileLen = ftell(fp);
fseek(fp, 0, SEEK_SET);
buffer = malloc(fileLen+1);
int i = 0;
for(i = 0;i < fileLen - 1;++i) {
(buffer[i]) = (char)fgetc(fp);;
}
fclose(fp);
}
void getNibbles() {
Nibble1 = (buffer[currentBufferPosition]) & 0xf;
Nibble2 = (buffer[currentBufferPosition] << 4) & 0xf0;
//printf("%x\n", Nibble1);
//printf("%x\n", Nibble2);
++currentBufferPosition;
Nibble3 = (buffer[currentBufferPosition]) & 0xf;
Nibble4 = (buffer[currentBufferPosition] << 4) & 0xf0;
//printf("%x\n", Nibble3);
//printf("%x\n", Nibble4);
}
void getKeyNibbles(char *k) {
w0 = (k[0]) & 0xf;
w1 = (k[0] << 4) & 0xf0;
//printf("%x\n", Nibble1);
//printf("%x\n", Nibble2);
w2 = (k[1]) & 0xf;
w3 = (k[1] << 4) & 0xf0;
//printf("%x\n", Nibble3);
//printf("%x\n", Nibble4);
}
void setChart() {
t[1][1] = 0x0;
t[1][2] = 0x1;
t[1][3] = 0x2;
t[1][4] = 0x3;
t[1][5] = 0x4;
t[1][6] = 0x5;
t[1][7] = 0x6;
t[1][8] = 0x7;
t[1][9] = 0x8;
t[1][10] = 0x9;
t[1][11] = 0xa;
t[1][12] = 0xb;
t[1][13] = 0xc;
t[1][14] = 0xd;
t[1][15] = 0xe;
t[1][16] = 0xf;
t[2][1] = 0xe;
t[2][2] = 0x4;
t[2][3] = 0xd;
t[2][4] = 0x1;
t[2][5] = 0x2;
t[2][6] = 0xf;
t[2][7] = 0xb;
t[2][8] = 0x8;
t[2][9] = 0x3;
t[2][10] = 0xa;
t[2][11] = 0x6;
t[2][12] = 0xc;
t[2][13] = 0x5;
t[2][14] = 0x9;
t[2][15] = 0x0;
t[2][16] = 0x7;
}
unsigned int nibbleSub(unsigned int n) {
int x;
for(x = 0; x < 16; x++) {
if (n == t[1][x]) {
n = t[2][x];
return n;
}
}
}
void mixColumn() {
unsigned int temp1, temp2, temp3, temp4;
temp1 = Nibble1;
temp2 = Nibble2;
temp3 = Nibble3;
temp4 = Nibble4;
Nibble1 = (0x3 * temp1) ^ (0x2 * temp2);
Nibble2 = (0x2 * temp1) ^ (0x3 * temp2);
Nibble3 = (0x3 * temp3) ^ (0x2 * temp4);
Nibble4 = (0x2 * temp3) ^ (0x3 * temp4);
}
void shiftRow() {
unsigned int temp;
temp = Nibble2;
Nibble2 = Nibble4;
Nibble4 = temp;
}
//inputted key must be 16 bits(2 chars)
void setKeys(char *s) {
unsigned int temp0, temp1, temp2, temp3;
getKeyNibbles(s);
w4 = w0 ^ nibbleSub(w3) ^ 0x1;
w5 = w1 ^ w4;
w6 = w2 ^ w5;
w7 = w3 ^ w6;
w8 = w4 ^ nibbleSub(w7) ^ 0x2;
w9 = w5 ^ w8;
w10 = w6 ^ w9;
w11 = w7 ^ w10;
}
void addKey(int round) {
if (round = 0) {
Nibble1 = Nibble1 ^ w0;
Nibble2 = Nibble2 ^ w1;
Nibble3 = Nibble3 ^ w2;
Nibble4 = Nibble4 ^ w3;
} else if (round = 1) {
Nibble1 = Nibble1 ^ w4;
Nibble2 = Nibble2 ^ w5;
Nibble3 = Nibble3 ^ w6;
Nibble4 = Nibble4 ^ w7;
} else if (round = 2) {
Nibble1 = Nibble1 ^ w8;
Nibble2 = Nibble2 ^ w9;
Nibble3 = Nibble3 ^ w10;
Nibble4 = Nibble4 ^ w11;
}
}
void encrypt(char *name) {
readFile(name);
setKeys(KEY);
setChart();
do {
getNibbles;
addKey(0);
Nibble1 = nibbleSub(Nibble1);
Nibble2 = nibbleSub(Nibble2);
Nibble3 = nibbleSub(Nibble3);
Nibble4 = nibbleSub(Nibble4);
shiftRow();
mixColumn();
addKey(1);
Nibble1 = nibbleSub(Nibble1);
Nibble2 = nibbleSub(Nibble2);
Nibble3 = nibbleSub(Nibble3);
Nibble4 = nibbleSub(Nibble4);
shiftRow();
addKey(2);
outputBuffer = outputBuffer + Nibble1 + Nibble2 + Nibble3 + Nibble4;
currentBufferPosition++;
} while (currentBufferPosition < fileLen - 1);
}
void printBuffer() {
int counter;
for (counter = 0; counter < fileLen -1; counter++) {
printf("%x", outputBuffer[counter]);
}
}
void main() {
encrypt("text");
}