1
$\begingroup$

I'm implementing SHA-256 in PHP using strings. Where do you truncate in the algorithm?

There are a lot of places where there's binary addition happening so the number of bits in $W(i)$ keep increasing. Same case with the compression function. So, where do I truncate the values?

When I var_dump my array I get this:

array(64) { 
[ 0]=> string(32) "01100001001000000111010001100101"
[ 1]=> string(32) "01110011011101001000000000000000" 
[ 2]=> string(32) "00000000000000000000000000000000" 
[ 3]=> string(32) "00000000000000000000000000000000" 
[ 4]=> string(32) "00000000000000000000000000000000" 
[ 5]=> string(32) "00000000000000000000000000000000" 
[ 6]=> string(32) "00000000000000000000000000000000" 
[ 7]=> string(32) "00000000000000000000000000000000" 
[ 8]=> string(32) "00000000000000000000000000000000" 
[ 9]=> string(32) "00000000000000000000000000000000" 
[10]=> string(32) "00000000000000000000000000000000" 
[11]=> string(32) "00000000000000000000000000000000" 
[12]=> string(32) "00000000000000000000000000000000" 
[13]=> string(32) "00000000000000000000000000000000" 
[14]=> string(32) "00000000000000000000000000000000" 
[15]=> string(32) "00000000000000000000000000110000" 
[16]=> string(32) "10001111101010001101101001000010" 
[17]=> string(32) "01110011100100101000000000000000" 
[18]=> string(32) "01110110010010101111110000010111" 
[19]=> string(32) "00010000000111001101001100011011" 
[20]=> string(32) "00100001100101001100011101010011" 
[21]=> string(32) "11110011111010101110110100111001" 
[22]=> string(32) "11111011010010111001000111111001" 
[23]=> string(32) "10111010101100000001011101110101" 
[24]=> string(33) "100101110100011111001000000101000" 
[25]=> string(32) "01111111110001011100001000100010" 
[26]=> string(33) "010000100100001011110010010001101" 
[27]=> string(32) "01111010110111110100100010111101" 
[28]=> string(33) "110010001011110100010000011100100" 
[29]=> string(33) "101001000101000110011011111011111" 
[30]=> string(33) "111000011011101011011101110010101" 
[31]=> string(34) "1111001011100001001101111011110110" 
[32]=> string(34) "1010010011000001000100101100000010" 
[33]=> string(35) "10101101001100111001111100111111010" 
[34]=> string(34) "1110100101001100001000011110000110" 
[35]=> string(36) "100101010100100001101110101111000011" 
[36]=> string(34) "1110001101001101001101110111011000" 
[37]=> string(36) "110101001100000100001110011000000101" 
[38]=> string(36) "100110010110100011010111011101100110" 
[39]=> string(36) "101110011100110000110001010010110000" 
[40]=> string(36) "100000101011010110001001000100011101" 
[41]=> string(37) "1001110001010110110001110110101011101" 
[42]=> string(37) "1000000101001011010111010000001100100" 
[43]=> string(37) "1001000011101111000001111110010100100" 
[44]=> string(38) "10010000101101110001100100010100010101" 
[45]=> string(37) "1011111001101101011111001000110010000" 
[46]=> string(39) "100101011110011011000000001001110010001" 
[47]=> string(37) "1001101011101110111000000100111010000" 
[48]=> string(39) "010001011100001101101000011010011100010" 
[49]=> string(38) "10011100010110000011111110010101101000" 
[50]=> string(39) "110011100111000010101100100101110011001" 
[51]=> string(38) "11001101111101010001000010110001100100" 
[52]=> string(40) "1001010000101100000100110100110000100011" 
[53]=> string(40) "1000010101011111001110000100101100111000" 
[54]=> string(40) "1111011111011011100000000001110000011101" 
[55]=> string(40) "0101110001001001110010001011110111010010" 
[56]=> string(40) "0100111101101110101111101011110000000011" 
[57]=> string(40) "1100101000101110000000101100000111111110" 
[58]=> string(40) "1101100110111011011010010001101100000100" 
[59]=> string(41) "10000100101011000111001100110101010010001" 
[60]=> string(41) "10111001010111100010000110001111101100110" 
[61]=> string(42) "110001101011011010100110111110101100101111" 
[62]=> string(42) "101001011000100100011001011101100100101111" 
[63]=> string(42) "101110011001100001001100001010110101001111" 
} 

Do I truncate the values before pushing them to the array or after the array is done with? If I truncate before, corresponding iterations will take up truncated values and that changes their values.

Also

$tmp=$q4;
$tr1=substr($tmp,-32);

$tmp2=binary_add($s0,$maj);
    $tr2=substr($tmp2,-32);

$H=$G;
$G=$F;
$F=$E;
$E=binary_add($tr1,$D);
    $D=$C;
    $C=$B;
    $B=$A;
    $A=binary_add($tr1,$tr2);

here too notice that I have truncated before adding the temp variables (and updating the var A to H) is this correct? Please let me know if I need to upload the whole code.

I am implementing this because this is my assignment.I have been getting different values and I am pretty much stuck on this step. I get 66 bits at the end of the script execution. It should actually be 64. And the value is incorrect too. I have tried reading algorithm in many sites and PDFs but no where is there a mention of truncating the bits. How is one supposed to get 64 bits if $H_0$ to $H_7$ values are more the 32 bits?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

All the arithmetic in SHA-256 should be 32-bit, it can be implemented at a low level using only 32-bit registers. If you use a representation that can go over 32 bits, then you need to truncate back to 32 bits on every calculation in your chosen language where it will not do so itself - i.e. on every addition.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.