2
$\begingroup$

What happens if we allow implementation of SHA-2 using negative values?

What happens if a negative value was used in the first round? Would the algorithm still result in a secure value?

I have a basic understanding of how the maths work. However, before I go writing my own function - could somebody indicate if they have any experience using negative values?

$\endgroup$
  • 3
    $\begingroup$ It's unclear what your asking. Do you mean what happens if the input is a negative value? Or if the constants in the compression function are negative? Negative input value is still just a sequence of bits, would be handled just fine. Changing the compression function would mean it's not SHA256 anymore. $\endgroup$ – puzzlepalace Jul 14 '17 at 22:52
  • $\begingroup$ @puzzleplace I'm not concerned with it being pure, functional SHA256, I'm trying to find ways to make it do unexpected things. So, what I was trying to ask was, when the input of any value in any round is switched to binary, what happens if it's a negative value? As far as I can tell, everything is always positive...maybe an easier way to ask would be...if all the IVs were negative at the start of the first round, does it change anything? What? How? $\endgroup$ – That Guy Jul 14 '17 at 22:57
  • $\begingroup$ Well SHA256 operates on binary input, for how to convert a negative number to binary take a look at two's complement, it's a common way used to represent negative numbers. The internals of SHA256 have no notion of "positive" or "negative", they're operating on purely binary data. The IV is also binary data, it can be convenient to represent them as 32 bit unsigned integers but they again have no real notion of "positive" or "negative". $\endgroup$ – puzzlepalace Jul 14 '17 at 23:06
  • $\begingroup$ @puzzleplace So, if a value is negative, when it gets converted to binary, it will have the same value as it would if it had been positive to start? $\endgroup$ – That Guy Jul 14 '17 at 23:09
  • $\begingroup$ (I may have inadvertently asked a question I am not smart enough to understand the answer too...) $\endgroup$ – That Guy Jul 14 '17 at 23:15
5
$\begingroup$

The 32-bit values in SHA-256 are unsigned integers, which means the only values they are allowed to have are between 0 and 4,294,967,295, there are no negative values allowed.

If you look at the 4th initial value (0xa54ff53a), that has an integer value of 2,773,480,762. Implementing SHA-256 in programming languages that do not have the a 32-bit unsigned integer, but rather a signed integer, results in that value being interpreted as -1,521,486,534, but the bits of the value are still the same.

For modular addition that is not an issue, (see two's complement), but can be an issue for the shifting and rotations, which need to be performed across all 32-bits, and not just the lower 31. XOR also does not care if the numbers are signed are not, and since SHA-2 only uses addition, XOR, and shifting (and thus rotation), implementing SHA-256 in a programming language that only supports unsigned integers is possible, just maybe a little slower if you have to deal with rotation through shifting of a signed integer, like in Visual Basic 6.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I program in VB6 as a hobby, and my SHA-256 implementation is the fastest one that exists as far as I am aware $\endgroup$ – Richie Frame Jul 15 '17 at 1:21
  • $\begingroup$ VB6 is My favorite language. It was the first one I learned, and as far as I know is still the best option for writing something you need to work on multiple versions of M$ Windows, without having to figure out a bunch of .NET nonsense. $\endgroup$ – That Guy Jul 15 '17 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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