⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 des_driver_size.s

📁 ATMEL XMEGA crypto program-task2.
💻 S
📖 第 1 页 / 共 2 页
字号:
//******************************************************************************
//*
//* XMEGA DES driver assembly source file optimized for size.
//*
//* This file contains the low-level implementations for the
//* XMEGA DES driver optimized for size. It is written for the GCC Assembler.
//*
//* Note on GCC calling convention:
//*      Scratch registers:   R0-R1, R18-R27, R30-R31
//*      Preserved registers: R2-R17, R28-R29
//*      Parameter registers: R25-R8 (2-byte alignment starting from R25)
//*      Return registers:    R25-R8 (2-byte alignment starting from R25)
//*
//* Application note:
//*      AVR1317: Using the XMEGA built in DES accelerator
//*
//* Documentation
//*      For comprehensive code documentation, supported compilers, compiler
//*      settings and supported devices see readme.html
//*
//*      Atmel Corporation: http://www.atmel.com \n
//*      Support email: avr@atmel.com
//*
//* $Revision: 1300 $
//* $Date: 2008-02-25 10:34:09 +0100 (ma, 25 feb 2008) $
//*
//* 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.
//******************************************************************************

/* Define 22 bit Program Counter if the device have 22 bits PC.
 * Comment out if it only have 16 bit PC.
 */
#define __22_BIT_PC__


// ----------
// This routine does a single DES encryption.
//
// Prototype:
//    void DES_Encrypt(uint8_t * plaintext, uint8_t * ciphertext, uint8_t * key);
//
// Input:
//    - R25:R24 - pointer to plaintext buffer.
//    - R23:R22 - pointer to ciphertext buffer.
//    - R21:R20 - pointer to key buffer.
//
// Register usage during DES_Encrypt:
//
// During execution:
//   - R31:R30 (Z) is used for misc memory pointing and is not preserved.
//   - R16 contains a variable that is non-zero for doing 3DES, zero for single DES.
//   - R15 - R8 contains current key being processed.
//   - R7  - R0 contains the data (plaintext or ciphertext).
// ----------
.global DES_Encrypt
DES_Encrypt:
	rcall	DES_INTERNAL_Prolog
	rcall	DES_INTERNAL_Load_Data

	// Clear R16 register to tell the DES_INTERNAL_DES_Encrypt to do single DES. 
	clr	r16

	rcall	DES_INTERNAL_DES_Encrypt
	rcall	DES_INTERNAL_Store_Data
	rjmp	DES_INTERNAL_Epilog


// ----------
// This routine does a single DES decryption.
//
// Prototype:
//    void DES_Decrypt(uint8_t * ciphertext, uint8_t * plaintext, uint8_t * key);
//
// Input:
//    - R25:R24 - pointer to ciphertext buffer.
//    - R23:R22 - pointer to plaintext buffer.
//    - R21:R20 - pointer to key buffer.
//
// Register usage during DES_Decrypt:
//
// During execution:
//   - R31:R30 (Z) is used for misc memory pointing and is not preserved.
//   - R16 contains a variable that is non-zero for doing 3DES, zero for single DES.
//   - R15 - R8 contains current key being processed.
//   - R7  - R0 contains the data (plaintext or ciphertext).
// ----------
.global DES_Decrypt
DES_Decrypt:
	rcall	DES_INTERNAL_Prolog
	rcall	DES_INTERNAL_Load_Data

	// Clear R16 register to tell the DES_INTERNAL_DES_Decrypt to do single DES. 
	clr	r16

	rcall	DES_INTERNAL_DES_Decrypt
	rcall	DES_INTERNAL_Store_Data
	rjmp 	DES_INTERNAL_Epilog


