?? aes_driver.c
字號:
}
/*! \brief Polled function that does an AES decryption on one 128-bit data block.
*
* \note This code is blocking and will dead lock if no interrupt flags are set.
*
* \param ciphertext Pointer to the ciphertext that shall be decrypted
* \param plaintext Pointer to where in memory the plaintext (answer) shall be stored.
* \param key Pointer to the DES key
*
* \retval true If the AES decryption was successful.
* \retval false If the AES decryption was not successful.
*/
bool AES_decrypt(uint8_t * ciphertext, uint8_t * plaintext,
uint8_t * key)
{
bool decrypt_ok;
/* 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 data into AES state memory. */
uint8_t * temp_ciphertext = ciphertext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(temp_ciphertext++);
}
/* Set AES in decryption mode and start the AES.*/
AES.CTRL |= (AES_START_bm | AES_DECRYPT_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 result. */
uint8_t * temp_plaintext = plaintext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
*(temp_plaintext++) = AES.STATE;
}
decrypt_ok = true;
}else{
decrypt_ok = false;
}
return decrypt_ok;
}
/*! \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;
}
/*! \brief Polled function that does AES CBC encryption on a given number of
* 128-bit data block.
*
* \note This code is blocking and will dead lock if no interrupt flags are set.
*
* \param plaintext Pointer to the plaintext that shall be encrypted.
* \param ciphertext Pointer to where in memory the ciphertext (answer) shall be stored.
* \param key Pointer to the key.
* \param init Pointer to the initialization vector used in the CBC.
* \param block_count The number of blocks to encrypt.
*
* \retval true: The AES CBC encryption was successful.
* \retval false: The AES CBC encryption was not successful.
*/
bool AES_CBC_encrypt(uint8_t * plaintext, uint8_t * ciphertext,
uint8_t * key, uint8_t * init, uint16_t block_count)
{
bool CBC_ok = true;
/* The first encryption uses the initialization vector. */
uint8_t * temp_init = init;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(temp_init++);
}
/* Set AES in encryption mode and enables the XOR feature and the AUTO start
* mode. */
AES.CTRL = (AES.CTRL & (~AES_DECRYPT_bm))| AES_XOR_bm |AES_AUTO_bm;
/* Temporary values used to reduce memory access. */
uint8_t * temp_plaintext = plaintext;
uint8_t * temp_ciphertext = ciphertext;
for(uint8_t blocks_left = block_count; blocks_left > 0; blocks_left--){
/* 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 plaintext into AES state memory. Auto starts. */
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(temp_plaintext++);
}
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 result. */
uint8_t * temp = temp_ciphertext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
*(temp++) = AES.STATE;
}
temp_ciphertext = temp;
}else{
CBC_ok = false;
}
}
/* Turn off auto mode and xor feature. */
AES.CTRL = (AES.CTRL & ~( AES_XOR_bm |AES_AUTO_bm));
return CBC_ok;
}
/*! \brief Polled function that does AES CBC decryption on a given number of
* 128-bit data block.
*
* \note This code is blocking and will dead lock if no interrupt flags are set.
*
* \param ciphertext Pointer to the ciphertext that shall be decrypted.
* \param plaintext Pointer to where the plaintext (answer) shall be stored.
* \param key Pointer to the last subkey of the Expanded Key.
* \param init Pointer to the initialization vector used in the CBC.
* \param block_count The number of blocks to decrypt.
*
* \retval true If the AES CBC decryption was successful.
* \retval false If the AES CBC decryption was not successful.
*/
bool AES_CBC_decrypt(uint8_t * ciphertext, uint8_t * plaintext,
uint8_t * key, uint8_t * init, uint16_t block_count)
{
bool CBC_ok = true;
/* Temporary values used to reduce memory access. */
uint8_t * temp_plaintext = plaintext;
uint8_t * temp_ciphertext = ciphertext;
for(uint8_t blocks_left = block_count; blocks_left > 0; blocks_left--){
/* 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 ciphertext into AES state memory. */
uint8_t * temp = temp_ciphertext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(temp++);
}
temp_ciphertext = temp;
/* Set AES in decryption mode and enable xor feature and start the AES. */
AES.CTRL |= (AES_DECRYPT_bm | AES_XOR_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){
/* The first block is xored with the initialization vector. */
if(blocks_left == block_count){
/* Load into AES state memory. */
uint8_t * temp_init = init;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(temp_init++);
}
}
/* The other blocks is xored with the previous ciphertext block. */
else{
/* Load into AES state memory. */
uint8_t * last_ciphertext = temp_ciphertext - (AES_BLOCK_LENGTH*2);
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(last_ciphertext++);
}
}
/* Disable XOR feature before next round. */
AES.CTRL = AES.CTRL & (~AES_XOR_bm);
/* Store the result. */
uint8_t * temp = temp_plaintext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
*(temp++) = AES.STATE;
}
temp_plaintext = temp;
}else{
CBC_ok = false;
}
}
return CBC_ok;
}
/*! \brief Function that sets AES interrupt level
*
* \param int_lvl The AES interrupt level
*/
void AES_interruptlevel_set(AES_INTLVL_t int_lvl)
{
AES.INTCTRL = int_lvl;
}
/*! \brief Polled function that does an AES encryption on one 128-bit data block.
*
* Function equal to the AES_encrypt function but the key is not loaded
* into the key memory. The function require that the key already is in the
* key memory. Used when encryption and decryption with the same key is
* done every other time.
*
* \note This code is blocking and will dead lock if no interrupt flags are set.
*
* \param plaintext Pointer to the plaintext that shall be encrypted
* \param ciphertext Pointer to where in memory the ciphertext (answer) shall be stored.
*
* \retval true If the AES encryption was successful.
* \retval false If the AES encryption was not successful.
*/
bool AES_encrypt_backtoback(uint8_t * plaintext, uint8_t * ciphertext)
{
bool encrypt_ok;
/* Load data into AES state memory. */
uint8_t * temp_plaintext = plaintext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(temp_plaintext++);
}
/* 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 result. */
uint8_t * temp_ciphertext = ciphertext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
*(temp_ciphertext++) = AES.STATE;
}
encrypt_ok = true;
}else{
encrypt_ok = false;
}
return encrypt_ok;
}
/*! \brief Polled function that does an AES decryption on one 128-bit data block.
*
* Function equal to the AES_decrypt function but the key is not loaded
* into the key memory. The function require that the key already is in the
* key memory. Used when encryption and decryption with the same key is
* done every other time.
*
* \note This code is blocking and will dead lock if no interrupt flags are set.
*
* \param ciphertext Pointer to the ciphertext that shall be decrypted
* \param plaintext Pointer to where in memory the plaintext (answer) shall be stored.
*
* \retval true If the AES decryption was successful.
* \retval false If the AES decryption was not successful.
*/
bool AES_decrypt_backtoback(uint8_t * ciphertext, uint8_t * plaintext)
{
bool decrypt_ok;
/* Load data into AES state memory. */
uint8_t * temp_ciphertext = ciphertext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
AES.STATE = *(temp_ciphertext++);
}
/* Set AES in decryption mode and start the AES.*/
AES.CTRL |= (AES_START_bm | AES_DECRYPT_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 result. */
uint8_t * temp_plaintext = plaintext;
for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
*(temp_plaintext++) = AES.STATE;
}
decrypt_ok = true;
}else{
decrypt_ok = false;
}
return decrypt_ok;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -