/* phatsig v0.1 by bushing and rmini * This software is in the public domain -- take it and make something useful! * * Requires two public-domain libraries: * libtomcrypt -- http://libtomcrypt.org/ * libtommath -- http://math.libtomcrypt.org/ * * To build: * wget http://math.libtomcrypt.org/files/ltm-0.36.tar.bz2 * wget http://libtomcrypt.org/files/crypt-1.06.tar.bz2 * tar xjvf ltm-0.36.tar.bz2 * tar xjvf crypt-1.06.tar.bz2 * cd libtommath-0.36/;make;cd .. * cd libtomcrypt-1.06/;CFLAGS="-DLTM_DESC -I../libtommath-0.36" EXTRALIBS="-L../libtommath-0.36 -ltommath" make;cd .. * gcc -Llibtommath-0.36/ -Llibtomcrypt-1.06/ -Wall -I libtomcrypt-1.06/src/headers/ -o phatsig phatsig.c -ltomcrypt -ltommath * * This code should be portable to any platform that can compile libtomcrypt * (which is apparently practically anything). However, the sig_struct_tag assumes * that an unsigned int is 32 bits, using little-endian byte ordering. Modify * appropriately if your architecture is different. */ /* Win32 Port - Visual Studio 6.0 using C++ Service Pack 5 * Modified by: VorTechS * Modified date: May 4th 2007 * Major help: sbingner, preparing for use as DLL * More help: Grimthorpe, preparing for use as importable unmanaged DLL */ #define LTM_DESC #define SIG_BUF_LEN 400 #include #include #include #include #include #include void scramble_loop(int input_byte, unsigned char *real_sig_buf, int *c, unsigned char *bitmask1, unsigned char *bitmask2); int get_md5_hash_for_file(char *filename, unsigned char *hash_buf); int get_file_size(char *filename); int bizarro_scramble(unsigned char *input, unsigned int length, unsigned char *output); unsigned char private_key_der[]={ // i found this written on scrap of paper, up the street... 0x30,0x82,0x02,0x5c,0x02,0x01,0x00,0x02,0x81,0x81,0x00,0xed,0x37,0xfb,0x9d,0x19, 0xe7,0x3e,0xa6,0xf1,0x1d,0xae,0xa2,0x15,0x3a,0x0f,0x6d,0x89,0xda,0xb7,0xb4,0x95, 0xd2,0x3d,0x8a,0xd3,0xdb,0x37,0x92,0xed,0x33,0x32,0x25,0xf3,0x30,0xea,0xdc,0xe7, 0xb4,0x48,0xb3,0xaa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,0xad,0x41,0x02,0x41,0x00,0xf9,0x34,0xef,0xe7,0x03,0x84,0xb6,0xaf,0x9d,0x42, 0x2d,0x90,0xf7,0xc4,0x44,0x87,0x36,0x07,0xac,0xbb,0x1d,0x88,0x71,0x7c,0xd9,0x82, 0x36,0xc1,0xcf,0xa8,0xf9,0x32,0x3a,0xfc,0x28,0x78,0x98,0x7d,0x61,0xf3,0xe1,0x7a, 0x92,0xb9,0x8b,0x54,0x25,0x24,0xea,0xf0,0xd8,0x8b,0x68,0x07,0xb7,0x65,0x65,0xe0, 0x51,0x92,0xdf,0x8e,0x3b,0x4b,0x02,0x41,0x00,0xf3,0xaf,0x63,0x57,0xad,0x7b,0x80, 0x81,0x6d,0xf6,0x99,0x4f,0x48,0xb0,0x81,0x46,0x9e,0xe8,0xed,0xa4,0x39,0x0a,0x7f, 0x6c,0x8d,0x10,0xb4,0x30,0x07,0x33,0x6f,0xb8,0x9f,0xbc,0x42,0x52,0xd8,0xe1,0x77, 0x54,0x25,0x21,0x51,0x7f,0x3e,0x30,0xae,0x50,0x90,0x1b,0x4d,0x8f,0x03,0x48,0x5e, 0x7b,0x5d,0x2f,0xa0,0x32,0xd7,0x6d,0x50,0x21,0x02,0x40,0x46,0x41,0x72,0x50,0xc2, 0x61,0x2b,0xcf,0x9e,0x9a,0x05,0x96,0x95,0x03,0x33,0xfe,0x9a,0x70,0xd7,0x75,0x79, 0x01,0xfa,0x5c,0xa4,0x45,0x5d,0x82,0x8e,0x34,0xa8,0xec,0x4c,0x4c,0xc2,0x6c,0x1d, 0x31,0x5b,0xe8,0x11,0x6d,0x28,0xe8,0x3e,0x2b,0x5e,0xbd,0x65,0xae,0xb8,0x9d,0xb4, 0x9a,0x58,0x4d,0xb1,0xc6,0xe7,0xb8,0x6c,0x66,0x00,0xb9,0x02,0x41,0x00,0xca,0xcf, 0x5d,0xce,0xcb,0x03,0xce,0x9f,0xe9,0xf5,0x23,0x03,0x69,0x6b,0xfd,0x2b,0xd9,0x75, 0xe2,0xd2,0x9a,0xe6,0xb1,0xc6,0x96,0x61,0x20,0x96,0x18,0x68,0x26,0xc3,0x71,0xae, 0x2d,0x44,0xe6,0x5c,0x40,0x04,0x38,0x22,0xd2,0xc1,0x23,0xf1,0x40,0x22,0xac,0xcd, 0x0c,0x6f,0xca,0x3b,0xa0,0xe6,0xeb,0x57,0x87,0xec,0xc8,0x7e,0xa2,0x01,0x02,0x40, 0x02,0x1f,0x0d,0x66,0xf3,0xbd,0x46,0x90,0x62,0x01,0x96,0x5d,0x1c,0x7c,0x46,0x2b, 0xd6,0xc2,0x91,0xa9,0xcd,0x86,0xdc,0xd1,0x9c,0x70,0xe2,0x14,0xbc,0x5c,0x82,0xae, 0x71,0x0e,0x1c,0x64,0xc9,0x05,0x70,0x5c,0xf6,0xc8,0x82,0x87,0xbb,0xcd,0xa6,0x39, 0x9c,0x14,0x9e,0xcd,0xb5,0x92,0x68,0x3d,0x50,0x1f,0x72,0x2c,0xc8,0xeb,0xc9,0x7d}; unsigned char asn1_block[128] = // this block means "i am an MD5 hash..." {0x00,0x01,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x00,0x30,0x20, 0x30,0x0c,0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05,0x05,0x00,0x04,0x10, 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; struct sig_struct_tag { unsigned int id1, id2, id3, hash_type, reserved, length; unsigned char rsa_sig[128]; unsigned int footer; } unscrambled_sig; rsa_key key; int hash_idx, prng_idx; int init_crypto() { /* register prng/hash */ if (register_prng(&sprng_desc) == -1) return 0; /* register a math library (in this case LibTomMath) */ ltc_mp = ltm_desc; if (register_hash(&md5_desc) == -1) return 0; hash_idx = find_hash("md5"); prng_idx = find_prng("sprng"); rsa_import(private_key_der,sizeof(private_key_der),&key); return 1; } DWORD sign_file(char *file_name, char *sig_file_name) { int output_length_in_bits; unsigned long out_len=128; unsigned char file_hash[16]; unsigned char * unscrambled_sig_ptr=(unsigned char *)&unscrambled_sig; unsigned char sig_buf[SIG_BUF_LEN]; FILE *outfile; if(file_name==NULL || sig_file_name==NULL) return 0; memset(sig_buf, 0, SIG_BUF_LEN); init_crypto(); if(!get_md5_hash_for_file(file_name,file_hash)) return 0; memcpy(asn1_block+128-16,file_hash,16); unscrambled_sig.id1=1; // these 3 numbers identify the key somehow out of a database unscrambled_sig.id2=2; unscrambled_sig.id3=2; unscrambled_sig.hash_type=5; // md5? unscrambled_sig.footer=0x80; // ? unscrambled_sig.length=get_file_size(file_name); rsa_exptmod(asn1_block,128,(unsigned char *)&unscrambled_sig.rsa_sig,&out_len,PK_PRIVATE,&key); output_length_in_bits=bizarro_scramble(unscrambled_sig_ptr,sizeof(unscrambled_sig),sig_buf); output_length_in_bits+=2; outfile=fopen(sig_file_name,"wb"); fwrite(&output_length_in_bits,1,4,outfile); if((output_length_in_bits%8)!=0) output_length_in_bits+=8-(output_length_in_bits%8); // round up fwrite(sig_buf,1,(output_length_in_bits/8),outfile); fclose(outfile); return 1; } int get_md5_hash_for_file(char *filename, unsigned char *hash_buf) { FILE *infile; unsigned char read_buffer[1024]; int bytes_read; hash_state md; if(!(infile=fopen(filename,"rb"))) return 0; md5_init(&md); /* add the data */ while(!feof(infile)) { bytes_read=fread(read_buffer,1,1024,infile); md5_process(&md, read_buffer, bytes_read); } fclose(infile); /* get the hash in out[0..15] */ md5_done(&md, hash_buf); return 1; } int get_file_size(char *filename) { struct stat stat_struct; stat(filename,&stat_struct); return stat_struct.st_size; } unsigned char bitmask_0[4] = { 0x00, 0x00, 0x00, 0x00 }; unsigned char bitmask_01041040[4] = { 0x01, 0x04, 0x10, 0x40 }; unsigned char bitmask_030c30c0[4] = { 0x03, 0x0c, 0x30, 0xc0 }; unsigned char bitmask_02082080[4] = { 0x02, 0x08, 0x20, 0x80 }; unsigned char bitmask_fcf3cf3f[4] = { 0xfc, 0xf3, 0xcf, 0x3f }; #define BUF_IDX(i) ((i) / 8) #define BIT_IDX(i) (((i) / 2) % 4) /* This code came to me in a dream ... or a nightmare ... */ int bizarro_scramble(unsigned char *input, unsigned int length, unsigned char *output) { int sign_bit; unsigned int i; int c; int null_count; int buf_idx, bit_idx; int cur_char; null_count = 0; c = 0; if ( length <= 0 ) return 0; for ( i = 0; i < length; i++ ) { if ( input[i] == 0 ) { null_count++; continue; } if ( null_count > 0 ) { scramble_loop(null_count, output, &c, bitmask_02082080, bitmask_030c30c0); if ( (null_count & 1) == 0 ) { c -= 2; output[BUF_IDX(c)] &= bitmask_fcf3cf3f[BIT_IDX(c)]; } null_count = 0; } cur_char = (unsigned char)input[i]; sign_bit = 0; if ( cur_char < 0 ) { sign_bit = 1; cur_char = -cur_char; } scramble_loop(cur_char + 1, output, &c, bitmask_01041040, bitmask_0); c -= 2; buf_idx = BUF_IDX(c); bit_idx = BIT_IDX(c); output[buf_idx] &= bitmask_fcf3cf3f[bit_idx]; if ( sign_bit ) { output[buf_idx] |= bitmask_030c30c0[bit_idx]; } else { output[buf_idx] |= bitmask_02082080[bit_idx]; } c += 2; } if ( null_count > 0 ) { scramble_loop(null_count, output, &c, bitmask_02082080, bitmask_030c30c0); null_count++; if ( (null_count & 1) == 0 ) c -= 2; // if ( null_count & 1 == 0 ) c -= 2; } return c; } void scramble_loop(int input_byte, unsigned char *real_sig_buf, int *c, unsigned char *bitmask1, unsigned char *bitmask2) { int bit_idx, buf_idx; // printf("scramble_loop(%d,%x,%x,%x,%x)\n",input_byte,real_sig_buf,*c,bitmask1,bitmask2); /* printf("scramble_loop(%d,%x,%x,%x,%x)\n",input_byte,real_sig_buf,*c,*((unsigned int *)bitmask1), *((unsigned int *)bitmask2)); */ while ( input_byte ) { buf_idx = BUF_IDX(*c); bit_idx = BIT_IDX(*c); if ( input_byte & 1 ) { real_sig_buf[buf_idx] |= bitmask1[bit_idx]; } else { real_sig_buf[buf_idx] |= bitmask2[bit_idx]; } *c += 2; input_byte >>= 1; // signed shift } } DWORD PASCAL phatsig_sign(const char *filename, const char *sigfilename) { return sign_file((char*)filename, (char*)sigfilename); } /* vim: set sw=4 ai et si: */