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
#
427 farthen 7
#    This file is part of emCORE.
179 theseven 8
#
427 farthen 9
#    emCORE is free software: you can redistribute it and/or
179 theseven 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
#
427 farthen 14
#    emCORE is distributed in the hope that it will be useful,
179 theseven 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
427 farthen 20
#    along with emCORE.  If not, see <http://www.gnu.org/licenses/>.
179 theseven 21
#
22
#
23
 
24
 
25
import sys
359 theseven 26
import os
179 theseven 27
import struct
28
import time
277 theseven 29
import hashlib
427 farthen 30
import libemcore
31
from libemcore import Error
32
import libemcoredata
179 theseven 33
 
34
 
272 theseven 35
def s5l8701cryptdfu(data):
179 theseven 36
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
37
    header = "87011.0\0\0\x08\0\0" + struct.pack("<I", len(data))
427 farthen 38
    emcore = libemcore.Emcore()
39
    emcore.write(0x08000000, header.ljust(0x800, "\0") + data)
40
    emcore.hmac_sha1(0x08000800, len(data), 0x08000010)
41
    emcore.hmac_sha1(0x08000000, 0x40, 0x08000040)
42
    emcore.aesencrypt(0x08000000, len(data) + 0x800, 1)
43
    return emcore.read(0x08000000, len(data) + 0x800)
179 theseven 44
 
45
 
272 theseven 46
def s5l8701decryptdfu(data):
427 farthen 47
    emcore = libemcore.Emcore()
48
    emcore.write(0x08000000, data)
49
    emcore.aesdecrypt(0x08000000, len(data), 1)
50
    return emcore.read(0x08000800, len(data) - 0x800)
179 theseven 51
 
52
 
272 theseven 53
def s5l8701cryptfirmware(data):
179 theseven 54
    data = data.ljust((len(data) + 0x3f) & ~0x3f, "\0")
186 theseven 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))
427 farthen 56
    emcore = libemcore.Emcore()
57
    emcore.write(0x08000000, header.ljust(0x800, "\0") + data)
58
    emcore.hmac_sha1(0x08000800, len(data), 0x0800001c)
59
    emcore.hmac_sha1(0x08000000, 0x200, 0x080001d4)
60
    emcore.aesencrypt(0x08000800, len(data), 1)
61
    return emcore.read(0x08000000, len(data) + 0x800)
179 theseven 62
 
63
 
272 theseven 64
def s5l8701decryptfirmware(data):
427 farthen 65
    emcore = libemcore.Emcore()
66
    emcore.write(0x08000000, data)
67
    emcore.aesdecrypt(0x08000800, len(data) - 0x800, 1)
68
    return emcore.read(0x08000800, len(data) - 0x800)
179 theseven 69
 
70
 
277 theseven 71
def s5l8702cryptnor(data):
72
    data = data.ljust((len(data) + 0xf) & ~0xf, "\0")
279 theseven 73
    header = "87021.0\x01\0\0\0\0" + struct.pack("<I", len(data)) + hashlib.sha1(data).digest()[:0x10]
427 farthen 74
    emcore = libemcore.Emcore()
75
    emcore.write(0x08000000, header.ljust(0x800, "\0") + data)
76
    emcore.aesencrypt(0x08000800, len(data), 2)
77
    emcore.aesencrypt(0x08000010, 0x10, 2)
78
    emcore.write(0x08000040, hashlib.sha1(emcore.read(0x08000000, 0x40)).digest()[:0x10])
79
    emcore.aesencrypt(0x08000040, 0x10, 2)
80
    return emcore.read(0x08000000, len(data) + 0x800)
277 theseven 81
 
82
 
83
def s5l8702decryptnor(data):
427 farthen 84
    emcore = libemcore.Emcore()
85
    emcore.write(0x08000000, data[0x800:])
86
    emcore.aesdecrypt(0x08000000, len(data) - 0x800, 1)
87
    return emcore.read(0x08000000, len(data) - 0x800)
277 theseven 88
 
89
 
359 theseven 90
def s5l8702genpwnage(data):
91
    cert = open(os.path.dirname(__file__) + "/libipodcrypto/s5l8702pwnage.cer", "rb").read()
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")
427 farthen 94
    emcore = libemcore.Emcore()
95
    emcore.write(0x08000000, header + hashlib.sha1(header).digest()[:0x10])
96
    emcore.aesencrypt(0x08000040, 0x10, 1)
97
    return emcore.read(0x08000000, 0x50) + data + cert.ljust((len(cert) + 0xf) & ~0xf, "\0")
359 theseven 98
 
99
 
272 theseven 100
def s5l8701cryptdfufile(infile, outfile):
179 theseven 101
    infile = open(infile, "rb")
102
    outfile = open(outfile, "wb")
272 theseven 103
    outfile.write(s5l8701cryptdfu(infile.read()))
179 theseven 104
    infile.close()
105
    outfile.close()
106
 
107
 
272 theseven 108
def s5l8701decryptdfufile(infile, outfile):
179 theseven 109
    infile = open(infile, "rb")
110
    outfile = open(outfile, "wb")
272 theseven 111
    outfile.write(s5l8701decryptdfu(infile.read()))
179 theseven 112
    infile.close()
113
    outfile.close()
114
 
115
 
272 theseven 116
def s5l8701cryptfirmwarefile(infile, outfile):
179 theseven 117
    infile = open(infile, "rb")
118
    outfile = open(outfile, "wb")
272 theseven 119
    outfile.write(s5l8701cryptfirmware(infile.read()))
179 theseven 120
    infile.close()
121
    outfile.close()
122
 
123
 
272 theseven 124
def s5l8701decryptfirmwarefile(infile, outfile):
179 theseven 125
    infile = open(infile, "rb")
126
    outfile = open(outfile, "wb")
272 theseven 127
    outfile.write(s5l8701decryptfirmware(infile.read()))
179 theseven 128
    infile.close()
129
    outfile.close()
277 theseven 130
 
131
 
132
def s5l8702cryptnorfile(infile, outfile):
133
    infile = open(infile, "rb")
134
    outfile = open(outfile, "wb")
135
    outfile.write(s5l8702cryptnor(infile.read()))
136
    infile.close()
137
    outfile.close()
138
 
139
 
140
def s5l8702decryptnorfile(infile, outfile):
141
    infile = open(infile, "rb")
142
    outfile = open(outfile, "wb")
143
    outfile.write(s5l8702decryptnor(infile.read()))
144
    infile.close()
145
    outfile.close()
359 theseven 146
 
147
 
148
def s5l8702genpwnagefile(infile, outfile):
149
    infile = open(infile, "rb")
150
    outfile = open(outfile, "wb")
151
    outfile.write(s5l8702genpwnage(infile.read()))
152
    infile.close()
153
    outfile.close()