Difference between revisions of "Kernel/XcDESKeyParity"

From xboxdevwiki
Jump to: navigation, search
(Port from my Python code, but with added comments)
 
Line 1: Line 1:
 +
The ''XcDESKeyParity'' function turns a key into a DES key which requires odd-parity per byte.
 +
 
=== Pseudocode ===
 
=== Pseudocode ===
 +
 +
{{FIXME|reason=This should be removed from here, and instead we should just link to an actual implementation of the algorithm}}
  
 
<pre>
 
<pre>

Revision as of 23:58, 2 October 2018

The XcDESKeyParity function turns a key into a DES key which requires odd-parity per byte.

Pseudocode

[FIXME]

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;
}