forked from zarboz/ruuveal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
171 lines (139 loc) · 4.31 KB
/
main.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* ruuveal - Decrypt HTC encrypted RUUs (rom.zip files).
*
* Copyright (C) 2013 Kenny Millington
*
* This file is part of ruuveal.
*
* ruuveal is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ruuveal is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ruuveal. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "htcaes.h"
#include "htckey.h"
#include "htc/devices.h"
#ifdef DEBUG_ENABLED
#define DEBUG(x) x
#else
#define DEBUG(x)
#endif
#define FAIL(x) rc = x; goto end;
#define HTC_ZIPAES_HDR "Htc@egi$"
#ifdef DEBUG_ENABLED
static void debug(const char *fmt, ...)
{
static int prefix = 0;
va_list args;
if(prefix == 0) {
fprintf(stderr, "debug> ");
prefix = 1;
}
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if(strstr(fmt, "\n")) {
prefix = 0;
}
}
static void dump(const char *label, const unsigned char *data, int len)
{
int i;
debug("%s dump (offset: 0x%08lx, size: %d):-\n",
label, (unsigned long int)data, len);
for(i = 0; i < len; i++) {
debug("%02x ", (unsigned char)data[i]);
if((i + 1) % 16 == 0)
debug("\n", i);
}
debug("\n");
}
#endif
int usage(const char **argv)
{
int i;
htc_device_t *ptr;
printf("usage:-\n\n");
printf("%s <device> <encrypted.zip> <decrypted.zip>\n\n", argv[0]);
printf("supported devices:-\n\n");
for(ptr = htc_get_devices(); *ptr->name; ptr++) {
printf("* %s (%s)\n", ptr->desc, ptr->name);
}
printf("\n");
return -1;
}
void progress_update(unsigned int pos, unsigned int size)
{
printf("Decrypting RUU... %d/%d\r", pos, size);
}
int main(int argc, const char **argv)
{
char key[HTC_AES_KEYSIZE] = {0};
char iv[HTC_AES_KEYSIZE] = {0};
int rc = 0;
unsigned short keymap_index = 0;
unsigned int chunk_size = 0;
FILE *in = NULL, *out = NULL;
if(argc != 4) {
return usage(argv);
}
printf("ruuveal - ALPHA BUILD\n");
printf("A HTC RUU decrypter\n");
printf("---------------------\n\n");
/* Open the encrypted.zip file. */
if((in = fopen(argv[2], "rb")) == NULL) {
perror("failed to open encrypted zip");
FAIL(-2)
}
/* Validate zip file is a HTC AES encrypted zip file. */
fread(key, sizeof(char), strlen(HTC_ZIPAES_HDR), in);
if(strncmp(key, HTC_ZIPAES_HDR, strlen(HTC_ZIPAES_HDR))) {
fseek(in, 0x100, SEEK_SET);
fread(key, sizeof(char), strlen(HTC_ZIPAES_HDR), in);
if(strncmp(key, HTC_ZIPAES_HDR, strlen(HTC_ZIPAES_HDR))) {
fprintf(stderr, "invalid htc aes encrypted zipfile\n");
FAIL(-4)
}
}
/* Read the keymap index. */
fread((void *)&keymap_index, sizeof(unsigned short), 1, in);
keymap_index = (keymap_index>>8) | (keymap_index & 0xFF);
/* Read the chunk size. */
fread(key, sizeof(char), 1, in);
chunk_size = key[0]<<20;
/* Advance to real start of zip file. */
fseek(in, 0x80 - (strlen(HTC_ZIPAES_HDR) + 2 + 1), SEEK_CUR);
/* Generate AES/IV for decryption. */
if(htc_generate_aes_keys(argv[1], keymap_index, key, iv) == 0) {
fprintf(stderr, "failed to generate htc aes keys\n");
FAIL(-5)
}
DEBUG(dump("key", key, sizeof(key));)
DEBUG(dump("iv", iv, sizeof(iv));)
/* Open the output decrypted.zip file. */
if((out = fopen(argv[3], "wb")) == NULL) {
perror("failed to open decrypted.zip destination");
FAIL(-6)
}
/* Decrypt the zip file. */
if(!htc_aes_decrypt(in, out, key, iv, chunk_size, progress_update)) {
fprintf(stderr, "failed to decrypt zip file!\n");
FAIL(-7)
}
printf("Decrypted RUU (zip) written to: %s\n", argv[3]);
end:
if(in) fclose(in);
if(out) fclose(out);
return rc;
}