0

after spending quite some time trying to understand the function of this method, I still couldn't figure what does it do. As I understand, stateAsBytes should contain hex strings like "\xA1\X32\X89\XB2", what does stateAsWords[i%5][i/5] |= (unsigned long )(stateAsBytes[i*(64/8)+j]) << (8*j) do? Why it uses bitwise assignment ?

void fromBytesToWords(unsigned long  **stateAsWords, unsigned char *stateAsBytes)
{
  for(int i=0; i<(1600/64); i++) {
    stateAsWords[i%5][i/5] = 0;
    for(int j=0; j<(64/8); j++)
      stateAsWords[i%5][i/5] |= (unsigned long )(stateAsBytes[i*(64/8)+j]) << (8*j);
  }
} 
Blake
  • 7,367
  • 19
  • 54
  • 80

1 Answers1

0

What it's doing is treating an array of 1600 bytes as an array of little endian 64-bit values and reorganizing them into the five stateAsWords arrays as native 64-bit words:

                 "index" of the 64 bit value 
                    from the stateAsBytes 
                         buffer

    stateAsWord[0]: 0, 5, 10, 15, 20
    stateAsWord[1]: 1, 6, 11, 16, 21
    stateAsWord[2]: 2, 7, 12, 17, 22
    stateAsWord[3]: 3, 8, 13, 18, 23
    stateAsWord[4]: 4, 9, 14, 19, 24

The bitwise assignment is just building up a native 64-bit word byte-by-byte from the little-endian buffer.

Note that the code you posted assumes that unsigned long is a 64-bit type so it has some portability issues - in particular it won't work on most 32-bit targets and it won't work on Windows (even 64-bit windows).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760