// ----------
// This routine does a triple DES encryption.
//
// Prototype:
//    void DES_3DES_Encrypt(uint8_t * plaintext, uint8_t * ciphertext, uint8_t * keys);
//
// Input:
//    - R25:R24 - pointer to plaintext buffer.
//    - R23:R22 - pointer to ciphertext buffer.
//    - R21:R20 - pointer to key buffer.
//
// Register usage during DES_3DES_Encrypt:
//
// During execution:
//   - R31:R30 (Z) is used for misc memory pointing and is not preserved.
//   - R16 contains a variable that is non-zero for doing 3DES, zero for single DES.
//   - R15 - R8 contains current key being processed.
//   - R7  - R0 contains the data (plaintext or ciphertext).
// ----------
.global DES_3DES_Encrypt
DES_3DES_Encrypt:
	rcall	DES_INTERNAL_Prolog
	rcall	DES_INTERNAL_Load_Data

	// Set R16 register to non-zero to tell the DES_INTERNAL_DES_Encrypt to do triple DES. 
	ser	r16

	rcall	DES_INTERNAL_DES_Encrypt
	rcall	DES_INTERNAL_Store_Data
	rjmp	DES_INTERNAL_Epilog


// ----------
// This routine does a triple DES decryption.
//
// Prototype:
//    void DES_3DES_Decrypt(uint8_t * ciphertext, uint8_t * plaintext, uint8_t * keys);
//
// Input:
//    - R25:R24 - pointer to ciphertext buffer.
//    - R23:R22 - pointer to plaintext buffer.
//    - R21:R20 - pointer to key buffer.
//
// Register usage during DES_3DES_Decrypt:
//
// During execution:
//   - R31:R30 (Z) is used for misc memory pointing and is not preserved.
//   - R16 contains a variable that is non-zero for doing 3DES, zero for single DES.
//   - R15 - R8 contains current key being processed.
//   - R7  - R0 contains the data (plaintext or ciphertext).
// ----------
.global DES_3DES_Decrypt
DES_3DES_Decrypt:
	rcall	DES_INTERNAL_Prolog
	rcall	DES_INTERNAL_Load_Data
	
	// Set R16 register to non-zero to tell the DES_INTERNAL_DES_Decrypt to do triple DES.
	ser	r16

	rcall	DES_INTERNAL_DES_Decrypt
	rcall	DES_INTERNAL_Store_Data
	rjmp	DES_INTERNAL_Epilog


// ----------
// This routine does cipher block chaining encoding using DES.
// The bool triple_DES decide if single DES or triple DES is used.
// The variable block_length decide the number of blocks encoded.
//
// Prototype:
//    void DES_CBC_Encrypt(uint8_t * plaintext, uint8_t * ciphertext,
//                         uint8_t * keys, uint8_t * init,
//                         bool triple_DES, uint16_t block_length);
//
// Input:
//    - R25:R24 - pointer to plaintext buffer.
//    - R23:R22 - pointer to ciphertext buffer.
//    - R21:R20 - pointer to key buffer.
//    - R19:R18 - pointer to initial vector (IV).
//    - R17:R16 - variable holding triple_DES bool.
//    - R15:R14 - variable holding block_length.
//
// Register usage during DES_CBC_Encrypt:
//
// During execution:
//   - R31:R30 (Z) is used for misc memory pointing and is not preserved.
//   - R27:R26 (X) holding block_length variable (moved from R15:R14).
//   - R25:R24 points to the current position in the input buffer (plaintext)
//   - R23:R22 points to the current position in the output buffer (ciphertext)
//   - R21:R20 points to the key buffer
//   - R16 contains a variable that is non-zero for doing 3DES, zero for single DES.
//   - R15 - R8 contains current key being processed.
//   - R7  - R0 contains the data (plaintext or ciphertext).
// ----------
.global DES_CBC_Encrypt
DES_CBC_Encrypt:
	rcall	DES_INTERNAL_Prolog

	// Move R15:R14 to R27:R26 to save the block_length during DES.
	movw	r26, r14

	rcall	DES_INTERNAL_Load_Data

	// Load the pointer to IV into the Z pointer, and load the IV to R15 - R8
	// to allow for XORing between the data and IV.
	movw	r30, r18
	rcall	DES_INTERNAL_Load_Into_R15_R8

