Secure Hash Algorithm Library

This Library provides a set of routines for easier handling of the Secure Hash Algorithm module available on CEC1x02.

Library Routines

hash_status

Prototype

uint32_t hash_status();

Description

This routine returns the status of the hash block.

Parameters

None.

Returns

Returns the status of the HASH block. The status register is read only. The bit definition is provided below :

Bit Number Description
0 If 0, there is no error. If 1, it indicates that AHB error has occurred.
Requires

Nothing.

Notes

None.

hash_busy

Prototype

uint8_t hash_busy();

Description

This routine is used to check if the HASH block is busy.

Parameters

None.

Returns
  • 1 - block is busy,
  • 0 - otherwise.
Requires

Nothing.

Notes

None.

hash_start

Prototype

void hash_start(uint8_t ien);

Description

This routine is used to start the HASH engine.

Parameters
  • ien: a Boolean value indicating the state of interrupts.
Returns

Nothing.

Requires

Nothing.

Notes

Once started, the GIRQ18 bit 9 reflects the done status (1 if done). It must be cleared after it is set.

hash_done_status

Prototype

uint8_t hash_done_status(uint32_t *hw_status);

Description

This routine is used to check the done status of HASH block. The status register value is updated into the pointer passed by the buffer.

Parameters
  • hw_status: pointer where the status value will be stored. The bit definitions are listed below :
    Bit Numbers Description
    31:16 Hash Status Register Value.
    15:0 Hash Control Register Value.
Returns
  • 1 - if the done status,
  • 0 - otherwise.
Requires

Nothing.

Notes

None.

sha12_init

Prototype

uint8_t sha12_init(SHA12_CONTEXT_T * psha12_ctx, uint8_t mode);

Description

This routine initializes the SHA12_CONTEXT_T Data structure for the specified mode. This routine does not effect a change on the Hash Hardware.

Parameters
  • psha12_ctx: pointer to the SHA context structure. The structure definition is given below :
    typedef struct sha12_context_s SHA12_CONTEXT_T;
    struct sha12_context_s {
        SHA12_DIGEST_U hash;
        union {
            uint32_t w[(SHA12_BLOCK_WLEN) * 2];
            uint8_t b[(SHA12_BLOCK_BLEN) * 2];
        } block;
        uint8_t mode;
        uint8_t block_len;
        uint8_t rsvd[2];
    #ifdef SHA_ENABLE_64BIT_MSG_LEN
        uint64_t total_msg_len;
    #else
        uint32_t total_msg_len;
    #endif
    #endif
    };
    

    The structure definition of SHA12_DIGEST_U is given below :

    typedef union {
      uint32_t w[SHA2_WLEN];
      uint8_t b[SHA2_BLEN];
    } SHA12_DIGEST_U; 
    
  • mode: indicates the mode of SHA. The permitted modes are SHA_MODE_256 and SHA_MODE_1.
Returns
  • SHA_RET_ERR_BAD_ADDR - if sha12_ctx2 points to NULL,
  • SHA_RET_ERR_UNSUPPORTED - if sha_mode has a value other than the permitted ones,
  • SHA_RET_OK - returned if the operation requested is successful.
Requires

Nothing.

Notes

None.

sha35_init

Prototype

uint8_t sha35_init(SHA35_CONTEXT *psha35_ctx, uint8_t mode);

Description

This routine initializes the SHA35_CONTEXT_T data structure for the mode specified.

Parameters
  • psha35_ctx: pointer to the SHA35_CONTEXT_T data structure. The structure definition is given below :
    typedef struct sha35_context_s SHA35_CONTEXT_T;
    struct sha35_context_s {
        SHA35_DIGEST_U hash;
        union {
            uint32_t w[(SHA35_BLOCK_WLEN) * 2];
            uint8_t  b[(SHA35_BLOCK_BLEN) * 2];
        } block;
        uint8_t mode;
        uint8_t block_len;
        uint8_t rsvd[2];
    #ifdef SHA_ENABLE_64BIT_MSG_LEN
        uint64_t total_msg_len[2];
    #else
        uint32_t total_msg_len;
    #endif
    } aligned(8);
    

    The structure definition of SHA35_DIGEST_U is given below :

    typedef union {
      uint32_t w[SHA5_WLEN];
      uint8_t b[SHA5_BLEN];
    } SHA35_DIGEST_U; 
    
  • mode: the permitted mode for this operation is SHA_MODE_512.
Returns
  • SHA_RET_ERR_BAD_ADDR - if sha35_ctx2 points to NULL,
  • SHA_RET_ERR_UNSUPPORTED - if sha_mode has a value other than the permitted ones,
  • SHA_RET_OK - returned if the operation requested is successful.
Requires

Nothing.

Notes

Valid only for CEC1702 MCU.

