1 // Copyright 2007,2008 Segher Boessenkool <segher@kernel.crashing.org>
2 // Licensed under the terms of the GNU GPL, version 2
3 // http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
7 #include <openssl/aes.h>
8 #include <openssl/sha.h>
19 return (p
[0] << 24) | (p
[1] << 16) | (p
[2] << 8) | p
[3];
24 return ((u64
)be32(p
) << 32) | be32(p
+ 4);
29 return 4 * (u64
)be32(p
);
36 void sha(u8
*data
, u32 len
, u8
*hash
)
38 SHA1(data
, len
, hash
);
41 void get_key(const char *name
, u8
*key
, u32 len
)
47 home
= getenv("HOME");
49 fatal("cannot find HOME");
50 snprintf(path
, sizeof path
, "%s/.wii/%s", home
, name
);
52 fp
= fopen(path
, "rb");
54 fatal("cannot open common-key");
55 if (fread(key
, len
, 1, fp
) != 1)
56 fatal("error reading common-key");
60 void aes_cbc_dec(u8
*key
, u8
*iv
, u8
*in
, u32 len
, u8
*out
)
64 AES_set_decrypt_key(key
, 128, &aes_key
);
65 AES_cbc_encrypt(in
, out
, len
, &aes_key
, iv
, AES_DECRYPT
);
68 void decrypt_title_key(u8
*title_key_crypted
, u8
*title_id
, u8
*title_key
)
73 get_key("common-key", common_key
, 16);
75 memset(iv
, 0, sizeof iv
);
76 memcpy(iv
, title_id
, 8);
77 aes_cbc_dec(common_key
, iv
, title_key_crypted
, 16, title_key
);
80 int check_cert(u8
*data
, u32 data_len
, u8
*cert
, u32 cert_len
)
89 void do_yaz0(u8
*in
, u32 in_size
, u8
*out
, u32 out_size
)
98 for (nout
= 0; nout
< out_size
; ) {
104 if ((bits
& 0x80) != 0) {
110 d
|= (n
<< 8) & 0xf00;
117 for (i
= 0; i
< n
; i
++) {
133 void fatal(const char *s
)
144 void print_bytes(u8
*x
, u32 n
)
148 for (i
= 0; i
< n
; i
++)
149 fprintf(stderr
, "%02x", x
[i
]);
152 void hexdump(u8
*x
, u32 n
)
156 for (i
= 0; i
< n
; i
+= 16) {
157 fprintf(stderr
, "%04x:", i
);
158 for (j
= 0; j
< 16 && i
+ j
< n
; j
++) {
160 fprintf(stderr
, " ");
161 fprintf(stderr
, "%02x", *x
++);
163 fprintf(stderr
, "\n");
166 fprintf(stderr
, "\n");