vendredi 27 mars 2015

Implementing SHA-256 in C++


I am trying to implement SHA-256 in MSVC++. I am nearly there, except for extending the first 16 words of the message schedule array into the remaining 48. I have identified my problem to be at this point because it exactly matches the example from nist.gov until round 17 of the compression algorithm. Pseudocode for the algorithm can be found in wikipedia. My code for the message schedule is as follows:



//Extend first 16 words into the remaining 48 words of the message schedule array:
for (int k = 16; k < 64; k++)
{
bitset<32> s0 = rotr(W[k - 15], 7) ^= rotr(W[k - 15], 18) ^= (W[k - 15] >> 3);
bitset<32> s1 = rotr(W[k - 2], 17) ^= rotr(W[k - 2], 19) ^= (W[k - 2] >> 10);
W[k] = add(add(W[k - 16], s0), add(W[i - 7], s1));
}

bitset<32> add(bitset<32> a, bitset<32> b)
{
unsigned long c = a.to_ulong();
unsigned long d = b.to_ulong();
return bitset<32>((c + d) % 4294967296);
}

bitset<32> rotr(bitset<32> b, int num)
{
int temp = (int)b.to_ulong();
temp = _rotr(temp, num);
return bitset<32> (temp);
}


Where W[0..15] is a copy of the padded message (which matches the example). Does anyone see a problem? I can post more code if needed.




Aucun commentaire:

Enregistrer un commentaire