cryptoTools::SHA256 Class Reference

Implements the SHA256 hashfunction, i.e. sha-256. Inherits from SHAx. More...

#include <sha256.hpp>

+ Inheritance diagram for cryptoTools::SHA256:
+ Collaboration diagram for cryptoTools::SHA256:

Public Member Functions

void preprocess (std::vector< uint8_t > initialMessage)
 Parses a vector of bytes and applies to it the transformations specified in the NIST's specification.
void round (unsigned int counter)
 Performs a round over the 16 message bytes contained in M, starting at counter and finishing at counter+"something". In the end, the internal state H is updated to the new value.
void hash (std::vector< uint8_t > initialMessage)
 Performs all the steps necessary to hash the message given on the input. In the end, the internal state H contains the digest of the message.
std::vector< uint8_t > getHash ()
 Returns a vector of uint8_t containing the hash.
std::string getType ()
 To know which hashfunction it is.
uint32_t getHashLength ()
 To know the bitlength of the hash we should expect.

Private Attributes

uint32_t H [8]
 The internal state; 8 words of 32 bits. It is modified during each call to the round method.
std::vector< uint32_t > M
 The message to hash.
uint64_t l
 The length of the message in bits.
32 bits temporary variables
uint32_t a
uint32_t b
uint32_t c
uint32_t d
uint32_t e
uint32_t f
uint32_t g
uint32_t h

Detailed Description

Implements the SHA256 hashfunction, i.e. sha-256. Inherits from SHAx.

Definition at line 25 of file sha256.hpp.

Member Function Documentation

std::vector< uint8_t > SHA256::getHash ( )
virtual

Returns a vector of uint8_t containing the hash.

Reimplemented from cryptoTools::SHAx.

Definition at line 136 of file sha256.cpp.

{
std::vector<uint8_t> digest;
for (unsigned int i=0; i<8; i++)
{
digest.push_back( (H[i]>>24) % 0x100);
digest.push_back( (H[i]>>16) % 0x100);
digest.push_back( (H[i]>>8) % 0x100);
digest.push_back( H[i] % 0x100);
}
return digest;
}
uint32_t SHA256::getHashLength ( )
virtual

To know the bitlength of the hash we should expect.

Returns
The length of the hash, in bits.

Reimplemented from cryptoTools::SHAx.

Definition at line 156 of file sha256.cpp.

{
return 256;
}
std::string SHA256::getType ( )
virtual

To know which hashfunction it is.

Returns
The name of the hashfunction.

Reimplemented from cryptoTools::SHAx.

Definition at line 150 of file sha256.cpp.

{
return "SHA-256";
}
void SHA256::hash ( std::vector< uint8_t >  initialMessage)
virtual

Performs all the steps necessary to hash the message given on the input. In the end, the internal state H contains the digest of the message.

Calls preprocess over its input and then uses a loop over the length of the message to call round on each of its blocks.

Parameters
initialMessageA byte vector containing the message to hash.

Reimplemented from cryptoTools::SHAx.

Definition at line 128 of file sha256.cpp.

{
preprocess(initialMessage);
for (unsigned int counter=0; counter<M.size(); counter+=16)
round(counter);
}
void SHA256::preprocess ( std::vector< uint8_t >  initialMessage)
virtual

Parses a vector of bytes and applies to it the transformations specified in the NIST's specification.

Once the call to this function is finished, the M attribute is correctly assigned i.e. it contains 32 bits words, the padding and the length of the initial input. The internal state is also initialised.

Parameters
initialMessageA byte vector containing the message to hash.

Reimplemented from cryptoTools::SHAx.

Definition at line 66 of file sha256.cpp.

{
// padding the initial message by...
l = initialMessage.size()*8;
// ... adding 10000000
initialMessage.push_back(0x80);
// ... adding the correct amount of zeroes
unsigned int k = ( (448-(l+8))%512 >= 0) ? (448-(l+8))%512 : 512+(448-(l+8))%512;
for (unsigned int i=0; i<k; i+=8)
initialMessage.push_back(0x00);
// ... and appending the length of the input.
initialMessage.push_back((l>>56)%0x100);
initialMessage.push_back((l>>48)%0x100);
initialMessage.push_back((l>>40)%0x100);
initialMessage.push_back((l>>32)%0x100);
initialMessage.push_back((l>>24)%0x100);
initialMessage.push_back((l>>16)%0x100);
initialMessage.push_back((l>>8 )%0x100);
initialMessage.push_back(l%0x100);
// Turning the initial byte message into a 32words one
M.clear();
for (unsigned int i=0; i<initialMessage.size() ; i+=4)
M.push_back( (initialMessage[i] << 24)
| (initialMessage[i+1] << 16)
| (initialMessage[i+2] << 8)
| initialMessage[i+3] );
// initialising the hash values H_i
for (unsigned int i=0; i<8; i++)
H[i] = init256[i];
}
void SHA256::round ( unsigned int  counter)
virtual

Performs a round over the 16 message bytes contained in M, starting at counter and finishing at counter+"something". In the end, the internal state H is updated to the new value.

Reimplemented from cryptoTools::SHAx.

Definition at line 99 of file sha256.cpp.

{
uint32_t W[64], T1, T2;
// initializing the working variables
a = H[0]; b = H[1];
c = H[2]; d = H[3];
e = H[4]; f = H[5];
g = H[6]; h = H[7];
// loop over the currently processed hash
for (unsigned int t=0; t<64; t++)
{
// preparing the message schedule
W[t] = (t<=15) ? M[counter+t] : SMALLSIGMA1(W[t-2]) + W[t-7] + SMALLSIGMA0(W[t-15]) + W[t-16];
// Re-assigning the temporary variables
T1 = h + BIGSIGMA1(e) + CH(e,f,g) + K256[t] + W[t];
T2 = BIGSIGMA0(a) + MAJ(a,b,c) ;
h = g; g = f; f = e;
e = d + T1;
d = c; c = b; b = a;
a = T1 + T2;
}
// computing the ith hash value H[8]
H[0] += a; H[1] += b;
H[2] += c; H[3] += d;
H[4] += e; H[5] += f;
H[6] += g; H[7] += h;
}

Member Data Documentation

uint32_t cryptoTools::SHA256::a
private

Definition at line 38 of file sha256.hpp.

uint32_t cryptoTools::SHA256::b
private

Definition at line 39 of file sha256.hpp.

uint32_t cryptoTools::SHA256::c
private

Definition at line 40 of file sha256.hpp.

uint32_t cryptoTools::SHA256::d
private

Definition at line 41 of file sha256.hpp.

uint32_t cryptoTools::SHA256::e
private

Definition at line 42 of file sha256.hpp.

uint32_t cryptoTools::SHA256::f
private

Definition at line 43 of file sha256.hpp.

uint32_t cryptoTools::SHA256::g
private

Definition at line 44 of file sha256.hpp.

uint32_t cryptoTools::SHA256::H[8]
private

The internal state; 8 words of 32 bits. It is modified during each call to the round method.

Definition at line 32 of file sha256.hpp.

uint32_t cryptoTools::SHA256::h
private

Definition at line 45 of file sha256.hpp.

uint64_t cryptoTools::SHA256::l
private

The length of the message in bits.

Definition at line 56 of file sha256.hpp.

std::vector<uint32_t> cryptoTools::SHA256::M
private

The message to hash.

Definition at line 51 of file sha256.hpp.


The documentation for this class was generated from the following files: