Пример #1
0
//
// Main Routine
//
int main(void) {
#ifdef DEBUG
	CSerial ser;		// declare a UART object
	ser.enable();
	CDebug dbg(ser);	// Debug stream use the UART object
	dbg.start();
#endif

	//
	// Optional: Enable tickless technology
	//
#ifndef DEBUG
	CPowerSave::tickless(true);
#endif

	//
	// buffer
	//
	uint8_t *ciphertext = new uint8_t[sizeof(plaintext)];
	uint8_t *cleartext = new uint8_t[sizeof(plaintext)];

	//
	// A private key for internal used.
	// use the same key in encrypt and decrypt objects.
	aesCTR encryptCTR(private_key);
	aesCTR decryptCTR(private_key);

	aesCFB encryptCFB(private_key, AES_ENCRYPT);
	aesCFB decryptCFB(private_key, AES_DECRYPT);

	uint32_t i;

	// create new random nonce block (public key)
	// assign the nonce block to decrypt object. it seem to exchange a random public key.
	decryptCTR = encryptCTR.new_nonce();

	// create new random IV block (public key)
	decryptCFB = encryptCFB.new_IV();

	//
    // Enter main loop.
	//
    while(1) {
    	//
    	// Debug the AES result
    	//
    	if ( dbg.available() && dbg.read()!=0x1B) {

    		//
    		// AES CTR
    		//
    		DBG("\n\nAES CTR Mode:\n");
    		// encryption
    		DBG("encryption:");
    		encryptCTR.crypt(ciphertext, plaintext, sizeof(plaintext));
    		for (i=0; i<sizeof(plaintext); i++) {
    			DBG("%02X ", ciphertext[i]);	// display the ciphertext contents
    		}

    		// decryption
    		DBG("\ndecryption:");
    		decryptCTR.crypt(cleartext, ciphertext, sizeof(plaintext));
    		for (i=0; i<sizeof(plaintext); i++) {
    			DBG("%02X ", cleartext[i]);		// display the cleartext contents
    		}

    		//
    		// AES CFB
    		//
    		DBG("\n\nAES CFB Mode:\n");
    		// encryption
    		DBG("encryption:");
    		encryptCFB.crypt(ciphertext, plaintext, sizeof(plaintext));
    		for (i=0; i<sizeof(plaintext); i++) {
    			DBG("%02X ", ciphertext[i]);	// display the ciphertext contents
    		}

    		// decryption
    		DBG("\ndecryption:");
    		decryptCFB.crypt(cleartext, ciphertext, sizeof(plaintext));
    		for (i=0; i<sizeof(plaintext); i++) {
    			DBG("%02X ", cleartext[i]);		// display the cleartext contents
    		}
    	}
    	sleep(100);	// give idle time for DFU button check.
    }
}
Пример #2
0
void main( const int argc, const char *argv[] )
	{
	FILE *inFilePtr, *dictFilePtr;
	BYTE cipherText[ BLOCKSIZE ], buffer[ BLOCKSIZE ];
	char key[ MAX_KEYLENGTH + 1 ];
	int count, keyLength;
	LONG salt;

	if( argc == 3 )
		{
		if( ( inFilePtr = fopen( argv[ 1 ], "rb" ) ) == NULL )
			{
			perror( argv[ 1 ] );
			exit( ERROR );
			}
		if( ( dictFilePtr = fopen( argv[ 2 ], "r" ) ) == NULL )
			{
			perror( argv[ 2 ] );
			exit( ERROR );
			}
		}
	else
		{
		puts( "Usage: ncrack <data file> <dictionary file>" );
		exit( ERROR );
		}

	initNSEA();

	/* Read in salt */
	if( fread( buffer, 1, 8, inFilePtr ) != 8 || \
		memcmp( buffer, NSEA_ID, 4 ) )
		{
		puts( "Not an NSEA-encrypted file" );
		exit( ERROR );
		}
	salt = ( ( LONG ) buffer[ 4 ] << 24 ) | \
		   ( ( LONG ) buffer[ 5 ] << 16 ) | \
		   ( ( LONG ) buffer[ 6 ] << 8 ) | \
					  buffer[ 7 ];

	/* Read in up to BLOCKSIZE bytes of input file */
	if( ( count = fread( cipherText, 1, BLOCKSIZE, inFilePtr ) ) < BLOCKSIZE )
		{
		puts( "Can't read input data" );
		exit( ERROR );
		}
	fclose( inFilePtr );

	while( TRUE )
		{
		/* Read in and initialize key */
		if( fgets( key, MAX_KEYLENGTH, dictFilePtr ) == NULL )
			{
			puts( "ncrack: Exhausted dictionary" );
			break;
			}
		if( ( keyLength = strlen( key ) - 1 ) < MIN_KEYLENGTH )
			/* Don't bother with too-short keys */
			continue;
		initSBoxes( ( BYTE * ) key, keyLength, salt );
		initIV( DEFAULT_SALT );

		/* Decrypt data and check if we have a match with the plaintext */
		memcpy( buffer, cipherText, count );
		decryptCFB( buffer, count );
		if( !memcmp( buffer, KNOWN_PLAINTEXT, KNOWN_PLAINTEXT_LEN ) )
			{
			printf( "ncrack: Found key: %s", key );
			break;
			}
		}

	fclose( dictFilePtr );

	endNSEA();
	}