Subversion Repositories freemyipod

Rev

Rev 424 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 424 Rev 427
Line 2... Line 2...
2
#
2
#
3
#
3
#
4
#    Copyright 2010 TheSeven
4
#    Copyright 2010 TheSeven
5
#
5
#
6
#
6
#
7
#    This file is part of emBIOS.
7
#    This file is part of emCORE.
8
#
8
#
9
#    emBIOS is free software: you can redistribute it and/or
9
#    emCORE is free software: you can redistribute it and/or
10
#    modify it under the terms of the GNU General Public License as
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
11
#    published by the Free Software Foundation, either version 2 of the
12
#    License, or (at your option) any later version.
12
#    License, or (at your option) any later version.
13
#
13
#
14
#    emBIOS is distributed in the hope that it will be useful,
14
#    emCORE is distributed in the hope that it will be useful,
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
#    See the GNU General Public License for more details.
17
#    See the GNU General Public License for more details.
18
#
18
#
19
#    You should have received a copy of the GNU General Public License
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/>.
20
#    along with emCORE.  If not, see <http://www.gnu.org/licenses/>.
21
#
21
#
22
#
22
#
23
 
23
 
24
 
24
 
25
import sys
25
import sys
26
import os
26
import os
27
import struct
27
import struct
28
import time
28
import time
29
import hashlib
29
import hashlib
30
import libembios
30
import libemcore
31
from libembios import Error
31
from libemcore import Error
32
import libembiosdata
32
import libemcoredata
33
 
33
 
34
 
34
 
35
def s5l8701cryptdfu(data):
35
def s5l8701cryptdfu(data):
36
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
36
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
37
    header = "87011.0\0\0\x08\0\0" + struct.pack("<I", len(data))
37
    header = "87011.0\0\0\x08\0\0" + struct.pack("<I", len(data))
38
    embios = libembios.Embios()
38
    emcore = libemcore.Emcore()
39
    embios.write(0x08000000, header.ljust(0x800, "\0") + data)
39
    emcore.write(0x08000000, header.ljust(0x800, "\0") + data)
40
    embios.hmac_sha1(0x08000800, len(data), 0x08000010)
40
    emcore.hmac_sha1(0x08000800, len(data), 0x08000010)
41
    embios.hmac_sha1(0x08000000, 0x40, 0x08000040)
41
    emcore.hmac_sha1(0x08000000, 0x40, 0x08000040)
42
    embios.aesencrypt(0x08000000, len(data) + 0x800, 1)
42
    emcore.aesencrypt(0x08000000, len(data) + 0x800, 1)
43
    return embios.read(0x08000000, len(data) + 0x800)
43
    return emcore.read(0x08000000, len(data) + 0x800)
44
 
44
 
45
 
45
 
46
def s5l8701decryptdfu(data):
46
def s5l8701decryptdfu(data):
47
    embios = libembios.Embios()
47
    emcore = libemcore.Emcore()
48
    embios.write(0x08000000, data)
48
    emcore.write(0x08000000, data)
49
    embios.aesdecrypt(0x08000000, len(data), 1)
49
    emcore.aesdecrypt(0x08000000, len(data), 1)
50
    return embios.read(0x08000800, len(data) - 0x800)
50
    return emcore.read(0x08000800, len(data) - 0x800)
51
 
51
 
52
 
52
 
53
def s5l8701cryptfirmware(data):
53
def s5l8701cryptfirmware(data):
54
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
54
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
55
    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))
55
    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))
56
    embios = libembios.Embios()
56
    emcore = libemcore.Emcore()
57
    embios.write(0x08000000, header.ljust(0x800, "\0") + data)
57
    emcore.write(0x08000000, header.ljust(0x800, "\0") + data)
58
    embios.hmac_sha1(0x08000800, len(data), 0x0800001c)
58
    emcore.hmac_sha1(0x08000800, len(data), 0x0800001c)
59
    embios.hmac_sha1(0x08000000, 0x200, 0x080001d4)
59
    emcore.hmac_sha1(0x08000000, 0x200, 0x080001d4)
60
    embios.aesencrypt(0x08000800, len(data), 1)
60
    emcore.aesencrypt(0x08000800, len(data), 1)
61
    return embios.read(0x08000000, len(data) + 0x800)
61
    return emcore.read(0x08000000, len(data) + 0x800)
62
 
62
 
63
 
63
 
64
def s5l8701decryptfirmware(data):
64
def s5l8701decryptfirmware(data):
65
    embios = libembios.Embios()
65
    emcore = libemcore.Emcore()
66
    embios.write(0x08000000, data)
66
    emcore.write(0x08000000, data)
67
    embios.aesdecrypt(0x08000800, len(data) - 0x800, 1)
67
    emcore.aesdecrypt(0x08000800, len(data) - 0x800, 1)
68
    return embios.read(0x08000800, len(data) - 0x800)
68
    return emcore.read(0x08000800, len(data) - 0x800)
69
 
69
 
70
 
70
 
71
def s5l8702cryptnor(data):
71
def s5l8702cryptnor(data):
72
    data = data.ljust((len(data) + 0xf) & ~0xf, "\0")
72
    data = data.ljust((len(data) + 0xf) & ~0xf, "\0")
73
    header = "87021.0\x01\0\0\0\0" + struct.pack("<I", len(data)) + hashlib.sha1(data).digest()[:0x10]
73
    header = "87021.0\x01\0\0\0\0" + struct.pack("<I", len(data)) + hashlib.sha1(data).digest()[:0x10]
74
    embios = libembios.Embios()
74
    emcore = libemcore.Emcore()
75
    embios.write(0x08000000, header.ljust(0x800, "\0") + data)
75
    emcore.write(0x08000000, header.ljust(0x800, "\0") + data)
76
    embios.aesencrypt(0x08000800, len(data), 2)
76
    emcore.aesencrypt(0x08000800, len(data), 2)
77
    embios.aesencrypt(0x08000010, 0x10, 2)
77
    emcore.aesencrypt(0x08000010, 0x10, 2)
78
    embios.write(0x08000040, hashlib.sha1(embios.read(0x08000000, 0x40)).digest()[:0x10])
78
    emcore.write(0x08000040, hashlib.sha1(emcore.read(0x08000000, 0x40)).digest()[:0x10])
79
    embios.aesencrypt(0x08000040, 0x10, 2)
79
    emcore.aesencrypt(0x08000040, 0x10, 2)
80
    return embios.read(0x08000000, len(data) + 0x800)
80
    return emcore.read(0x08000000, len(data) + 0x800)
81
 
81
 
82
 
82
 
83
def s5l8702decryptnor(data):
83
def s5l8702decryptnor(data):
84
    embios = libembios.Embios()
84
    emcore = libemcore.Emcore()
85
    embios.write(0x08000000, data[0x800:])
85
    emcore.write(0x08000000, data[0x800:])
86
    embios.aesdecrypt(0x08000000, len(data) - 0x800, 1)
86
    emcore.aesdecrypt(0x08000000, len(data) - 0x800, 1)
87
    return embios.read(0x08000000, len(data) - 0x800)
87
    return emcore.read(0x08000000, len(data) - 0x800)
88
 
88
 
89
 
89
 
90
def s5l8702genpwnage(data):
90
def s5l8702genpwnage(data):
91
    cert = open(os.path.dirname(__file__) + "/libipodcrypto/s5l8702pwnage.cer", "rb").read()
91
    cert = open(os.path.dirname(__file__) + "/libipodcrypto/s5l8702pwnage.cer", "rb").read()
92
    data = data.ljust(max(0x840, (len(data) + 0xf) & ~0xf), "\0")
92
    data = data.ljust(max(0x840, (len(data) + 0xf) & ~0xf), "\0")
93
    header = ("87021.0\x03\0\0\0\0" + struct.pack("<IIII", len(data) - 0x830, len(data) - 0x4f6, len(data) - 0x7b0, 0x2ba)).ljust(0x40, "\0")
93
    header = ("87021.0\x03\0\0\0\0" + struct.pack("<IIII", len(data) - 0x830, len(data) - 0x4f6, len(data) - 0x7b0, 0x2ba)).ljust(0x40, "\0")
94
    embios = libembios.Embios()
94
    emcore = libemcore.Emcore()
95
    embios.write(0x08000000, header + hashlib.sha1(header).digest()[:0x10])
95
    emcore.write(0x08000000, header + hashlib.sha1(header).digest()[:0x10])
96
    embios.aesencrypt(0x08000040, 0x10, 1)
96
    emcore.aesencrypt(0x08000040, 0x10, 1)
97
    return embios.read(0x08000000, 0x50) + data + cert.ljust((len(cert) + 0xf) & ~0xf, "\0")
97
    return emcore.read(0x08000000, 0x50) + data + cert.ljust((len(cert) + 0xf) & ~0xf, "\0")
98
 
98
 
99
 
99
 
100
def s5l8701cryptdfufile(infile, outfile):
100
def s5l8701cryptdfufile(infile, outfile):
101
    infile = open(infile, "rb")
101
    infile = open(infile, "rb")
102
    outfile = open(outfile, "wb")
102
    outfile = open(outfile, "wb")