Skip to content
Browse files

Add somewhat hacky support for providing your own keydata from the f…

…ilesystem.
  • Loading branch information...
1 parent b4b5da9 commit 4d31413ca036fd635f99838a84618fc9268c2404 @kmdm committed Aug 10, 2013
Showing with 53 additions and 14 deletions.
  1. +13 −10 htckey.c
  2. +2 −1 htckey.h
  3. +38 −3 main.c
View
23 htckey.c
@@ -1,7 +1,7 @@
/* 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
@@ -34,26 +34,29 @@ static int get_keydata_offset(const char *device)
}
}
- return -1;
+ return -1;
}
-int htc_generate_aes_keys(const char *device, int keymap_offset, char *aeskey,
- char *aesiv)
+int htc_generate_aes_keys(const char *device, int keymap_offset, char *aeskey,
+ char *aesiv, char *keydata)
{
- char *keymap, *keydata;
+ char *keymap;
int i, offset;
- if((offset = get_keydata_offset(device)) == -1) {
- return 0;
+ if(keydata == NULL) {
+ if((offset = get_keydata_offset(device)) == -1) {
+ return 0;
+ }
+
+ keydata = &htc_keydata[offset * 96];
}
-
+
if((keymap_offset - 1)<<5 >= sizeof(htc_keymap)) {
return 0;
}
keymap = &htc_keymap[(keymap_offset-1)<<5];
- keydata = &htc_keydata[offset * 96];
-
+
for(i=0; i < 0x10; i++) {
aeskey[i] = keydata[keymap[i]];
aesiv[i] = keydata[keymap[i + 0x10]];
View
3 htckey.h
@@ -1,4 +1,5 @@
#ifndef HTCKEY_H
#define HTCKEY_H
-int htc_generate_aes_keys(const char *, int, char *, char *);
+#define HTC_KEYDATA_LEN 0x60
+int htc_generate_aes_keys(const char *, int, char *, char *, const char *);
#endif
View
41 main.c
@@ -46,6 +46,7 @@ static struct {
char device[32];
char source[256];
char dest[256];
+ char keydata_file[256];
} opts = {0};
#ifdef DEBUG_ENABLED
@@ -99,6 +100,7 @@ int usage(char * const *argv)
printf("--mainver MAINVER, -m\t\tSet the mainver when encrypting\n");
printf("--key KEYINDEX, -k\t\tSet the encryption key index\n");
printf("--chunks CHUNKS, -c\t\tSet the number of encryption chunks\n\n");
+ printf("--keydata-file, -K\t\tOverride the compiled in keydata\n\n");
printf("supported devices:-\n\n");
for(ptr = htc_get_devices(); *ptr->name; ptr++) {
@@ -125,11 +127,12 @@ static int parse_opts(int argc, char * const *argv)
{ "device", required_argument, NULL, 'd' },
{ "mainver", required_argument, NULL, 'm' },
{ "info", no_argument, NULL, 'I' },
+ { "keydata-file", required_argument, NULL, 'K' },
{ 0, 0, 0, 0}
};
while(
- (c = getopt_long(argc, argv, "Ek:c:d:m:I", longopts, &index)) != -1
+ (c = getopt_long(argc, argv, "Ek:c:d:m:IK:", longopts, &index)) != -1
) switch(c) {
case 'E':
opts.encrypt = 1;
@@ -149,11 +152,15 @@ static int parse_opts(int argc, char * const *argv)
case 'I':
opts.info = 1;
break;
+ case 'K':
+ strncpy(opts.keydata_file, optarg, sizeof(opts.keydata_file));
+ break;
+
}
if(opts.encrypt && opts.info) {
return 0;
- } else if(strlen(opts.device) == 0) {
+ } else if(strlen(opts.device) == 0 && strlen(opts.keydata_file) == 0) {
fprintf(stderr, "error: --device is a required argument.\n\n");
return 0;
}
@@ -177,8 +184,11 @@ int main(int argc, char * const *argv)
{
char key[HTC_AES_KEYSIZE] = {0};
char iv[HTC_AES_KEYSIZE] = {0};
+ char *keydata = NULL;
int rc = 0;
+ FILE *fh;
+
htc_zip_header_t header;
// FIXME: Better way to set these defaults.
@@ -228,8 +238,33 @@ int main(int argc, char * const *argv)
strncpy(header.mainver, opts.mainver, sizeof(header.mainver));
}
+ if(strlen(opts.keydata_file) > 0) {
+ if(!(keydata = malloc(HTC_KEYDATA_LEN))) {
+ fprintf(stderr, "failed to allocate memory for keydata!\n");
+ FAIL(-8)
+ }
+
+ if(!(fh = fopen(opts.keydata_file, "rb"))) {
+ fprintf(stderr, "failed to open keydata file: %s\n",
+ opts.keydata_file);
+ FAIL(-9)
+ }
+
+ if(fread(keydata,sizeof(char),HTC_KEYDATA_LEN,fh) != HTC_KEYDATA_LEN) {
+ fprintf(stderr, "failed to read keydata file: %s\n",
+ opts.keydata_file);
+ fclose(fh);
+ FAIL(-10)
+ }
+
+ fclose(fh);
+ }
+
/* Generate AES/IV for decryption. */
- if(htc_generate_aes_keys(opts.device, header.keymap_index, key, iv) == 0) {
+ if(htc_generate_aes_keys(opts.device,
+ header.keymap_index,
+ key, iv, keydata) == 0
+ ) {
fprintf(stderr, "failed to generate htc aes keys\n");
FAIL(-5)
}

1 comment on commit 4d31413

@kmdm
Owner
kmdm commented on 4d31413 Aug 10, 2013

Anyone looking at this commit might want to take a look at: http://psi.kennynet.co.uk/software/ruuveal/keydata/

(Yes this is a stopgap measure until I can implement a better solution.)

Please sign in to comment.
Something went wrong with that request. Please try again.