Esempio n. 1
0
File: motet.c Progetto: cckayne/MOTE
int main(int argc, char *argv[])
{
	// stir depth and nonce length
	ub4 dep, sdep = MAXM, lnce = NLEN;
	ub4 rounds = 7;
	#ifdef MOTE-REPO
	enum CSPRNG rng = MOTE8;
	enum CSPRNG hasher = BB512;
	#else
	#ifdef BB-REPO
	enum CSPRNG rng = BB128;
	enum CSPRNG hasher = MOTE32;	
	#else
	enum CSPRNG rng = MOTE32;
	enum CSPRNG hasher = BB512;
	#endif
	#endif
	enum ciphermode cmode   = cmNone;
	enum ciphertype ctype	= ctNone;
	enum outputform oform	= ofASC;
	// input: message & key-phrase
	char msg[MAXM] = ""; 
	char key[MAXM] = "";
	// ciphertext & plaintext
	char ctx[MAXM], ptx[MAXM];
	// derived & stretched key
	char kdf[MAXK] = "";
	// IV/nonce
	char nce[MAXK] = "";
	// check the command line
	if (argc >= 5) {
		if ((argc>=2) && strlen(argv[1])<MAXM) strcpy(msg,argv[1]);
		if ((argc>=3) && strlen(argv[1])<MAXK) strcpy(key,argv[2]);
		if (argc>=4)
			if ((strcmp(argv[3],"d")==0) || (strcmp(argv[3],"D")==0))
				 cmode = cmDecipher; else
			if ((strcmp(argv[3],"e")==0) || (strcmp(argv[3],"E")==0)) 
				 cmode = cmEncipher; else cmode = cmNone;
		if (argc>=5)
			#ifdef NEVER
			if ((strcmp(argv[4],"v")==0) || (strcmp(argv[4],"V")==0))
				 ctype = ctVernam; else
			#endif
			if ((strcmp(argv[4],"c")==0) || (strcmp(argv[4],"C")==0))
				 ctype = ctCaesar; else
			if ((strcmp(argv[4],"m")==0) || (strcmp(argv[4],"M")==0)) 
				ctype = ctCaesarM; else ctype = ctNone;
		if (argc>=6) 
			if ((strcmp(argv[5],"a")==0) || (strcmp(argv[5],"A")==0))
				oform = ofASC; else oform = ofHEX;
		if (argc>=7) rng = (enum CSPRNG)(atoi(argv[6]) % 7);
	}
	// sanity checks
	if (TRUE) {
		if ((strlen(msg)<MINM) || (strlen(key)<MINK)) { info(); exit(0); }
		if ((cmode==cmNone) || (ctype==ctNone))       { info(); exit(0); }
		// only hex output available for Vernam
		if (ctype==ctVernam) oform=ofHEX;
		// output mode MOD 26? (not possible with Vernam)
		if ((oform==ofASC) && (ctype!=ctVernam)) { MOD=26; START='A'; }
		// no nonce scrambling or mixing available with hex output
		if (oform==ofHEX) SCRAMBLER=NONCE=MIX=FALSE;
	}
	// B E G I N  P R E P A R A T I O N
	// preliminary seeding
	rSeedAll(key,rounds);
	
	if (SCRAMBLER) {
		sdep = SetDepth(rng,strlen(key));
		#ifdef LOG
		char tmp[12]=""; sprintf(tmp,"%d",sdep);
		log_add("RNG",rName(rng));
		log_add("HSH",rName(hasher));
		log_add("DEP",tmp);
		#endif
	}
	
	if (NONCE) {
		// obtain nonce/IV hash of fixed or random length
		strcpy(nce,rNonce(hasher,FALSE));
		// note nonce length for later
		lnce = strlen(nce);
	}

	// Key-derivation starts:
	if (TRUE) {
		// 1) seed MOTE with a key-derived hash
		strcpy(kdf,rHash(hasher,key,rStateSize(rng)*4));
		rSeed(rng,kdf,rounds);
		// 2) calculate stir-depth
		dep = rDepth(rng,kdf);
		// 3) warm up MOTE with <dep> rounds
		rStir(rng,dep); 
	}
	#ifdef TEST
		#ifdef LOG
		log_add("DKY",leftstr(kdf,LINE));
		#endif
	#endif
	// Key-derivation ends.
	
	if (SCRAMBLER) {
		// prepare scrambler's random pool
		RandPool_Fill(rng);
	}
	// E N D  P R E P A R A T I O N.
	
	// B E G I N  M A I N  C I P H E R  S E Q U E N C E
	// Mode: Encipher
	if (cmode==cmEncipher) {
		// pre-process message if output is mod 26
		if (oform==ofASC) strcpy(msg, PreProcessText(msg));
			#ifdef LOG
			if (oform==ofASC) log_add("MSG",msg);
			#endif
		// Encrypt: Vernam XOR
		if (ctype==ctVernam)  strcpy(ctx, Vernam(rng,msg));
		// Encrypt: Caesar MOD
		if (ctype==ctCaesar)  strcpy(ctx, rCaesarStr(rng, cmEncipher, msg, MOD, START));
		// Encrypt: Caesar MIX
		if (ctype==ctCaesarM) strcpy(ctx, rmCaesarStr(rng, cmEncipher, msg, MOD, START));
		// convert to hexadecimal as appropriate
		if (oform==ofHEX) strcpy(ctx,ascii2hex(ctx));
			#ifdef LOG
			log_add(" CT",ctx);
			#endif
		if (MIX) {
			// Mix: Vigenere-cipher the ciphertext on the nonce
			strcpy(ctx,Vig(rng,cmode,ctx,nce,MOD,START,FALSE));
				#ifdef LOG
				log_add("NCE",nce);
				log_add("VCT",ctx);
				#endif
		}
		if (NONCE) {
			// append ciphertext to nonce
			strcat(nce,ctx); strcpy(ctx,nce);
				#ifdef LOG
				log_add("NCT",ctx);
				#endif
		}
		if (SCRAMBLER) {
			// prepare scrambler context & scramble ciphertext
			InitRandPairs(rng,sdep,strlen(ctx));
			strcpy(ctx,Scrambled(ctx,sdep));
				#ifdef LOG
				log_add("SCT",ctx);
				#endif
		}
	}
	// Mode: Decipher
	if (cmode==cmDecipher) {
		// Convert hexadecimal ciphertext to ASCII (not in mod 26)
		if (oform==ofHEX) strcpy(ctx, hex2ascii(msg)); else strcpy(ctx, msg);
		if (SCRAMBLER) {
				#ifdef LOG
				log_add("SCT",ctx);
				#endif
			// prepare scrambler context & unscramble ciphertext
			InitRandPairs(rng,sdep,strlen(ctx));
			strcpy(ctx,unScrambled(ctx,sdep));
				#ifdef LOG
				log_add("UST",ctx);
				#endif
		}
		if (NONCE) {
			// detach ciphertext from nonce
			strcpy(nce,leftstr(ctx,lnce));
			strcpy(ctx,rightstr(ctx,strlen(msg)-lnce));
				#ifdef LOG
				log_add("VCT",ctx);
				log_add("NCE",nce);
				#endif
		}
		if (MIX) {
			// Un-mix: Vigenere-decipher the ciphertext on the nonce
			strcpy(ctx,Vig(rng,cmode,ctx,nce,MOD,START,FALSE));
				#ifdef LOG
				log_add("UVC",ctx);
				#endif
		}
		// Decrypt: Vernam XOR
		if (ctype==ctVernam) strcpy(ptx, Vernam(rng,ctx));
		// Decrypt: Caesar MOD
		if (ctype==ctCaesar)  strcpy(ptx, rCaesarStr(rng, cmDecipher, ctx, MOD, START));
		// Decrypt: Caesar MIX
		if (ctype==ctCaesarM) strcpy(ptx, rmCaesarStr(rng, cmDecipher,ctx, MOD, START));
		// post-process plaintext if output is mod 26
		if (oform==ofASC) strcpy(ptx, PostProcessText(ptx));
	}  
	// E N D  M A I N  C I P H E R  S E Q U E N C E .
	#ifdef LOG
	log_show ();
	log_clear();
	#endif
	// P R O G R A M  O U T P U T
	if ((strcmp(ptx,"") != 0) || (strcmp(ctx,"") != 0)) {
		// Mode: Encipher
		if (cmode==cmEncipher) puts(ctx);
		// Mode: Decipher
		if (cmode==cmDecipher) puts(ptx);
		
		// belt'n'braces memory wipe
		if (TRUE) {
			rResetAll(); rResetAll();
			memset(msg,0,sizeof(msg));memset(msg,0xFF,sizeof(msg));memset(msg,0,sizeof(msg));
			memset(key,0,sizeof(key));memset(key,0xFF,sizeof(key));memset(key,0,sizeof(key));
			memset(kdf,0,sizeof(kdf));memset(kdf,0xFF,sizeof(kdf));memset(kdf,0,sizeof(kdf));
			memset(ctx,0,sizeof(ctx));memset(ctx,0xFF,sizeof(ctx));memset(ctx,0,sizeof(ctx));
			memset(ptx,0,sizeof(ptx));memset(ptx,0xFF,sizeof(ptx));memset(ptx,0,sizeof(ctx));
			dep=0;
		}
		
	} else info();

	return 0;
}
Esempio n. 2
0
QString VirtualChannelMapSubtable::toStringXML(uint indent_level) const
{
    QString indent_0 = xml_indent(indent_level);
    QString indent_1 = xml_indent(indent_level + 1);
    QString indent_2 = xml_indent(indent_level + 2);
    QString str = indent_0 + "<DefinedChannelsMap ";
    str += QString("number_of_vc_records=\"%1\"")
        .arg(NumberOfVCRecords());
    str += "\n" + indent_1;
    str += QString("descriptors_included=\"%1\" ")
        .arg(xml_bool_to_string(DescriptorsIncluded()));
    str += QString("splice=\"%1\" ")
        .arg(xml_bool_to_string(Splice()));
    str += "\n" + indent_1;
    str += QString("activation_time=\"%1\" actication_time_desc=\"%2\"")
        .arg(ActivationTimeRaw())
        .arg(ActivationTimeUTC().toString(Qt::ISODate));
    str += ">\n";

    for (uint i = 0; i < NumberOfVCRecords(); i++)
    {
        str += indent_1 + "<Channel ";
        str += QString("virtual_channel_number=\"%1\" ")
            .arg(VirtualChannelNumber(i));
        str += "\n" + indent_2;
        str += QString("application_virtual_channel=\"%1\" ")
            .arg(xml_bool_to_string(ApplicationVirtualChannel(i)));
        str += QString("path_select=\"%1\" ").arg(PathSelect(i));
        str += "\n" + indent_2;
        str += QString("transport_type=\"%1\" transport_type_desc=\"%2\" ")
            .arg(TransportType(i)).arg(TransportTypeString(i));
        str += "\n" + indent_2;
        str += QString("channel_type=\"%1\" channel_type_desc=\"%2\" ")
            .arg(ChannelType(i)).arg(ChannelTypeString(i));
        if (ApplicationVirtualChannel(i))
            str += QString("application_id=\"%1\" ").arg(ApplicationID(i));
        else
            str += QString("source_id=\"%1\" ").arg(SourceID(i));
        str += "\n" + indent_2;
        if (kMPEG2Transport == TransportType(i))
        {
            str += QString("cds_reference=\"%1\" ").arg(CDSReference(i));
            str += QString("program_number=\"%1\" ").arg(ProgramNumber(i));
            str += QString("mms_reference=\"%1\" ").arg(MMSReference(i));
        }
        else
        {
            str += QString("cds_reference=\"%1\" ").arg(CDSReference(i));
            str += QString("scrampled=\"%1\" ").arg(Scrambled(i));
            str += "\n" + indent_2;
            str += QString("video_standard=\"%1\" video_standard_desc=\"%2\" ")
                .arg(VideoStandard(i)).arg(VideoStandardString(i));
        }
        if (!DescriptorsIncluded())
        {
            str += "/>\n";
            continue;
        }

        str += "\n" + indent_2;
        str += QString("descriptors_count=\"%1\"").arg(DescriptorsCount(i));
        str += ">\n";
        
        vector<const unsigned char*> desc =
            MPEGDescriptor::Parse(Descriptors(i), DescriptorsLength(i));
        for (uint i = 0; i < desc.size(); i++)
        {
            str += MPEGDescriptor(desc[i], 300)
                .toStringXML(indent_level + 2) + "\n";
        }
        str += indent_1 + "</Channel>";
    }

    return str + indent_0 + "</DefinedChannelsMap>";
}