The thread-based SHA256 implementation consists of two parts: a process that computes the sha256 hash, and four function calls that can be used to supply data to the hashing process.
In order to use this, call the sha256Process() function from within a par, then in another thread call the function sha256Begin() to start computing a new hash, the function sha256Update() to incorporate some more data into the hash, the function sha256End() to obtain the hash, and the function sha256Terminate() to stop the hashing process when you are done computing hashes; this terminates sha256Process()
The rationale for doing the computations in a separate thread is that it keeps the state in that thread, speeding up computation by around 10%.
Note that the current interface can only perform the computation on a byte stream. This can be changed to a bit stream, the hashing thread itself works on a bit stream already, it just requires a different set of functions.
The two threads communicate by means of a streaming channel; optimised for computation on a single core.
This function is the SHA256 encryption server.
When ready to encrypt data it transmit a single control token over the channel end, and it then reads bytes over the channel. It assumes that the number of bytes supplied is a multiple of 64, and that the trailing ‘1’ bit and the length are encoded as defined by the SHA256 standard. At the end of the packet, a single control token must be sent over the channel, whereupon the hash is supplied back over the channel end, terminated by an end-of-message.
To make it easier to use this process, four auxiliary functions are supplied that handle the trailing bit, the length, and the channel communication.
Parameters: |
|
---|
This is one of the four functions that can be used with sha256Process().
It must be called to initialise the hashing process.
Parameters: |
|
---|
This is one of the four functions that can be used with sha256Process().
It is used to incorporate a block of data into the hash. This function must be called after the hash has been initialised with sha256Begin(). It can be called multiple times, to add multiple blocks of data. Blocks can be an arbitrary number of bytes long.
Parameters: |
|
---|
This is one of the four functions that can be used with sha256Process().
It adds the length into the hash, and retrieves the hash.
Parameters: |
|
---|
This is one of the four functions that can be used with sha256Process().
It requests the hash thread to terminate, and should only be called after sha256End() has been called. After this call, sha256Process will return.
Parameters: |
|
---|
An example program is shown below:
void comp(streaming chanend c) {
unsigned int hash[8];
sha256Begin(c);
sha256Update(c, "Hello", 5);
sha256End(c, hash);
sha256Terminate(c);
}
int main(void) {
streaming chan c;
par {
sha256Process(c);
comp(c);
}
return 0;
}