CHDK Wiki
(Created page with "<source lang="python"> #!/usr/bin/env python # FI2 key extraction from disassembled files produced with disassemble.pl from http://chdk.wikia.com/wiki/GPL:disassemble.pl' # Cop...")
 
(Adding categories)
 
Line 78: Line 78:
   
 
</source>
 
</source>
  +
[[Category:GPL]]
  +
[[Category:Development]]

Latest revision as of 19:04, 6 January 2012

#!/usr/bin/env python


# FI2 key extraction from disassembled files produced with disassemble.pl from http://chdk.wikia.com/wiki/GPL:disassemble.pl'
# Copyright (C) 2012  Stefan Talpalaru <stefantalpalaru@yahoo.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import sys
import re
from pprint import pprint

def extract_key(f, addr):
    f.seek(0)
    processed = 0
    start = False
    mem = []
    for line in f:
        if line.startswith(addr):
            start = True
        if start:
            processed += 1
            line_data = re.split(r'\s*', line)
            mem.append(line_data[1])
        if processed == 4:
            break
    key = []
    for data in mem:
        tmp = []
        for i in xrange(len(data) / 2):
            tmp.append(data[i * 2 : i * 2 + 2])
        key.append(''.join(reversed(tmp)))
    return ''.join(key)

def main(dis_file):
    f = open(dis_file)
    # FI2 key
    for line in f:
        if line.startswith('ffff0004'):
            line_data = re.split(r'\s*', line)
            key_addr = line_data[8][1:-1]
            break
    fi2key = extract_key(f, key_addr)
    print 'FI2KEY=%s' % fi2key

    # FI2 iv
    f.seek(0)
    for line in f:
        if line.endswith('rsbne\tr1, r1, #16\n'):
            while not line.endswith(') \n'):
                line = f.next()
            line_data = re.split(r'\s*', line)
            iv_addr = line_data[8][1:-1]
            break
    fi2iv = extract_key(f, iv_addr)
    print 'FI2IV=%s' % fi2iv

def usage():
    print 'usage: %s file.dis' % sys.argv[0]
    print '       where file.dis is produced with disassemble.pl from http://chdk.wikia.com/wiki/GPL:disassemble.pl'

if __name__ == '__main__':
    if len(sys.argv) != 2:
        usage()
        exit(1)
    main(*sys.argv[1:])