亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
国产成人av一区二区| 国产精品99久久久| 亚洲视频在线一区| 中文字幕av在线一区二区三区| 91精品国产色综合久久ai换脸 | 午夜久久久影院| 伊人婷婷欧美激情| 亚洲精品免费在线播放| 亚洲欧美日韩国产综合在线 | 99国内精品久久| 99精品视频在线播放观看| av在线这里只有精品| 色诱亚洲精品久久久久久| 一本色道综合亚洲| 69p69国产精品| 精品福利一二区| 国产肉丝袜一区二区| 国产欧美日产一区| 亚洲国产精品影院| 黄色日韩三级电影| 成人av电影观看| 欧美视频第二页| 精品国产乱码91久久久久久网站| 久久婷婷国产综合精品青草| 久久久www成人免费毛片麻豆| 国产亚洲一区二区三区在线观看 | 亚洲男人的天堂在线观看| 亚洲综合区在线| 精品一区二区三区日韩| 99久久精品免费精品国产| 91精品免费在线观看| 久久精品一区八戒影视| 亚洲自拍偷拍av| 国产乱码精品1区2区3区| 91成人网在线| 国产午夜精品福利| 婷婷久久综合九色综合伊人色| 国产综合色精品一区二区三区| 波多野结衣中文字幕一区二区三区 | 欧美丰满少妇xxxbbb| 精品处破学生在线二十三| 中文字幕在线不卡一区二区三区| 日韩国产在线观看一区| 国产 欧美在线| 欧美高清视频不卡网| 国产精品另类一区| 麻豆91免费观看| 欧美在线短视频| 国产精品福利一区二区三区| 日本欧洲一区二区| 欧美性大战xxxxx久久久| 国产亚洲1区2区3区| 调教+趴+乳夹+国产+精品| 91在线视频在线| 久久精品欧美日韩| 蜜桃传媒麻豆第一区在线观看| 日本黄色一区二区| 国产精品色在线| 国产成人综合网| 精品国内二区三区| 免费观看在线综合色| 欧美三级日韩三级| 亚洲男女毛片无遮挡| 不卡的av网站| 中文字幕一区二区三区色视频| 国模娜娜一区二区三区| 欧美一区二区三区四区在线观看| 亚洲国产成人va在线观看天堂| 色先锋aa成人| 亚洲猫色日本管| av成人动漫在线观看| 国产亚洲一二三区| 国产精品18久久久久| 2017欧美狠狠色| 韩国成人在线视频| 国产日韩影视精品| 丰满少妇在线播放bd日韩电影| 2023国产精华国产精品| 国产精品综合视频| 国产精品视频你懂的| 高清视频一区二区| 中文av一区二区| 91天堂素人约啪| 亚洲一区二区高清| 91精品国产乱码久久蜜臀| 男女男精品视频| 久久久不卡网国产精品一区| 成人小视频免费观看| 国产精品第四页| 欧美日韩精品一区二区天天拍小说| 亚洲一区二区三区中文字幕| 欧美午夜精品一区二区三区| 日韩国产在线观看| 国产午夜精品久久久久久免费视| 成人精品国产福利| 亚洲综合在线第一页| 91麻豆精品91久久久久久清纯 | 国产精品91一区二区| 国产精品狼人久久影院观看方式| 色综合欧美在线视频区| 日韩在线观看一区二区| 日韩欧美一区二区免费| 国产69精品久久久久毛片| 亚洲另类在线视频| 日韩欧美高清dvd碟片| 成人白浆超碰人人人人| 天堂精品中文字幕在线| 国产清纯在线一区二区www| 色综合久久久网| 蜜桃视频在线一区| 亚洲免费观看高清完整版在线观看| 欧美日韩一区在线观看| 国产精品99精品久久免费| 亚洲精品国产一区二区精华液 | 一区二区三区丝袜| 久久综合久久综合九色| 91福利在线免费观看| 国产精品一区二区在线观看不卡| 亚洲精品va在线观看| 久久久综合激的五月天| 欧美日免费三级在线| 国产suv精品一区二区三区| 亚洲综合免费观看高清完整版 | 欧美激情一区三区| 欧美一区二区在线观看| 色综合久久中文字幕综合网 | 91免费精品国自产拍在线不卡| 免费不卡在线视频| 亚洲国产精品嫩草影院| 国产精品成人免费| 精品国内二区三区| 日韩欧美一级片| 制服丝袜中文字幕一区| 99久久精品情趣| 国产91综合网| 国内精品视频666| 日韩电影在线免费观看| 亚洲一级二级在线| 亚洲少妇30p| 国产精品的网站| 中文字幕va一区二区三区| 久久天堂av综合合色蜜桃网| 日韩亚洲欧美中文三级| 欧美亚洲国产bt| 欧美性猛交xxxxxx富婆| 在线影视一区二区三区| 91亚洲精品久久久蜜桃| www.色综合.com| www.欧美日韩| 99久久久无码国产精品| 成人免费的视频| 91亚洲国产成人精品一区二区三| 国产一区在线不卡| 国产精品中文有码| 国产激情一区二区三区| 国产精品正在播放| 成人免费精品视频| 91视频免费播放| 日本韩国一区二区三区| 欧美少妇bbb| 91麻豆精品国产91久久久使用方法| 欧美精品 国产精品| 51久久夜色精品国产麻豆| 7777精品久久久大香线蕉| 日韩一区二区三区视频在线| 日韩精品中文字幕一区 | 99re8在线精品视频免费播放| 床上的激情91.| av一区二区不卡| 日本国产一区二区| 欧美高清性hdvideosex| 精品日韩99亚洲| 久久久国产精品麻豆| 国产精品毛片久久久久久| 亚洲欧美视频在线观看| 三级影片在线观看欧美日韩一区二区| 日韩av一区二区三区| 国产黑丝在线一区二区三区| voyeur盗摄精品| 欧美日韩精品一区视频| 2024国产精品| 亚洲日本在线视频观看| 亚洲一区二区在线免费看| 日本成人在线看| 国产69精品久久99不卡| 在线观看亚洲a| 久久久美女艺术照精彩视频福利播放| 国产精品免费网站在线观看| 亚洲国产cao| 国产69精品久久久久毛片| 欧美三日本三级三级在线播放| 日韩精品自拍偷拍| 亚洲激情图片qvod| 国产乱子轮精品视频| 欧美色网站导航| 2023国产一二三区日本精品2022| 亚洲精品国产第一综合99久久| 久久精品国产精品青草| 91免费观看视频| 国产亚洲婷婷免费|