sha12_update

Prototype

uint8_t sha12_update(SHA12_CONTEXT_T *psha12_ctx, const uint32_t *pdata, uint32_t num_bytes);

Description

This routine runs Hash block on data and updates the SHA12_CONTEXT_T data structure with the number of bytes processed. The data must be aligned to a 4-byte boundary for SHA1 or SHA256.
If data length is not a multiple of 64-bytes, then the remaining bytes will be copied into SHA12_CONTEXT_T data structure to be processed by sha12_finalize.

Parameters
  • psha12_ctx: pointer to the SHA context structure. The structure definition is given below :
    typedef struct sha12_context_s SHA12_CONTEXT_T;
    struct sha12_context_s {
        SHA12_DIGEST_U hash;
        union {
            uint32_t w[(SHA12_BLOCK_WLEN) * 2];
            uint8_t b[(SHA12_BLOCK_BLEN) * 2];
        } block;
        uint8_t mode;
        uint8_t block_len;
        uint8_t rsvd[2];
    #ifdef SHA_ENABLE_64BIT_MSG_LEN
        uint64_t total_msg_len;
    #else
        uint32_t total_msg_len;
    #endif
    #endif
    };
    

    The structure definition of SHA12_DIGEST_U is given below :

    typedef union {
      uint32_t w[SHA2_WLEN];
      uint8_t b[SHA2_BLEN];
    } SHA12_DIGEST_U; 
    
  • pdata: pointer to constant data consisting of data to be updated.
  • num_bytes: length of data.
Returns
  • SHA_RET_ERR_BAD_ADDR - if either sha12_ctx3 or data1 points to NULL,
  • SHA_RET_ERR_UNSUPPORTED - if sha_mode has a value other than the permitted ones,
  • SHA_RET_OK - returned if the operation requested is successful.
Requires

Nothing.

Notes

None.

sha35_update

Prototype

uint8_t sha35_update(SHA35_CONTEXT *psha35_ctx, const uint32_t *pdata, uint32_t num_bytes);

Description

This routine runs Hash block on data and updates the SHA35_CONTEXT_T data structure with the number of bytes processed. The data must be aligned to a 4-byte boundary for SHA1 or SHA256.
If data length is not a multiple of 64-bytes, then the remaining bytes will be copied into SHA35_CONTEXT_T data structure to be processed by sha35_finalize.

Parameters
  • psha35_ctx: pointer to the SHA context structure. The structure definition is given below :
    typedef struct sha35_context_s SHA35_CONTEXT_T;
    struct sha35_context_s {
        SHA35_DIGEST_U hash;
        union {
            uint32_t w[(SHA35_BLOCK_WLEN) * 2];
            uint8_t  b[(SHA35_BLOCK_BLEN) * 2];
        } block;
        uint8_t mode;
        uint8_t block_len;
        uint8_t rsvd[2];
    #ifdef SHA_ENABLE_64BIT_MSG_LEN
        uint64_t total_msg_len[2];
    #else
        uint32_t total_msg_len;
    #endif
    } aligned(8);
    

    The structure definition of SHA35_DIGEST_U is given below :

    typedef union {
      uint32_t w[SHA5_WLEN];
      uint8_t b[SHA5_BLEN];
    } SHA35_DIGEST_U; 
    
  • pdata: pointer to constant data consisting of data to be updated.
  • num_bytes: length of data.
Returns
  • SHA_RET_ERR_BAD_ADDR - if either sha12_ctx3 or data1 points to NULL,
  • SHA_RET_ERR_UNSUPPORTED - if sha_mode has a value other than the permitted ones,
  • SHA_RET_OK - returned if the operation requested is successful.
Requires

Nothing.

Notes

Valid only for CEC1702 MCU.

sha12_finalize

Prototype

uint8_t sha12_finalize(SHA12_CONTEXT_T *psha12_ctx);

Description

This routine applies FIPS padding to SHA256 and performs final hash calculations. It must be used in sequence, sha256_init, sha256_update_start, wait for hash engine to finish, sha256_finalize.
The SHA256_CONTEXT_T object will be filled in with nay remaining bytes on the last call to sha256_update_start. SHA engine is approximately 1 cycle per byte for SHA1 and SHA256. It is 64 cycles per 64-byte block.

If the original message length has greater than 56 remaining bytes, the API will need to hash two additional blocks, one for the remaining 56 bytes and one to hold the message bit length.

