Update repo
This commit is contained in:
@@ -0,0 +1,486 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: sha256.h
|
||||
//
|
||||
// TITLE: SHA-256 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 SHA256_H_
|
||||
#define SHA256_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 sha256_api SHA256 API
|
||||
//! @{
|
||||
//
|
||||
//*****************************************************************************
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 object
|
||||
//
|
||||
//*****************************************************************************
|
||||
typedef struct {
|
||||
uint32_t bitsProcessed; // Only 2^32 bits (512 MiBytes) are supported
|
||||
int16_t offsetWb; // Byte offset into Ws, used to load message
|
||||
uint32_t digest[8]; // Holds intermediate/final digest
|
||||
uint32_t Ws[16];
|
||||
} SHA256_ObjectWordWise;
|
||||
|
||||
typedef struct {
|
||||
uint32_t bitsProcessed; // Only 2^32 bits (512 MiBytes) are supported
|
||||
int16_t offsetWb; // Byte offset into Ws, used to load message
|
||||
uint32_t digest[8]; // Holds intermediate/final digest
|
||||
uint16_t Ws[64];
|
||||
} SHA256_ObjectByteWise;
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Handle to the SHA-256 object
|
||||
//
|
||||
//*****************************************************************************
|
||||
typedef SHA256_ObjectWordWise* SHA256_HandleWordWise;
|
||||
typedef SHA256_ObjectByteWise* SHA256_HandleByteWise;
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Define Macros
|
||||
//
|
||||
//*****************************************************************************
|
||||
/** SHA256- NULL input error */
|
||||
#define SHA256_STATUS_NULL_INPUT (uint16_t)0x0000
|
||||
/** SHA256 operation successfull */
|
||||
#define SHA256_STATUS_SUCCESS (uint16_t)0xABCD
|
||||
/** SHA256- length of input too large */
|
||||
#define SHA256_STATUS_LENGTH_TOO_LARGE (uint16_t)0xBADA
|
||||
/** SHA256- input not aligned */
|
||||
#define SHA256_STATUS_NOT_ALIGNED (uint16_t)0x0BAD
|
||||
|
||||
#define SHA256_GETU32(plaintext) (((uint32_t)(plaintext)[0] << 24U) ^ \
|
||||
((uint32_t)(plaintext)[1] << 16U) ^ \
|
||||
((uint32_t)(plaintext)[2] << 8U) ^ \
|
||||
((uint32_t)(plaintext)[3]))
|
||||
#define SHA256_PUTU32(ciphertext, st) \
|
||||
{ (ciphertext)[0] = (uint16_t)((uint16_t)((st) >> 24U) & 0x00FFU); \
|
||||
(ciphertext)[1] = (uint16_t)((uint16_t)((st) >> 16U) & 0x00FFU); \
|
||||
(ciphertext)[2] = (uint16_t)((uint16_t)((st) >> 8U) & 0x00FFU); \
|
||||
(ciphertext)[3] = (uint16_t)((uint16_t)(st) & 0x00FFU); }
|
||||
|
||||
#define SHA256_SHR(x, a) ((x) >> (a))
|
||||
#define SHA256_ROTR32(x, n) (( SHA256_SHR(x, n) ) ^ ( (x) << (32 - (n)) ))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA256_CH function from NIST FIPS 180-4, Eq 4.2.
|
||||
// Implementation is refactored for efficiency.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA256_CH(x, y, z) (((x) & ((y) ^ (z))) ^ (z))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA256_MAJ function from NIST FIPS 180-4, Eq 4.3.
|
||||
// Implementation is refactored for efficiency.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA256_MAJ(x, y, z) ((((y)^(z)) & (x)) ^ ((y) & (z)))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Big Sigma 0-512 function from NIST FIPS 180-4, Eq 4.4.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA256_SIGZ(x) (SHA256_ROTR32(x, 2) ^ SHA256_ROTR32(x, 13) ^ \
|
||||
SHA256_ROTR32(x, 22))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Big Sigma 1-512 function from NIST FIPS 180-4, Eq 4.5.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA256_SIG1(x) (SHA256_ROTR32(x, 6) ^ SHA256_ROTR32(x, 11) ^ \
|
||||
SHA256_ROTR32(x, 25))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Little Sigma 0-512 function from NIST FIPS 180-4, Eq 4.6.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA256_SIGMAZ(x) (SHA256_ROTR32(x, 7) ^ SHA256_ROTR32(x, 18) ^ \
|
||||
SHA256_SHR(x, 3))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Little Sigma 1-512 function from NIST FIPS 180-4, Eq 4.7.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA256_SIGMA1(x) (SHA256_ROTR32(x, 17) ^ SHA256_ROTR32(x, 19) ^ \
|
||||
SHA256_SHR(x, 10))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform a complete hash operation when input is supplied as 32-bit words,
|
||||
//! producing a final digest for the data.
|
||||
//! - This function wraps #SHA256_startWordWise(), #SHA256_addDataWordWise(),
|
||||
//! and #SHA256_finalizeWordWise().
|
||||
//! - There is no need to call #SHA256_startWordWise() prior to calling
|
||||
//! this function.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.)
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleWordWise' handle
|
||||
//! \param data 32-bit pointer pointing to the starting memory location of
|
||||
//! the data to be hashed
|
||||
//! \param length Length of the data (in number of bytes) to be hashed.
|
||||
//! Note that for word-wise input, length of the data should be
|
||||
//! a multiple of 4.
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! #SHA256_STATUS_LENGTH_TOO_LARGE -
|
||||
//! The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! #SHA256_STATUS_NULL_INPUT - One or
|
||||
//! more of the pointer inputs is NULL.
|
||||
//! #SHA256_STATUS_NOT_ALIGNED - Length.
|
||||
//! is not a multiple of 4
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_hashWordWise(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length, uint32_t digest[8]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform a complete hash operation when input is supplied as 32-bit words,
|
||||
//! with High-Low words Swapped,
|
||||
//! producing a final digest for the data.
|
||||
//! - This function wraps #SHA256_startWordWise(), #SHA256_addDataWordWiseSwapped(),
|
||||
//! and #SHA256_finalizeWordWise().
|
||||
//! - There is no need to call #SHA256_startWordWise() prior to calling
|
||||
//! this function.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.)
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleWordWise' handle
|
||||
//! \param data 32-bit pointer pointing to the starting memory location of
|
||||
//! the data to be hashed
|
||||
//! \param length Length of the data (in number of bytes) to be hashed.
|
||||
//! Note that for word-wise input, length of the data should be
|
||||
//! a multiple of 4.
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! #SHA256_STATUS_LENGTH_TOO_LARGE -
|
||||
//! The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! #SHA256_STATUS_NULL_INPUT - One or
|
||||
//! more of the pointer inputs is NULL.
|
||||
//! #SHA256_STATUS_NOT_ALIGNED - Length.
|
||||
//! is not a multiple of 4
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_hashWordWiseSwapped(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length, uint32_t digest[8]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform a complete hash operation when input is supplied byte-wise,
|
||||
//! producing a final digest for the data.
|
||||
//! - This function wraps #SHA256_startByteWise(), #SHA256_addDataByteWise(),
|
||||
//! and #SHA256_finalizeByteWise().
|
||||
//! - There is no need to call #SHA256_startByteWise() prior to calling
|
||||
//! this function.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.)
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleByteWise' handle
|
||||
//! \param data 16-bit pointer pointing to the memory location of the
|
||||
//! starting byte the data to be hashed.
|
||||
//! \param length Length of the data (in number of bytes) to be hashed.
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA256_STATUS_LENGTH_TOO_LARGE The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! \return #SHA256_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_hashByteWise(SHA256_HandleByteWise handle, uint16_t *data,
|
||||
size_t length, uint32_t digest[8]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Initialize a 'SHA256SW_HandleWordWise' handle, preparing for hashing data.
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleWordWise' handle
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA256_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_startWordWise(SHA256_HandleWordWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Initialize a 'SHA256SW_HandleByteWise' handle, preparing for hashing data.
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleByteWise' handle
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA256_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_startByteWise(SHA256_HandleByteWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Add data to SHA256 operation when inputs are supplied as 32-bit words
|
||||
//! - Adds data to a hash operation. The @c handle must have been
|
||||
//! initialized by a call to SHA256_startWordWise first.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.).
|
||||
//! - After passing in all data to be hashed, call #SHA256_finalizeWordWise()
|
||||
//! to obtain the final digest.
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleWordWise' handle.
|
||||
//! \param data 32-bit pointer pointing to the starting memory location of
|
||||
//! the data to be hashed.
|
||||
//! \param length Length of the data (in number of bytes) to be hashed
|
||||
//! Note that for word-wise input, length of the data should
|
||||
//! be a multiple of 4.
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA256_STATUS_LENGTH_TOO_LARGE The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! \return #SHA256_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//! \return #SHA256_STATUS_NOT_ALIGNED Length is not a multiple of 4
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_addDataWordWise(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Add data to SHA256 operation when inputs are supplied as 32-bit words
|
||||
//! - Input words are Swapped High-Low
|
||||
//! - Adds data to a hash operation. The @c handle must have been
|
||||
//! initialized by a call to SHA256_startWordWise first.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.).
|
||||
//! - After passing in all data to be hashed, call #SHA256_finalizeWordWise()
|
||||
//! to obtain the final digest.
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleWordWise' handle.
|
||||
//! \param data 32-bit pointer pointing to the starting memory location of
|
||||
//! the data to be hashed.
|
||||
//! \param length Length of the data (in number of bytes) to be hashed
|
||||
//! Note that for word-wise input, length of the data should
|
||||
//! be a multiple of 4.
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA256_STATUS_LENGTH_TOO_LARGE The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! \return #SHA256_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//! \return #SHA256_STATUS_NOT_ALIGNED Length is not a multiple of 4
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_addDataWordWiseSwapped(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Add data to SHA256 operation when inputs are supplied byte-wise
|
||||
//! - Adds data to a hash operation. The @c handle must have been
|
||||
//! initialized by a call to SHA256_startByteWise first.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.).
|
||||
//! - After passing in all data to be hashed, call #SHA256_finalizeWordWise()
|
||||
//! to obtain the final digest.
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleByteWise' handle.
|
||||
//! \param data 16-bit pointer pointing to the memory location of the
|
||||
//! starting byte the data to be hashed.
|
||||
//! \param length Length of the data (in number of bytes) to be hashed
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS hash operation succeeded.
|
||||
//! #SHA256_STATUS_LENGTH_TOO_LARGE -
|
||||
//! requested length of data to
|
||||
//! hash is more than supported length.
|
||||
//! #SHA256_STATUS_NULL_INPUT- One or
|
||||
//! more of the pointer inputs is NULL
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_addDataByteWise(SHA256_HandleByteWise handle, uint16_t *data,
|
||||
size_t length);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Finalize the SHA256 operation when input is supplied as 32-bit words,
|
||||
//! creating the final digest.
|
||||
//! - After calling this function, @c handle should not be used again
|
||||
//! until it has been reinitialized via a call to #SHA256_startWordWise().
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleWordWise' handle
|
||||
//! \param digest Output location for the final digest
|
||||
//! \param digest Output location for the final digest
|
||||
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA256_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_finalizeWordWise(SHA256_HandleWordWise handle,
|
||||
uint32_t digest[8]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Finalize the SHA256 operation when input is supplied byte-wise,
|
||||
//! creating the final digest.
|
||||
//! - After calling this function, @c handle should not be used again
|
||||
//! until it has been reinitialized via a call to #SHA256_startByteWise().
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleByteWise' handle
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA256_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA256_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_finalizeByteWise(SHA256_HandleByteWise handle,
|
||||
uint32_t digest[8]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Cancels SHA256 operation by clearing intermediate
|
||||
//! data stored in the 'SHA256_HandleWordWise' handle.
|
||||
//! - The handle will not be ready for a new operation until after
|
||||
//! #SHA256_startWordWise() is called on the handle.
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleWordWise' handle
|
||||
//!
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_cancelWordWise(SHA256_HandleWordWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Cancels SHA256 operation by clearing intermediate
|
||||
//! data stored in the 'SHA256_HandleByteWise' handle.
|
||||
//! - The handle will not be ready for a new operation until after
|
||||
//! #SHA256_startByteWise() is called on the handle.
|
||||
//!
|
||||
//! \param handle A 'SHA256_HandleByteWise' handle
|
||||
//!
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_cancelByteWise(SHA256_HandleByteWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Performs core SHA2-256 algorithm on one 512 bit block of data when input
|
||||
//! is supplied as 32-bit words.
|
||||
//! - object->digest will contain the updated digest resulting from
|
||||
//! hashing the block.
|
||||
//!
|
||||
//! \param digest pre-loaded with previous block's digest (or initial
|
||||
//! digest if call is to process first block)
|
||||
//! On function return digest will hold the updated digest.
|
||||
//! \param Ws loaded with data block, including padding as specified by
|
||||
//! the SHA256 standard.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_processBlockWordWise(uint32_t digest[8], uint32_t Ws[16]);
|
||||
extern void _SHA256_processBlockWordWise_casm_C28(uint32_t digest[8], uint32_t Ws[16]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Performs core SHA2-256 algorithm on one 512 bit block of data when input
|
||||
//! is supplied byte-wise.
|
||||
//! - object->digest will contain the updated digest resulting from
|
||||
//! hashing the block.
|
||||
//!
|
||||
//! \param digest pre-loaded with previous block's digest (or initial
|
||||
//! digest if call is to process first block)
|
||||
//! On function return digest will hold the updated digest.
|
||||
//! \param Ws loaded with data block, including padding as specified by
|
||||
//! the SHA256 standard.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_processBlockByteWise(uint32_t digest[8], uint16_t Ws[64]);
|
||||
extern void _SHA256_processBlockByteWise_casm_C28(uint32_t digest[8], uint16_t Ws[64]);
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Close the Doxygen group.
|
||||
//! @}
|
||||
//
|
||||
//****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Mark the end of the C bindings section for C++ compilers.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* SHA256_H_ */
|
||||
@@ -0,0 +1,439 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: sha384.h
|
||||
//
|
||||
// TITLE: SHA-384 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 SHA384_H_
|
||||
#define SHA384_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 sha384_api SHA384 API
|
||||
//! @{
|
||||
//
|
||||
//*****************************************************************************
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 object
|
||||
//
|
||||
//*****************************************************************************
|
||||
typedef struct {
|
||||
uint32_t bitsProcessed; // Only 2^32 bits (512 MiBytes) are supported
|
||||
int16_t offsetWb; // Byte offset into Ws, used to load message
|
||||
uint64_t digest[8]; // Holds intermediate/final digest
|
||||
uint32_t Ws[32];
|
||||
} SHA384_ObjectWordWise;
|
||||
|
||||
typedef struct {
|
||||
uint32_t bitsProcessed; // Only 2^32 bits (512 MiBytes) are supported
|
||||
int16_t offsetWb; // Byte offset into Ws, used to load message
|
||||
uint64_t digest[8]; // Holds intermediate/final digest
|
||||
uint16_t Ws[128];
|
||||
} SHA384_ObjectByteWise;
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Handle to the SHA-384 object
|
||||
//
|
||||
//*****************************************************************************
|
||||
typedef SHA384_ObjectWordWise* SHA384_HandleWordWise;
|
||||
typedef SHA384_ObjectByteWise* SHA384_HandleByteWise;
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Define Macros
|
||||
//
|
||||
//*****************************************************************************
|
||||
/** SHA384- NULL input error */
|
||||
#define SHA384_STATUS_NULL_INPUT (uint16_t)0x0000
|
||||
/** SHA384 operation successful */
|
||||
#define SHA384_STATUS_SUCCESS (uint16_t)0xABCD
|
||||
/** SHA384- length of input too large */
|
||||
#define SHA384_STATUS_LENGTH_TOO_LARGE (uint16_t)0xBADA
|
||||
/** SHA384- input not aligned */
|
||||
#define SHA384_STATUS_NOT_ALIGNED (uint16_t)0x0BAD
|
||||
|
||||
#define SHA384_GETU32(plaintext) (((uint32_t)(plaintext)[0] << 24U) ^ \
|
||||
((uint32_t)(plaintext)[1] << 16U) ^ \
|
||||
((uint32_t)(plaintext)[2] << 8U) ^ \
|
||||
((uint32_t)(plaintext)[3]))
|
||||
#define SHA384_GETU64(plaintext) (((uint64_t)(plaintext)[0] << 56U) ^ \
|
||||
((uint64_t)(plaintext)[1] << 48U) ^ \
|
||||
((uint64_t)(plaintext)[2] << 40U) ^ \
|
||||
((uint64_t)(plaintext)[3] << 32U) ^ \
|
||||
((uint64_t)(plaintext)[4] << 24U) ^ \
|
||||
((uint64_t)(plaintext)[5] << 16U) ^ \
|
||||
((uint64_t)(plaintext)[6] << 8U) ^ \
|
||||
((uint64_t)(plaintext)[7]))
|
||||
#define SHA384_PUTU64(ciphertext, st) \
|
||||
{ (ciphertext)[0] = (uint16_t)((uint16_t)((st) >> 56U) & 0x00FFU); \
|
||||
(ciphertext)[1] = (uint16_t)((uint16_t)((st) >> 48U) & 0x00FFU); \
|
||||
(ciphertext)[2] = (uint16_t)((uint16_t)((st) >> 40U) & 0x00FFU); \
|
||||
(ciphertext)[3] = (uint16_t)((uint16_t)((st) >> 32U) & 0x00FFU); \
|
||||
(ciphertext)[4] = (uint16_t)((uint16_t)((st) >> 24U) & 0x00FFU); \
|
||||
(ciphertext)[5] = (uint16_t)((uint16_t)((st) >> 16U) & 0x00FFU); \
|
||||
(ciphertext)[6] = (uint16_t)((uint16_t)((st) >> 8U) & 0x00FFU); \
|
||||
(ciphertext)[7] = (uint16_t)((uint16_t)(st) & 0x00FFU); }
|
||||
#define SHA384_PUTU32(ciphertext, st) \
|
||||
{ (ciphertext)[0] = (uint16_t)((uint16_t)((st) >> 24U) & 0x00FFU); \
|
||||
(ciphertext)[1] = (uint16_t)((uint16_t)((st) >> 16U) & 0x00FFU); \
|
||||
(ciphertext)[2] = (uint16_t)((uint16_t)((st) >> 8U) & 0x00FFU); \
|
||||
(ciphertext)[3] = (uint16_t)((uint16_t)(st) & 0x00FFU); }
|
||||
|
||||
#define SHA384_SHR(x, a) ((x) >> (a))
|
||||
#define SHA384_ROTR32(x, n) (( SHA384_SHR(x, n) ) ^ ( (x) << (32 - (n)) ))
|
||||
#define SHA384_ROTR64(x, n) (( SHA384_SHR(x, n) ) ^ ( (x) << (64 - (n)) ))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA384_CH function from NIST FIPS 180-4, Eq 4.8.
|
||||
// Implementation is refactored for efficiency.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA384_CH(x, y, z) (((x) & ((y) ^ (z))) ^ (z))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA384_MAJ function from NIST FIPS 180-4, Eq 4.9.
|
||||
// Implementation is refactored for efficiency.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA384_MAJ(x, y, z) ((((y)^(z)) & (x)) ^ ((y) & (z)))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Big Sigma 0-512 function from NIST FIPS 180-4, Eq 4.10.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA384_SIGZ(x) (SHA384_ROTR64(x, 28) ^ SHA384_ROTR64(x, 34) ^ \
|
||||
SHA384_ROTR64(x, 39))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Big Sigma 1-512 function from NIST FIPS 180-4, Eq 4.11.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA384_SIG1(x) (SHA384_ROTR64(x, 14) ^ SHA384_ROTR64(x, 18) ^ \
|
||||
SHA384_ROTR64(x, 41))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Little Sigma 0-512 function from NIST FIPS 180-4, Eq 4.12.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA384_SIGMAZ(x) (SHA384_ROTR64(x, 1) ^ SHA384_ROTR64(x, 8) ^ \
|
||||
SHA384_SHR(x, 7))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Little Sigma 1-512 function from NIST FIPS 180-4, Eq 4.13.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#define SHA384_SIGMA1(x) (SHA384_ROTR64(x, 19) ^ SHA384_ROTR64(x, 61) ^ \
|
||||
SHA384_SHR(x, 6))
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform a complete hash operation when input is supplied as 32-bit words,
|
||||
//! producing a final digest for the data.
|
||||
//! - This function wraps #SHA384_startWordWise(), #SHA384_addDataWordWise(),
|
||||
//! and #SHA384_finalizeWordWise().
|
||||
//! - There is no need to call #SHA384_startWordWise() prior to calling
|
||||
//! this function.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.)
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleWordWise' handle
|
||||
//! \param data 32-bit pointer pointing to the starting memory location of
|
||||
//! the data to be hashed
|
||||
//! \param length Length of the data (in number of bytes) to be hashed.
|
||||
//! Note that for word-wise input, length of the data should be
|
||||
//! a multiple of 4.
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! #SHA384_STATUS_LENGTH_TOO_LARGE -
|
||||
//! The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! #SHA384_STATUS_NULL_INPUT - One or
|
||||
//! more of the pointer inputs is NULL.
|
||||
//! #SHA384_STATUS_NOT_ALIGNED - Length.
|
||||
//! is not a multiple of 4
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_hashWordWise(SHA384_HandleWordWise handle, uint32_t *data,
|
||||
size_t length, uint64_t digest[6]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Perform a complete hash operation when input is supplied byte-wise,
|
||||
//! producing a final digest for the data.
|
||||
//! - This function wraps #SHA384_startByteWise(), #SHA384_addDataByteWise(),
|
||||
//! and #SHA384_finalizeByteWise().
|
||||
//! - There is no need to call #SHA384_startByteWise() prior to calling
|
||||
//! this function.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.)
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleByteWise' handle
|
||||
//! \param data 16-bit pointer pointing to the memory location of the
|
||||
//! starting byte the data to be hashed.
|
||||
//! \param length Length of the data (in number of bytes) to be hashed.
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA384_STATUS_LENGTH_TOO_LARGE The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! \return #SHA384_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_hashByteWise(SHA384_HandleByteWise handle, uint16_t *data,
|
||||
size_t length, uint64_t digest[6]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Initialize a 'SHA384SW_HandleWordWise' handle, preparing for hashing data.
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleWordWise' handle
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA384_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_startWordWise(SHA384_HandleWordWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Initialize a 'SHA384SW_HandleByteWise' handle, preparing for hashing data.
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleByteWise' handle
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA384_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_startByteWise(SHA384_HandleByteWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Add data to SHA384 operation when inputs are supplied as 32-bit words
|
||||
//! - Adds data to a hash operation. The @c handle must have been
|
||||
//! initialized by a call to SHA384_startWordWise first.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.).
|
||||
//! - After passing in all data to be hashed, call #SHA384_finalizeWordWise()
|
||||
//! to obtain the final digest.
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleWordWise' handle.
|
||||
//! \param data 32-bit pointer pointing to the starting memory location of
|
||||
//! the data to be hashed.
|
||||
//! \param length Length of the data (in number of bytes) to be hashed
|
||||
//! Note that for word-wise input, length of the data should
|
||||
//! be a multiple of 4.
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA384_STATUS_LENGTH_TOO_LARGE The requested length of data to
|
||||
//! hash is more than the
|
||||
//! implementation supports.
|
||||
//! \return #SHA384_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//! \return #SHA384_STATUS_NOT_ALIGNED Length is not a multiple of 4
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_addDataWordWise(SHA384_HandleWordWise handle, uint32_t *data,
|
||||
size_t length);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Add data to SHA384 operation when inputs are supplied byte-wise
|
||||
//! - Adds data to a hash operation. The @c handle must have been
|
||||
//! initialized by a call to SHA384_startByteWise first.
|
||||
//! - The total length of data that can be hashed by this implementation
|
||||
//! is 512MiB (0x20000000 bytes.).
|
||||
//! - After passing in all data to be hashed, call #SHA384_finalizeWordWise()
|
||||
//! to obtain the final digest.
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleByteWise' handle.
|
||||
//! \param data 16-bit pointer pointing to the memory location of the
|
||||
//! starting byte the data to be hashed.
|
||||
//! \param length Length of the data (in number of bytes) to be hashed
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS hash operation succeeded.
|
||||
//! #SHA384_STATUS_LENGTH_TOO_LARGE -
|
||||
//! requested length of data to
|
||||
//! hash is more than supported length.
|
||||
//! #SHA384_STATUS_NULL_INPUT- One or
|
||||
//! more of the pointer inputs is NULL
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_addDataByteWise(SHA384_HandleByteWise handle, uint16_t *data,
|
||||
size_t length);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Finalize the SHA384 operation when input is supplied as 32-bit words,
|
||||
//! creating the final digest.
|
||||
//! - After calling this function, @c handle should not be used again
|
||||
//! until it has been reinitialized via a call to #SHA384_startWordWise().
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleWordWise' handle
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA384_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_finalizeWordWise(SHA384_HandleWordWise handle,
|
||||
uint64_t digest[6]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Finalize the SHA384 operation when input is supplied byte-wise,
|
||||
//! creating the final digest.
|
||||
//! - After calling this function, @c handle should not be used again
|
||||
//! until it has been reinitialized via a call to #SHA384_startByteWise().
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleByteWise' handle
|
||||
//! \param digest Output location for the final digest
|
||||
//!
|
||||
//! \return #SHA384_STATUS_SUCCESS The hash operation succeeded.
|
||||
//! \return #SHA384_STATUS_NULL_INPUT One or more of the pointer inputs
|
||||
//! is NULL.
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_finalizeByteWise(SHA384_HandleByteWise handle,
|
||||
uint64_t digest[6]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Cancels SHA384 operation by clearing intermediate
|
||||
//! data stored in the 'SHA384_HandleWordWise' handle.
|
||||
//! - The handle will not be ready for a new operation until after
|
||||
//! #SHA384_startWordWise() is called on the handle.
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleWordWise' handle
|
||||
//!
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA384_cancelWordWise(SHA384_HandleWordWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Cancels SHA384 operation by clearing intermediate
|
||||
//! data stored in the 'SHA384_HandleByteWise' handle.
|
||||
//! - The handle will not be ready for a new operation until after
|
||||
//! #SHA384_startByteWise() is called on the handle.
|
||||
//!
|
||||
//! \param handle A 'SHA384_HandleByteWise' handle
|
||||
//!
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA384_cancelByteWise(SHA384_HandleByteWise handle);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Performs core SHA2-384 algorithm on one 1024 bit block of data when input
|
||||
//! is supplied as 32-bit words.
|
||||
//! - object->digest will contain the updated digest resulting from
|
||||
//! hashing the block.
|
||||
//!
|
||||
//! \param digest pre-loaded with previous block's digest (or initial
|
||||
//! digest if call is to process first block)
|
||||
//! On function return digest will hold the updated digest.
|
||||
//! \param Ws loaded with data block, including padding as specified by
|
||||
//! the SHA384 standard.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA384_processBlockWordWise(uint64_t digest[8], uint64_t Ws[16]);
|
||||
extern void _SHA384_processBlockWordWise_casm_C28(uint64_t digest[8], uint64_t Ws[16]);
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
//! Performs core SHA2-384 algorithm on one 1024 bit block of data when input
|
||||
//! is supplied byte-wise.
|
||||
//! - object->digest will contain the updated digest resulting from
|
||||
//! hashing the block.
|
||||
//!
|
||||
//! \param digest pre-loaded with previous block's digest (or initial
|
||||
//! digest if call is to process first block)
|
||||
//! On function return digest will hold the updated digest.
|
||||
//! \param Ws loaded with data block, including padding as specified by
|
||||
//! the SHA384 standard.
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA384_processBlockByteWise(uint64_t digest[8], uint16_t Ws[128]);
|
||||
|
||||
extern void _SHA384_processBlockByteWise_casm_C28(uint64_t digest[8], uint16_t Ws[128]);
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Close the Doxygen group.
|
||||
//! @}
|
||||
//
|
||||
//****************************************************************************
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// Mark the end of the C bindings section for C++ compilers.
|
||||
//
|
||||
//*****************************************************************************
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* SHA384_H_ */
|
||||
@@ -0,0 +1,822 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: sha256.c
|
||||
//
|
||||
// TITLE: Implementation of the SHA-256 algorithm
|
||||
//
|
||||
//###########################################################################
|
||||
// $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 <sha256.h>
|
||||
|
||||
// Uncomment line when copying constants from FLASH to RAM
|
||||
#pragma DATA_SECTION(SHA256_K, ".TI.ramfunc"); // map the TX data to memory
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 round constants
|
||||
//
|
||||
//*****************************************************************************
|
||||
const uint32_t SHA256_K[64] = { 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
|
||||
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
||||
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
|
||||
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
||||
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
|
||||
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
||||
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
|
||||
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
||||
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
|
||||
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
||||
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
|
||||
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
||||
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
|
||||
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
||||
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
|
||||
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2};
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 single call hash function
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_hashWordWise(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length, uint32_t digest[8])
|
||||
{
|
||||
uint16_t result;
|
||||
|
||||
//
|
||||
// Initialize the SHA256 handle
|
||||
//
|
||||
result = SHA256_startWordWise(handle);
|
||||
|
||||
//
|
||||
// Add data block by block
|
||||
//
|
||||
if(result == SHA256_STATUS_SUCCESS) {
|
||||
result = SHA256_addDataWordWise(handle, data, length);
|
||||
}
|
||||
|
||||
//
|
||||
// Finalize SHA-256 hashing
|
||||
//
|
||||
if(result == SHA256_STATUS_SUCCESS) {
|
||||
result = SHA256_finalizeWordWise(handle, digest);
|
||||
}
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 single call hash function
|
||||
// Input message : Word-wise, High-Low words Swapped
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_hashWordWiseSwapped(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length, uint32_t digest[8])
|
||||
{
|
||||
uint16_t result;
|
||||
|
||||
//
|
||||
// Initialize the SHA256 handle
|
||||
//
|
||||
result = SHA256_startWordWise(handle);
|
||||
|
||||
//
|
||||
// Add data block by block
|
||||
//
|
||||
if(result == SHA256_STATUS_SUCCESS) {
|
||||
result = SHA256_addDataWordWiseSwapped(handle, data, length);
|
||||
}
|
||||
|
||||
//
|
||||
// Finalize SHA-256 hashing
|
||||
//
|
||||
if(result == SHA256_STATUS_SUCCESS) {
|
||||
result = SHA256_finalizeWordWise(handle, digest);
|
||||
}
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 single call hash function
|
||||
// Input message : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_hashByteWise(SHA256_HandleByteWise handle, uint16_t *data,
|
||||
size_t length, uint32_t digest[8])
|
||||
{
|
||||
uint16_t result;
|
||||
|
||||
//
|
||||
// Initialize the SHA256 handle
|
||||
//
|
||||
result = SHA256_startByteWise(handle);
|
||||
|
||||
//
|
||||
// Add data block by block
|
||||
//
|
||||
if(result == SHA256_STATUS_SUCCESS) {
|
||||
result = SHA256_addDataByteWise(handle, data, length);
|
||||
}
|
||||
|
||||
//
|
||||
// Finalize SHA-256 hashing
|
||||
//
|
||||
if(result == SHA256_STATUS_SUCCESS) {
|
||||
result = SHA256_finalizeByteWise(handle, digest);
|
||||
}
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 start function
|
||||
// Input type : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_startWordWise(SHA256_HandleWordWise handle)
|
||||
{
|
||||
uint16_t result = SHA256_STATUS_SUCCESS;
|
||||
|
||||
if(handle == NULL)
|
||||
{
|
||||
result = SHA256_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize handle with starting values
|
||||
//
|
||||
handle->bitsProcessed = 0;
|
||||
handle->offsetWb = 0;
|
||||
|
||||
handle->digest[0] = 0x6A09E667;
|
||||
handle->digest[1] = 0xBB67AE85;
|
||||
handle->digest[2] = 0x3C6EF372;
|
||||
handle->digest[3] = 0xA54FF53A;
|
||||
handle->digest[4] = 0x510E527F;
|
||||
handle->digest[5] = 0x9B05688C;
|
||||
handle->digest[6] = 0x1F83D9AB;
|
||||
handle->digest[7] = 0x5BE0CD19;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 start function
|
||||
// Input type : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_startByteWise(SHA256_HandleByteWise handle)
|
||||
{
|
||||
uint16_t result = SHA256_STATUS_SUCCESS;
|
||||
|
||||
if(handle == NULL)
|
||||
{
|
||||
result = SHA256_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize handle with starting values
|
||||
//
|
||||
handle->bitsProcessed = 0;
|
||||
handle->offsetWb = 0;
|
||||
|
||||
handle->digest[0] = 0x6A09E667;
|
||||
handle->digest[1] = 0xBB67AE85;
|
||||
handle->digest[2] = 0x3C6EF372;
|
||||
handle->digest[3] = 0xA54FF53A;
|
||||
handle->digest[4] = 0x510E527F;
|
||||
handle->digest[5] = 0x9B05688C;
|
||||
handle->digest[6] = 0x1F83D9AB;
|
||||
handle->digest[7] = 0x5BE0CD19;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 add data block function
|
||||
// Input data : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_addDataWordWise(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length)
|
||||
{
|
||||
int16_t i;
|
||||
size_t dataOffset;
|
||||
uint16_t result = SHA256_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (data == NULL))
|
||||
{
|
||||
result = SHA256_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(length >= 0x20000000U)
|
||||
{
|
||||
result = SHA256_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(((length << 3u) + handle->bitsProcessed) < handle->bitsProcessed)
|
||||
{
|
||||
result = SHA256_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if((length & 0x3U) != 0)
|
||||
{
|
||||
result = SHA256_STATUS_NOT_ALIGNED;
|
||||
return(result);
|
||||
}
|
||||
|
||||
dataOffset = 0;
|
||||
length = length >> 2;
|
||||
while(dataOffset < length)
|
||||
{
|
||||
i = (int16_t) handle->offsetWb;
|
||||
|
||||
//
|
||||
// Load data block into the handle
|
||||
//
|
||||
while((i < 16) && (dataOffset < length))
|
||||
{
|
||||
handle->Ws[i] = data[dataOffset];
|
||||
i++;
|
||||
dataOffset++;
|
||||
}
|
||||
|
||||
handle->offsetWb = (int16_t) i;
|
||||
|
||||
//
|
||||
// After loading a block completely, process it
|
||||
//
|
||||
if(handle->offsetWb >= 16)
|
||||
{
|
||||
_SHA256_processBlockWordWise_casm_C28(handle->digest, handle->Ws);
|
||||
handle->offsetWb = 0;
|
||||
}
|
||||
}
|
||||
|
||||
handle->bitsProcessed += length << 5;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 add data block function
|
||||
// Input data : Word-wise , High-Low words Swapped
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_addDataWordWiseSwapped(SHA256_HandleWordWise handle, uint32_t *data,
|
||||
size_t length)
|
||||
{
|
||||
int16_t i;
|
||||
size_t dataOffset;
|
||||
uint16_t result = SHA256_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (data == NULL))
|
||||
{
|
||||
result = SHA256_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(length >= 0x20000000U)
|
||||
{
|
||||
result = SHA256_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(((length << 3u) + handle->bitsProcessed) < handle->bitsProcessed)
|
||||
{
|
||||
result = SHA256_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if((length & 0x3U) != 0)
|
||||
{
|
||||
result = SHA256_STATUS_NOT_ALIGNED;
|
||||
return(result);
|
||||
}
|
||||
|
||||
dataOffset = 0;
|
||||
length = length >> 2;
|
||||
while(dataOffset < length)
|
||||
{
|
||||
i = (int16_t) handle->offsetWb;
|
||||
|
||||
//
|
||||
// Load data block into the handle
|
||||
//
|
||||
while((i < 16) && (dataOffset < length))
|
||||
{
|
||||
//
|
||||
// Flip High-Low words
|
||||
//
|
||||
uint32_t low_word_32 = (data[dataOffset] & 0xFFFF0000) >> 16;
|
||||
uint32_t high_word_32 = (data[dataOffset] & 0x0000FFFF) << 16;
|
||||
handle->Ws[i] = high_word_32 | low_word_32;
|
||||
i++;
|
||||
dataOffset++;
|
||||
}
|
||||
|
||||
handle->offsetWb = (int16_t) i;
|
||||
|
||||
//
|
||||
// After loading a block completely, process it
|
||||
//
|
||||
if(handle->offsetWb >= 16)
|
||||
{
|
||||
_SHA256_processBlockWordWise_casm_C28(handle->digest, handle->Ws);
|
||||
handle->offsetWb = 0;
|
||||
}
|
||||
}
|
||||
|
||||
handle->bitsProcessed += length << 5;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 add data block function
|
||||
// Input data : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_addDataByteWise(SHA256_HandleByteWise handle, uint16_t *data,
|
||||
size_t length)
|
||||
{
|
||||
int16_t i;
|
||||
size_t dataOffset;
|
||||
uint16_t result = SHA256_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (data == NULL))
|
||||
{
|
||||
result = SHA256_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(length >= 0x20000000U)
|
||||
{
|
||||
result = SHA256_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(((length << 3u) + handle->bitsProcessed) < handle->bitsProcessed)
|
||||
{
|
||||
result = SHA256_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
dataOffset = 0;
|
||||
while(dataOffset < length)
|
||||
{
|
||||
i = (int16_t) handle->offsetWb;
|
||||
|
||||
//
|
||||
// Load data block into the handle
|
||||
//
|
||||
while((i < 64) && (dataOffset < length))
|
||||
{
|
||||
handle->Ws[i] = data[dataOffset];
|
||||
i++;
|
||||
dataOffset++;
|
||||
}
|
||||
|
||||
handle->offsetWb = (int16_t) i;
|
||||
|
||||
//
|
||||
// After loading a block completely, process it
|
||||
//
|
||||
if(handle->offsetWb >= 64)
|
||||
{
|
||||
_SHA256_processBlockByteWise_casm_C28(handle->digest, handle->Ws);
|
||||
handle->offsetWb = 0;
|
||||
}
|
||||
}
|
||||
|
||||
handle->bitsProcessed += length << 3;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 finalize hash function
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_finalizeWordWise(SHA256_HandleWordWise handle,
|
||||
uint32_t digest[8])
|
||||
{
|
||||
int16_t i;
|
||||
uint16_t paddedBlocks;
|
||||
uint16_t result = SHA256_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (digest == NULL))
|
||||
{
|
||||
result = SHA256_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
i = handle->offsetWb;
|
||||
|
||||
//
|
||||
// '1' pad bit
|
||||
//
|
||||
handle->Ws[i] = 0x80000000;
|
||||
i++;
|
||||
|
||||
//
|
||||
// If fewer than 8 bytes (64 bits) remain in current
|
||||
// block, must pad out the current block and then pad out
|
||||
// another block. (Need space to store 64 bit value
|
||||
// of bits processed in the end of the last block.)
|
||||
//
|
||||
if(handle->offsetWb >= 14)
|
||||
{
|
||||
paddedBlocks = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
paddedBlocks = 1;
|
||||
}
|
||||
|
||||
//
|
||||
// Perform padding
|
||||
//
|
||||
do
|
||||
{
|
||||
//
|
||||
// Finish out block with 0 pad.
|
||||
//
|
||||
while(i < 15)
|
||||
{
|
||||
handle->Ws[i] = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
//
|
||||
// Write bitsProcessed into last block of padding.
|
||||
// Note: implementation only supports 2^32 bits of input.
|
||||
//
|
||||
if(paddedBlocks == 1)
|
||||
{
|
||||
handle->Ws[i] = handle->bitsProcessed;
|
||||
}
|
||||
else
|
||||
{
|
||||
handle->Ws[i] = 0;
|
||||
}
|
||||
|
||||
//
|
||||
// Process the final block
|
||||
//
|
||||
_SHA256_processBlockWordWise_casm_C28(handle->digest, handle->Ws);
|
||||
i = 0;
|
||||
|
||||
} while(--paddedBlocks > 0);
|
||||
|
||||
//
|
||||
// Copy out final digest
|
||||
//
|
||||
digest[0] = handle->digest[0];
|
||||
digest[1] = handle->digest[1];
|
||||
digest[2] = handle->digest[2];
|
||||
digest[3] = handle->digest[3];
|
||||
digest[4] = handle->digest[4];
|
||||
digest[5] = handle->digest[5];
|
||||
digest[6] = handle->digest[6];
|
||||
digest[7] = handle->digest[7];
|
||||
|
||||
//
|
||||
// For security - clear data held by handle
|
||||
//
|
||||
SHA256_cancelWordWise(handle);
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 finalize hash function
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA256_finalizeByteWise(SHA256_HandleByteWise handle,
|
||||
uint32_t digest[8])
|
||||
{
|
||||
int16_t i;
|
||||
uint16_t paddedBlocks;
|
||||
uint16_t result = SHA256_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (digest == NULL))
|
||||
{
|
||||
result = SHA256_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
i = handle->offsetWb;
|
||||
|
||||
//
|
||||
// '1' pad bit
|
||||
//
|
||||
handle->Ws[i] = 0x80;
|
||||
i++;
|
||||
|
||||
//
|
||||
// If fewer than 8 bytes (64 bits) remain in current
|
||||
// block, must pad out the current block and then pad out
|
||||
// another block. (Need space to store 64 bit value
|
||||
// of bits processed in the end of the last block.)
|
||||
//
|
||||
if(handle->offsetWb >= 56)
|
||||
{
|
||||
paddedBlocks = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
paddedBlocks = 1;
|
||||
}
|
||||
|
||||
//
|
||||
// Perform padding
|
||||
//
|
||||
do
|
||||
{
|
||||
//
|
||||
// Finish out block with 0 pad.
|
||||
//
|
||||
while(i < 64)
|
||||
{
|
||||
handle->Ws[i] = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
//
|
||||
// Write bitsProcessed into last block of padding.
|
||||
// Note: implementation only supports 2^32 bits of input.
|
||||
//
|
||||
if(paddedBlocks == 1)
|
||||
{
|
||||
SHA256_PUTU32(&handle->Ws[60], handle->bitsProcessed)
|
||||
}
|
||||
|
||||
//
|
||||
// Process the final block
|
||||
//
|
||||
_SHA256_processBlockByteWise_casm_C28(handle->digest, handle->Ws);
|
||||
i = 0;
|
||||
|
||||
} while(--paddedBlocks > 0);
|
||||
|
||||
//
|
||||
// Copy out final digest
|
||||
//
|
||||
digest[0] = handle->digest[0];
|
||||
digest[1] = handle->digest[1];
|
||||
digest[2] = handle->digest[2];
|
||||
digest[3] = handle->digest[3];
|
||||
digest[4] = handle->digest[4];
|
||||
digest[5] = handle->digest[5];
|
||||
digest[6] = handle->digest[6];
|
||||
digest[7] = handle->digest[7];
|
||||
|
||||
//
|
||||
// For security - clear data held by handle
|
||||
//
|
||||
SHA256_cancelByteWise(handle);
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 clear handle
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_cancelWordWise(SHA256_HandleWordWise handle)
|
||||
{
|
||||
int16_t i, j;
|
||||
uint16_t * objAsBytes;
|
||||
j = sizeof(SHA256_ObjectWordWise);
|
||||
|
||||
if(handle != NULL)
|
||||
{
|
||||
objAsBytes = (uint16_t *) handle;
|
||||
|
||||
for (i = 0; i < j; i++)
|
||||
{
|
||||
objAsBytes[i] = 0x0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 clear handle
|
||||
// Input message : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_cancelByteWise(SHA256_HandleByteWise handle)
|
||||
{
|
||||
int16_t i, j;
|
||||
uint16_t * objAsBytes;
|
||||
j = sizeof(SHA256_ObjectByteWise);
|
||||
|
||||
if(handle != NULL)
|
||||
{
|
||||
objAsBytes = (uint16_t *) handle;
|
||||
|
||||
for (i = 0; i < j; i++)
|
||||
{
|
||||
objAsBytes[i] = 0x0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 process a block of data
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_processBlockWordWise(uint32_t digest[8], uint32_t Ws[64])
|
||||
{
|
||||
uint32_t wt; // Wt from standard
|
||||
int16_t s; // s is the message schedule index
|
||||
uint32_t temp1; // T1 from standard
|
||||
uint32_t temp2; // T2 from standard
|
||||
|
||||
//
|
||||
// Initialize working variables
|
||||
//
|
||||
uint32_t a = digest[0];
|
||||
uint32_t b = digest[1];
|
||||
uint32_t c = digest[2];
|
||||
uint32_t d = digest[3];
|
||||
uint32_t e = digest[4];
|
||||
uint32_t f = digest[5];
|
||||
uint32_t g = digest[6];
|
||||
uint32_t h = digest[7];
|
||||
|
||||
//
|
||||
// Perform 64 rounds of compression function
|
||||
//
|
||||
for(s = 0; s < 64; s++)
|
||||
{
|
||||
if(s >= 16)
|
||||
{
|
||||
Ws[s & 0xF] += SHA256_SIGMA1(Ws[(s + 14) & 0xF]) +
|
||||
Ws[(s + 9) & 0xF] +
|
||||
SHA256_SIGMAZ(Ws[(s + 1) & 0xF]);
|
||||
}
|
||||
|
||||
wt = Ws[s & 0xF];
|
||||
temp1 = h + SHA256_SIG1(e) + SHA256_CH(e, f, g) + SHA256_K[s] + wt;
|
||||
temp2 = SHA256_SIGZ(a) + SHA256_MAJ(a, b, c);
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + temp1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = temp1 + temp2;
|
||||
}
|
||||
|
||||
//
|
||||
// Intermediate digest value
|
||||
//
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
digest[5] += f;
|
||||
digest[6] += g;
|
||||
digest[7] += h;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 process a block of data
|
||||
// Input message : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA256_processBlockByteWise(uint32_t digest[8], uint16_t Ws[64])
|
||||
{
|
||||
uint32_t wt; // Wt from standard
|
||||
int16_t s, s0; // s is the message schedule index
|
||||
uint32_t temp1; // T1 from standard
|
||||
uint32_t temp2; // T2 from standard
|
||||
|
||||
//
|
||||
// Initialize working variables
|
||||
//
|
||||
uint32_t a = digest[0];
|
||||
uint32_t b = digest[1];
|
||||
uint32_t c = digest[2];
|
||||
uint32_t d = digest[3];
|
||||
uint32_t e = digest[4];
|
||||
uint32_t f = digest[5];
|
||||
uint32_t g = digest[6];
|
||||
uint32_t h = digest[7];
|
||||
|
||||
//
|
||||
// Perform 64 rounds of compression function
|
||||
//
|
||||
for(s = 0; s < 64; s++)
|
||||
{
|
||||
s0 = s << 2;
|
||||
if(s0 >= 64)
|
||||
{
|
||||
wt = SHA256_SIGMA1(SHA256_GETU32(&Ws[(s0-8) & 0x3F])) +
|
||||
SHA256_GETU32(&Ws[(s0-28) & 0x3F]) +
|
||||
SHA256_SIGMAZ(SHA256_GETU32(&Ws[(s0-60) & 0x3F])) +
|
||||
SHA256_GETU32(&Ws[(s0-64) & 0x3F]);
|
||||
|
||||
SHA256_PUTU32(&Ws[s0 & 0x3F], wt);
|
||||
}
|
||||
else
|
||||
{
|
||||
wt = SHA256_GETU32(&Ws[s0]);
|
||||
}
|
||||
|
||||
temp1 = h + SHA256_SIG1(e) + SHA256_CH(e, f, g) + SHA256_K[s] + wt;
|
||||
temp2 = SHA256_SIGZ(a) + SHA256_MAJ(a, b, c);
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + temp1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = temp1 + temp2;
|
||||
}
|
||||
|
||||
//
|
||||
// Intermediate digest value
|
||||
//
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
digest[5] += f;
|
||||
digest[6] += g;
|
||||
digest[7] += h;
|
||||
}
|
||||
|
||||
//
|
||||
// End of file
|
||||
//
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,729 @@
|
||||
//###########################################################################
|
||||
//
|
||||
// FILE: sha384.c
|
||||
//
|
||||
// TITLE: Implementation of the SHA-384 algorithm
|
||||
//
|
||||
//###########################################################################
|
||||
// $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 <sha384.h>
|
||||
|
||||
// Uncomment line when copying constants from FLASH to RAM
|
||||
#pragma DATA_SECTION(SHA384_K, ".TI.ramfunc"); // map the TX data to memory
|
||||
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 round constants
|
||||
//
|
||||
//*****************************************************************************
|
||||
const uint64_t SHA384_K[80] = { 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f, 0xe9b5dba58189dbbc,
|
||||
0x3956c25bf348b538, 0x59f111f1b605d019, 0x923f82a4af194f9b, 0xab1c5ed5da6d8118,
|
||||
0xd807aa98a3030242, 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
|
||||
0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235, 0xc19bf174cf692694,
|
||||
0xe49b69c19ef14ad2, 0xefbe4786384f25e3, 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65,
|
||||
0x2de92c6f592b0275, 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
|
||||
0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f, 0xbf597fc7beef0ee4,
|
||||
0xc6e00bf33da88fc2, 0xd5a79147930aa725, 0x06ca6351e003826f, 0x142929670a0e6e70,
|
||||
0x27b70a8546d22ffc, 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
|
||||
0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6, 0x92722c851482353b,
|
||||
0xa2bfe8a14cf10364, 0xa81a664bbc423001, 0xc24b8b70d0f89791, 0xc76c51a30654be30,
|
||||
0xd192e819d6ef5218, 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
|
||||
0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99, 0x34b0bcb5e19b48a8,
|
||||
0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb, 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3,
|
||||
0x748f82ee5defb2fc, 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
|
||||
0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915, 0xc67178f2e372532b,
|
||||
0xca273eceea26619c, 0xd186b8c721c0c207, 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178,
|
||||
0x06f067aa72176fba, 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
|
||||
0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc, 0x431d67c49c100d4c,
|
||||
0x4cc5d4becb3e42b6, 0x597f299cfc657e2a, 0x5fcb6fab3ad6faec, 0x6c44198c4a475817};
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 single call hash function
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_hashWordWise(SHA384_HandleWordWise handle, uint32_t *data,
|
||||
size_t length, uint64_t digest[6])
|
||||
{
|
||||
uint16_t result;
|
||||
|
||||
//
|
||||
// Initialize the SHA384 handle
|
||||
//
|
||||
result = SHA384_startWordWise(handle);
|
||||
|
||||
//
|
||||
// Add data block by block
|
||||
//
|
||||
if(result == SHA384_STATUS_SUCCESS) {
|
||||
result = SHA384_addDataWordWise(handle, data, length);
|
||||
}
|
||||
|
||||
//
|
||||
// Finalize SHA-384 hashing
|
||||
//
|
||||
if(result == SHA384_STATUS_SUCCESS) {
|
||||
result = SHA384_finalizeWordWise(handle, digest);
|
||||
}
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 single call hash function
|
||||
// Input message : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_hashByteWise(SHA384_HandleByteWise handle, uint16_t *data,
|
||||
size_t length, uint64_t digest[6])
|
||||
{
|
||||
uint16_t result;
|
||||
|
||||
//
|
||||
// Initialize the SHA256 handle
|
||||
//
|
||||
result = SHA384_startByteWise(handle);
|
||||
|
||||
//
|
||||
// Add data block by block
|
||||
//
|
||||
if(result == SHA384_STATUS_SUCCESS) {
|
||||
result = SHA384_addDataByteWise(handle, data, length);
|
||||
}
|
||||
|
||||
//
|
||||
// Finalize SHA-384 hashing
|
||||
//
|
||||
if(result == SHA384_STATUS_SUCCESS) {
|
||||
result = SHA384_finalizeByteWise(handle, digest);
|
||||
}
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 start function
|
||||
// Input type : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_startWordWise(SHA384_HandleWordWise handle)
|
||||
{
|
||||
uint16_t result = SHA384_STATUS_SUCCESS;
|
||||
|
||||
if(handle == NULL)
|
||||
{
|
||||
result = SHA384_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize handle with starting values
|
||||
//
|
||||
handle->bitsProcessed = 0;
|
||||
handle->offsetWb = 0;
|
||||
|
||||
handle->digest[0] = 0xcbbb9d5dc1059ed8;
|
||||
handle->digest[1] = 0x629a292a367cd507;
|
||||
handle->digest[2] = 0x9159015a3070dd17;
|
||||
handle->digest[3] = 0x152fecd8f70e5939;
|
||||
handle->digest[4] = 0x67332667ffc00b31;
|
||||
handle->digest[5] = 0x8eb44a8768581511;
|
||||
handle->digest[6] = 0xdb0c2e0d64f98fa7;
|
||||
handle->digest[7] = 0x47b5481dbefa4fa4;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 start function
|
||||
// Input type : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_startByteWise(SHA384_HandleByteWise handle)
|
||||
{
|
||||
uint16_t result = SHA384_STATUS_SUCCESS;
|
||||
|
||||
if(handle == NULL)
|
||||
{
|
||||
result = SHA384_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
//
|
||||
// Initialize handle with starting values
|
||||
//
|
||||
handle->bitsProcessed = 0;
|
||||
handle->offsetWb = 0;
|
||||
|
||||
handle->digest[0] = 0xcbbb9d5dc1059ed8;
|
||||
handle->digest[1] = 0x629a292a367cd507;
|
||||
handle->digest[2] = 0x9159015a3070dd17;
|
||||
handle->digest[3] = 0x152fecd8f70e5939;
|
||||
handle->digest[4] = 0x67332667ffc00b31;
|
||||
handle->digest[5] = 0x8eb44a8768581511;
|
||||
handle->digest[6] = 0xdb0c2e0d64f98fa7;
|
||||
handle->digest[7] = 0x47b5481dbefa4fa4;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 add data block function
|
||||
// Input data : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_addDataWordWise(SHA384_HandleWordWise handle, uint32_t *data,
|
||||
size_t length)
|
||||
{
|
||||
int16_t i;
|
||||
size_t dataOffset;
|
||||
uint16_t result = SHA384_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (data == NULL))
|
||||
{
|
||||
result = SHA384_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(length >= 0x20000000U)
|
||||
{
|
||||
result = SHA384_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(((length << 3u) + handle->bitsProcessed) < handle->bitsProcessed)
|
||||
{
|
||||
result = SHA384_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if((length & 0x3U) != 0)
|
||||
{
|
||||
result = SHA384_STATUS_NOT_ALIGNED;
|
||||
return(result);
|
||||
}
|
||||
|
||||
dataOffset = 0;
|
||||
length = length >> 2;
|
||||
while(dataOffset < length)
|
||||
{
|
||||
i = (int16_t) handle->offsetWb;
|
||||
|
||||
//
|
||||
// Load data block into the handle
|
||||
//
|
||||
while((i < 32) && (dataOffset < length))
|
||||
{
|
||||
handle->Ws[i] = data[dataOffset];
|
||||
i++;
|
||||
dataOffset++;
|
||||
}
|
||||
|
||||
handle->offsetWb = (int16_t) i;
|
||||
|
||||
//
|
||||
// After loading a block completely, process it
|
||||
//
|
||||
if(handle->offsetWb >= 32)
|
||||
{
|
||||
_SHA384_processBlockWordWise_casm_C28(handle->digest, (uint64_t *)handle->Ws);
|
||||
handle->offsetWb = 0;
|
||||
}
|
||||
}
|
||||
|
||||
handle->bitsProcessed += length << 5;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 add data block function
|
||||
// Input data : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_addDataByteWise(SHA384_HandleByteWise handle, uint16_t *data,
|
||||
size_t length)
|
||||
{
|
||||
int16_t i;
|
||||
size_t dataOffset;
|
||||
uint16_t result = SHA384_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (data == NULL))
|
||||
{
|
||||
result = SHA384_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(length >= 0x20000000U)
|
||||
{
|
||||
result = SHA384_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
if(((length << 3u) + handle->bitsProcessed) < handle->bitsProcessed)
|
||||
{
|
||||
result = SHA384_STATUS_LENGTH_TOO_LARGE;
|
||||
return(result);
|
||||
}
|
||||
|
||||
dataOffset = 0;
|
||||
while(dataOffset < length)
|
||||
{
|
||||
i = (int16_t) handle->offsetWb;
|
||||
|
||||
//
|
||||
// Load data block into the handle
|
||||
//
|
||||
while((i < 128) && (dataOffset < length))
|
||||
{
|
||||
handle->Ws[i] = data[dataOffset];
|
||||
i++;
|
||||
dataOffset++;
|
||||
}
|
||||
|
||||
handle->offsetWb = (int16_t) i;
|
||||
|
||||
//
|
||||
// After loading a block completely, process it
|
||||
//
|
||||
if(handle->offsetWb >= 128)
|
||||
{
|
||||
// C-based implementation
|
||||
// SHA384_processBlockByteWise(handle->digest, handle->Ws);
|
||||
|
||||
// ASM implementation
|
||||
_SHA384_processBlockByteWise_casm_C28(handle->digest, handle->Ws);
|
||||
handle->offsetWb = 0;
|
||||
}
|
||||
}
|
||||
|
||||
handle->bitsProcessed += length << 3;
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 finalize hash function
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_finalizeWordWise(SHA384_HandleWordWise handle,
|
||||
uint64_t digest[6])
|
||||
{
|
||||
int16_t i;
|
||||
uint16_t paddedBlocks;
|
||||
uint16_t result = SHA384_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (digest == NULL))
|
||||
{
|
||||
result = SHA384_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
i = handle->offsetWb;
|
||||
|
||||
//
|
||||
// '1' pad bit
|
||||
//
|
||||
handle->Ws[i] = 0x80000000;
|
||||
i++;
|
||||
|
||||
//
|
||||
// If fewer than 16 bytes (128 bits) remain in current
|
||||
// block, must pad out the current block and then pad out
|
||||
// another block. (Need space to store 128 bit value
|
||||
// of bits processed in the end of the last block.)
|
||||
//
|
||||
if(handle->offsetWb >= 28)
|
||||
{
|
||||
paddedBlocks = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
paddedBlocks = 1;
|
||||
}
|
||||
|
||||
//
|
||||
// Perform padding
|
||||
//
|
||||
do
|
||||
{
|
||||
//
|
||||
// Finish out block with 0 pad.
|
||||
//
|
||||
while(i < 31)
|
||||
{
|
||||
handle->Ws[i] = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
//
|
||||
// Write bitsProcessed into last block of padding.
|
||||
// Note: implementation only supports 2^32 bits of input.
|
||||
//
|
||||
if(paddedBlocks == 1)
|
||||
{
|
||||
handle->Ws[i] = handle->bitsProcessed;
|
||||
}
|
||||
else
|
||||
{
|
||||
handle->Ws[i] = 0;
|
||||
}
|
||||
|
||||
//
|
||||
// Process the final block
|
||||
//
|
||||
_SHA384_processBlockWordWise_casm_C28(handle->digest, (uint64_t *)handle->Ws);
|
||||
i = 0;
|
||||
|
||||
} while(--paddedBlocks > 0);
|
||||
|
||||
//
|
||||
// Copy out final digest
|
||||
//
|
||||
digest[0] = handle->digest[0];
|
||||
digest[1] = handle->digest[1];
|
||||
digest[2] = handle->digest[2];
|
||||
digest[3] = handle->digest[3];
|
||||
digest[4] = handle->digest[4];
|
||||
digest[5] = handle->digest[5];
|
||||
|
||||
//
|
||||
// For security - clear data held by handle
|
||||
//
|
||||
SHA384_cancelWordWise(handle);
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-256 finalize hash function
|
||||
//
|
||||
//*****************************************************************************
|
||||
uint16_t SHA384_finalizeByteWise(SHA384_HandleByteWise handle,
|
||||
uint64_t digest[6])
|
||||
{
|
||||
int16_t i;
|
||||
uint16_t paddedBlocks;
|
||||
uint16_t result = SHA384_STATUS_SUCCESS;
|
||||
|
||||
if((handle == NULL) || (digest == NULL))
|
||||
{
|
||||
result = SHA384_STATUS_NULL_INPUT;
|
||||
return(result);
|
||||
}
|
||||
|
||||
i = handle->offsetWb;
|
||||
|
||||
//
|
||||
// '1' pad bit
|
||||
//
|
||||
handle->Ws[i] = 0x80;
|
||||
i++;
|
||||
|
||||
//
|
||||
// If fewer than 16 bytes (128 bits) remain in current
|
||||
// block, must pad out the current block and then pad out
|
||||
// another block. (Need space to store 128 bit value
|
||||
// of bits processed in the end of the last block.)
|
||||
//
|
||||
if(handle->offsetWb >= 112)
|
||||
{
|
||||
paddedBlocks = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
paddedBlocks = 1;
|
||||
}
|
||||
|
||||
//
|
||||
// Perform padding
|
||||
//
|
||||
do
|
||||
{
|
||||
//
|
||||
// Finish out block with 0 pad.
|
||||
//
|
||||
while(i < 128)
|
||||
{
|
||||
handle->Ws[i] = 0;
|
||||
i++;
|
||||
}
|
||||
|
||||
//
|
||||
// Write bitsProcessed into last block of padding.
|
||||
// Note: implementation only supports 2^32 bits of input.
|
||||
//
|
||||
if(paddedBlocks == 1)
|
||||
{
|
||||
SHA384_PUTU32(&handle->Ws[124], handle->bitsProcessed)
|
||||
}
|
||||
|
||||
//
|
||||
// Process the final block
|
||||
//
|
||||
|
||||
// C base implementation
|
||||
// SHA384_processBlockByteWise(handle->digest, handle->Ws);
|
||||
|
||||
// ASM implementation
|
||||
_SHA384_processBlockByteWise_casm_C28(handle->digest, handle->Ws);
|
||||
|
||||
i = 0;
|
||||
|
||||
} while(--paddedBlocks > 0);
|
||||
|
||||
//
|
||||
// Copy out final digest
|
||||
//
|
||||
digest[0] = handle->digest[0];
|
||||
digest[1] = handle->digest[1];
|
||||
digest[2] = handle->digest[2];
|
||||
digest[3] = handle->digest[3];
|
||||
digest[4] = handle->digest[4];
|
||||
digest[5] = handle->digest[5];
|
||||
|
||||
//
|
||||
// For security - clear data held by handle
|
||||
//
|
||||
SHA384_cancelByteWise(handle);
|
||||
|
||||
return(result);
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 clear handle
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA384_cancelWordWise(SHA384_HandleWordWise handle)
|
||||
{
|
||||
int16_t i, j;
|
||||
uint16_t * objAsBytes;
|
||||
j = sizeof(SHA384_ObjectWordWise);
|
||||
|
||||
if(handle != NULL)
|
||||
{
|
||||
objAsBytes = (uint16_t *) handle;
|
||||
|
||||
for (i = 0; i < j; i++)
|
||||
{
|
||||
objAsBytes[i] = 0x0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 clear handle
|
||||
// Input message : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA384_cancelByteWise(SHA384_HandleByteWise handle)
|
||||
{
|
||||
int16_t i, j;
|
||||
uint16_t * objAsBytes;
|
||||
j = sizeof(SHA384_ObjectByteWise);
|
||||
|
||||
if(handle != NULL)
|
||||
{
|
||||
objAsBytes = (uint16_t *) handle;
|
||||
|
||||
for (i = 0; i < j; i++)
|
||||
{
|
||||
objAsBytes[i] = 0x0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 process a block of data
|
||||
// Input message : Word-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
void SHA384_processBlockWordWise(uint64_t digest[8], uint64_t Ws[16])
|
||||
{
|
||||
uint64_t wt; // Wt from standard
|
||||
int16_t s; // s is the message schedule index
|
||||
uint64_t temp1; // T1 from standard
|
||||
uint64_t temp2; // T2 from standard
|
||||
|
||||
//
|
||||
// Initialize working variables
|
||||
//
|
||||
uint64_t a = digest[0];
|
||||
uint64_t b = digest[1];
|
||||
uint64_t c = digest[2];
|
||||
uint64_t d = digest[3];
|
||||
uint64_t e = digest[4];
|
||||
uint64_t f = digest[5];
|
||||
uint64_t g = digest[6];
|
||||
uint64_t h = digest[7];
|
||||
|
||||
//
|
||||
// Perform 80 rounds of compression function
|
||||
//
|
||||
for(s = 0; s < 80; s++)
|
||||
{
|
||||
if(s >= 16)
|
||||
{
|
||||
Ws[s & 0xF] += SHA384_SIGMA1(Ws[(s + 14) & 0xF]) +
|
||||
Ws[(s + 9) & 0xF] +
|
||||
SHA384_SIGMAZ(Ws[(s + 1) & 0xF]);
|
||||
}
|
||||
else
|
||||
{
|
||||
Ws[s] = ((uint64_t)(Ws[s] << 32) | (uint64_t)(Ws[s] >> 32));
|
||||
}
|
||||
|
||||
wt = Ws[s & 0xF];
|
||||
temp1 = h + SHA384_SIG1(e) + SHA384_CH(e, f, g) + SHA384_K[s] + wt;
|
||||
temp2 = SHA384_SIGZ(a) + SHA384_MAJ(a, b, c);
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + temp1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = temp1 + temp2;
|
||||
}
|
||||
|
||||
//
|
||||
// Intermediate digest value
|
||||
//
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
digest[5] += f;
|
||||
digest[6] += g;
|
||||
digest[7] += h;
|
||||
}
|
||||
|
||||
//*****************************************************************************
|
||||
//
|
||||
// SHA-384 process a block of data
|
||||
// Input message : Byte-wise
|
||||
//
|
||||
//*****************************************************************************
|
||||
|
||||
void SHA384_processBlockByteWise(uint64_t digest[8], uint16_t Ws[128])
|
||||
{
|
||||
uint64_t wt; // Wt from standard
|
||||
int16_t s, s0; // s is the message schedule index
|
||||
uint64_t temp1; // T1 from standard
|
||||
uint64_t temp2; // T2 from standard
|
||||
|
||||
//
|
||||
// Initialize working variables
|
||||
//
|
||||
uint64_t a = digest[0];
|
||||
uint64_t b = digest[1];
|
||||
uint64_t c = digest[2];
|
||||
uint64_t d = digest[3];
|
||||
uint64_t e = digest[4];
|
||||
uint64_t f = digest[5];
|
||||
uint64_t g = digest[6];
|
||||
uint64_t h = digest[7];
|
||||
|
||||
//
|
||||
// Perform 64 rounds of compression function
|
||||
//
|
||||
for(s = 0; s < 80; s++)
|
||||
{
|
||||
s0 = s << 3;
|
||||
if(s >= 16)
|
||||
{
|
||||
wt = SHA384_SIGMA1(SHA384_GETU64(&Ws[(s0 + 112) & 0x7F])) +
|
||||
SHA384_GETU64(&Ws[(s0 + 72) & 0x7F]) +
|
||||
SHA384_SIGMAZ(SHA384_GETU64(&Ws[(s0 + 8) & 0x7F])) +
|
||||
SHA384_GETU64(&Ws[s0 & 0x7F]);
|
||||
|
||||
SHA384_PUTU64(&Ws[s0 & 0x7F], wt);
|
||||
}
|
||||
else
|
||||
{
|
||||
wt = SHA384_GETU64(&Ws[s0]);
|
||||
}
|
||||
|
||||
temp1 = h + SHA384_SIG1(e) + SHA384_CH(e, f, g) + SHA384_K[s] + wt;
|
||||
temp2 = SHA384_SIGZ(a) + SHA384_MAJ(a, b, c);
|
||||
|
||||
h = g;
|
||||
g = f;
|
||||
f = e;
|
||||
e = d + temp1;
|
||||
d = c;
|
||||
c = b;
|
||||
b = a;
|
||||
a = temp1 + temp2;
|
||||
}
|
||||
|
||||
//
|
||||
// Intermediate digest value
|
||||
//
|
||||
digest[0] += a;
|
||||
digest[1] += b;
|
||||
digest[2] += c;
|
||||
digest[3] += d;
|
||||
digest[4] += e;
|
||||
digest[5] += f;
|
||||
digest[6] += g;
|
||||
digest[7] += h;
|
||||
}
|
||||
|
||||
//
|
||||
// End of file
|
||||
//
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user