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

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

?? aes_driver.c

?? ATMEL XMEGA crypto program-task4.
?? 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一区二区三区免费野_久草精品视频
欧美午夜一区二区三区免费大片| 国产aⅴ综合色| 一区二区三区在线观看动漫| 国产蜜臀av在线一区二区三区| 精品国产一区久久| 国产亚洲婷婷免费| 欧美激情在线看| 一色桃子久久精品亚洲| 亚洲日本免费电影| 亚洲线精品一区二区三区八戒| 亚洲国产成人高清精品| 亚洲成人免费在线| 男男视频亚洲欧美| 高清在线成人网| eeuss国产一区二区三区| 色av综合在线| 欧美一二三四区在线| 国产欧美精品国产国产专区| 国产精品国产馆在线真实露脸| 亚洲精品视频自拍| 亚洲成人午夜影院| 国内精品在线播放| 91在线观看地址| 91精品国产91久久综合桃花| 国产午夜精品在线观看| 亚洲高清在线精品| 国产精品亚洲第一区在线暖暖韩国| 国产成人av资源| 欧美三级中文字幕| 国产欧美一区二区精品秋霞影院| 亚洲一本大道在线| 国产成人免费视| 欧美日韩成人一区二区| 久久精品一区二区三区av| 亚洲免费在线观看视频| 久久97超碰国产精品超碰| 成人高清视频在线观看| 欧美制服丝袜第一页| 精品99久久久久久| 一区二区三区免费观看| 老汉av免费一区二区三区| 91在线观看视频| 久久久久久久电影| 五月婷婷久久丁香| 91尤物视频在线观看| 欧美精品一区二区三区视频| 偷拍与自拍一区| 99久久精品99国产精品 | 日韩福利视频导航| 岛国一区二区三区| 日韩免费观看高清完整版| 亚洲美女区一区| 成人性色生活片免费看爆迷你毛片| 欧美性猛片xxxx免费看久爱| 中文字幕乱码久久午夜不卡| 久久er精品视频| 777奇米成人网| 午夜精品爽啪视频| 91黄色免费看| 国产精品高潮呻吟久久| 国产精品一区二区久久精品爱涩| 日韩午夜精品视频| 视频一区二区国产| 欧美日韩国产成人在线91| 伊人夜夜躁av伊人久久| 92精品国产成人观看免费| 亚洲国产精品99久久久久久久久 | 99国产精品视频免费观看| 精品国产乱码久久久久久免费| 日韩成人一级片| 这里是久久伊人| 日本伊人色综合网| 日韩午夜电影在线观看| 青青草精品视频| 日韩亚洲欧美一区| 免费亚洲电影在线| 2023国产精品自拍| 国产精品一区不卡| 国产精品福利av| 色综合咪咪久久| 亚欧色一区w666天堂| 91精品国产综合久久久久| 日韩成人免费电影| 欧美一卡2卡3卡4卡| 韩国三级在线一区| 国产精品色噜噜| 色婷婷av一区二区三区gif | 99国产欧美另类久久久精品| 亚洲女人****多毛耸耸8| 欧美三级韩国三级日本一级| 免费在线欧美视频| 国产欧美一区二区三区鸳鸯浴| 成人av网站免费| 亚洲五月六月丁香激情| 日韩一区二区三区免费观看| 国内外精品视频| 中文字幕一区免费在线观看 | 蜜臀av亚洲一区中文字幕| 久久综合资源网| 一本久道久久综合中文字幕| 日韩一区精品视频| 国产精品乱人伦| 欧美日韩国产不卡| 国产一区二区不卡在线| 亚洲精品中文在线观看| 欧美va在线播放| 91美女福利视频| 欧美a级一区二区| 亚洲欧美综合色| 日韩天堂在线观看| 99re6这里只有精品视频在线观看| 亚洲一卡二卡三卡四卡| 国产三级一区二区三区| 欧美高清精品3d| 成人国产精品免费网站| 蜜桃视频一区二区| 一区二区在线观看免费视频播放| 日韩欧美国产一区二区在线播放| 91视频在线观看| 国产精品一区久久久久| 日韩精品视频网| 亚洲精品少妇30p| 国产日韩欧美激情| 日韩你懂的在线播放| 色婷婷av一区二区三区大白胸 | 激情另类小说区图片区视频区| 亚洲美女视频一区| 国产精品日韩精品欧美在线| 91精品久久久久久蜜臀| 欧美专区在线观看一区| 国产91色综合久久免费分享| 美腿丝袜亚洲三区| 午夜影院久久久| 亚洲一区二区三区四区在线观看| 国产精品色哟哟| 欧美韩国日本一区| 久久精品人人做人人爽人人| 日韩午夜三级在线| 正在播放亚洲一区| 欧美高清性hdvideosex| 日本二三区不卡| 99精品欧美一区二区蜜桃免费 | 欧美色综合网站| 色婷婷综合久久久久中文一区二区| 东方aⅴ免费观看久久av| 国产精品一线二线三线精华| 麻豆精品在线播放| 久久精品免费观看| 老司机免费视频一区二区三区| 蜜臀av在线播放一区二区三区| 日韩av电影一区| 蜜臀99久久精品久久久久久软件| 天堂成人国产精品一区| 日本亚洲视频在线| 久久精品国产精品亚洲综合| 麻豆精品在线播放| 国产精品一区二区在线观看网站| 韩国精品久久久| 国产成人精品在线看| 成人黄色av网站在线| 91免费看片在线观看| 日本久久一区二区| 欧美日韩日日摸| 日韩精品一区二区三区中文精品| 精品国产三级a在线观看| 久久婷婷国产综合精品青草| 欧美国产日本韩| 亚洲欧美日韩小说| 热久久一区二区| 国产经典欧美精品| 91小视频免费观看| 精品视频999| 久久亚洲综合色一区二区三区| 国产精品全国免费观看高清| 亚洲免费av高清| 日本女人一区二区三区| 国产精品一区一区三区| 在线视频中文字幕一区二区| 日韩欧美中文一区二区| 国产精品国产自产拍高清av| 天天综合色天天综合色h| 国产一区二区网址| 色综合久久久网| 日韩美女天天操| 亚洲男人的天堂在线观看| 免费高清在线一区| 91视频免费播放| 精品理论电影在线| 一区二区三区不卡在线观看 | 一区二区三区成人在线视频| 韩国三级中文字幕hd久久精品| 色综合久久中文字幕| 久久这里只有精品首页| 亚洲综合网站在线观看| 国产一区二区主播在线| 欧美日本韩国一区二区三区视频 | **欧美大码日韩| 国产制服丝袜一区| 在线成人午夜影院| 亚洲黄色在线视频|