Check the H3s vs. the H4. Check the ticket and tmd
[svpe-tools.git] / tools.c
blob2c4062213912fbdd4f3be0c978478a16d7ef2d90
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
5 #include "tools.h"
7 #include <openssl/aes.h>
8 #include <openssl/sha.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <stdio.h>
14 // basic data types
17 u32 be32(u8 *p)
19 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
22 u64 be64(u8 *p)
24 return ((u64)be32(p) << 32) | be32(p + 4);
27 u64 be34(u8 *p)
29 return 4 * (u64)be32(p);
33 // crypto
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)
43 char path[256];
44 char *home;
45 FILE *fp;
47 home = getenv("HOME");
48 if (home == 0)
49 fatal("cannot find HOME");
50 snprintf(path, sizeof path, "%s/.wii/%s", home, name);
52 fp = fopen(path, "rb");
53 if (fp == 0)
54 fatal("cannot open common-key");
55 if (fread(key, len, 1, fp) != 1)
56 fatal("error reading common-key");
57 fclose(fp);
60 void aes_cbc_dec(u8 *key, u8 *iv, u8 *in, u32 len, u8 *out)
62 AES_KEY aes_key;
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)
70 u8 common_key[16];
71 u8 iv[16];
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)
82 return -1;
86 // compression
89 void do_yaz0(u8 *in, u32 in_size, u8 *out, u32 out_size)
91 u32 nout;
92 u8 bits;
93 u32 nbits;
94 u32 n, d, i;
96 nbits = 0;
97 in += 0x10;
98 for (nout = 0; nout < out_size; ) {
99 if (nbits == 0) {
100 bits = *in++;
101 nbits = 8;
104 if ((bits & 0x80) != 0) {
105 *out++ = *in++;
106 nout++;
107 } else {
108 n = *in++;
109 d = *in++;
110 d |= (n << 8) & 0xf00;
111 n >>= 4;
112 if (n == 0)
113 n = 0x10 + *in++;
114 n += 2;
115 d++;
117 for (i = 0; i < n; i++) {
118 *out = *(out - d);
119 out++;
121 nout += n;
124 nbits--;
125 bits <<= 1;
130 // error handling
133 void fatal(const char *s)
135 perror(s);
137 exit(1);
141 // output formatting
144 void print_bytes(u8 *x, u32 n)
146 u32 i;
148 for (i = 0; i < n; i++)
149 fprintf(stderr, "%02x", x[i]);
152 void hexdump(u8 *x, u32 n)
154 u32 i, j;
156 for (i = 0; i < n; i += 16) {
157 fprintf(stderr, "%04x:", i);
158 for (j = 0; j < 16 && i + j < n; j++) {
159 if ((j & 3) == 0)
160 fprintf(stderr, " ");
161 fprintf(stderr, "%02x", *x++);
163 fprintf(stderr, "\n");
165 if (n & 15)
166 fprintf(stderr, "\n");