Parameters
  • psha12_ctx pointer to the SHA context structure. The structure definition is given below :
    typedef struct sha12_context_s SHA12_CONTEXT_T;
    struct sha12_context_s {
        SHA12_DIGEST_U hash;
        union {
            uint32_t w[(SHA12_BLOCK_WLEN) * 2];
            uint8_t b[(SHA12_BLOCK_BLEN) * 2];
        } block;
        uint8_t mode;
        uint8_t block_len;
        uint8_t rsvd[2];
    #ifdef SHA_ENABLE_64BIT_MSG_LEN
        uint64_t total_msg_len;
    #else
        uint32_t total_msg_len;
    #endif
    #endif
    };
    

    The structure definition of SHA12_DIGEST_U is given below :

    typedef union {
      uint32_t w[SHA2_WLEN];
      uint8_t b[SHA2_BLEN];
    } SHA12_DIGEST_U; 
    
Returns
  • SHA_RET_ERR_BAD_ADDR - if sha12_ctx2 points to NULL,
  • SHA_RET_ERR_BUSY - this error value is returned if hash block is busy,
  • SHA_RET_START - returned if the operation requested is successful.
Requires

Nothing.

Notes

None.

sha35_finalize

Prototype

uint8_t sha35_finalize(SHA35_CONTEXT_T *psha35_ctx);

Description

Finalizes the Hash operations by running Hash engine if bytes are left over and adds FIPS padding.

Parameters
  • psha35_ctx pointer to the SHA context structure. The structure definition is given below :
    typedef struct sha35_context_s SHA35_CONTEXT_T;
    struct sha35_context_s {
        SHA35_DIGEST_U hash;
        union {
            uint32_t w[(SHA35_BLOCK_WLEN) * 2];
            uint8_t  b[(SHA35_BLOCK_BLEN) * 2];
        } block;
        uint8_t mode;
        uint8_t block_len;
        uint8_t rsvd[2];
    #ifdef SHA_ENABLE_64BIT_MSG_LEN
        uint64_t total_msg_len[2];
    #else
        uint32_t total_msg_len;
    #endif
    } aligned(8);
    

    The structure definition of SHA35_DIGEST_U is given below :

    typedef union {
      uint32_t w[SHA5_WLEN];
      uint8_t b[SHA5_BLEN];
    } SHA35_DIGEST_U; 
    
Returns
  • SHA_RET_ERR_BAD_ADDR - if sha35_ctx points to NULL,
  • SHA_RET_ERR_BUSY - this error value is returned if hash block is busy,
  • SHA_RET_START - returned if the operation requested is successful.
Requires

Nothing.

Notes

Valid only for CEC1702 MCU.

sha_init

Prototype // for CEC1302 MCU :

uint8_t sha_init(uint8_t mode, uint32_t *pdigest);

// for CEC1702 MCU :

uint8_t sha_init(uint8_t mode, uint16_t *pdigest);

Description

This routine initializes the hash engine for SHA operation. Programs supported SHA operation’s initial value, digest address and operation (SHA1, SHA256 or SHA384).
Hash engine does not need to be started. SHA1 and SHA256 require 4 byte alignment, while SHA384 require 8-byte alignment.

Parameters
  • mode: paramteter indicating the mode. Permitted modes are SHA_MODE_1 and SHA_MODE_256.
  • pdigest: pointer to digest.
Returns
  • 0 - Success.
  • 1 - Hash Engine Busy.
  • 2 - Unsupported SHA operation.
  • 3 - Bad digest pointer, NULL or mis-aligned.
Requires

Nothing.

Notes

None.

sha_update

Prototype

uint8_t sha_update(const uint32_t *pdata, uint16_t nblocks, uint8_t flags);

Description

This routine programs hash engine with data address and the number of data blocks to process. SHA block must be initialized before this routine is called.
SHA1 and SHA256 require 4 byte alignment while SHA384 require 8-byte alignment. Hash engine is not started, if return value is non-zero caller must call hash_start.

Parameters
  • pdata: pointer to data,
  • nblocks: number of blocks,
  • flags: Bit 0 indicates clear status(1-clear), bit enable interrupt status(1-enable) and bit indicates start/stop (1-start).
Returns
  • 0 - Success.
  • 1 - Hash Engine Busy.
  • 2 - pdata is a null pointer.
  • 3 - data is misaligned.
Requires

Nothing.

Notes

None.

sha_final

Prototype

uint8_t sha_final(uint32_t *padbuf, uint32_t total_msg_len, const uint8_t *prem, uint8_t flags);

Description

This routine implement the standard SHA padding described in the FIPS standard.

Parameters
  • padbuf: pointer to the message padded according to the rules of fips standard.
  • total_msg_len: length of message.
  • prem: to constant data consisting of …?
  • flags: Bit 0 indicates clear status(1-clear), bit enable interrupt status(1-enable) and bit indicates start/stop (1-start).
Returns
  • 0 - Success.
  • 1 - Hash Engine Busy.
  • 2 - pdata is a null pointer.
  • 3 - data is misaligned.
Requires

Nothing.

Notes

None.

Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code