12.3.3.2 SHA-256 API
The SHA-256 API can be used to hash variable lengths of input data.
The API is composed of three different functions which must be called in a
specific order:
- SHA-256 Init to initiate a SHA256_CTX structure
- SHA-256 Update to add a message to be computed in the digest
- SHA-256 Final to compute the digest
Note: SHA-256 Update can be called several times in the case several messages are to be
included in the digest computation.
The SHA-256 structure to define is called SHA56_CTX:
/*
*Context structure used by the crya_sha256_init, crya_sha256_update andcrya_sha256_final functions.
*/
typedef struct {
/* Digest result of SHA256 */
uint32_t digest[8];
/* Length of the message */
uint64_t length;
/* Holds the size of the remaining part of data */
uint32_t remain_size;
/* Buffer of remaining part of data (512 bits data block) */
uint8_t remain_ram[64];
/* RAM buffer of 256 bytes used by crya_sha_process */
uint32_t process_buf[64];
} SHA256_CTX;
The SHA-256 Init function entry point is located at the Boot ROM address 0x02006810:
/**
*\brief Type definition for SHA256 initialization function.
*
*\param context[In/Out]: A pointer to a SHA256_CTX structure.
*/
typedef void (*crya_sha256_init_t) (SHA256_CTX *context);
#define crya_sha_init ((crya_sha256_init_t) (0x02006810 | 0x1))
The SHA-256 Update function entry point is located at the Boot ROM address 0x02006814:
/**
*\brief Type definition for SHA256 update function.
*
*\param context[In/Out]: A pointer to a SHA256_CTX structure.
*\param data[In]:A pointer to a byte-aligned message
*\param length[In]:Size in bytes of the message
*/
typedef void (*crya_sha256_update_t) (SHA256_CTX *context, const unsigned char *data, size_t length);
#define crya_sha_update ((crya_sha256_update_t) (0x02006814 | 0x1))
The SHA-256 Final function entry point is located at the Boot ROM address 0x02006818:
/**
*\brief Type definition for SHA256 final function.
*
*\param context[In/Out]: A pointer to a SHA256_CTX structure.
*\param output[Out]:A pointer to the final SHA256 result.
*/
typedef void (*crya_sha256_final_t) (SHA256_CTX *context, unsigned char output[32]);
#define crya_sha256_final ((crya_sha256_final_t) (0x02006818 | 0x1))
Code example of using CRYA SHA-256 API:
void sha256_hash(SHA256_CTX *ctx, const uint8_t *message, uint32_t length, unsigned char digest[32]) { crya_sha256_init(ctx); crya_sha256_update(ctx, message, length); crya_sha256_final(ctx, digest); }
Note: crya_sha_update can be called several times in the case several messages are to be
included in the digest computation.