Advertisement
Guest User

Untitled

a guest
May 17th, 2015
1,428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.43 KB | None | 0 0
  1. import struct, binascii, random
  2.  
  3. import psyco; psyco.full()
  4.  
  5. alpha = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"
  6.  
  7. table = []
  8. for c in binascii.a2b_hex("00010102010202030102020302030304010202030203030402030304030404050102020302030304020303040304040502030304030404050304040504050506010202030203030402030304030404050203030403040405030404050405050602030304030404050304040504050506030404050405050604050506050606070102020302030304020303040304040502030304030404050304040504050506020303040304040503040405040505060304040504050506040505060506060702030304030404050304040504050506030404050405050604050506050606070304040504050506040505060506060704050506050606070506060706070708"):
  9.     table.append(ord(c))
  10.  
  11. shuftable = (0, 1, 3, 2, 6, 7, 5, 4, 12, 13, 15, 14, 10, 11, 9, 8)
  12. bitcount = (0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4)
  13.  
  14.  
  15. def flip(v):
  16.     return struct.unpack("<I", struct.pack(">I", v))[0]
  17.  
  18. def key_to_dwords(key):
  19.     n = 0
  20.     for c in key:
  21.         v = alpha.find(c)
  22.         if v == -1:
  23.             continue
  24.         n = (n << 5) | v
  25.        
  26.     last = n & 0x7fff
  27.     n = n >> 15
  28.     words = []
  29.     for i in xrange(5):
  30.         v = flip(n & 0xffffffff)
  31.         words.append(v)
  32.         n = n >> 32
  33.        
  34.     words = words[::-1]
  35.     last = flip((last & 0x7f) | ((last << 1) & 0xff00)) >> 16
  36.     words.append(last)
  37.    
  38.     return words
  39.    
  40. def dwords_to_key(words):
  41.     n = 0
  42.     for i in xrange(5):
  43.         n = (n << 32) | flip(words[i])
  44.        
  45.     last = flip(words[5] << 16)
  46.     last = ((last >> 1) & 0x7f80) | (last & 0x7f)
  47.     n = (n << 15) | last
  48.  
  49.     s = ""
  50.     for i in xrange(35):
  51.         s += alpha[n & 0x1f]
  52.         n = n >> 5
  53.         if i % 5 == 4:
  54.             s += "-"
  55.        
  56.     key = s[::-1][1:]
  57.     return key
  58.  
  59. def make_checksum(words):
  60.     s = ""
  61.     for w in words:
  62.         s += struct.pack("<I", w)
  63.    
  64.     bytea = 0;
  65.     byteb = 0;
  66.     for i in xrange(len(s)):
  67.         v2 = bytea + ord(s[i])
  68.         bytea = (v2 - 255 * (((v2 + (0xFFFFFFFF80808081 * v2 >> 32)) >> 7) - (v2 >> 31))) & 0xffff
  69.         byteb = (byteb + bytea) % 255
  70.  
  71.     csum = ((byteb << 8) | bytea) & 0x7fff
  72.  
  73.     words2 = list(words)
  74.     words2.append(csum)
  75.     return words2
  76.    
  77. key1 = "ABCDE-FGHJK-LMNPQ-RSTUV-WXYZ2-34567-899D9"
  78. key1 = "WTFYE-FGHJK-LMNPQ-RSTUV-WXYZ2-34567-899D9"
  79. dwords = key_to_dwords(key1)
  80. print dwords
  81. for w in dwords:
  82.     print "%08x" % w
  83.  
  84. key2 = dwords_to_key(dwords)
  85. print key2
  86. print key1 == key2
  87.  
  88. def scramble(n):
  89.     res = 0
  90.     for i in xrange(0, 64, 8):
  91.         t = (n >> i) & 0xff
  92.         res += table[t]
  93.     return res
  94.  
  95. def test_word_14():
  96.     for a1 in xrange(0x10000):
  97.         if not (((a1 >> 12) ^ (a1 >> 8)) & 0xF):
  98.             continue
  99.         if not (((a1 >> 8) ^ (a1 >> 4)) & 0xF):
  100.             continue
  101.         if not (((a1 >> 4) ^ a1) & 0xF):
  102.             continue
  103.         print "%04x" % a1
  104.      
  105. def neighbor_nibbles(a1):
  106.     t = (a1 >> 4) ^ a1
  107.     for i in xrange(7):
  108.         if t & 0xf == 0:
  109.             return True
  110.         t = t >> 4
  111.    
  112.     return False
  113.      
  114. def neighbor_bytes(a1):
  115.     t = (a1 >> 8) ^ a1
  116.     for i in xrange(3):
  117.         if t & 0xff == 0:
  118.             return True
  119.         t = t >> 8
  120.    
  121.     return False
  122.    
  123. def is_prime(a1):
  124.     if a1 <= 0xFFFFFE:
  125.         return False
  126.     n = 3
  127.     while n*n < a1:
  128.         if a1 % n == 0:
  129.             return False
  130.         n += 2
  131.     return True
  132.  
  133. def check_word04(a1):
  134.     v2 = 0;
  135.     v4 = 15;
  136.     for i in xrange(16):
  137.         v2 = (a1 >> (2 * v4 + 1)) & 1 | 2 * v2
  138.         v4 -= 1
  139.     return v2 == 7 * (v2 / 7)
  140.  
  141. def check_word08(a1):
  142.     v2 = 0;
  143.     v4 = 15;
  144.     for i in xrange(16):
  145.         v2 = (a1 >> (2 * v4 + 1)) & 1 | 2 * v2
  146.         v4 -= 1
  147.     return v2 == 13 * ((0x4EC5 * v2 >> 16) >> 2)
  148.  
  149. def check_word10(a1):
  150.     # t = (a1 >> 16)
  151.     # t2 = (t * 0xa79c7b17) >> 40
  152.     # t2 = t2 * 0x187
  153.     # t = t - t2
  154.     # print hex(t)
  155.     # print hex(((a1 >> 16) - 0x187 * (a1 / 0x1870000)))
  156.     # exit()
  157.    
  158.     if not ((a1 >> 16) - 0x187 * (a1 / 0x1870000)) == a1 & 0xff:
  159.         return False
  160.     if not ((a1 >> 16) & (0xffff % (((a1 & 0xff00) >> 8) + 1) == 0)):
  161.         return False
  162.     if not scramble(a1) > 8:
  163.         return False
  164.     return True
  165.        
  166.  
  167. def stupidsum(w00, w04, w08):
  168.     eax = w04
  169.     w00 = (w00 - eax) & 0xffffffff
  170.     eax = w08
  171.     w00 = (w00 - eax) & 0xffffffff
  172.     eax = w08
  173.     eax = eax >> 0xd
  174.     w00 ^= eax
  175.    
  176.     eax = w08
  177.     w04 = (w04 - eax) & 0xffffffff
  178.     eax = w00
  179.     w04 = (w04 - eax) & 0xffffffff
  180.     eax = w00
  181.     eax = (eax << 8) & 0xffffffff
  182.     w04 ^= eax
  183.    
  184.     eax = w00
  185.     w08 = (w08 - eax) & 0xffffffff
  186.     eax = w04
  187.     w08 = (w08 - eax) & 0xffffffff
  188.     eax = w04
  189.     eax = eax >> 0xd
  190.     w08 ^= eax
  191.    
  192.     eax = w04
  193.     w00 = (w00 - eax) & 0xffffffff
  194.     eax = w08
  195.     w00 = (w00 - eax) & 0xffffffff
  196.     eax = w08
  197.     eax = eax >> 0xc
  198.     w00 ^= eax
  199.    
  200.     eax = w08
  201.     w04 = (w04 - eax) & 0xffffffff
  202.     eax = w00
  203.     w04 = (w04 - eax) & 0xffffffff
  204.     eax = w00
  205.     eax = (eax << 0x10) & 0xffffffff
  206.     w04 ^= eax
  207.    
  208.     eax = w00
  209.     w08 = (w08 - eax) & 0xffffffff
  210.     eax = w04
  211.     w08 = (w08 - eax) & 0xffffffff
  212.     eax = w04
  213.     eax = eax >> 5
  214.     w08 ^= eax
  215.    
  216.     eax = w04
  217.     w00 = (w00 - eax) & 0xffffffff
  218.     eax = w08
  219.     w00 = (w00 - eax) & 0xffffffff
  220.     eax = w08
  221.     eax = eax >> 3
  222.     w00 ^= eax
  223.    
  224.     eax = w08
  225.     w04 = (w04 - eax) & 0xffffffff
  226.     eax = w00
  227.     w04 = (w04 - eax) & 0xffffffff
  228.     eax = w00
  229.     eax = (eax << 0xa) & 0xffffffff
  230.     w04 ^= eax
  231.    
  232.     eax = w00
  233.     w08 = (w08 - eax) & 0xffffffff
  234.     eax = w04
  235.     w08 = (w08 - eax) & 0xffffffff
  236.     eax = w04
  237.     eax = eax >> 0xf
  238.     w08 ^= eax
  239.     return w08
  240.    
  241. def commoncheck1(w00, w04, w08):
  242.     v4 = w00;
  243.     v5 = 0;
  244.    
  245.     v9 = 15;
  246.     for i in xrange(16):
  247.         v5 = (w04 >> 2 * v9) & 1 | 2 * v5
  248.         v9 -= 1
  249.        
  250.     if not scramble(v5) > 2:
  251.         return False
  252.  
  253.     v6 = 0;
  254.     v10 = 15;
  255.     for i in xrange(16):
  256.         v6 = (w08 >> 2 * v10) & 1 | 2 * v6
  257.         v10 -= 1
  258.        
  259.     if not scramble(v6) > 3:
  260.         return False
  261.        
  262.     return w00 + 1 == v5 * v6
  263.  
  264. def updatedcommon(w04r, w08r):
  265.     if scramble(w04r) <= 2:
  266.         return None
  267.        
  268.     if scramble(w08r) <= 3:
  269.         return None
  270.        
  271.     return (w04r * w08r) - 1
  272.    
  273. def check_bits(w04, w08):
  274.     res = 0
  275.     if bitcount[shuftable[(w04 >> 28) & 0xf]] == 2:
  276.         res |= 1
  277.     if bitcount[shuftable[(w04 >> 28) & 0xf] ^ shuftable[(w04 >> 24) & 0xf]] == 2:
  278.         res |= 2
  279.     if bitcount[shuftable[(w04 >> 24) & 0xf] ^ shuftable[(w04 >> 20) & 0xf]] == 2:
  280.         res |= 4
  281.     if bitcount[shuftable[(w04 >> 20) & 0xf] ^ shuftable[(w04 >> 16) & 0xf]] == 2:
  282.         res |= 8
  283.     if bitcount[shuftable[(w04 >> 16) & 0xf] ^ shuftable[(w04 >> 12) & 0xf]] == 2:
  284.         res |= 16
  285.     if bitcount[shuftable[(w04 >> 12) & 0xf] ^ shuftable[(w04 >> 8) & 0xf]] == 2:
  286.         res |= 32
  287.     if bitcount[shuftable[(w04 >> 8) & 0xf] ^ shuftable[(w04 >> 4) & 0xf]] == 2:
  288.         res |= 64
  289.     if bitcount[shuftable[(w04 >> 4) & 0xf] ^ shuftable[(w04 >> 0) & 0xf]] == 2:
  290.         res |= 128
  291.     if bitcount[shuftable[(w04 >> 0) & 0xf] ^ shuftable[(w08 >> 28) & 0xf]] == 2:
  292.         res |= 0x100
  293.     if bitcount[shuftable[(w08 >> 28) & 0xf] ^ shuftable[(w08 >> 24) & 0xf]] == 2:
  294.         res |= 0x200
  295.     if bitcount[shuftable[(w08 >> 24) & 0xf] ^ shuftable[(w08 >> 20) & 0xf]] == 2:
  296.         res |= 0x400
  297.     if bitcount[shuftable[(w08 >> 20) & 0xf] ^ shuftable[(w08 >> 16) & 0xf]] == 2:
  298.         res |= 0x800
  299.     if bitcount[shuftable[(w08 >> 16) & 0xf] ^ shuftable[(w08 >> 12) & 0xf]] == 2:
  300.         res |= 0x1000
  301.     if bitcount[shuftable[(w08 >> 12) & 0xf] ^ shuftable[(w08 >> 8) & 0xf]] == 2:
  302.         res |= 0x2000
  303.     if bitcount[shuftable[(w08 >> 8) & 0xf] ^ shuftable[(w08 >> 4) & 0xf]] == 2:
  304.         res |= 0x4000
  305.     if bitcount[shuftable[(w08 >> 4) & 0xf] ^ shuftable[(w08 >> 0) & 0xf]] == 2:
  306.         res |= 0x8000
  307.    
  308.     return res
  309.    
  310. dwords = [0x2019fab1, 0x26efb3bd, 0xc7f1cb7c, 0x7c96b08d, 0x1f5110c5]
  311.    
  312. # print hex(check_bits(0x26efb3bd, 0xc7f1cb7c))
  313. # exit()
  314. def main():
  315.  
  316.     w10bucket = []
  317.     while len(w10bucket) < 20:
  318.         w10 = random.randint(0, 0xffffffff)
  319.         if check_word10(w10):
  320.             w10bucket.append(w10)
  321.     print "bucket full"
  322.    
  323.     while True:
  324.         w04r = random.randint(0, 0xffff)
  325.         w08r = random.randint(0, 0xffff)
  326.         w00 = updatedcommon(w04r, w08r)
  327.        
  328.         if not is_prime(w00) or neighbor_nibbles(w00) or neighbor_bytes(w00):
  329.             continue
  330.            
  331.         while True:
  332.             w04l = random.randint(0, 0xffff)
  333.             if w04l == 7 * (w04l / 7):
  334.                 break
  335.                
  336.         while True:
  337.             w08l = random.randint(0, 0xffff)
  338.             if w08l == 13 * ((0x4EC5 * w08l >> 16) >> 2):
  339.                 break
  340.                
  341.         w04 = 0
  342.         for i in xrange(16):
  343.             w04 = w04 | ((w04l & 1) << (i*2+1)) | ((w04r & 1) << (i*2))
  344.             w04l >>= 1
  345.             w04r >>= 1
  346.            
  347.         w08 = 0
  348.         for i in xrange(16):
  349.             w08 = w08 | ((w08l & 1) << (i*2+1)) | ((w08r & 1) << (i*2))
  350.             w08l >>= 1
  351.             w08r >>= 1
  352.            
  353.         if not check_word04(w04):
  354.             print "BAAAAAAAAD"
  355.             exit()
  356.            
  357.         if not check_word08(w08):
  358.             print "BAAAAAAAAD"
  359.             exit()
  360.            
  361.         if not commoncheck1(w00, w04, w08):
  362.             print "AAAAAAAA"
  363.             exit()
  364.            
  365.         w0c = stupidsum(w00, w04, w08)
  366.  
  367.         csumtarget = check_bits(w04, w08)
  368.         if csumtarget & 0x8000:
  369.             continue
  370.            
  371.         for w10 in w10bucket:
  372.             dwords = [w00, w04, w08, w0c, w10]
  373.             dwords = make_checksum(dwords)
  374.             if dwords[-1] != csumtarget:
  375.                 print ".",
  376.                 continue
  377.             else:
  378.                 print "0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x" % (w00, w04, w08, w0c, w10)
  379.                 dwords = [w00, w04, w08, w0c, w10]
  380.                 dwords = make_checksum(dwords)
  381.                 for w in dwords:
  382.                     print "%08x" % w
  383.                 print dwords_to_key(dwords)
  384.                 print
  385.                 exit()
  386.            
  387.        
  388.            
  389. # two neighboring nibbles must NOT be equal
  390. # two neighboring bytes must NOT be equal
  391.  
  392. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement