// Perform the key expansion for AES-128 using 16-byte key; each round stored in round_key // Pseudocode taken from Fig.11 in FIPS-197 (page 20) void key_expansion(unsigned char init_key[16], unsigned char round_key[44][4], FILE* table) { unsigned char* temp = (unsigned char*)calloc(1,4); unsigned char* s_box = (unsigned char*)calloc(1,256); unsigned char* rcon = (unsigned char*)calloc(1,256); init_s_box(s_box, table); init_rcon(rcon); // Store initial round keys (form words from each piece of init key) for(int i=0; i<4; i++) { round_key[i][0] = init_key[4*i]; round_key[i][1] = init_key[4*i+1]; round_key[i][2] = init_key[4*i+2]; round_key[i][3] = init_key[4*i+3]; // Tried defining round key as a WORD (unsigned int), but I forgot there are // little-endian/big-endian issues with different architectures. F**k that. } // Generate subsequent round keys (from Nk -> Nb*(Nr+1)) for(int i=4; i<44; i++) { // Set temp var to previous round key temp[0] = round_key[i-1][0]; temp[1] = round_key[i-1][1]; temp[2] = round_key[i-1][2]; temp[3] = round_key[i-1][3]; // Perform Rijndael key schedule core every 4 iterations if (i%4 == 0) { // For AES-128 and AES-192; rotate temp vals, put through s_box, xor the MSB with rcon rot_word(temp); temp[0] = s_box[ temp[0] ] ^ rcon[i/4]; temp[1] = s_box[ temp[1] ]; temp[2] = s_box[ temp[2] ]; temp[3] = s_box[ temp[3] ]; } // Store current round key round_key[i][0] = round_key[i-4][0] ^ temp[0]; round_key[i][1] = round_key[i-4][1] ^ temp[1]; round_key[i][2] = round_key[i-4][2] ^ temp[2]; round_key[i][3] = round_key[i-4][3] ^ temp[3]; } free(temp); free(s_box); free(rcon); }
int op_3(char **strarray, long N) { long perm; scanf("%li", &perm); int i, j; int inceput; int stare = 0; //0-nu suntem in cuvant 1-suntem in cuvant for (i = 0 ; i < N ; i++) { for (j = 0 ; j <= strlen(strarray[i]) ; j++) { // <= pt ca luam si backslash 0 if (stare == 0) { if ( ! (one_of(&strarray[i][j])) ) { //daca am ajuns la un cuvant; inceput = j; stare = 1; } } else //daca stare==1 { if (one_of(&strarray[i][j])) { stare = 0; rot_word(strarray[i], inceput, j, perm); } } } } printare(strarray, N); return 0; }
/* * Key Expansion */ void key_expansion(uint8_t *key, uint8_t *w) { uint8_t tmp[4]; uint8_t i, j; uint8_t len = Nb*(Nr+1); for (i = 0; i < Nk; i++) { w[4*i+0] = key[4*i+0]; w[4*i+1] = key[4*i+1]; w[4*i+2] = key[4*i+2]; w[4*i+3] = key[4*i+3]; } for (i = Nk; i < len; i++) { tmp[0] = w[4*(i-1)+0]; tmp[1] = w[4*(i-1)+1]; tmp[2] = w[4*(i-1)+2]; tmp[3] = w[4*(i-1)+3]; if (i%Nk == 0) { rot_word(tmp); sub_word(tmp); coef_add(tmp, Rcon(i/Nk), tmp); } else if (Nk > 6 && i%Nk == 4) { sub_word(tmp); } w[4*i+0] = w[4*(i-Nk)+0]^tmp[0]; w[4*i+1] = w[4*(i-Nk)+1]^tmp[1]; w[4*i+2] = w[4*(i-Nk)+2]^tmp[2]; w[4*i+3] = w[4*(i-Nk)+3]^tmp[3]; } }