super happy dickhouse productions
authorinfamouspat <itgpmc@gmail.com>
Sun, 23 Oct 2011 05:52:01 +0000 (22:52 -0700)
committerinfamouspat <itgpmc@gmail.com>
Sun, 23 Oct 2011 05:59:58 +0000 (22:59 -0700)
assets/utilities/itg2-util/src/itg2-swiss-army-knife.c
assets/utilities/itg2-util/src/itg2util.c
assets/utilities/itg2-util/src/itg2util.h

index 61016ea..50348e4 100644 (file)
@@ -21,35 +21,29 @@ int main(int argc, char **argv) {
        int option_index = 0;
        while (1) {
                static struct option long_options[] = {
-                       {"data", no_argument, 0, 'x'},
                        {"patch", no_argument, 0, 'p'},
                        {"decrypt", no_argument, 0, 'd'},
                        {"static", required_argument, 0, 's'},
-                       /*{"source", required_argument, 0, 'i'},
-                       {"dest",  required_argument, 0, 'o'},*/
+                       {"help", no_argument, 0, 'h'},
                        {0, 0, 0, 0}
                };
 
-               c = getopt_long(argc, argv, "xpds:", long_options, &option_index);
+               c = getopt_long(argc, argv, "hxpds:", long_options, &option_index);
                option_index++;
                if (c == -1) break;
 
                switch(c) {
                case 0:
-                       /*if (!strcmp(long_options[option_index].name,"source") && optarg) {
-                               openFile = optarg;
-                       } else if (!strcmp(long_options[option_index].name,"dest") && optarg) {
-                               destFile = optarg;
-                       } else */
-                       if (!strcmp(long_options[option_index].name,"data")) {
-                               type = KEYDUMP_ITG2_FILE_DATA;
-                       } else if (!strcmp(long_options[option_index].name,"patch")) {
+                       if (!strcmp(long_options[option_index].name,"patch")) {
                                type = KEYDUMP_ITG2_FILE_PATCH;
                        } else if (!strcmp(long_options[option_index].name,"static")) {
-                               type = KEYDUMP_ITG2_FILE_STATIC;
                                keyFile = optarg;
+                               ++option_index;
                        } else if (!strcmp(long_options[option_index].name,"decrypt")) {
                                direction = 1;
+                       } else if (!strcmp(long_options[option_index].name,"help")) {
+                               printHelp(argv[0]);
+                               exit(0);
                        }
                        break;
 
@@ -66,19 +60,13 @@ int main(int argc, char **argv) {
                        break;
 
                case 's':
-                       type = KEYDUMP_ITG2_FILE_STATIC;
                        keyFile = optarg;
+                       ++option_index;
                        break;
 
-/*
-               case 'i':
-                       openFile = optarg;
-                       break;
-
-               case 'o':
-                       destFile = optarg;
-                       break;
-*/
+               case 'h':
+                       printHelp(argv[0]);
+                       exit(0);
 
                default:
                        return -1;
@@ -110,22 +98,52 @@ int main(int argc, char **argv) {
        }
 
        // encrypt
-       if (direction == 0) {
+       if (direction == 0 && keyFile != NULL) {
+               // TODO: everything about this is terrible -- should be moved into itg2util.c somewhere
+               FILE *fd, *dfd, *kf;
+               char magic[2];
+               unsigned int fileSize = 0, subkeySize = 0, totalBytes = 0, numCrypts = 0, padMisses = 0, got = 0;
+               unsigned char *subkey, aesKey[24], verifyBlock[16], plaintext[16], backbuffer[16];
+
+               if ((fd = fopen(openFile, "rb")) == NULL) {
+                       fprintf(stderr, "%s: fopen(%s) failed D=\n", __FUNCTION__, openFile);
+                       return -1;
+               }
+
+               fread(magic, 1, 2, fd);
+               fread(&fileSize, 1, 4, fd);
+               fread(&subkeySize, 1, 4, fd);
+               subkey = (unsigned char*)malloc(subkeySize * sizeof(unsigned char));
+               got = fread(subkey, 1, subkeySize, fd);
+               if (keydump_itg2_retrieve_aes_key(subkey, 1024, aesKey, type, direction, keyFile) == -1) {
+                       fprintf(stderr, "%s: could not retrieve AES key, exiting...\n", argv[0]);
+                       return -1;
+               }
+               kf = fopen(keyFile, "wb");
+               if ( kf == NULL ) {
+                       fprintf(stderr, "%s: could not open key file for output, exiting...\n", argv[0]);
+                       return -1;
+               }
+               fwrite(aesKey, 1, 24, kf);
+               fclose(kf);
+       } else if (direction == 0) {
                int i;
                for (i = 0; i < 1024; i++)
                        subkey[i] = (unsigned char)rand() * 255;
-
-               if (keydump_itg2_retrieve_aes_key(subkey, 1024, aesKey, type, keyFile) == -1) {
+               
+               if (keydump_itg2_retrieve_aes_key(subkey, 1024, aesKey, type, direction, keyFile) == -1) {
                        fprintf(stderr, "%s: could not retrieve AES key, exiting...\n", argv[0]);
                        return -1;
                }
 
-               if (keydump_itg2_encrypt_file(openFile, destFile, subkey, 1024, aesKey, type) == -1) {
+               if (keydump_itg2_encrypt_file(openFile, destFile, subkey, 1024, aesKey, type, keyFile) == -1) {
                        fprintf(stderr, "%s: could not encrypt %s, exiting...\n", argv[0], openFile);
                        return -1;
                }
+
        // decrypt
        } else {
+               printf("keyFile before: %s\n", keyFile);
                if (keydump_itg2_decrypt_file(openFile, destFile, type, keyFile) == -1) {
                        fprintf(stderr, "%s: could not decrypt %s, exiting...\n", argv[0], openFile);
                        return -1;
@@ -138,8 +156,6 @@ int main(int argc, char **argv) {
 void printHelp( const char *argv0 ) {
        printf("Usage: %s -f <source file> -w <dest file> [extra args]\n", argv0);
        printf("\t--decrypt (-d)\tDecrypt mode\n\n");
-       //printf("\t--source (-f)\tSource File (required argument)\n");
-       //printf("\t--dest (-w)\tDestination File (required argument)\n\n");
        printf("\t--data (-f)\tTreat source file as data file (default)\n");
        printf("\t--patch (-p)\tTreat source file as patch file\n");
        printf("\t--static (-s)\tStatic Encryption/Decryption: AES key is in a separate file (required argument as key file)\n\n");
index c0de3c2..3c20d20 100644 (file)
@@ -7,7 +7,7 @@
 #include "itg2util.h"
 #include "itg2-patch-constants.h"
 
-int keydump_itg2_encrypt_file(const char *srcFile, const char *destFile, const unsigned char *subkey, const unsigned int subkeySize, const unsigned char *aesKey, const int type) {
+int keydump_itg2_encrypt_file(const char *srcFile, const char *destFile, const unsigned char *subkey, const unsigned int subkeySize, const unsigned char *aesKey, const int type, char *keyFile) {
        FILE *fd, *dfd;
        unsigned int numcrypts, got, fileSize;
        int i,j;
@@ -76,6 +76,7 @@ int keydump_itg2_decrypt_file(const char *srcFile, const char *destFile, int typ
        aes_decrypt_ctx ctx[1];
        int i, j, got, numcrypts;
 
+       printf("keyFile after: %s\n", keyFile);
        if ((fd = fopen(srcFile, "rb")) == NULL) {
                fprintf(stderr, "%s: fopen(%s) failed D=\n", __FUNCTION__, srcFile);
                return -1;
@@ -95,7 +96,8 @@ int keydump_itg2_decrypt_file(const char *srcFile, const char *destFile, int typ
        printf("first 4 bytes of subkey: %02x %02x %02x %02x\n", subkey[0], subkey[1], subkey[2], subkey[3]);
        fread(verifyBlock, 1, 16, fd);
 
-       if ( keydump_itg2_retrieve_aes_key(subkey, subkeySize, aesKey, type, keyFile) == -1 ) {
+       printf("keyFile before retrieve_aes_key: %s\n", keyFile);
+       if ( keydump_itg2_retrieve_aes_key(subkey, subkeySize, aesKey, type, 1, keyFile) == -1 ) {
                fprintf(stderr, "%s: could not retrieve AES key\n", __FUNCTION__);
                fclose(fd);
                return -1;
@@ -151,48 +153,47 @@ int keydump_itg2_decrypt_file(const char *srcFile, const char *destFile, int typ
        return padMisses;
 }
 
-int keydump_itg2_retrieve_aes_key(const unsigned char *subkey, const unsigned int subkeySize, unsigned char *out, int type, const char *keyFile) {
+int keydump_itg2_retrieve_aes_key(const unsigned char *subkey, const unsigned int subkeySize, unsigned char *out, int type, int direction, const char *keyFile) {
        FILE *kfd;
        unsigned char *SHAworkspace, *SHAout;
-       
-       switch (type) {
-       case KEYDUMP_ITG2_FILE_DATA:
-               if (getKey(subkey, out, KEYDUMP_GETKEY_ITG2, subkeySize) != 0) {
-                       fprintf(stderr, "%s: getKey() failed\n", __FUNCTION__);
-                       return -1;
-               }
-               break;
-
-       case KEYDUMP_ITG2_FILE_PATCH:
-               SHAworkspace = (unsigned char*)malloc(sizeof(unsigned char) * (subkeySize+47));
-               SHAout = (unsigned char*)malloc(sizeof(unsigned char) * 64);
-               memcpy(SHAworkspace, subkey, subkeySize);
-               memcpy(SHAworkspace+subkeySize, ITG2SubkeySalt, 47);
-               gcry_md_hash_buffer(GCRY_MD_SHA512, SHAout, SHAworkspace, subkeySize+47);
-               memcpy(out, SHAout, 24);
-               free(SHAworkspace); free(SHAout);
-               break;
-
-       case KEYDUMP_ITG2_FILE_STATIC:
-               if (keyFile == NULL) {
-                       fprintf(stderr, "%s: no key file given\n", __FUNCTION__);
-               }
+       int numread = 0;
+
+       printf("keyFile after retrieve_aes_key: %s\n", keyFile);
+       if ( direction == 1 && keyFile != NULL ) {      
                kfd = fopen(keyFile, "rb");
                if (kfd == NULL) {
                        fprintf(stderr, "%s: cannot open key file (you sure you typed the right file?)\n", __FUNCTION__);
                        return -1;
                }
-               if (fread(out, 1, 24, kfd) < 24) {
+               if ((numread = fread(out, 1, 24, kfd)) < 24) {
                        fprintf(stderr, "%s: unexpected key file size (sure it\'s the right file?)\n", __FUNCTION__);
                        fclose(kfd);
                        return -1;
                }
+               printf("numread: %d\n", numread);
                fclose(kfd);
-               break;
-
-       default:
-               fprintf("%s: huh?\n", __FUNCTION__);
-               return -1;
+       } else {
+               switch (type) {
+               case KEYDUMP_ITG2_FILE_DATA:
+                       if (getKey(subkey, out, KEYDUMP_GETKEY_ITG2, subkeySize) != 0) {
+                               fprintf(stderr, "%s: getKey() failed\n", __FUNCTION__);
+                               return -1;
+                       }
+                       break;
+
+               case KEYDUMP_ITG2_FILE_PATCH:
+                       SHAworkspace = (unsigned char*)malloc(sizeof(unsigned char) * (subkeySize+47));
+                       SHAout = (unsigned char*)malloc(sizeof(unsigned char) * 64);
+                       memcpy(SHAworkspace, subkey, subkeySize);
+                       memcpy(SHAworkspace+subkeySize, ITG2SubkeySalt, 47);
+                       gcry_md_hash_buffer(GCRY_MD_SHA512, SHAout, SHAworkspace, subkeySize+47);
+                       memcpy(out, SHAout, 24);
+                       free(SHAworkspace); free(SHAout);
+                       break;
+               default:
+                       fprintf("%s: huh?\n", __FUNCTION__);
+                       return -1;
+               }
        }
 
        return 0;
index fa1c50d..a712e03 100644 (file)
@@ -5,8 +5,8 @@
 #define KEYDUMP_ITG2_FILE_PATCH 1
 #define KEYDUMP_ITG2_FILE_STATIC 2
 
-int keydump_itg2_encrypt_file(const char *srcFile, const char *destFile, const unsigned char *subkey, const unsigned int subkeySize, const unsigned char *aesKey, const int type);
+int keydump_itg2_encrypt_file(const char *srcFile, const char *destFile, const unsigned char *subkey, const unsigned int subkeySize, const unsigned char *aesKey, const int type, char *keyFile);
 int keydump_itg2_decrypt_file(const char *srcFile, const char *destFile, int type, const char *keyFile);
-int keydump_itg2_retrieve_aes_key(const unsigned char *subkey, const unsigned int subkeySize, unsigned char *out, int type, const char *keyFile);
+int keydump_itg2_retrieve_aes_key(const unsigned char *subkey, const unsigned int subkeySize, unsigned char *out, int type, int direction, const char *keyFile);
 
 #endif /* ITG2UTIL_H */