byte * AES128::decrypt(byte *message) { int i; memcpy((void*)state, (const void*)message, 16); initKey(); /*Generate Round Keys*/ /* for(i = 0; i < 10; i++) { computeKey(rcon[i], i+1); } */ inv_addRoundKey(10); inv_shiftRows(); inv_subBytes(); for(i = 0; i < 9; i++) { inv_addRoundKey(9-i); inv_mixColumns(); inv_shiftRows(); inv_subBytes(); } inv_addRoundKey(0); memcpy((void*)message,(const void*)state,16); return message; }
//Decrypts one 16-byte array of data void AES::decryptBlock(int keySize, uint8_t* state, uint8_t* key) { int rounds=6+keySize/32; addRoundKey(state,key+rounds*16); for(int i=rounds-1;i>=0;i--) { inv_shiftRows(state); inv_subBytes(state); addRoundKey(state,key+16*i); if(i!=0) //don't mix columns on last round inv_mixColumns(state); } }
//!!!MAIN FUNKTION!!!// void inv_aes128(uint8_t state[16]) { //Damn English - German Mix... //Firste define all Masks[1-10] //then precalculate Sbox_masked and also the correct roundKeys //Try generating random sequence for Subbytes: uint8_t hiding_sequence[16]; gen_random_sequence(hiding_sequence); init_masking(); // 1.: Maskiert State mit m1'-m4' remask(state,Mask[6],Mask[7],Mask[8],Mask[9],0,0,0,0); //2.: Rundenschlüssel 11 (beinhalltet schon m1'-m4' und m // Maske m1'-m4' => m addRoundKey_masked(state, 10); //3.: Maskierte Subbyte Operation // Maske M => m' //inv_subBytes_masked(state); // m => m' //inv_subBytes_masked_rand(state,hiding_sequence); //4.: Shiftrows // Maske m' => m' (bleibt natürlich gleich) //inv_shiftRows(state); // m' bleibt m' if(rand()%2 == 1){ inv_subBytes_masked_rand(state,hiding_sequence); inv_shiftRows(state); } else{ inv_shiftRows(state); inv_subBytes_masked_rand(state,hiding_sequence); } //Loop für Rundenschlüssel 10->2 for (uint8_t i = 9; i > 0; i--) { //5. Maske m' => m1-m4 addRoundKey_masked(state, i); //6. Maske m1-m4 -> m1'-m4' inv_mixColumns(state); //7. Maske m1'-m4' -> m remask(state,Mask[6],Mask[7],Mask[8],Mask[9],Mask[4],Mask[4],Mask[4],Mask[4]); /*with Shuffling:*/ //8. Maske m => m' //inv_subBytes_masked(state); //9. Maske m' bleibt m' //inv_shiftRows(state); //gen_random_sequence(hiding_sequence); if(rand()%2 == 1){ inv_subBytes_masked_rand(state,hiding_sequence); inv_shiftRows(state); } else{ inv_shiftRows(state); inv_subBytes_masked_rand(state,hiding_sequence); } /**/ } //10. Addiert Rundenschlüssel 1: //Maske m' => 0 addRoundKey_masked(state, 0); //Fertig:-) }