亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? aes_driver.c

?? ATMEL XMEGA crypto program-task3.
?? C
?? 第 1 頁 / 共 2 頁
字號:
/* This file has been prepared for Doxygen automatic documentation generation.*/
/*! \file *********************************************************************
 *
 * \brief
 *      XMEGA AES driver source file.
 *
 *      This file contains the function implementations the XMEGA AES driver.
 *
 *      The driver is not intended for size and/or speed critical code, since
 *      most functions are just a few lines of code, and the function call
 *      overhead would decrease code performance. The driver is intended for
 *      rapid prototyping and documentation purposes for getting started with
 *      the XMEGA AES module.
 *
 *      For size and/or speed critical code, it is recommended to copy the
 *      function contents directly into your application instead of making
 *      a function call.
 *
 *      Several functions use the following construct:
 *          "some_register = ... | (some_parameter ? SOME_BIT_bm : 0) | ..."
 *      Although the use of the ternary operator ( if ? then : else ) is discouraged,
 *      in some occasions the operator makes it possible to write pretty clean and
 *      neat code. In this driver, the construct is used to set or not set a
 *      configuration bit based on a boolean input parameter, such as
 *      the "some_parameter" in the example above.
 *
 * \par Application note:
 *      AVR1317 Using the XMEGA built in AES accelerator
 *
 * \par Documentation
 *      For comprehensive code documentation, supported compilers, compiler
 *      settings and supported devices see readme.html
 *
 * \author
 *      Atmel Corporation: http://www.atmel.com \n
 *      Support email: avr@atmel.com
 *
 * $Revision: 1569 $
 * $Date: 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008) $  \n
 *
 * Copyright (c) 2008, Atmel Corporation All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. The name of ATMEL may not be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
#include "AES_driver.h"


/*! \brief  Function that initialize the interrupt driver
 *
 *  \param  interrupt_driver  Pointer to interrupt driver struct .
 *  \param  input_ptr         Pointer to the input blocks (plaintext/ciphertext).
 *  \param  output_ptr        Pointer to where to store the output.
 *  \param  AES_key           Pointer to the key used by the AES algorithm.
 *  \param  AES_CBC_init      Pointer to initialization vector needed in CBC.
 *  \param  block_count       The number of block that is being encrypted/decrypted.
 *  \param  decrypt           Bool that determine if encryption or decryption is done.
 *
 */
void AES_interrupt_driver_init(AES_interrupt_driver_t * interrupt_driver,
                               uint8_t * input_ptr,
                               uint8_t * output_ptr,
                               uint8_t * AES_key,
                               uint8_t * AES_CBC_init,
                               uint8_t block_count,
                               bool decrypt)
{
	/* Initialize interrupt driver struct. */
	interrupt_driver->block_count = block_count;
	interrupt_driver->blocks_left = block_count;
	interrupt_driver->output_ptr = output_ptr;
	interrupt_driver->input_ptr = input_ptr;
	interrupt_driver->key_ptr = AES_key;
	interrupt_driver->init_ptr = AES_CBC_init;
	interrupt_driver->decrypt = decrypt;
}



/*! \brief  Function that starts the AES interrupt driver
 *
 *  CBC is used if the number of blocks is more than one.
 *
 *  \param  interrupt_driver  Pointer to interrupt driver struct.
 *  \param  int_lvl           Interrupt level for the AES module.
 *
 *  \retval true   Starting the AES interrupt driver was successful.
 *  \retval false  Starting the AES interrupt driver was not successful.
 *
 */
