desref.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/usr/bin/env python3
  2. # Reference implementation of DES.
  3. #
  4. # As discussed in sshdes.c itself, this module implements DES in two
  5. # different ways. The class DES is close to the official spec, with
  6. # S-box contents you might recognise; the class SGTDES changes a lot
  7. # of the details but in a way that compensate for each other, so it
  8. # should end up overall functionally equivalent. But SGTDES's S-boxes
  9. # look like the ones in sshdes.c, so diagnostics from this code can be
  10. # used in the event that sshdes.c needs to be debugged.
  11. import sys
  12. import struct
  13. import functools
  14. import argparse
  15. assert sys.version_info[:2] >= (3,0), "This is Python 3 code"
  16. def bitor(x, y):
  17. return x | y
  18. def split_words(val, width=32):
  19. mask = ((1<<width)-1)
  20. return mask & (val >> width), mask & val
  21. def combine_words(hi, lo, width=32):
  22. mask = ((1<<width)-1)
  23. return ((mask & hi) << width) | (mask & lo)
  24. def ror(val, shift, width=32):
  25. mask = ((1<<width)-1)
  26. return mask & ((val >> (shift % width)) | (val << (-shift % width)))
  27. def rol(val, shift, width=32):
  28. return ror(val, -shift, width)
  29. def bitselect(bits, val):
  30. # bits[i] gives the input bit index of the output bit at index i
  31. return functools.reduce(
  32. bitor, ((1 & (val >> inbit)) << outbit
  33. for outbit, inbit in enumerate(bits)))
  34. def SB(hexstring):
  35. return [int(c,16) for c in hexstring]
  36. def debug(string):
  37. sys.stdout.write(string + "\n")
  38. class DESBase(object):
  39. def __init__(self):
  40. # Automatically construct FP by inverting IP
  41. self.FP = [None] * 64
  42. for i, j in enumerate(self.IP):
  43. self.FP[j] = i
  44. def f(self, word, key_material):
  45. debug("computing f({:08x}, {}):".format(
  46. word, " ".join(map("{:02x}".format,key_material))))
  47. sbox_inputs = [0x3F & (ror(word, offset) ^ key_element)
  48. for offset, key_element in
  49. zip(self.sbox_index_offsets, key_material)]
  50. sbox_outputs = [sbox[sbox_input] for sbox, sbox_input
  51. in zip(self.sboxes, sbox_inputs)]
  52. debug(" S-boxes: {} -> {}".format(
  53. " ".join(map("{:02x}".format,sbox_inputs)),
  54. " ".join(map("{:x}".format,sbox_outputs))))
  55. word = functools.reduce(
  56. bitor, (v << (4*i) for i,v in enumerate(sbox_outputs)))
  57. debug(" S output = {:08x}".format(word))
  58. word = bitselect(self.P, word)
  59. debug(" P output = {:08x}".format(word))
  60. return word
  61. def cipher(self, integer, key_schedule):
  62. L, R = split_words(bitselect(self.IP, integer))
  63. debug("cipher start {:016x} -> {:08x} {:08x}".format(integer, L, R))
  64. for roundIndex, key_material in enumerate(key_schedule):
  65. L, R = R, L ^ self.f(R, key_material)
  66. debug("after round {:2d}: {:08x} {:08x}".format(roundIndex, L, R))
  67. output = bitselect(self.FP, combine_words(R, L))
  68. debug("cipher end {:08x} {:08x} -> {:016x}".format(R, L, output))
  69. return output
  70. def encipher(self, integer):
  71. return self.cipher(integer, self.key_schedule)
  72. def decipher(self, integer):
  73. return self.cipher(integer, list(reversed(self.key_schedule)))
  74. def setkey(self, key):
  75. self.key_schedule = []
  76. CD = bitselect(self.PC1, key)
  77. debug("initial CD = {:014x}".format(CD))
  78. for roundIndex, shift in enumerate(self.key_setup_shifts):
  79. C, D = split_words(CD, 28)
  80. C = rol(C, shift, 28)
  81. D = rol(D, shift, 28)
  82. CD = combine_words(C, D, 28)
  83. self.key_schedule.append(
  84. [bitselect(bits, CD) for bits in self.PC2])
  85. debug("CD[{:d}] = {:014x} -> {}):".format(
  86. roundIndex, CD, " ".join(
  87. map("{:02x}".format,self.key_schedule[-1]))))
  88. # The PC1 permutation is fixed and arbitrary
  89. PC1 = [
  90. 0x3c, 0x34, 0x2c, 0x24, 0x3b, 0x33, 0x2b,
  91. 0x23, 0x1b, 0x13, 0x0b, 0x03, 0x3a, 0x32,
  92. 0x2a, 0x22, 0x1a, 0x12, 0x0a, 0x02, 0x39,
  93. 0x31, 0x29, 0x21, 0x19, 0x11, 0x09, 0x01,
  94. 0x1c, 0x14, 0x0c, 0x04, 0x3d, 0x35, 0x2d,
  95. 0x25, 0x1d, 0x15, 0x0d, 0x05, 0x3e, 0x36,
  96. 0x2e, 0x26, 0x1e, 0x16, 0x0e, 0x06, 0x3f,
  97. 0x37, 0x2f, 0x27, 0x1f, 0x17, 0x0f, 0x07,
  98. ]
  99. PC2 = [
  100. [0x18, 0x1b, 0x14, 0x06, 0x0e, 0x0a],
  101. [0x03, 0x16, 0x00, 0x11, 0x07, 0x0c],
  102. [0x08, 0x17, 0x0b, 0x05, 0x10, 0x1a],
  103. [0x01, 0x09, 0x13, 0x19, 0x04, 0x0f],
  104. [0x36, 0x2b, 0x24, 0x1d, 0x31, 0x28],
  105. [0x30, 0x1e, 0x34, 0x2c, 0x25, 0x21],
  106. [0x2e, 0x23, 0x32, 0x29, 0x1c, 0x35],
  107. [0x33, 0x37, 0x20, 0x2d, 0x27, 0x2a],
  108. ]
  109. key_setup_shifts = [1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1]
  110. # IP is better understood as a permutation and flipping of the
  111. # bits _in the index of each actual bit_ than as a long list of
  112. # individual indices
  113. IP = [bitselect([5,3,4,0,1,2], index ^ 0x27) for index in range(64)]
  114. class DES(DESBase):
  115. sboxes = [
  116. SB('d12f8d486af3b714ac9536eb500ec97272b14e1794cae82d0f6ca9d0f335568b'),
  117. SB('4db02be7f40981da3ec3957c52af6816164bbdd8c1347ae7a9f5608f0e52932c'),
  118. SB('ca1fa4f2972c698506d13d4ee07b53b894e3f25c2985cf3a7b0e41a716d0b86d'),
  119. SB('2ecb421c74a7bd6185503ffad309e8964b281cb7a1de728df69fc0596a3405e3'),
  120. SB('7dd8eb35066f90a31427825cb1ca4ef9a36f9006cab17dd8f91435eb5c27824e'),
  121. SB('ad0790e96334f65a12d8c57ebc4b2f81d16a4d9086f93807b41f2ec35ba5e27c'),
  122. SB('f31d84e76fb2384e9c7021dac6095ba50de87ab1a34fd4125b86c76c90352ef9'),
  123. SB('e04fd7142ef2bd813aa66ccb599503784f1ce882d46921b7f5cb937e3aa0560d'),
  124. ]
  125. P = [
  126. 0x07, 0x1c, 0x15, 0x0a, 0x1a, 0x02, 0x13, 0x0d,
  127. 0x17, 0x1d, 0x05, 0x00, 0x12, 0x08, 0x18, 0x1e,
  128. 0x16, 0x01, 0x0e, 0x1b, 0x06, 0x09, 0x11, 0x1f,
  129. 0x0f, 0x04, 0x14, 0x03, 0x0b, 0x0c, 0x19, 0x10,
  130. ]
  131. sbox_index_offsets = [4*i-1 for i in range(8)]
  132. class SGTDES(DESBase):
  133. sboxes = [
  134. SB('e41f8e2839f5d7429ac653bd600bac7171d42b47c2a9b81e0f3a9ce0f556638d'),
  135. SB('4db02be7f40981da3ec3957c52af6816164bbdd8c1347ae7a9f5608f0e52932c'),
  136. SB('c52f58f16b1c964a09e23e8dd0b7a37468d3f1ac164acf35b70d825b29e0749e'),
  137. SB('4ead241a72c7db6183305ffcb509e8962d481ad7c1be748bf69fa0396c5203e5'),
  138. SB('edd1b76c0aaf5036482e12c974938bf536af500a9374edd1f5486cb7c92e128b'),
  139. SB('9e07a0da5334f56921e8c67dbc4b1f82e2594ea085fa3807b42f1dc36b96d17c'),
  140. SB('f31d84e76fb2384e9c7021dac6095ba50de87ab1a34fd4125b86c76c90352ef9'),
  141. SB('d08feb281df17e4235599cc7a66a03b48f2cd441e896127bfac763bd3550a90e'),
  142. ]
  143. P = [
  144. 0x1d, 0x14, 0x0b, 0x1a, 0x01, 0x10, 0x0e, 0x17,
  145. 0x1c, 0x05, 0x02, 0x13, 0x09, 0x18, 0x1f, 0x16,
  146. 0x00, 0x0d, 0x1b, 0x06, 0x08, 0x11, 0x1e, 0x0f,
  147. 0x04, 0x15, 0x03, 0x0a, 0x0c, 0x19, 0x12, 0x07
  148. ]
  149. sbox_index_offsets = [4*i-2 for i in range(8)]
  150. IP = [DES.IP[i ^ ((i^(i+1)) & 0x1F)] for i in range(64)]
  151. def main():
  152. hexstr = lambda s: int(s, 16)
  153. parser = argparse.ArgumentParser(description='')
  154. group = parser.add_mutually_exclusive_group()
  155. group.add_argument("--des", action="store_const", dest="cipher", const=DES,
  156. help="Use the official DES definition.")
  157. group.add_argument("--sgtdes", action="store_const", dest="cipher",
  158. const=SGTDES, help="Use the equivalent SGT-DES.")
  159. group = parser.add_mutually_exclusive_group(required=True)
  160. group.add_argument("--encipher", "-e", action="store_const", dest="method",
  161. const="encipher", help="Encipher.")
  162. group.add_argument("--decipher", "-d", action="store_const", dest="method",
  163. const="decipher", help="Decipher.")
  164. parser.add_argument("key", type=hexstr, help="Cipher key (hex, 8 bytes, "
  165. "low bit of each byte unused).")
  166. parser.add_argument("input", type=hexstr,
  167. help="Cipher input (hex, 8 bytes).")
  168. parser.set_defaults(const=SGTDES) # main purpose is to debug sshdes.c
  169. args = parser.parse_args()
  170. des = args.cipher()
  171. des.setkey(args.key)
  172. method = getattr(des, args.method)
  173. output = method(args.input)
  174. sys.stdout.write("{} with key {:016x}: {:016x} -> {:016x}\n".format(
  175. args.method, args.key, args.input, output))
  176. if __name__ == '__main__':
  177. main()