Ejemplo n.º 1
0
/*! \brief  Polled function that generates the last subkey of the Expanded Key
 *          needed during decryption.
 *
 *  \note This code is blocking and will dead lock if no interrupt flags are set.
 *
 *  \param  key           Pointer to AES key.
 *  \param  last_sub_key  Pointer to where the last subkey of the Expanded Key
 *                        shall be stored.
 *
 *  \retval true   If generating the last subkey was successful.
 *  \retval false  If generating the last subkey was not successful.
 */
bool AES_lastsubkey_generate(uint8_t * key, uint8_t * last_sub_key)
{
	bool keygen_ok;
	AES_software_reset();

	/* Load key into AES key memory. */
	uint8_t * temp_key = key;
	for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
		AES.KEY =  *(temp_key++);
	}

	/* Load dummy data into AES state memory. */
	for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
		AES.STATE =  0x00;
	}

	/* Set AES in encryption mode and start AES. */
	AES.CTRL = (AES.CTRL & (~AES_DECRYPT_bm)) | AES_START_bm;


	do{
		/* Wait until AES is finished or an error occurs. */
	}while((AES.STATUS & (AES_SRIF_bm|AES_ERROR_bm) ) == 0);

	/* If not error. */
	if((AES.STATUS & AES_ERROR_bm) == 0){
		/* Store the last subkey. */
		uint8_t * temp_last_sub_key = last_sub_key;
		for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
			*(temp_last_sub_key++) = AES.KEY;
		}
		AES.STATUS = AES_SRIF_bm;
		keygen_ok = true;
	}else{
		AES.STATUS = AES_ERROR_bm;
		keygen_ok = false;

	}
	return keygen_ok;
}
Ejemplo n.º 2
0
int main( void )
{
  	/* Assume that everything is ok*/
  	success = true;

	/* Before using the AES it is recommended to do an AES software reset to put
	 * the module in known state, in case other parts of your code has accessed
	 * the AES module. */
	AES_software_reset();

	/* Generate last subkey. */
	AES_lastsubkey_generate(key, lastsubkey);

	/* Do AES Cipher Block Chaining encryption and decryption on three blocks. */
	
	success = AES_CBC_encrypt(); /* MODIFY THE FUNCTION CALL */
	success = AES_CBC_decrypt(); /* MODIFY THE FUNCTION CALL */


	/* Check if decrypted answer is equal to plaintext. */
	for(uint8_t i = 0; i < BLOCK_LENGTH * BLOCK_COUNT ; i++ ){
		if (data_block[i] != block_ans[i]){
			success = false;
		}
	}

	if(success){
	    	while(true){
			/* If the example ends up here every thing is ok. */
                        nop();
		}
	}else{
		while(true){
			/* If the example ends up here something is wrong. */
                        nop();
		}
	}
}