bool AES_interrupt_driver_start(AES_interrupt_driver_t * interrupt_driver,
                                AES_INTLVL_t int_lvl)
{
  	bool start_ok;

  	/* Remove pending AES interrupts. */
  	AES.STATUS = (AES_ERROR_bm | AES_SRIF_bm);

	/* Set AES to the desired interrupt level.
	 * NOTE: If interrupt level is set to off, interrupts will never execute. */
	AES.INTCTRL = int_lvl;

	/* Put AES module in right mode. */
	if(interrupt_driver->decrypt){
		AES.CTRL |= AES_DECRYPT_bm;
	}else{
		AES.CTRL = AES.CTRL & (~AES_DECRYPT_bm);
	}

	/* If encryption and there are more than one block CBC is used. In CBC
	 * encryption the first plaintext block is xored with the initialization
	 * vector. */
	if((interrupt_driver->block_count > 1) && !(interrupt_driver->decrypt)){

		/* Load key to AES Key memory. */
		uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
		for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
		  	AES.KEY =  *(temp_key_ptr++);
		}

		/* Load the first plaintext block to AES State memory. */
		uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
		for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
			AES.STATE =  *(temp_input_ptr++);
		}
		interrupt_driver->input_ptr = temp_input_ptr;

		/* Enable Auto mode and the XOR feature. */
		AES.CTRL = AES.CTRL | AES_XOR_bm | AES_AUTO_bm;

		/* Load the initialization vector to the AES State memory.
		 * The initialization vector is xored with the plaintext block already
		 * loaded into the memory and the AES module is auto started. */
		uint8_t * temp_init_ptr = interrupt_driver->init_ptr;
		for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
			AES.STATE =  *(temp_init_ptr++);
		}

		/* Check if error flag is set. */
		start_ok = !(AES_error_flag_check());
	}
	/* If decryption or encryption of a single block the xor feature is not
	 * used. */
	else{

		/* Load key to AES Key memory. */
	  	volatile uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
		for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
			AES.KEY =  *(temp_key_ptr++);
		}

		/* Enable Auto mode. */
		AES.CTRL |= AES_AUTO_bm;

		/* Load the first input block to AES State memory. */
		uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
		for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
			AES.STATE =  *(temp_input_ptr++);
		}

		/* Update input pointer. */
		interrupt_driver->input_ptr = temp_input_ptr;

		/* Check if error flag is set. */
		start_ok = !(AES_error_flag_check());
	}

	return start_ok;
}



/*! \brief  Function that control the AES when State Ready Interrupts occurs.
 *
 *  CBC is used if the number of blocks is more than one.
 *
 *  \param  interrupt_driver  Pointer to interrupt driver struct.
 */
void AES_interrupt_handler(AES_interrupt_driver_t * interrupt_driver)
{
	/* If encryption is done, the answer can be read out directly from the
	 * state memory. Then the output pointer is updated. */
  	if(!(interrupt_driver->decrypt)){

		/* Store result to memory. */
		uint8_t * temp_output_ptr = interrupt_driver->output_ptr;
		for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
			*(temp_output_ptr++) = AES.STATE;
		}

		/* Update output pointer and the number of blocks left to
		 * encrypt/decrypt. */
		interrupt_driver->output_ptr = temp_output_ptr;
		interrupt_driver->blocks_left -= 1;

		/* If there are more blocks to encrypt a new encryption is started. */
	 	if(interrupt_driver->blocks_left > 0){

			/* Load key to AES Key memory. */
		  	uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
			for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
				AES.KEY =  *(temp_key_ptr++);
			}

			/* Load the next plaintext to the AES State memory. The block is xored
			 * with the previous encrypted block and the AES module is auto
			 * started. */
			uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
			for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
				AES.STATE =  *(temp_input_ptr++);
			}

			/* Update input pointer. */
			interrupt_driver->input_ptr = temp_input_ptr;
		}
	}
	/* When decryption is done, the answer can only be read out directly if
	 * there only is one block to decrypt. If there are more than one block and
	 * CBC is used the answer must be xored with the previous cipher text or
	 * the initialization vector to reconstruct the plaintext. */
	else{

		/* If only one block should be decrypted the plaintext can be read out
		 * directly from the AES State memory. */
		if(interrupt_driver->block_count == 1){

			/* Store result to memory. */
			uint8_t * temp_output_pointer = interrupt_driver->output_ptr;
			for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
				*(temp_output_pointer + i) = AES.STATE;
			}
		}
		/* If there are more than one block to decrypt. */
		else{

			/* Disable of Auto mode and enable on the xor feature. */
			AES.CTRL = (AES.CTRL & (~AES_AUTO_bm)) | AES_XOR_bm;

			/* If it is the first block that is decrypted the answer must be
			 * xored with initialization vector to reconstruct the first
			 * plaintext. */
			uint8_t * temp_ptr;
			uint8_t temp_blocks_left = interrupt_driver->blocks_left;
			if(interrupt_driver->block_count == temp_blocks_left){
			  	temp_ptr = interrupt_driver->init_ptr;
			}
			/* Else the answer must be xored with previous ciphertext value to
			 * reconstruct the plaintext. */
			else{
				temp_ptr = interrupt_driver->input_ptr -(AES_BLOCK_LENGTH*2);
			}

			/* Xor the initialization vector or the previous ciphertext with
			 * the answer from the decryption. */
			for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
				AES.STATE =  *(temp_ptr++);
			}

			/* Store the result. */
			uint8_t * temp_output_ptr = interrupt_driver->output_ptr;
			for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
				*(temp_output_ptr++) = AES.STATE;
			}

			/* Update output pointer and the number of blocks left to
			 * encrypt/decrypt. */
			interrupt_driver->output_ptr = temp_output_ptr;
			interrupt_driver->blocks_left -= 1;

			/* If there are more block to decrypt a new decryption is started. */
			if(interrupt_driver->blocks_left > 0){

				/* Enable the Auto mode and disable the xor feature. */
				AES.CTRL = (AES.CTRL & (~AES_XOR_bm)) | AES_AUTO_bm;

				/* Load key to AES Key memory. */
				uint8_t * temp_key_ptr = interrupt_driver->key_ptr;
				for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
					AES.KEY =  *(temp_key_ptr++);
				}

				/* Load the next ciphertext block to the AES State memory. The
				* AES module is auto started. */
				uint8_t * temp_input_ptr = interrupt_driver->input_ptr;
				for(uint8_t i = 0; i < AES_BLOCK_LENGTH; i++){
					AES.STATE =  *(temp_input_ptr++);
				}

				/* Update the input pointer. */
				interrupt_driver->input_ptr = temp_input_ptr;
			}
		}
	}
}



