Update repo
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_cbc.h
|
||||
//
|
||||
// TITLE: AES CBC mode header file
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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.
|
||||
// $
|
||||
//###########################################################################
|
||||
|
||||
#ifndef AES_CBC_H
|
||||
#define AES_CBC_H
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// If building with a C++ compiler, make all of the definitions in this header
|
||||
// have a C binding.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! \addtogroup aes_cbc_api AES CBC API
|
||||
//! @{
|
||||
//
|
||||
//*****************************************************************************
|
||||
#include <stdint.h>
|
||||
#include "aes_common.h"
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES CBC Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//! \param iv is the initialization vector of size 128 bits
|
||||
//!
|
||||
//! This function performs AES Cipher Block Chaining (CBC) mode
|
||||
//! encryption or decryption on the requested number of 128-bit input blocks.
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern void AES_performCBC(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode,
|
||||
uint16_t *iv);
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES CBC fast Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//! \param iv is the initialization vector of size 128 bits
|
||||
//!
|
||||
//! This function performs AES Cipher Block Chaining (CBC) mode fast
|
||||
//! encryption or decryption on the requested number of 128-bit input blocks.
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern void AES_performCBCFast(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode,
|
||||
uint16_t *iv);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Close the Doxygen group.
|
||||
//! @}
|
||||
//
|
||||
//****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Mark the end of the C bindings section for C++ compilers.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //AES_CBC_H
|
||||
@@ -0,0 +1,132 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_cmac.h
|
||||
//
|
||||
// TITLE: AES CMAC mode header file
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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.
|
||||
// $
|
||||
//###########################################################################
|
||||
|
||||
#ifndef AES_CMAC_H
|
||||
#define AES_CMAC_H
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// If building with a C++ compiler, make all of the definitions in this header
|
||||
// have a C binding.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! \addtogroup aes_cmac_api AES CMAC API
|
||||
//! @{
|
||||
//
|
||||
//*****************************************************************************
|
||||
//*****************************************************************************
|
||||
#include <stdint.h>
|
||||
#include "aes_common.h"
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES CMAC to generate Message Authentication Code (MAC)
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//!
|
||||
//! This function performs AES Cipher-based Message Authentication
|
||||
//! (CMAC) on the requested number of 128-bit input plain text blocks.
|
||||
//! The calculated Message Authentication Code(MAC) is returned.
|
||||
//!
|
||||
//! - \b Note: Size of the plain text must be a multiple of 128-bits
|
||||
//!
|
||||
//! \return MAC Starting address of a 128 element array
|
||||
//! containing the MAC(128-bits)
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern uint16_t *AES_performCMAC(uint16_t *text, uint16_t length,
|
||||
uint16_t *key, AES_KeySize keysize);
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES fast CMAC to generate Message Authentication Code (MAC)
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//!
|
||||
//! This function performs AES fast Cipher-based Message Authentication
|
||||
//! (CMAC) on the requested number of 128-bit input plain text blocks.
|
||||
//! The calculated Message Authentication Code(MAC) is returned.
|
||||
//!
|
||||
//! - \b Note: Size of the plain text must be a multiple of 128-bits
|
||||
//!
|
||||
//! \return MAC Starting address of a 128 element array
|
||||
//! containing the MAC(128-bits)
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern uint16_t *AES_performCMACFast(uint16_t *text, uint16_t length,
|
||||
uint16_t *key, AES_KeySize keysize);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Close the Doxygen group.
|
||||
//! @}
|
||||
//
|
||||
//****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Mark the end of the C bindings section for C++ compilers.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //AES_CMAC_H
|
||||
@@ -0,0 +1,155 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_common.h
|
||||
//
|
||||
// TITLE: AES algorithm header file
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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.
|
||||
// $
|
||||
//###########################################################################
|
||||
|
||||
#ifndef AES_H
|
||||
#define AES_H
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// If building with a C++ compiler, make all of the definitions in this header
|
||||
// have a C binding.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Modes that can be used in the encryption/decryption functions
|
||||
//
|
||||
//*****************************************************************************
|
||||
typedef enum
|
||||
{
|
||||
AES_OPMODE_ENCRYPT, //!< Encryption mode
|
||||
AES_OPMODE_DECRYPT //!< Decryption mode
|
||||
} AES_OperationMode;
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Key sizes that can be used in the AES algorithm
|
||||
//
|
||||
//*****************************************************************************
|
||||
typedef enum
|
||||
{
|
||||
AES_128, //!< AES 128 bit key size
|
||||
AES_256 //!< AES 128 bit key size
|
||||
} AES_KeySize;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//!
|
||||
//! This function performs AES encryption/decryption algorithm
|
||||
//! with 128-bit/256-bit key sizes
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void AES_perform(uint16_t *text, AES_OperationMode mode);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES key expansion
|
||||
//!
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//!
|
||||
//! This function performs AES key expansion algorithm for 128/256 bit keys
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void AES_expandKey(uint16_t *key, AES_KeySize keysize);
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES fast Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//!
|
||||
//! This function performs AES fast encryption/decryption algorithm
|
||||
//! with 128-bit/256-bit key sizes
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void AES_performFast(uint16_t *text, AES_OperationMode mode);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES key expansion
|
||||
//!
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//! \param mode is operation mode encryption or decryption
|
||||
//!
|
||||
//! This function performs AES key expansion algorithm for 128/256 bit keys
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void AES_expandKeyFast(uint16_t *key, AES_KeySize keysize,
|
||||
AES_OperationMode mode);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Mark the end of the C bindings section for C++ compilers.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* AES_AES */
|
||||
@@ -0,0 +1,132 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_ctr.h
|
||||
//
|
||||
// TITLE: AES CTR mode header file
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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.
|
||||
// $
|
||||
//###########################################################################
|
||||
|
||||
#ifndef AES_CTR_H
|
||||
#define AES_CTR_H
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// If building with a C++ compiler, make all of the definitions in this header
|
||||
// have a C binding.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! \addtogroup aes_ctr_api AES CTR API
|
||||
//! @{
|
||||
//
|
||||
//*****************************************************************************
|
||||
#include <stdint.h>
|
||||
#include "aes_common.h"
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES CTR Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//! \param nonceCounter is the required number-used-once 128-bit counter
|
||||
//! value
|
||||
//!
|
||||
//! This function performs AES Counter (CTR) mode encryption or
|
||||
//! decryption on the requested number of 128-bit input blocks.
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern void AES_performCTR(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode,
|
||||
uint16_t *nonceCounter);
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES CTR fast Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//! \param nonceCounter is the required number-used-once 128-bit counter
|
||||
//! value
|
||||
//!
|
||||
//! This function performs AES Counter (CTR) mode fast encryption or
|
||||
//! decryption on the requested number of 128-bit input blocks.
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern void AES_performCTRFast(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode,
|
||||
uint16_t *nonceCounter);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Close the Doxygen group.
|
||||
//! @}
|
||||
//
|
||||
//****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Mark the end of the C bindings section for C++ compilers.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //AES_CTR_H
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_ecb.h
|
||||
//
|
||||
// TITLE: AES ECB mode header file
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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.
|
||||
// $
|
||||
//###########################################################################
|
||||
|
||||
#ifndef AES_ECB_H_
|
||||
#define AES_ECB_H_
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// If building with a C++ compiler, make all of the definitions in this header
|
||||
// have a C binding.
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
#include <stdint.h>
|
||||
#include "aes_common.h"
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES ECB Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//!
|
||||
//! This function performs AES Electronic Code Book (ECB) mode
|
||||
//! encryption or decryption on the requested number of 128-bit input blocks.
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern void AES_performECB(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode);
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform AES ECB fast Encryption/Decryption
|
||||
//!
|
||||
//! \param text is the input plaintext or ciphertext and once operation is
|
||||
//! complete, this parameter contains the output plaintext or ciphertext
|
||||
//! \param length is the length of the input data in blocks (block size is
|
||||
//! 128-bits)
|
||||
//! \param key is the AES key
|
||||
//! \param keysize is the size of the AES key
|
||||
//! \param mode is the AES operation mode of encryption or decryption
|
||||
//!
|
||||
//! This function performs AES Electronic Code Book (ECB) mode fast
|
||||
//! encryption or decryption on the requested number of 128-bit input blocks.
|
||||
//!
|
||||
//! \return None.
|
||||
//
|
||||
//*****************************************************************************
|
||||
extern void AES_performECBFast(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Close the Doxygen group.
|
||||
//! @}
|
||||
//
|
||||
//****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Mark the end of the C bindings section for C++ compilers.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* AES_ECB_H_ */
|
||||
@@ -0,0 +1,341 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_cbc.c
|
||||
//
|
||||
// TITLE: Implementation of the AES CBC mode
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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_cbc.h"
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CBC_splitText - Split text into 128 bit blocks based on blockNumber
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CBC_splitText(uint16_t *text, uint16_t *output, uint16_t blockNumber)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = text[(16 * blockNumber) + i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = text[(8 * blockNumber) + i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CBC_perform128bitXOR - Perform XOR operation on two 16 byte arrays
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CBC_perform128bitXOR(uint16_t *input1, uint16_t *input2, uint16_t *out)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0U; i < 16U; i++)
|
||||
#else
|
||||
for(i = 0U; i < 8U; i++)
|
||||
#endif
|
||||
{
|
||||
out[i] = input1[i] ^ input2[i];
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CBC_move128bitArray - Move input array into output array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CBC_move128bitArray(uint16_t *input, uint16_t *output)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CBC_mergeText - Merge final plaintext/ciphertext into text array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CBC_mergeText(uint16_t *text, uint16_t *input, uint16_t blockNumber)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
text[(16 * blockNumber) + i] = input[i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
text[(8 * blockNumber) + i] = input[i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES CBC mode of operation
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
void AES_performCBC(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode, uint16_t *iv)
|
||||
{
|
||||
//
|
||||
// Initialize local variables
|
||||
//
|
||||
uint16_t previousCiphertext[16];
|
||||
uint16_t currentCiphertext[16];
|
||||
uint16_t decryptionCiphertext[16];
|
||||
uint16_t i;
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKey(key, keysize);
|
||||
|
||||
//
|
||||
// set previousCiphertext equal to the initialization vector
|
||||
//
|
||||
CBC_move128bitArray(iv, previousCiphertext);
|
||||
|
||||
//
|
||||
// main loop
|
||||
// Encryption order of operations: XOR plaintext with previousCiphertext,
|
||||
// encrypt plaintext, save result for next
|
||||
// round
|
||||
// Decryption order of operations: save ciphertext for next round, decrypt
|
||||
// ciphertext, XOR ciphertext with
|
||||
// previousCiphertext
|
||||
//
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
if(mode == AES_OPMODE_ENCRYPT)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
CBC_splitText(text, currentCiphertext, i);
|
||||
|
||||
//
|
||||
// XOR initialization vector/previousCiphertext with new plaintext
|
||||
//
|
||||
CBC_perform128bitXOR(currentCiphertext, previousCiphertext,
|
||||
currentCiphertext);
|
||||
|
||||
//
|
||||
// AES encryption
|
||||
//
|
||||
AES_perform(currentCiphertext, mode);
|
||||
|
||||
//
|
||||
// move result to previousCiphertext for use in next round
|
||||
//
|
||||
CBC_move128bitArray(currentCiphertext, previousCiphertext);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of ciphertext from array passed in
|
||||
//
|
||||
CBC_splitText(text, currentCiphertext, i);
|
||||
|
||||
//
|
||||
// Copy ciphertext into temporary variable so it can be moved into
|
||||
// previousCiphertext later on
|
||||
//
|
||||
CBC_move128bitArray(currentCiphertext, decryptionCiphertext);
|
||||
|
||||
//
|
||||
// AES decryption
|
||||
//
|
||||
AES_perform(currentCiphertext, mode);
|
||||
|
||||
//
|
||||
// XOR initialization vector/previousCiphertext with new ciphertext
|
||||
//
|
||||
CBC_perform128bitXOR(currentCiphertext, previousCiphertext,
|
||||
currentCiphertext);
|
||||
|
||||
//
|
||||
// Move temporary ciphertext array into previousCiphertext
|
||||
//
|
||||
CBC_move128bitArray(decryptionCiphertext, previousCiphertext);
|
||||
}
|
||||
|
||||
//
|
||||
// Move the final plaintext/ciphertext into text
|
||||
//
|
||||
CBC_mergeText(text, currentCiphertext , i);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES CBC fast mode of operation
|
||||
//
|
||||
//*****************************************************************************
|
||||
void AES_performCBCFast(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode,
|
||||
uint16_t *iv)
|
||||
{
|
||||
//
|
||||
// Initialize local variables
|
||||
//
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t previousCiphertext[16];
|
||||
#else
|
||||
uint16_t previousCiphertext[8];
|
||||
#endif
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t currentCiphertext[16];
|
||||
#else
|
||||
uint16_t currentCiphertext[8];
|
||||
#endif
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t decryptionCiphertext[16];
|
||||
#else
|
||||
uint16_t decryptionCiphertext[8];
|
||||
#endif
|
||||
|
||||
uint16_t i;
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKeyFast(key, keysize, mode);
|
||||
|
||||
//
|
||||
// set previousCiphertext equal to the initialization vector
|
||||
//
|
||||
CBC_move128bitArray(iv, previousCiphertext);
|
||||
|
||||
//
|
||||
// Main loop
|
||||
// Encryption order of operations: XOR plaintext with previousCiphertext,
|
||||
// encrypt plaintext, save result for next
|
||||
// round
|
||||
// Decryption order of operations: save ciphertext for next round, decrypt
|
||||
// ciphertext, XOR ciphertext with
|
||||
// previousCiphertext
|
||||
//
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
if(mode == AES_OPMODE_ENCRYPT)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
CBC_splitText(text, currentCiphertext, i);
|
||||
|
||||
//
|
||||
// XOR initialization vector/previousCiphertext with new plaintext
|
||||
//
|
||||
CBC_perform128bitXOR(currentCiphertext, previousCiphertext,
|
||||
currentCiphertext);
|
||||
|
||||
//
|
||||
// AES fast encryption
|
||||
//
|
||||
AES_performFast(currentCiphertext, mode);
|
||||
|
||||
//
|
||||
// move result to previousCiphertext for use in next round
|
||||
//
|
||||
CBC_move128bitArray(currentCiphertext, previousCiphertext);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of ciphertext from array passed in
|
||||
//
|
||||
CBC_splitText(text, currentCiphertext, i);
|
||||
|
||||
//
|
||||
// Copy ciphertext into temporary variable so it can be moved into
|
||||
// previousCiphertext later on
|
||||
//
|
||||
CBC_move128bitArray(currentCiphertext, decryptionCiphertext);
|
||||
|
||||
//
|
||||
// AES fast decryption
|
||||
//
|
||||
AES_performFast(currentCiphertext, mode);
|
||||
|
||||
//
|
||||
// XOR initialization vector/previousCiphertext with new ciphertext
|
||||
//
|
||||
CBC_perform128bitXOR(currentCiphertext, previousCiphertext,
|
||||
currentCiphertext);
|
||||
|
||||
//
|
||||
// Move temporary ciphertext array into previousCiphertext
|
||||
//
|
||||
CBC_move128bitArray(decryptionCiphertext, previousCiphertext);
|
||||
}
|
||||
|
||||
//
|
||||
// Move the final plaintext/ciphertext into text
|
||||
//
|
||||
CBC_mergeText(text, currentCiphertext , i);
|
||||
}
|
||||
}
|
||||
//
|
||||
// End of File
|
||||
//
|
||||
@@ -0,0 +1,417 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_cmac.c
|
||||
//
|
||||
// TITLE: Implementation of the AES CMAC mode.
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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_cmac.h"
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Array to store the generated Message Authentication Code
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t CMAC_Tag[16];
|
||||
#else
|
||||
uint16_t CMAC_Tag[8];
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CMAC_splitText - Split text into 128 bit blocks based on blockNumber
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CMAC_splitText(uint16_t *text, uint16_t *output, uint16_t blockNumber)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = text[(16 * blockNumber) + i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = text[(8 * blockNumber) + i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CMAC_perform128bitXOR - Perform XOR operation on two 16 byte arrays
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CMAC_perform128bitXOR(uint16_t *input1, uint16_t *input2)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0U; i < 16U; i++)
|
||||
#else
|
||||
for(i = 0U; i < 8U; i++)
|
||||
#endif
|
||||
{
|
||||
input1[i] ^= input2[i];
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CMAC_performOneBitLeftshift - Perform left shift on 16 byte array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CMAC_performOneBitLeftshift(uint16_t *input, uint16_t *output)
|
||||
{
|
||||
int16_t i;
|
||||
uint16_t overflow = 0U;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 15; i >= 0; i--)
|
||||
{
|
||||
output[i] = (input[i] << 1U) & 0xFFU;
|
||||
output[i] |= overflow;
|
||||
overflow = (input[i] & 0x80U)?1U:0U;
|
||||
}
|
||||
#else
|
||||
for(i = 7; i >= 0; i--)
|
||||
{
|
||||
output[i] = (input[i] << 1U) & 0xFFFFU;
|
||||
output[i] |= overflow;
|
||||
overflow = (input[i] & 0x8000U)?1U:0U;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CMAC_memset - Copy of compiler memset to quickly clear arrays
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CMAC_memset(void *mem, int16_t ch, uint32_t length)
|
||||
{
|
||||
char *m = (char *)mem;
|
||||
|
||||
while(length--)
|
||||
{
|
||||
*m++ = ch;
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CMAC_move128bitArray - Move input array into output array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CMAC_move128bitArray(uint16_t *input, uint16_t *output)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES256_performCMAC
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t *AES_performCMAC(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize)
|
||||
{
|
||||
//
|
||||
// Setup local variables for calculation
|
||||
//
|
||||
int16_t i = 0;
|
||||
uint16_t k0[16];
|
||||
uint16_t k1[16];
|
||||
uint16_t CMAC_AES_CONSTANT[16];
|
||||
uint16_t currentMessage[16];
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKey(key, keysize);
|
||||
|
||||
//
|
||||
// Set arrays to zero
|
||||
//
|
||||
CMAC_memset(currentMessage, 0, sizeof(currentMessage));
|
||||
CMAC_memset(CMAC_Tag, 0, sizeof(CMAC_Tag));
|
||||
CMAC_memset(CMAC_AES_CONSTANT, 0, sizeof(CMAC_AES_CONSTANT));
|
||||
|
||||
//
|
||||
// Configure AES Constant
|
||||
//
|
||||
CMAC_AES_CONSTANT[15] = 0x87U;
|
||||
|
||||
//
|
||||
// Begin CMAC Algorithm
|
||||
//
|
||||
|
||||
//
|
||||
// Calculate k0 with encryption of 0
|
||||
//
|
||||
AES_perform(currentMessage, AES_OPMODE_ENCRYPT);
|
||||
|
||||
//
|
||||
// Move ciphertext into k0 for future use
|
||||
//
|
||||
CMAC_move128bitArray(currentMessage, k0);
|
||||
|
||||
//
|
||||
// Generate k1
|
||||
// Note: Not generating k2 since plain text length is in multiple of
|
||||
// 128-bits
|
||||
// No padding will be required or performed.
|
||||
//
|
||||
if((k0[0] & 0x80U) == 0U)
|
||||
{
|
||||
//
|
||||
// Left shift k0 by 1 to calculate k1
|
||||
//
|
||||
CMAC_performOneBitLeftshift(k0, k1);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Left shift k0 by 1 to calculate temporary k1. Then XOR temporary k1
|
||||
// by the AES-256bit constant to calculate k1.
|
||||
//
|
||||
CMAC_performOneBitLeftshift(k0, k1);
|
||||
CMAC_perform128bitXOR(k1, CMAC_AES_CONSTANT);
|
||||
}
|
||||
|
||||
//
|
||||
// Perform message block cipher encryption on all blocks except last one
|
||||
//
|
||||
for(i = 0; i < length - 1; i++)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
CMAC_splitText(text, currentMessage, i);
|
||||
|
||||
//
|
||||
// XOR initialization vector/previousCiphertext with new plaintext
|
||||
//
|
||||
CMAC_perform128bitXOR(CMAC_Tag,currentMessage);
|
||||
|
||||
//
|
||||
// Perform AES encryption on CMAC_TAG
|
||||
//
|
||||
AES_perform(CMAC_Tag, AES_OPMODE_ENCRYPT);
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
CMAC_splitText(text, currentMessage, i);
|
||||
|
||||
//
|
||||
// XOR last message with k1
|
||||
//
|
||||
CMAC_perform128bitXOR(currentMessage, k1);
|
||||
|
||||
//
|
||||
// Generate final tag - XOR CMAC_TAG and CMAC_LAST_MESSAGE.
|
||||
// Encrypt CMAC_TAG.
|
||||
//
|
||||
CMAC_perform128bitXOR(CMAC_Tag, currentMessage);
|
||||
AES_perform(CMAC_Tag, AES_OPMODE_ENCRYPT);
|
||||
|
||||
return(CMAC_Tag);
|
||||
}
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES perform fast CMAC
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t *AES_performCMACFast(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize)
|
||||
{
|
||||
//
|
||||
// Setup local variables for calculation
|
||||
//
|
||||
int16_t i = 0;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t k0[16];
|
||||
#else
|
||||
uint16_t k0[8];
|
||||
#endif
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t k1[16];
|
||||
#else
|
||||
uint16_t k1[8];
|
||||
#endif
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t CMAC_AES_CONSTANT[16];
|
||||
#else
|
||||
uint16_t CMAC_AES_CONSTANT[8];
|
||||
#endif
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t currentMessage[16];
|
||||
#else
|
||||
uint16_t currentMessage[8];
|
||||
#endif
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKeyFast(key, keysize, AES_OPMODE_ENCRYPT);
|
||||
|
||||
//
|
||||
// Set arrays to zero
|
||||
//
|
||||
CMAC_memset(currentMessage, 0, sizeof(currentMessage));
|
||||
CMAC_memset(CMAC_Tag, 0, sizeof(CMAC_Tag));
|
||||
CMAC_memset(CMAC_AES_CONSTANT, 0, sizeof(CMAC_AES_CONSTANT));
|
||||
|
||||
//
|
||||
// Configure AES Constant
|
||||
//
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
CMAC_AES_CONSTANT[15] = 0x87U;
|
||||
#else
|
||||
CMAC_AES_CONSTANT[7] = 0x87U;
|
||||
#endif
|
||||
|
||||
//
|
||||
// Begin CMAC Algorithm
|
||||
//
|
||||
|
||||
//
|
||||
// Calculate k0 with fast encryption of 0
|
||||
//
|
||||
AES_performFast(currentMessage, AES_OPMODE_ENCRYPT);
|
||||
|
||||
//
|
||||
// Move ciphertext into k0 for future use
|
||||
//
|
||||
CMAC_move128bitArray(currentMessage, k0);
|
||||
|
||||
//
|
||||
// Generate k1
|
||||
// Not generating k2 since plain text length is in multiple of 128-bits
|
||||
// No padding will be required or performed.
|
||||
//
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
if((k0[0] & 0x80U) == 0U)
|
||||
#else
|
||||
if((k0[0] & 0x8000U) == 0U)
|
||||
#endif
|
||||
{
|
||||
//
|
||||
// Left shift k0 by 1 to calculate k1
|
||||
//
|
||||
CMAC_performOneBitLeftshift(k0, k1);
|
||||
}
|
||||
else
|
||||
{
|
||||
//
|
||||
// Left shift k0 by 1 to calculate temporary k1. Then XOR temporary k1
|
||||
// by the AES-256bit constant to calculate k1.
|
||||
//
|
||||
CMAC_performOneBitLeftshift(k0, k1);
|
||||
CMAC_perform128bitXOR(k1, CMAC_AES_CONSTANT);
|
||||
}
|
||||
|
||||
//
|
||||
// Perform message block cipher encryption on all blocks except last one
|
||||
//
|
||||
for(i = 0; i < length - 1; i++)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
CMAC_splitText(text, currentMessage, i);
|
||||
|
||||
//
|
||||
// XOR initialization vector/previousCiphertext with new plaintext
|
||||
//
|
||||
CMAC_perform128bitXOR(CMAC_Tag, currentMessage);
|
||||
|
||||
//
|
||||
// Perform AES fast encryption on CMAC_TAG
|
||||
//
|
||||
AES_performFast(CMAC_Tag, AES_OPMODE_ENCRYPT);
|
||||
|
||||
}
|
||||
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
CMAC_splitText(text, currentMessage, i);
|
||||
|
||||
//
|
||||
// XOR last message with k1
|
||||
//
|
||||
CMAC_perform128bitXOR(currentMessage, k1);
|
||||
|
||||
//
|
||||
// Generate final CMAC_Tag - XOR CMAC_TAG and CMAC_LAST_MESSAGE.
|
||||
// Encrypt CMAC_TAG.
|
||||
//
|
||||
CMAC_perform128bitXOR(CMAC_Tag, currentMessage);
|
||||
AES_performFast(CMAC_Tag, AES_OPMODE_ENCRYPT);
|
||||
|
||||
return(CMAC_Tag);
|
||||
}
|
||||
|
||||
//
|
||||
// End of File
|
||||
//
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,315 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_ctr.c
|
||||
//
|
||||
// TITLE: Implementation of the AES CTR mode
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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_ctr.h"
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CTR_splitText - Split text into 128 bit blocks based on blockNumber
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CTR_splitText(uint16_t *text, uint16_t *output, uint16_t blockNumber)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = text[(16 * blockNumber) + i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = text[(8 * blockNumber) + i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CTR_perform128bitXOR - Perform XOR operation on two 16 byte arrays
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CTR_perform128bitXOR(uint16_t *input1, uint16_t *input2, uint16_t *out)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0U; i < 16U; i++)
|
||||
#else
|
||||
for(i = 0U; i < 8U; i++)
|
||||
#endif
|
||||
{
|
||||
out[i] = input1[i] ^ input2[i];
|
||||
}
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CTR_move128bitArray - Move input array into output array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CTR_move128bitArray(uint16_t *input, uint16_t *output)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CTR_incrementCounter - Increment nonceCounter for next round
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CTR_incrementCounter(uint16_t *input)
|
||||
{
|
||||
int16_t i = 0;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
input[15] = input[15] + 1;
|
||||
|
||||
//
|
||||
// If overflow, then carry the overflow into more significant bytes
|
||||
// until there is no more overflow. If the input is 0xFFFFFFFFFFFFFFFF,
|
||||
// it will become 0x0000000000000000 when incremented.
|
||||
//
|
||||
|
||||
for(i = 15; i >= 0; i--)
|
||||
{
|
||||
if(((input[i] & 0x100) >> 8) == 1)
|
||||
{
|
||||
if(i != 0)
|
||||
{
|
||||
input[i - 1] = input[i - 1] + 1;
|
||||
}
|
||||
input[i] = 0x00;
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
uint32_t CTR_temp;
|
||||
CTR_temp = (uint32_t)input[7] + 1;
|
||||
input[7] = input[7] + 1;
|
||||
for(i = 7; i >= 0; i--)
|
||||
{
|
||||
if(((CTR_temp & 0x10000) >> 16) == 1)
|
||||
{
|
||||
if( i != 0)
|
||||
{
|
||||
CTR_temp = (uint32_t)input[i - 1] + 1;
|
||||
input[i - 1] = input[i - 1] + 1;
|
||||
}
|
||||
input[i] = 0x0000;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// CTR_mergeText - Merge final plaintext/ciphertext into text array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void CTR_mergeText(uint16_t *text, uint16_t *input, uint16_t blockNumber)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
text[(16 * blockNumber) + i] = input[i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
text[(8 * blockNumber) + i] = input[i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES CTR mode of operation
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
void AES_performCTR(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode,
|
||||
uint16_t *nonceCounter)
|
||||
{
|
||||
//
|
||||
// Initialize local variables
|
||||
//
|
||||
uint16_t i;
|
||||
uint16_t roundText[16];
|
||||
uint16_t encryptionInput[16];
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKey(key, keysize);
|
||||
|
||||
//
|
||||
// main loop
|
||||
// Encryption/Decryption order of operations: get block of text, encrypt
|
||||
// nonceCounter, XOR with text,
|
||||
// update text
|
||||
//
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext/ciphertext from array passed in
|
||||
//
|
||||
CTR_splitText(text, roundText, i);
|
||||
|
||||
//
|
||||
// Set encryption input for round equal to round counter
|
||||
//
|
||||
CTR_move128bitArray(nonceCounter, encryptionInput);
|
||||
|
||||
//
|
||||
// AES ECB encryption
|
||||
//
|
||||
AES_perform(encryptionInput, AES_OPMODE_ENCRYPT);
|
||||
|
||||
//
|
||||
// XOR ciphertext with new plaintext/ciphertext to get final
|
||||
// ciphertext/plaintext
|
||||
//
|
||||
CTR_perform128bitXOR(encryptionInput, roundText, roundText);
|
||||
|
||||
//
|
||||
// Increment counter for next round
|
||||
//
|
||||
CTR_incrementCounter(nonceCounter);
|
||||
|
||||
//
|
||||
// Move the final plaintext/ciphertext into text
|
||||
//
|
||||
CTR_mergeText(text, roundText , i);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES CTR fast mode of operation
|
||||
//
|
||||
//*****************************************************************************
|
||||
void AES_performCTRFast(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode,
|
||||
uint16_t *nonceCounter)
|
||||
{
|
||||
//
|
||||
// Initialize local variables
|
||||
//
|
||||
uint16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t roundText[16];
|
||||
#else
|
||||
uint16_t roundText[8];
|
||||
#endif
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
uint16_t encryptionInput[16];
|
||||
#else
|
||||
uint16_t encryptionInput[8];
|
||||
#endif
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKeyFast(key, keysize, AES_OPMODE_ENCRYPT);
|
||||
|
||||
//
|
||||
// Main loop
|
||||
// Encryption/Decryption order of operations: get block of text, encrypt
|
||||
// nonceCounter, XOR with text,
|
||||
// update text
|
||||
//
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext/ciphertext from array passed in
|
||||
//
|
||||
CTR_splitText(text, roundText, i);
|
||||
|
||||
//
|
||||
// Set encryption input for round equal to round counter
|
||||
//
|
||||
CTR_move128bitArray(nonceCounter, encryptionInput);
|
||||
|
||||
//
|
||||
// AES fast encryption
|
||||
//
|
||||
AES_performFast(encryptionInput, AES_OPMODE_ENCRYPT);
|
||||
|
||||
//
|
||||
// XOR ciphertext with new plaintext/ciphertext to get final
|
||||
// ciphertext/plaintext
|
||||
//
|
||||
CTR_perform128bitXOR(encryptionInput, roundText, roundText);
|
||||
|
||||
//
|
||||
// Increment counter for next round
|
||||
//
|
||||
CTR_incrementCounter(nonceCounter);
|
||||
|
||||
//
|
||||
// Move the final plaintext/ciphertext into text
|
||||
//
|
||||
CTR_mergeText(text, roundText , i);
|
||||
}
|
||||
}
|
||||
//
|
||||
// End of File
|
||||
//
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: aes_ecb.c
|
||||
//
|
||||
// TITLE: Implementation of the AES ECB mode
|
||||
//
|
||||
//###########################################################################
|
||||
// $Copyright:
|
||||
// Copyright (C) 2024 Texas Instruments Incorporated - http://www.ti.com/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions
|
||||
// are met:
|
||||
//
|
||||
// Redistributions of source code must retain the above copyright
|
||||
// notice, this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 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.
|
||||
//
|
||||
// Neither the name of Texas Instruments Incorporated nor the names of
|
||||
// its contributors may be used to endorse or promote products derived
|
||||
// from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
// "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 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
// OWNER OR CONTRIBUTORS 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_ecb.h"
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// ECB_splitText - Split text into 128 bit blocks based on blockNumber
|
||||
//
|
||||
//*****************************************************************************
|
||||
void ECB_splitText(uint16_t *text, uint16_t *output, uint16_t blockNumber)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = text[(16 * blockNumber) + i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = text[(8 * blockNumber) + i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// ECB_move128bitArray - Move input array into output array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void ECB_move128bitArray(uint16_t *input, uint16_t *output)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
output[i] = input[i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// ECB_mergeText - Merge final plaintext/ciphertext into text array
|
||||
//
|
||||
//*****************************************************************************
|
||||
void ECB_mergeText(uint16_t *text, uint16_t *input, uint16_t blockNumber)
|
||||
{
|
||||
int16_t i;
|
||||
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
for(i = 0; i < 16; i++)
|
||||
{
|
||||
text[(16 * blockNumber) + i] = input[i];
|
||||
}
|
||||
#else
|
||||
for(i = 0; i < 8; i++)
|
||||
{
|
||||
text[(8 * blockNumber) + i] = input[i];
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES ECB mode of operation
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifndef MEM_PACKED_SPEED_OPT
|
||||
void AES_performECB(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode)
|
||||
{
|
||||
//
|
||||
// Initialize local variables
|
||||
//
|
||||
uint16_t currentText[16];
|
||||
uint16_t i;
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKey(key, keysize);
|
||||
|
||||
//
|
||||
// main loop
|
||||
//
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
ECB_splitText(text, currentText, i);
|
||||
|
||||
//
|
||||
// AES encryption/decryption
|
||||
//
|
||||
AES_perform(currentText, mode);
|
||||
|
||||
//
|
||||
// Move the final plaintext/ciphertext into text
|
||||
//
|
||||
ECB_mergeText(text, currentText , i);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
//*****************************************************************************
|
||||
//
|
||||
// AES ECB fast mode of operation
|
||||
//
|
||||
//*****************************************************************************
|
||||
void AES_performECBFast(uint16_t *text, uint16_t length, uint16_t *key,
|
||||
AES_KeySize keysize, AES_OperationMode mode)
|
||||
{
|
||||
//
|
||||
// Initialize local variables
|
||||
//
|
||||
uint16_t currentText[16];
|
||||
uint16_t i;
|
||||
|
||||
//
|
||||
// Key expansion
|
||||
//
|
||||
AES_expandKeyFast(key, keysize, mode);
|
||||
|
||||
//
|
||||
// Main loop
|
||||
//
|
||||
for(i = 0; i < length; i++)
|
||||
{
|
||||
//
|
||||
// Get 128 bit block of plaintext from array passed in
|
||||
//
|
||||
ECB_splitText(text, currentText, i);
|
||||
|
||||
//
|
||||
// AES fast encryption/decryption
|
||||
//
|
||||
AES_performFast(currentText, mode);
|
||||
|
||||
//
|
||||
// Move the final plaintext/ciphertext into text
|
||||
//
|
||||
ECB_mergeText(text, currentText , i);
|
||||
}
|
||||
}
|
||||
//
|
||||
// End of File
|
||||
//
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user