static uint32_t *expand_key(uint32_t *key, int *status) { int i; uint32_t temp, *W; if((W = malloc(Nb * (Nr + 1) * sizeof(uint32_t))) == NULL) { *status = LNC_ERR_MALLOC; return NULL; } for(i = 0; i < Nk; i++) W[i] = key[i]; for(i = Nk; i < Nb * (Nr + 1); i++) { temp = W[i - 1]; if(i % Nk == 0) { temp = sub_byte(rot_byte(temp)) ^ (rcon[i / Nk] << 24); } #if Nk > 6 else if(i % Nk == 4) temp = sub_byte(temp); #endif W[i] = W[i - Nk] ^ temp; } *status = LNC_OK; return W; }
static void *key_expansion(uint32_t *key, uint32_t *w) { int i; uint32_t temp; for(i = 0; i < NK; i++) w[i] = key[i]; for(i = NK; i < NB * (NR + 1); i++) { temp = w[i - 1]; if(i % NK == 0) { temp = sub_byte(rot_byte(temp)) ^ (rcon[i / NK] << 24); } #if NK > 6 else if(i % NK == 4) temp = sub_byte(temp); #endif w[i] = w[i - NK] ^ temp; } return w; }
int main(int before_gen_k[4][4], int round){ /*get key into before_gen_k, get the round of encryption*/ int r_con[4][10] = {{0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; /*Just a tested matrix*/ /*int mat[4][4] = {{0x2b, 0x28, 0xab, 0x09}, {0x7e, 0xae, 0xf7, 0xcf}, {0x15, 0xd2, 0x15, 0x4f}, {0x16, 0xa6, 0x88, 0x3c}};*/ int i, j, temp, after_gen_k[4][4], k_sub_byte[4][1]; /*rot word [a] ----- [b] [b] | [c] [c] | [d] [d] -----> [a] */ temp = before_gen_k[0][3]; before_gen_k[0][3] = before_gen_k[1][3]; before_gen_k[1][3] = before_gen_k[2][3]; before_gen_k[2][3] = before_gen_k[3][3]; before_gen_k[3][3] = temp; /*s_box*/ k_sub_byte = sub_byte(before_gen_k); /*sub_byte only last column*/ for(i=0; i<4; i++){ for(j=0; j<4; j++){ if (i==0) { after_gen_k[j][i] = before_gen_k[j][i] ^ k_sub_byte[j][0] ^ r_con[j][round]; /*put the generated key to after_gen_k*/ } else { after_gen_k[j][i] = before_gen_k[j][i] ^ after_gen_k[j][i-1]; /*put the generated key to after_gen_k*/ } } } return after_gen_k; }
int main(){ int rounds, i, j, count, numberround, answer, rconround = 0, subbyteround = 0;; char plaintext[17] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'}; char key[17] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'}; char index[16][3]; char indexkey[16][3]; char b[3], a[3]; printf("Input Text: "); gets(plaintext); printf("Input Key: "); scanf(" %[^\n]s\n", key); rounds = ceil(strlen(plaintext)/16); for(i=0; i<16; i++){ sprintf(a, "%x", plaintext[i]); /* keep char ascii to hex base */ sprintf(b, "%x", key[i]); strcpy(index[i], a); strcpy(indexkey[i], b); //printf("%x\n", index[i]); //printf("%s\n", indexkey[i]); } for(i=0; i<16; i++){//we do add round key first before do 10 loop //printf("%d\n", (((tonum(index[i][0]))*10)+(tonum(index[i][1])))); answer = (((tonum(index[i][0]))*10)+(tonum(index[i][1])))^(((tonum(indexkey[i][0]))*10)+(tonum(indexkey[i][1]))); printf("%d\n", answer); sprintf(a, "%x", answer); printf("%s\n", a); strcpy(index[i], a); //printf("%s\n", index[i]); } for(numberround=1; numberround<=10; numberround++){ printf("%d %d\n", rconround,subbyteround); if(numberround==1){ sub_byte_key(indexkey); roundkey(matrixkey, rconround); } else{ roundkey(matrixkey, rconround); } sub_byte(index, subbyteround); printf("roundkey:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrixkey[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); printf("Subbyte:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); shiftrow(); printf("shiftrow value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); if(numberround<10){ mixcolumn(); printf("mixcolumn value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } } printf("-------------------------------------------\n"); addroundkey(); printf("Addroundkey value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%c", matrix[j][i]); } //printf("\n"); } printf("\n"); subbyteround++; rconround++; } }
int main(){ int rounds, i, j, count, numberround; char plaintext[17] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'}; char key[17] = {'\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0','\0'}; char index[16][3]; char indexkey[16][3]; char b[3], a[3]; printf("Input Text: "); gets(plaintext); printf("Input Key: "); scanf(" %[^\n]s\n", key); rounds = ceil(strlen(plaintext)/16); for(i=0; i<16; i++){ sprintf(a, "%x", plaintext[i]); /* keep char ascii to hex base */ sprintf(b, "%x", key[i]); strcpy(index[i], a); strcpy(indexkey[i], b); } for(numberround=1; numberround<=10; numberround++){ if(numberround==1){ sub_byte_key(indexkey); roundkey(matrixkey); } else{ roundkey(matrixkey); } sub_byte(index); printf("roundkey:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrixkey[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); printf("Subbyte:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); shiftrow(); printf("shiftrow value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); if(numberround<10){ mixcolumn(); printf("mixcolumn value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } } printf("-------------------------------------------\n"); addroundkey(); printf("Addroundkey value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%c", cipher[i][j]); } } printf("\n"); } /* for(i=0; i<16; i++){ sprintf(a, "%x", plaintext[i]); //keep char ascii to hex base sprintf(b, "%x", key[i]); strcpy(index[i], a); strcpy(indexkey[i], b); } roundkey(indexkey); sub_byte(index); printf("matrixkey:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrixkey[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); printf("matrix:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); shiftrow(); printf("shiftrow value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); mixcolumn(); printf("mixcolumn value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } printf("-------------------------------------------\n"); addroundkey(); printf("Addroundkey value:\n"); for(i=0; i<4; i++){ for(j=0; j<4; j++){ printf("%x ", matrix[i][j]); } printf("\n"); } */ }