/*! \brief  Function that check if the interrupt driver is finished.
 *
 *  \param  interrupt_driver  Pointer to interrupt driver struct.
 *
 *  \retval true   The AES interrupt driver is finished.
 *  \retval false  The AES interrupt driver is not finished.
 */
bool AES_interrupt_driver_finished(AES_interrupt_driver_t * interrupt_driver)
{
 	bool finished = (interrupt_driver->blocks_left == 0);
	return finished;
}



/*! \brief  Polled function that does an AES encryption on one 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 AES key
 *
 *  \retval true   If the AES encryption was successful.
 *  \retval false  If the AES encryption was not successful.
 */
bool AES_encrypt(uint8_t * plaintext, uint8_t * ciphertext, uint8_t * key)
{
  	bool encrypt_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_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;

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
久久久蜜桃精品| 欧美一级免费大片| 国产最新精品免费| 美国欧美日韩国产在线播放| 亚洲成人av资源| 亚洲一区在线视频观看| 亚洲成av人片在www色猫咪| 一区二区三区免费观看| 一区二区三区国产| 午夜欧美在线一二页| 视频一区在线播放| 久久99国产精品成人| 久久99国产精品免费网站| 激情五月婷婷综合| 国产精品69毛片高清亚洲| www.99精品| 色女孩综合影院| 6080yy午夜一二三区久久| 日韩写真欧美这视频| 精品精品国产高清a毛片牛牛| 精品欧美久久久| 国产女主播视频一区二区| 国产精品第13页| 丝袜美腿一区二区三区| 国产伦精品一区二区三区在线观看 | 99精品国产热久久91蜜凸| 在线观看亚洲精品| 欧美一区二区精美| 欧美国产一区在线| 亚洲午夜免费电影| 国产伦精品一区二区三区免费迷 | 亚洲精品写真福利| 日本亚洲三级在线| 99精品欧美一区二区三区小说| 欧美无砖专区一中文字| 久久综合九色综合97_久久久| 国产精品福利在线播放| 免费一级片91| 972aa.com艺术欧美| 欧美电影免费观看高清完整版在线| 欧美激情一区二区三区全黄| 亚洲专区一二三| 丰满亚洲少妇av| 欧美福利一区二区| 中文字幕中文字幕在线一区| 三级欧美韩日大片在线看| av成人动漫在线观看| 欧美www视频| 亚洲第一激情av| 91碰在线视频| 国产亚洲欧美在线| 另类小说视频一区二区| 欧美日韩一区二区三区高清| 国产精品视频免费看| 韩日av一区二区| 欧美精品一级二级三级| 一区二区不卡在线播放| 成人免费毛片a| 久久久亚洲国产美女国产盗摄 | 一本到高清视频免费精品| 国产情人综合久久777777| 免费精品99久久国产综合精品| 欧美视频日韩视频在线观看| 亚洲欧洲另类国产综合| 成人综合婷婷国产精品久久蜜臀 | 69堂国产成人免费视频| 亚洲美女免费在线| 99视频一区二区| 成人免费视频在线观看| 成人中文字幕合集| 欧美激情一区不卡| 成人免费看黄yyy456| 亚洲国产成人午夜在线一区| 激情久久五月天| 精品国产乱码久久久久久牛牛| 日本特黄久久久高潮| 欧美一区国产二区| 免费成人在线观看| 精品入口麻豆88视频| 精品一区二区久久| 久久久久久久综合| 成人在线视频一区二区| 国产精品久久久久久久久免费桃花 | 亚洲制服丝袜一区| 欧美性猛交xxxxxxxx| 视频一区中文字幕| 久久综合999| 懂色av中文字幕一区二区三区| 亚洲国产精品成人久久综合一区| 成人网在线免费视频| 亚洲欧美一区二区三区久本道91| 91网站最新地址| 亚洲成人在线观看视频| 日韩亚洲欧美高清| 成人一区在线看| 夜夜揉揉日日人人青青一国产精品| 欧美图区在线视频| 奇米精品一区二区三区四区| 精品99999| 成人av第一页| 亚洲高清一区二区三区| 久久综合久久久久88| 91亚洲精品久久久蜜桃| 天堂一区二区在线免费观看| 精品毛片乱码1区2区3区| 成人高清免费观看| 日韩高清不卡一区| 中文字幕av一区 二区| 欧美性大战久久久久久久蜜臀 | 欧美性色欧美a在线播放| 久久国产人妖系列| 亚洲欧洲另类国产综合| 日韩一区二区三区电影| 99国产精品久久久久久久久久| 亚洲妇女屁股眼交7| 国产清纯白嫩初高生在线观看91| 91麻豆福利精品推荐| 久久电影国产免费久久电影| 综合久久给合久久狠狠狠97色| 欧美成人在线直播| 欧美三级日韩三级国产三级| 国产suv一区二区三区88区| 亚洲成人综合在线| 中文字幕在线播放不卡一区| 日韩精品一区二区三区中文不卡| 91一区二区三区在线播放| 国产麻豆日韩欧美久久| 日本中文字幕一区| 亚洲综合在线视频| 国产精品久久久久久久浪潮网站| 日韩欧美视频一区| 欧美日韩一区国产| 在线观看日韩国产| 成人激情午夜影院| 国产一区视频在线看| 日本vs亚洲vs韩国一区三区| 一区二区久久久久久| 亚洲欧美在线观看| 亚洲国产激情av| 久久人人爽人人爽| 日韩三级.com| 日韩欧美亚洲一区二区| 在线综合亚洲欧美在线视频| 91国偷自产一区二区三区观看| 成人免费毛片高清视频| 成人黄色电影在线| 粉嫩aⅴ一区二区三区四区五区 | 99国产精品久久久久久久久久久| 国产成人av福利| 国产成人自拍在线| 成人动漫一区二区在线| 国产成人精品免费看| 风间由美一区二区av101| 国产成人免费高清| 成人一区二区三区| 91在线精品一区二区三区| 99在线精品免费| 91免费看片在线观看| 在线欧美小视频| 欧美挠脚心视频网站| 日韩欧美的一区| 26uuu成人网一区二区三区| 久久久亚洲精品一区二区三区| 久久久久国产精品厨房| 国产精品久久久久影院| 亚洲人成在线观看一区二区| 亚洲精品日韩一| 午夜国产精品一区| 看国产成人h片视频| 国产成人午夜精品影院观看视频| 国产精品一二二区| 91在线免费看| 欧美美女喷水视频| 337p粉嫩大胆噜噜噜噜噜91av| 国产农村妇女毛片精品久久麻豆| 国产精品卡一卡二卡三| 一区二区三区在线看| 青青草一区二区三区| 国产馆精品极品| 在线一区二区三区四区| 日韩一级片网址| 国产精品三级av在线播放| 亚洲午夜精品久久久久久久久| 美国十次了思思久久精品导航| 国产盗摄精品一区二区三区在线| 在线免费视频一区二区| 精品电影一区二区三区| 综合婷婷亚洲小说| 精品影视av免费| 91老师片黄在线观看| 精品免费日韩av| 亚洲精品欧美在线| 国产在线乱码一区二区三区| 色综合天天综合色综合av | 国产精品一区二区在线观看不卡| 91啪亚洲精品| 欧美激情在线一区二区三区| 性欧美疯狂xxxxbbbb| 成人免费视频网站在线观看| 日韩一级黄色大片|