tari

tari

Mar 26th, 2009
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.18 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # Pack8xu; a utility to generate TI-83/84+ OS upgrade files
  4. # Obviously not complete, but getting there
  5. import sys, md5, tempfile, binascii, getopt
  6. def rabin_crypt(hash):
  7.     '''Encrypts a number with Rabin, where the private key is 7 and 11'''
  8.     p = 7
  9.     q = 11
  10.     N = p*q
  11.     #D2(M) / M^k%N
  12.     M = hash ** (((p-1)*(q-1)+4)/8) #raise the hash to that power
  13.     M = M % N
  14.     #D1(M)
  15.     if (M == 0%4):
  16.         M = (M/4-1)/2
  17.     elif (M == 1%4):
  18.         M = ((N-M)/4-1)/2
  19.     elif (M == 2%4):
  20.         M = (M/2-1)/2
  21.     elif (M == 3%4):
  22.         M = ((N-M)/2-1)/2
  23.     return M
  24.  
  25. def parse_cmd():
  26.     '''Parse the command line (sys.argv[2:])'''
  27.     try:
  28.         opts, args = getopt.getopt( sys.argv[2:], "hs", ["help","sign"] )
  29.     except getopt.GetoptError:
  30.         usage()
  31.         sys.exit(1)
  32.     for opt, arg in opts:
  33.         if opt in ("-h","--help"):
  34.             usage()
  35.             sys.exit()
  36.         elif opt in ("-s","--sign"):
  37.             global _sign
  38.             _sign = 1
  39.  
  40. def usage():
  41.     '''Prints usage information'''
  42.     print '''pack8xu: utility to generate TI-83+/84+ OS upgrade files
  43.  
  44. Usage: pack8xu infile [option]
  45.  
  46. Where infile is an Intel Hex file.
  47.  
  48. -h/--help: Print usage information
  49. -s/--sign: Generate a signed OS file (with the OpenCert key)'''
  50.    
  51.        
  52.  
  53. #Grab my infile, quit if none
  54. try:
  55.     hex = open( sys.argv[1], 'r' )
  56. except:
  57.     usage()
  58.     sys.exit(1)
  59.    
  60. parse_cmd()
  61.  
  62. #Now for some OS hashing
  63. bin = tempfile.TemporaryFile()
  64.  
  65. #converting an Intel Hex outfile (from the assembler) to binary for hashing
  66. binsize = 0
  67. for line in hex:
  68.     rtype = line[8]
  69.     if (rtype == '1') and (line[9:11] == 'FF'):
  70.         #found EOF
  71.         break
  72.     dsize = int(line[1:3], 16)
  73.     bin.write( binascii.unhexlify( line[9:dsize+10] ) )
  74.     binsize = binsize + dsize
  75.    
  76. #Now we can hash the binary file
  77. bin.seek(0)
  78. m = md5.new()
  79. m.update( bin.read(binsize) )
  80. hash = m.digest()
  81. print "OS MD5 hash is", binascii.unhexlify( m.hexdigest() )
  82.  
  83. #now we can discard the binary file
  84. bin.close()
  85.  
  86.  
  87.  
  88. # Our public key will be 77, where our primes p and q were 7 and 11, so 7(11)=77
  89. #   Then, our private key is simply p and q.  TI appears to do some interesting things,
  90. #   namely 'decrypting' the MD5 hash as a way to encrypt it, given that if the private key
  91. #   were stored in the certificate, it would be trivial to find the 0004 key.  It then 'encrypts'
  92. #   to retrieve the MD5 hash, which also has the advantage of no ambiguity, since no matter
  93. #   which of the four roots were gotten for decryption, the result will always the be same MD5
  94. #   hash.
  95. #   To decrypt then, we do D1(D2(M)), where D2 is M^k % n (and k is ((p-1)*(q-1)+4)/8, and n is pq), and
  96. #   D1 is..
  97. #   { (M/4-1)/2      when M = 0 mod 4
  98. #   {D1(M) =  { ((N-M)/4-1)/2  when M = 1 mod 4
  99. #   { (M/2-1)/2      when M = 2 mod 4
  100. #   { ((N-M)/2-1)/2  when M = 3 mod 4
  101.  
  102.    
  103. # The formula for encryption is E2(E1(M)), where M is the message (MD5 hash), E2 is M^2 % N,
  104. #   and E1 is 4(2M+1) if J(2M+1,N)==1, or 2(2M+1) if J(2M+1,N)==-1
  105. #   See http://www.google.com/codesearch?hl=en&q=+rabin+show:cUROMF4qSf0:ixMWadSbUfY:MLlGweR8yNc&sa=N&cd=2&ct=rc&cs_p=http://dist.okws.org/dist/sfslite-0.8.6.tar.gz&cs_f=sfslite-0.8.6/crypt/rabin.h#a0
  106.  
  107.  
  108. header_dat = [0x80,0x0f,0x00,0x00,0x00,0x00, 0x80,0x11,0x00, 0x80,0x21,0x00, 0x80,0x31,0x00, 0x80,0xa1,0x00, 0x80,0x7f, 0x00,0x00,0x00,0x00]
  109.  
Advertisement
Add Comment
Please, Sign In to add comment