void aes_decrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){ uint8_t i; aes_dec_firstround(state, &(ks->key[i=rounds])); for(;rounds>1;--rounds){ --i; aes_dec_round(state, &(ks->key[i])); } for(i=0; i<16; ++i){ state->s[i] ^= ks->key[0].ks[i]; } }
// when decrypting the algorithm has the opposite order compared to the encryption // the PDF gives us the encryption direction // the comments use the naming conventions from the PDF void aes_decrypt_core(aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds){ uint8_t i; // buffer == state // aes_cipher_state_t represents the 4x4 byte cipher-matrix // "last round" aes_dec_firstround(state, &(ks->key[i=rounds])); // start with rounds == 10 // 9 "normal rounds" // including "first round" and "second last round" for(;rounds>1;--rounds){ // i is the counter for the roundkey which is applied backwards --i; aes_dec_round(state, &(ks->key[i])); } // "Initial Round" // adding roundkey for(i=0; i<16; ++i){ state->s[i] ^= ks->key[0].ks[i]; } }
/* * aes_decrypt_core * * This function performs the steps to undo the encryption * */ void aes_decrypt_core( aes_cipher_state_t* state, const aes_genctx_t* ks, uint8_t rounds) { uint8_t i; // Firstly, undo the final encryption step // using the round keys from backwards aes_dec_firstround(state, &(ks->key[i=rounds])); // Secondly, undo all the remaining rounds // using the round keys from backwards for(; rounds>1; --rounds) { --i; aes_dec_round(state, &(ks->key[i])); } // Finally add the key to get the original message decrypted from the input for(i=0; i<16; ++i) { state->s[i] ^= ks->key[0].ks[i]; } }