Difference between revisions of "Kernel/XcDESKeyParity"

From xboxdevwiki
Jump to: navigation, search
(Port from my Python code, but with added comments)
(No difference)

Revision as of 23:56, 2 October 2018

Pseudocode

XBAPI VOID NTAPI XcDESKeyParity(IN OUT PUCHAR pbKey, IN ULONG dwKeyLength) {

  // For each number between 0x0 and 0xF, this tells how many set bits there are.
  const uint8_t parity_table[] = {
    0x00, 0x01, 0x01, 0x02, 0x01, 0x02, 0x02, 0x03,
    0x01, 0x02, 0x02, 0x03, 0x02, 0x03, 0x03, 0x04
  };

  // Count number of set bits in each byte, and flip the lowest bit if there's an even number.
  // This forces each byte to have odd parity, as required by DES.
  for(ULONG i = 0; i < dwKeyLength; i++) {
    high_parity = parity_table[(pbKey[i] >> 4) & 0xF];
    low_parity = parity_table[(pbKey[i] >> 0) & 0xF];
    if ((high_parity + low_parity) % 2) == 0 {
      pbKey[i] ^= 0x01;
    }
  }

  return;
}