Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # Pack8xu; a utility to generate TI-83/84+ OS upgrade files
- # Obviously not complete, but getting there
- import sys, md5, tempfile, binascii, getopt
- def rabin_crypt(hash):
- '''Encrypts a number with Rabin, where the private key is 7 and 11'''
- p = 7
- q = 11
- N = p*q
- #D2(M) / M^k%N
- M = hash ** (((p-1)*(q-1)+4)/8) #raise the hash to that power
- M = M % N
- #D1(M)
- if (M == 0%4):
- M = (M/4-1)/2
- elif (M == 1%4):
- M = ((N-M)/4-1)/2
- elif (M == 2%4):
- M = (M/2-1)/2
- elif (M == 3%4):
- M = ((N-M)/2-1)/2
- return M
- def parse_cmd():
- '''Parse the command line (sys.argv[2:])'''
- try:
- opts, args = getopt.getopt( sys.argv[2:], "hs", ["help","sign"] )
- except getopt.GetoptError:
- usage()
- sys.exit(1)
- for opt, arg in opts:
- if opt in ("-h","--help"):
- usage()
- sys.exit()
- elif opt in ("-s","--sign"):
- global _sign
- _sign = 1
- def usage():
- '''Prints usage information'''
- print '''pack8xu: utility to generate TI-83+/84+ OS upgrade files
- Usage: pack8xu infile [option]
- Where infile is an Intel Hex file.
- -h/--help: Print usage information
- -s/--sign: Generate a signed OS file (with the OpenCert key)'''
- #Grab my infile, quit if none
- try:
- hex = open( sys.argv[1], 'r' )
- except:
- usage()
- sys.exit(1)
- parse_cmd()
- #Now for some OS hashing
- bin = tempfile.TemporaryFile()
- #converting an Intel Hex outfile (from the assembler) to binary for hashing
- binsize = 0
- for line in hex:
- rtype = line[8]
- if (rtype == '1') and (line[9:11] == 'FF'):
- #found EOF
- break
- dsize = int(line[1:3], 16)
- bin.write( binascii.unhexlify( line[9:dsize+10] ) )
- binsize = binsize + dsize
- #Now we can hash the binary file
- bin.seek(0)
- m = md5.new()
- m.update( bin.read(binsize) )
- hash = m.digest()
- print "OS MD5 hash is", binascii.unhexlify( m.hexdigest() )
- #now we can discard the binary file
- bin.close()
- # Our public key will be 77, where our primes p and q were 7 and 11, so 7(11)=77
- # Then, our private key is simply p and q. TI appears to do some interesting things,
- # namely 'decrypting' the MD5 hash as a way to encrypt it, given that if the private key
- # were stored in the certificate, it would be trivial to find the 0004 key. It then 'encrypts'
- # to retrieve the MD5 hash, which also has the advantage of no ambiguity, since no matter
- # which of the four roots were gotten for decryption, the result will always the be same MD5
- # hash.
- # 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
- # D1 is..
- # { (M/4-1)/2 when M = 0 mod 4
- # {D1(M) = { ((N-M)/4-1)/2 when M = 1 mod 4
- # { (M/2-1)/2 when M = 2 mod 4
- # { ((N-M)/2-1)/2 when M = 3 mod 4
- # The formula for encryption is E2(E1(M)), where M is the message (MD5 hash), E2 is M^2 % N,
- # and E1 is 4(2M+1) if J(2M+1,N)==1, or 2(2M+1) if J(2M+1,N)==-1
- # 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
- 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]
Advertisement
Add Comment
Please, Sign In to add comment