Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
179 theseven 1
#!/usr/bin/env python
2
#
3
#
4
#    Copyright 2010 TheSeven
5
#
6
#
7
#    This file is part of emBIOS.
8
#
9
#    emBIOS is free software: you can redistribute it and/or
10
#    modify it under the terms of the GNU General Public License as
11
#    published by the Free Software Foundation, either version 2 of the
12
#    License, or (at your option) any later version.
13
#
14
#    emBIOS is distributed in the hope that it will be useful,
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
#    See the GNU General Public License for more details.
18
#
19
#    You should have received a copy of the GNU General Public License
20
#    along with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
21
#
22
#
23
 
24
 
25
import sys
26
import struct
27
import time
277 theseven 28
import hashlib
179 theseven 29
import libembios
30
from libembios import Error
31
import libembiosdata
32
 
33
 
272 theseven 34
def s5l8701cryptdfu(data):
179 theseven 35
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
36
    header = "87011.0\0\0\x08\0\0" + struct.pack("<I", len(data))
37
    embios = libembios.Embios()
38
    embios.write(0x08000000, header.ljust(0x800, "\0") + data)
180 theseven 39
    embios.lib.dev.timeout = 20000
179 theseven 40
    embios.hmac_sha1(0x08000800, len(data), 0x08000010)
41
    embios.hmac_sha1(0x08000000, 0x40, 0x08000040)
42
    embios.aesencrypt(0x08000000, len(data) + 0x800, 1)
43
    return embios.read(0x08000000, len(data) + 0x800)
44
 
45
 
272 theseven 46
def s5l8701decryptdfu(data):
179 theseven 47
    embios = libembios.Embios()
48
    embios.write(0x08000000, data)
180 theseven 49
    embios.lib.dev.timeout = 20000
179 theseven 50
    embios.aesdecrypt(0x08000000, len(data), 1)
51
    return embios.read(0x08000800, len(data) - 0x800)
52
 
53
 
272 theseven 54
def s5l8701cryptfirmware(data):
179 theseven 55
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
186 theseven 56
    header = "\0\0\0\0\x02\0\0\0\x01\0\0\0\x40\0\0\0\0\0\0\0" + struct.pack("<I", len(data))
179 theseven 57
    embios = libembios.Embios()
58
    embios.write(0x08000000, header.ljust(0x800, "\0") + data)
180 theseven 59
    embios.lib.dev.timeout = 20000
179 theseven 60
    embios.hmac_sha1(0x08000800, len(data), 0x0800001c)
61
    embios.hmac_sha1(0x08000000, 0x200, 0x080001d4)
62
    embios.aesencrypt(0x08000800, len(data), 1)
63
    return embios.read(0x08000000, len(data) + 0x800)
64
 
65
 
272 theseven 66
def s5l8701decryptfirmware(data):
179 theseven 67
    embios = libembios.Embios()
68
    embios.write(0x08000000, data)
180 theseven 69
    embios.lib.dev.timeout = 20000
179 theseven 70
    embios.aesdecrypt(0x08000800, len(data) - 0x800, 1)
71
    return embios.read(0x08000800, len(data) - 0x800)
72
 
73
 
277 theseven 74
def s5l8702cryptnor(data):
75
    data = data.ljust((len(data) + 0xf) & ~0xf, "\0")
279 theseven 76
    header = "87021.0\x01\0\0\0\0" + struct.pack("<I", len(data)) + hashlib.sha1(data).digest()[:0x10]
277 theseven 77
    embios = libembios.Embios()
78
    embios.write(0x08000000, header.ljust(0x800, "\0") + data)
79
    embios.lib.dev.timeout = 20000
279 theseven 80
    embios.aesencrypt(0x08000800, len(data), 2)
81
    embios.aesencrypt(0x08000010, 0x10, 2)
277 theseven 82
    embios.write(0x08000040, hashlib.sha1(embios.read(0x08000000, 0x40)).digest()[:0x10])
279 theseven 83
    embios.aesencrypt(0x08000040, 0x10, 2)
277 theseven 84
    return embios.read(0x08000000, len(data) + 0x800)
85
 
86
 
87
def s5l8702decryptnor(data):
88
    embios = libembios.Embios()
89
    embios.write(0x08000000, data[0x800:])
90
    embios.lib.dev.timeout = 20000
91
    embios.aesdecrypt(0x08000000, len(data) - 0x800, 1)
92
    return embios.read(0x08000000, len(data) - 0x800)
93
 
94
 
272 theseven 95
def s5l8701cryptdfufile(infile, outfile):
179 theseven 96
    infile = open(infile, "rb")
97
    outfile = open(outfile, "wb")
272 theseven 98
    outfile.write(s5l8701cryptdfu(infile.read()))
179 theseven 99
    infile.close()
100
    outfile.close()
101
 
102
 
272 theseven 103
def s5l8701decryptdfufile(infile, outfile):
179 theseven 104
    infile = open(infile, "rb")
105
    outfile = open(outfile, "wb")
272 theseven 106
    outfile.write(s5l8701decryptdfu(infile.read()))
179 theseven 107
    infile.close()
108
    outfile.close()
109
 
110
 
272 theseven 111
def s5l8701cryptfirmwarefile(infile, outfile):
179 theseven 112
    infile = open(infile, "rb")
113
    outfile = open(outfile, "wb")
272 theseven 114
    outfile.write(s5l8701cryptfirmware(infile.read()))
179 theseven 115
    infile.close()
116
    outfile.close()
117
 
118
 
272 theseven 119
def s5l8701decryptfirmwarefile(infile, outfile):
179 theseven 120
    infile = open(infile, "rb")
121
    outfile = open(outfile, "wb")
272 theseven 122
    outfile.write(s5l8701decryptfirmware(infile.read()))
179 theseven 123
    infile.close()
124
    outfile.close()
277 theseven 125
 
126
 
127
def s5l8702cryptnorfile(infile, outfile):
128
    infile = open(infile, "rb")
129
    outfile = open(outfile, "wb")
130
    outfile.write(s5l8702cryptnor(infile.read()))
131
    infile.close()
132
    outfile.close()
133
 
134
 
135
def s5l8702decryptnorfile(infile, outfile):
136
    infile = open(infile, "rb")
137
    outfile = open(outfile, "wb")
138
    outfile.write(s5l8702decryptnor(infile.read()))
139
    infile.close()
140
    outfile.close()