DES_INTERNAL_CBC_Encrypt_Next:
	rcall	DES_INTERNAL_XOR_Routine
	rcall	DES_INTERNAL_DES_Encrypt
	rcall	DES_INTERNAL_Store_Data

	// Subtract one block from the counter for each pass and go to end if zero.
	sbiw	r26, 1
	breq	DES_INTERNAL_CBC_Encrypt_End

	// Load the pointer to data into the Z pointer, and load the data to R15 - R8
	// to allow for XORing between the last cipherblock and data.
	movw	r30, r24
	rcall	DES_INTERNAL_Load_Into_R15_R8
	movw	r24, r30	

	rjmp	DES_INTERNAL_CBC_Encrypt_Next

DES_INTERNAL_CBC_Encrypt_End:
	rjmp	DES_INTERNAL_Epilog


// ----------
// This routine does cipher block chaining decoding using DES.
// The bool triple_DES decide if single DES or triple DES is used.
// The variable block_length decide the number of blocks encoded.
//
// Prototype:
//    void DES_CBC_Decrypt(uint8_t * ciphertext, uint8_t * plaintext,
//                         uint8_t * keys, uint8_t * init,
//                         bool triple_DES, uint16_t block_length);
//
// Input:
//    - R25:R24 - pointer to ciphertext buffer.
//    - R23:R22 - pointer to plaintext buffer.
//    - R21:R20 - pointer to key buffer.
//    - R19:R18 - pointer to initial vector (IV).
//    - R17:R16 - variable holding triple_DES bool.
//    - R15:R14 - variable holding block_length.
//
// Register usage during DES_CBC_Decrypt:
//
// During execution:
//   - R31:R30 (Z) is used for misc memory pointing and is not preserved.
//   - R27:R26 (X) holding block_length variable (moved from R15:R14).
//   - R25:R24 points to the current position in the input buffer (ciphertext)
//   - R23:R22 points to the current position in the output buffer (plaintext)
//   - R21:R20 points to the key buffer
//   - R16 contains a variable that is non-zero for doing 3DES, zero for single DES.
//   - R15 - R8 contains current key being processed.
//   - R7  - R0 contains the data (plaintext or ciphertext).
// ----------
.global DES_CBC_Decrypt
DES_CBC_Decrypt:
	rcall	DES_INTERNAL_Prolog

	// Move R15:R14 to R27:26 to save the block_count during DES.
	movw	r26, r14

	// Load data and decrypt.
	rcall	DES_INTERNAL_Load_Data
	rcall	DES_INTERNAL_DES_Decrypt

	// Load the initial vector into Z pointer and do the first XORing with the IV.
	movw	r30, r18

DES_INTERNAL_CBC_Decrypt_Next:
	rcall	DES_INTERNAL_Load_Into_R15_R8
	rcall	DES_INTERNAL_XOR_Routine
	rcall	DES_INTERNAL_Store_Data

	// Subtract one block from the counter for each pass and go to end if zero.
	sbiw	r26, 1
	breq	DES_INTERNAL_CBC_Decrypt_End

	// Load the next cipher block into R7-R0 and decrypt.
	// The pointer to the ciphers are updated on each load.
	rcall	DES_INTERNAL_Load_Data
	rcall	DES_INTERNAL_DES_Decrypt

	// Move the updated cipher block pointer to Z pointer, and subtract the pointer
	// by 16 to get the previous cipher block to XOR with the decrypted data.
	movw	r30, r24
	sbiw	r30, 16
	rjmp	DES_INTERNAL_CBC_Decrypt_Next

DES_INTERNAL_CBC_Decrypt_End:
	rjmp	DES_INTERNAL_Epilog


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -