Permalink
Browse files

Completed the block based interface

  • Loading branch information...
1 parent 466b0d1 commit 2a7fefa81f408bcf4185e8228341d2299ed3b4c5 @henkmuller henkmuller committed Sep 3, 2011
Showing with 76 additions and 9 deletions.
  1. +74 −5 doc/index.rst
  2. +2 −4 module_SHA2/src/sha2.xc
View
@@ -1,5 +1,14 @@
sha 256 user guide
-------------------
+''''''''''''''''''
+
+There are two interfaces to SHA256 - process based and function based. The
+process based interface uses separate thread to perform the hashing. The
+function based interface uses the calling thread for the computations. The
+former interface is slightly faster. Both interfaces can be sped up further
+by judicious use of word vs byte manipulation instructions.
+
+Process based
+-------------
The current SHA256 implementation consists of two parts: a process that
@@ -13,10 +22,8 @@ function ``sha256End()`` to obtain the hash, and the
function ``sha256Terminate()`` to stop the hashing process when you are
done computing hashes.
-The rationale for doing the cmoputations in a separate thread is that it
-keeps the state in that thread, slightly speeding up computation. It is
-perfectly feasible to make a different interface that does not need a
-separate thread.
+The rationale for doing the computations in a separate thread is that it
+keeps the state in that thread, slightly speeding up computation.
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
@@ -93,3 +100,65 @@ An example program is shown below::
}
return 0;
}
+
+
+Function based
+--------------
+
+
+Call the function ``sha256BlockBegin()`` to start computing a new hash, the
+function ``sha256BlockUpdate()`` to incorporate some more data into the
+hash, and the
+function ``sha256BlockEnd()`` to obtain the hash.
+
+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.
+
+
+sha256BlockBegin
+================
+
+Call this function with a single argument, the streaming channel::
+
+ sha256BlockBegin(unsigned int hash[8])
+
+
+
+sha256BlockUpdate
+=================
+
+Call this function with a three arguments, the streaming channel, an array
+of bytes (unsigned characters), and an integer denoting the total number of
+arguments::
+
+ sha256BlockUpdate(unsigned int hash[8], unsigned char data[], int n)
+
+
+sha256BlockEnd
+==============
+
+Call this function with a two arguments, the streaming channel, and an
+array of 8 unsigned integers in which the hash will be stored::
+
+ sha256BlockEnd(unsigned int hash[8])
+
+
+
+
+Example
+=======
+
+An example program is shown below::
+
+ int main(void) {
+ unsigned int hash[8];
+ sha256BlockStart(hash);
+ sha256BlockUpdate(hash, "Hello", 5);
+ sha256BlockEnd(hash);
+ return 0;
+ }
View
@@ -122,21 +122,19 @@ void sha256BlockBegin(unsigned int hash[8]) {
#pragma unsafe arrays
void sha256BlockUpdate(unsigned int hash[8], unsigned char bytes[], int n) {
for(int i = 0; i < n; i++) {
- (blockData, unsigned char[64])[blockCount++] = bytes[i];
+ (blockData, unsigned char[64])[(blockCount++)&63] = bytes[i];
if ((blockCount&63) == 0) {
sha256Block(hash, blockData);
- blockCount = 0;
}
}
}
#pragma unsafe arrays
void sha256BlockEnd(unsigned int hash[8]) {
int len = blockCount * 8;
- (blockData, unsigned char[64])[blockCount++] = 0x80;
+ (blockData, unsigned char[64])[(blockCount++)&63] = 0x80;
if ((blockCount&63) == 0) {
sha256Block(hash, blockData);
- blockCount = 0;
}
blockCount &= 63;
if (blockCount > 56) {

0 comments on commit 2a7fefa

Please sign in to comment.