-
Notifications
You must be signed in to change notification settings - Fork 4
/
twlbf.c
103 lines (90 loc) · 3.43 KB
/
twlbf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "common.h"
#include "utils.h"
#include "dsi.h"
#include "crypto.h"
const char * const CRYPT = "crypt";
const char * const CRYPT_3DS = "crypt_3ds";
const char * const DECRYPT_MBR = "decrypt_mbr";
const char * const ENCRYPT_ES = "encrypt_es";
const char * const DECRYPT_ES = "decrypt_es";
const char * const EMMC_CID = "emmc_cid";
const char * const CONSOLE_ID = "console_id";
const char * const CONSOLE_ID_BCD = "console_id_bcd";
const char * const CONSOLE_ID_3DS = "console_id_3ds";
const char * const INVALID_PARAMETERS = "invalid parameters";
int main(int argc, const char *argv[]){
if(argc == 6){
// twlbf crypt(_3ds) [Console ID] [EMMC CID] [offset] [src]
// twlbf decrypt_mbr [Console ID] [EMMC CID] [in_file] [out_file]
u8 console_id[8], emmc_cid[16], src[16], offset[2];
hex2bytes(console_id, 8, argv[2], 1);
hex2bytes(emmc_cid, 16, argv[3], 1);
if (!strcmp(argv[1], CRYPT) || !strcmp(argv[1], CRYPT_3DS)) {
hex2bytes(offset, 2, argv[4], 1);
hex2bytes(src, 16, argv[5], 1);
}
if(!strcmp(argv[1], CRYPT)){
dsi_aes_ctr_crypt_block(console_id, emmc_cid, offset, src, 0);
}else if(!strcmp(argv[1], CRYPT_3DS)){
dsi_aes_ctr_crypt_block(console_id, emmc_cid, offset, src, 1);
}else if(!strcmp(argv[1], DECRYPT_MBR)){
dsi_decrypt_mbr(console_id, emmc_cid, argv[4], argv[5]);
}else{
puts(INVALID_PARAMETERS);
}
}else if(argc == 7){
// twlbf emmc_cid/console_id(_bcd) [Console ID] [EMMC CID] [offset] [src] [verify]
u8 console_id[8], emmc_cid[16], src[16], verify[16], offset[2];
hex2bytes(console_id, 8, argv[2], 1);
hex2bytes(emmc_cid, 16, argv[3], 1);
hex2bytes(offset, 2, argv[4], 1);
hex2bytes(src, 16, argv[5], 1);
hex2bytes(verify, 16, argv[6], 1);
if(!strcmp(argv[1], "emmc_cid")){
dsi_brute_emmc_cid(console_id, emmc_cid, offset, src, verify);
}else if(!strcmp(argv[1], "console_id")){
dsi_brute_console_id(console_id, emmc_cid, offset, src, verify, NORMAL);
}else if(!strcmp(argv[1], "console_id_bcd")){
dsi_brute_console_id(console_id, emmc_cid, offset, src, verify, BCD);
}else if(!strcmp(argv[1], "console_id_3ds")){
dsi_brute_console_id(console_id, emmc_cid, offset, src, verify, CTR);
}else{
puts(INVALID_PARAMETERS);
}
}else if(argc == 5){
// BEWARE the decrypted content comes with a decrypted es block footer, thus incompatible with twltool
// with the benefit of preservation of original nonce, we can encrypt it back to original byte exact
// twlbf es_decrypt [Console ID] [in_file] [out_file]
// twlbf es_encrypt [Console ID] [in_file] [out_file]
u8 console_id[8];
hex2bytes(console_id, 8, argv[2], 1);
if (!strcmp(argv[1], ENCRYPT_ES)) {
dsi_es_block_crypt(console_id, ENCRYPT, argv[3], argv[4]);
} else if (!strcmp(argv[1], DECRYPT_ES)){
dsi_es_block_crypt(console_id, DECRYPT, argv[3], argv[4]);
} else {
puts(INVALID_PARAMETERS);
}
}else if(argc >= 2 && !strcmp(argv[1], "crypto_test")){
u8 key[16] = {1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8};
u8 src[32] = {8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1};
u8 out[32];
crypto_init();
aes_128_ecb_set_key(key);
aes_128_ecb_crypt(out, src, 16);
aes_128_ecb_crypt(out + 16, src + 16, 16);
puts(hexdump(out, 32, 1));
aes_128_ecb_crypt(out, src, 32);
puts(hexdump(out, 32, 1));
}else{
puts(INVALID_PARAMETERS);
}
#ifdef _WIN32
system("pause");
#endif
return 0;
}