cryptoTools::SHA384 Class Reference

Implements the SHA384 hashfunction, i.e. sha-384. Inherits from SHAx. More...

#include <sha384.hpp>

+ Inheritance diagram for cryptoTools::SHA384:
+ Collaboration diagram for cryptoTools::SHA384:

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

uint64_t H [8]
 The internal state; 8 words of 64 bits.
std::vector< uint64_t > M
 Message to be hashed.
uint64_t l
 Length of the message (in bits).
64 bits temporary variables
uint64_t a
uint64_t b
uint64_t c
uint64_t d
uint64_t e
uint64_t f
uint64_t g
uint64_t h

Detailed Description

Implements the SHA384 hashfunction, i.e. sha-384. Inherits from SHAx.

Definition at line 24 of file sha384.hpp.

Member Function Documentation

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

Returns a vector of uint8_t containing the hash.

Reimplemented from cryptoTools::SHAx.

Definition at line 152 of file sha384.cpp.

{
std::vector<uint8_t> digest;
// this time, we take only the five first elements
for (unsigned int i=0; i<6; i++)
{
digest.push_back( (H[i]>>56) % 0x100);
digest.push_back( (H[i]>>48) % 0x100);
digest.push_back( (H[i]>>40) % 0x100);
digest.push_back( (H[i]>>32) % 0x100);
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 SHA384::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 177 of file sha384.cpp.

{
return 384;
}
std::string SHA384::getType ( )
virtual

To know which hashfunction it is.

Returns
The name of the hashfunction.

Reimplemented from cryptoTools::SHAx.

Definition at line 171 of file sha384.cpp.

{
return "SHA-384";
}
void SHA384::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 144 of file sha384.cpp.

{
preprocess(initialMessage);
for (unsigned int counter=0; counter<M.size(); counter+=16)
round(counter);
}
void SHA384::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 77 of file sha384.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 = ( (896-(l+8))%1024 >= 0) ? (896-(l+8))%1024 :1024+(960-(l+8))%1024;
k += 64; // Adding 64 bits to zero: we don't treat cases where l>2^64
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+=8)
M.push_back( ((uint64_t)initialMessage[i] << 56)
| ((uint64_t)initialMessage[i+1] << 48)
| ((uint64_t)initialMessage[i+2] << 40)
| ((uint64_t)initialMessage[i+3] << 32)
| ((uint64_t)initialMessage[i+4] << 24)
| ((uint64_t)initialMessage[i+5] << 16)
| ((uint64_t)initialMessage[i+6] << 8)
| (uint64_t)initialMessage[i+7] );
// initialising the hash values H_i
for (unsigned int i=0; i<8; i++)
H[i] = init384[i];
}
void SHA384::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 115 of file sha384.cpp.

{
uint64_t W[80], 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<80; 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) + K384[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

uint64_t cryptoTools::SHA384::a
private

Definition at line 37 of file sha384.hpp.

uint64_t cryptoTools::SHA384::b
private

Definition at line 38 of file sha384.hpp.

uint64_t cryptoTools::SHA384::c
private

Definition at line 39 of file sha384.hpp.

uint64_t cryptoTools::SHA384::d
private

Definition at line 40 of file sha384.hpp.

uint64_t cryptoTools::SHA384::e
private

Definition at line 41 of file sha384.hpp.

uint64_t cryptoTools::SHA384::f
private

Definition at line 42 of file sha384.hpp.

uint64_t cryptoTools::SHA384::g
private

Definition at line 43 of file sha384.hpp.

uint64_t cryptoTools::SHA384::H[8]
private

The internal state; 8 words of 64 bits.

Definition at line 30 of file sha384.hpp.

uint64_t cryptoTools::SHA384::h
private

Definition at line 44 of file sha384.hpp.

uint64_t cryptoTools::SHA384::l
private

Length of the message (in bits).

Definition at line 55 of file sha384.hpp.

std::vector<uint64_t> cryptoTools::SHA384::M
private

Message to be hashed.

Definition at line 50 of file sha384.hpp.


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