Ejemplo n.º 1
0
int main(int argc, char **argv) {
	
	if (argc < 6 || argc > 6) {
		std::cout << "Usage: ./prog --encrypt --files_dir= --keys_dir=" << std::endl
			      << "or ./prog --decrypt --file_dir= --key-dir=" << std::endl;
		return 0;
	}

	Cipher cipher;

	if (!strcmp(argv[1], "--encrypt")) {

		path filesDir(argv[3]);
		path keysDir(argv[5]);

		cipher.groupUp(filesDir);
		cipher.encrypt(filesDir, keysDir);
	}

	else if (!strcmp(argv[1], "--decrypt")) {

		path fileDir(argv[3]);
		path keyDir(argv[5]);

		cipher.decrypt(fileDir, keyDir);
	}
	 
	else {
		std::cout << "Usage: ./prog --encrypt --files_dir= --keys_dir=" << std::endl
			<< "or ./prog --decrypt --file_dir= --key-dir=" << std::endl;
	}


	return 0;
}
Ejemplo n.º 2
0
	/**Call Templated encode */
	void encode(istream & in, ostream & out){
		
		while(getline(in,line)){ // get line from stream
			std::transform(line.begin(), line.end(),line.begin(), ::toupper);
			out<<cp.encode(key,line,lenVec)+"\n";//encode & write char into file
		}		
	}
Ejemplo n.º 3
0
VLT_U16 CipherGetBlockSize( void )
{
    if( ST_UNKNOWN != cipherState )
    {
        return( theCipher.cipherGetBlockSize() );
    }
    return( ECPHBLKNOTSUPPORTED );
}
Ejemplo n.º 4
0
VLT_STS CipherClose( void )
{
    if( ST_UNKNOWN != cipherState )
    {
        return( theCipher.cipherClose() );
    }
    return( ECPHCLSNOTSUPPORTED );
}
Ejemplo n.º 5
0
QByteArray IrcParser::decrypt(Network *network, const QString &bufferName, const QByteArray &message, bool isTopic)
{
#ifdef HAVE_QCA2
    if (message.isEmpty())
        return message;

    if (!Cipher::neededFeaturesAvailable())
        return message;

    Cipher *cipher = qobject_cast<CoreNetwork *>(network)->cipher(bufferName);
    if (!cipher || cipher->key().isEmpty())
        return message;

    return isTopic ? cipher->decryptTopic(message) : cipher->decrypt(message);
#else
    Q_UNUSED(network);
    Q_UNUSED(bufferName);
    Q_UNUSED(isTopic);
    return message;
#endif
}
Ejemplo n.º 6
0
	/**Call Templated decode */	
	void decode(istream & in, ostream & out){
		while(getline(in,line)){ // get line from stream
			out<< cp.decode(key,line,lenVec)+"\n";//encode & write char into file
		}		
	}	
Ejemplo n.º 7
0
VLT_STS CipherInit( VLT_U8 opMode, const KEY_BLOB* pKey, VLT_PU8 pParams )
{   
    VLT_U8 keyMode ;
    VLT_STS status = VLT_FAIL;

    /**
     * Make sure we have a valid params pointer
     */
    if( NULL == pParams )
    {
        return( ECPHIIVLDPRM );
    }
    else
    {
        /**
         * Cache the parameters.
         */
        params = *((CIPHER_PARAMS*)pParams);

        if( ( params.algoID != VLT_ALG_CIP_DES ) &&
            ( params.algoID != VLT_ALG_CIP_TDES_2K_EDE ) &&
            ( params.algoID != VLT_ALG_CIP_TDES_3K_EDE ) &&
            ( params.algoID != VLT_ALG_CIP_TDES_3K_EEE ) &&
            ( params.algoID != VLT_ALG_CIP_AES ) &&
            ( params.algoID != VLT_ALG_KTS_TDES_3K_EEE ) &&
            ( params.algoID != VLT_ALG_KTS_TDES_3K_EDE ) &&
            ( params.algoID != VLT_ALG_KTS_AES ) )
        {
            /**
             * Clear the cipherState to signify the
             * fact that something has gone pear
             * shaped and we shouldn't deligate
             * any further calls to the concrete
             * cipher methods.
             */
            cipherState = ST_UNKNOWN;

            /**
             * Return the appropriate error and
             * exit gracefully. 
             */
            return( ECPHIINVLDALGO );
        }
    }

    /**
     * Set all the function pointers
     * to the actual concrete cipher 
     * methods based on the algo Id.
     */
    switch(params.algoID)
    {

    #if( VLT_ENABLE_CIPHER_DES == VLT_ENABLE )
        case VLT_ALG_CIP_DES:
            theCipher.cipherInit = DesInit;
            theCipher.cipherDoFinal = DesDoFinal;
            theCipher.cipherGetBlockSize = DesGetBlockSize;         
            theCipher.cipherUpdate = DesUpdate;
            theCipher.cipherClose = DesClose;
        break;
    #endif /* ( VLT_ENABLE_CIPHER_DES == VLT_ENABLE )*/

    #if( VLT_ENABLE_CIPHER_TDES == VLT_ENABLE )
        case VLT_ALG_CIP_TDES_2K_EDE:   
        case VLT_ALG_CIP_TDES_3K_EDE:
        case VLT_ALG_CIP_TDES_3K_EEE:
        case VLT_ALG_KTS_TDES_3K_EEE:
        case VLT_ALG_KTS_TDES_3K_EDE:
            theCipher.cipherInit = TDesInit;
            theCipher.cipherDoFinal = TDesDoFinal;
            theCipher.cipherGetBlockSize = TDesGetBlockSize;            
            theCipher.cipherUpdate = TDesUpdate;
            theCipher.cipherClose = TDesClose;
            break;
    #endif /* ( VLT_ENABLE_CIPHER_TDES == VLT_ENABLE ) */

    #if( VLT_ENABLE_CIPHER_AES == VLT_ENABLE )
        case VLT_ALG_CIP_AES:
        case VLT_ALG_KTS_AES:
            theCipher.cipherInit = AesInit;
            theCipher.cipherDoFinal = AesDoFinal;
            theCipher.cipherGetBlockSize = AesGetBlockSize;         
            theCipher.cipherUpdate = AesUpdate;
            theCipher.cipherClose = AesClose;
            break;          
    #endif/*( VLT_ENABLE_CIPHER_AES == VLT_ENABLE )*/

        default:
            return( ECPHINOTSUPPORTED );
    }
    
    /**
     * Check and setup the Keying mode.
     */
    switch(params.algoID)
    {
        case VLT_ALG_CIP_TDES_2K_EDE:
            keyMode = TDES_EDE;
            break;          
        case VLT_ALG_CIP_TDES_3K_EDE:
        case VLT_ALG_KTS_TDES_3K_EDE:
            keyMode = TDES_EDE;
            break;          
        case VLT_ALG_CIP_TDES_3K_EEE:
        case VLT_ALG_KTS_TDES_3K_EEE:
            keyMode = TDES_EEE; 
            break;
        case VLT_ALG_CIP_DES:
            /**
             * Do nothing for des the 
             * key mode is not relevant.
             */
            break;
        case VLT_ALG_CIP_AES:
        case VLT_ALG_KTS_AES:
            /**
             * Do nothing for aes the 
             * key mode is not relevant.
             */
            break;
        default:
            return( ECPHINOTSUPPORTED );
    }

    /**
     * Check the chaining mode.
     */
    switch( params.chainMode )
    {   
        case BLOCK_MODE_ECB:        
        case BLOCK_MODE_CBC:
            break;
        case BLOCK_MODE_CFB:
        case BLOCK_MODE_OFB:
        default:
            return(ECPHICHNMODE);
    }   

    /**
     * Initialise the chaining block to zeros
     */
    /*
    * No need to check the return type as pointer has been validated
    */
    (void)host_memset( chainBlock, 0x00, theCipher.cipherGetBlockSize() );
        
    /**
     * Check the padding scheme.
     */
    switch( params.paddingScheme )
    {   
        case PADDING_ISO9797_METHOD2:                   
        case PADDING_NONE:
        case PADDING_PKCS5:
        case PADDING_PKCS7:
            break;
        default:
            return(ECPHIPADUNKNOWN);
    }       

    /**
     * Cache the operationalMode, we'll need it
     * when we are doing the padding.
     */
    operationalMode = opMode;

    /**
     * Delegate the call to the initialisation method
     * of the appropriate cipher.
     */
    status = theCipher.cipherInit( opMode, pKey, &keyMode );

    /**
     * Prepare to accept the first block 
     * of data.
     */
    if( VLT_OK == status )
    {
        cipherState = ST_INITIALISED;
    }

    return( status );
}
Ejemplo n.º 8
0
VLT_STS CipherUpdate( VLT_PU8 pDataIn, 
        VLT_U32 DataInLen, 
        VLT_U32 dataInCapacity, 
        VLT_PU8 pDataOut, 
        VLT_PU32 pDataOutLen, 
        VLT_U32 dataOutCapacity )
{
    VLT_STS status = VLT_FAIL;
    VLT_U16 blockSize = 0;
    VLT_U32 byteCount = 0;
    VLT_U32 workingLen = 0;
    

    if ( ( ST_UNKNOWN == cipherState ) ||
         ( ST_FINALISED == cipherState ) )
    {
        return( ECPHUPDNOTSUPPORTED );
    }

    /**
     * Cache the block size, we'll use it 
     * frequently.
     */
    blockSize = theCipher.cipherGetBlockSize( );

    /**
     * Ensure we haven't been passed
     * null pointers by the caller.
     */
    if( ( NULL == pDataIn )     ||
        ( NULL == pDataOutLen ) ||
        ( NULL == pDataOut ) )
    {
        return( ECPHUPDNULLPARAM );
    }

    /**
     * For the CipherUpdate the capacity of
     * the buffer passed to us by the caller
     * should be equal or larger than that
     * of the data buffer length.
     */
    if( ( DataInLen > dataInCapacity ) || 
        ( DataInLen > dataOutCapacity ) )
    {
        return( ECPHUPDINVLDCPCT );
    }

    /**
     * Update only deals with data lengths
     * multiple of the block size, if the 
     * client has passed us anything else 
     * other than that then we should exit
     * gracefully-ish!
     */
    if( 0 != ( DataInLen % blockSize ) )
    {
        return( ECPHUPDINVLDLEN );
    }

    /**
     * Chunk things up in multiples of the
     * block size.
     */
    while( 0 != ( DataInLen - byteCount ) )
    {
        /*
         * Perform a copy of the input data into a temp buffer
         * this is needed to ensure the input data is not trashed
         * if CBC mode is selected.
         */
        /*
        * No need to check the return type as pointer has been validated
        */
        (void)host_memcpy( &workingBlock[0], &pDataIn[byteCount], blockSize );
        
        /**
         * Do the chaining
         */
        if( VLT_ENCRYPT_MODE == operationalMode )
        {
            if( BLOCK_MODE_CBC == params.chainMode )
            {
                if( ST_INITIALISED == cipherState )
                {
                    /*
                     * Make a copy of the IV of the first round.
                     */
                    /*
                    * No need to check the return type as pointer has been validated
                    */
                    (void)host_memcpy( chainBlock, &(params.pIV[0]), blockSize );
                    
                    cipherState = ST_UPDATED;
                }

                /*
                * No need to check the return type as pointer has been validated
                */
                (void)host_memxor( &workingBlock[0], chainBlock, blockSize );
            }
        }
        else
        {
            if( BLOCK_MODE_CBC == params.chainMode )
            {
                /*
                * No need to check the return type as pointer has been validated
                */
                (void)host_memcpy( tempBlock, &pDataIn[byteCount], blockSize );
            }
        }

        
        /**
         * Set the working length
         */
        workingLen = blockSize;


        /**
         * Do the Encrypt/Decrypt
         */
        if( VLT_OK == ( status = theCipher.cipherUpdate( 
            //&pData[byteCount], 
            &workingBlock[0], /* workingBlock is used to ensure the pDataIn is preserved */
            &workingLen,
            &pDataOut[byteCount],
            &workingLen) ) )
        {
            /**
             * It should be impossible for the block
             * cipher to return a length not equal to 
             * the blockSize, nevertheless if it does
             * exit with an appropriate error code and 
             * set the chaining block back to the IV 
             * for the next call.
             */
            if( workingLen != blockSize )
            {               
                return( ECPHUPDINVLDBLOCK );
            }                       
            
        }
        else
        {
            return( status );
        }

        /**
         * Do the chaining
         */
        if( VLT_ENCRYPT_MODE == operationalMode )
        {
            if( BLOCK_MODE_CBC == params.chainMode )
            {
                /*
                * No need to check the return type as pointer has been validated
                */
                (void)host_memcpy( chainBlock, &pDataOut[byteCount], blockSize );
            }
        }
        else
        {
            if( BLOCK_MODE_CBC == params.chainMode )
            {
                if( ST_INITIALISED == cipherState )
                {
                    /*
                    * No need to check the return type as pointer has been validated
                    */
                    (void)host_memxor( &pDataOut[byteCount], &(params.pIV[0]), blockSize );
                    cipherState = ST_UPDATED;
                }
                else
                {
                    /*
                    * No need to check the return type as pointer has been validated
                    */
                    (void)host_memxor( &pDataOut[byteCount], chainBlock, blockSize );             
                }

                /*
                * No need to check the return type as pointer has been validated
                */
                (void)host_memcpy( chainBlock, tempBlock, blockSize );
            }
        }
        
        /**
         * Update the byte count to 
         * move to the next block of data.
         */
        byteCount += workingLen;
    }       
    
    *pDataOutLen = byteCount;

    return( VLT_OK );
}
Ejemplo n.º 9
0
VLT_STS CipherDoFinal( VLT_PU8 pDataIn, 
        VLT_U32 DataInLen, 
        VLT_U32 dataInCapacity, 
        VLT_PU8 pDataOut, 
        VLT_PU32 pDataOutLen, 
        VLT_U32 dataOutCapacity )
{
    VLT_STS status = VLT_FAIL;      

    if ( ( ST_UNKNOWN == cipherState ) ||
         ( ST_FINALISED == cipherState ) )

    {
        return( ECPHDFNOTSUPPORTED );   
    }

    /**
     * Ensure we haven't been passed
     * null pointers by the caller.
     */
    if( ( NULL == pDataIn )||
        ( NULL == pDataOutLen ) ||
        ( NULL == pDataOut ) )
    {
        return( ECPHUPDNULLPARAM );
    }
    
    /**
     * Apply the padding if we have been called to
     * encrypt data.
     */
    if( ( VLT_ENCRYPT_MODE == operationalMode ) && 
        ( PADDING_NONE != params.paddingScheme ) )
    {
        status = PaddingAdd( params.paddingScheme, 
            theCipher.cipherGetBlockSize(), 
            pDataIn,
            &DataInLen, 
            dataInCapacity );
    }
    else
    {
        status = VLT_OK;
    }

    /**
     * Process the data, encrypt/decrypt
     */
    if( VLT_OK == status )
    {
        status = CipherUpdate( 
            pDataIn, 
            DataInLen,
            dataInCapacity,
            pDataOut, 
            pDataOutLen,
            dataOutCapacity);
    }
    
    
    if( VLT_OK == status )
    {
        if( VLT_DECRYPT_MODE == operationalMode )
        {
            status = PaddingRemove( params.paddingScheme, 
                theCipher.cipherGetBlockSize(), 
                pDataOut, 
                pDataOutLen );
        }
    }

    /**
     * Set the appropriate cipher state;
     */ 
    cipherState = ST_FINALISED;

    return( status );
}
Ejemplo n.º 10
0
CBC::CBC(Cipher& f, NTuple<symbol>& IV) : m_f(f), m_lastblock(f.GetSize()) 
{
	Copy(m_lastblock,IV,m_f.GetSize());
}
Ejemplo n.º 11
0
	void EncryptionModeXTS::EncryptBufferXTS (const Cipher &cipher, const Cipher &secondaryCipher, byte *buffer, uint64 length, uint64 startDataUnitNo, unsigned int startCipherBlockNo) const
	{
		byte finalCarry;
		byte whiteningValues [ENCRYPTION_DATA_UNIT_SIZE];
		byte whiteningValue [BYTES_PER_XTS_BLOCK];
		byte byteBufUnitNo [BYTES_PER_XTS_BLOCK];
		uint64 *whiteningValuesPtr64 = (uint64 *) whiteningValues;
		uint64 *whiteningValuePtr64 = (uint64 *) whiteningValue;
		uint64 *bufPtr = (uint64 *) buffer;
		uint64 *dataUnitBufPtr;
		unsigned int startBlock = startCipherBlockNo, endBlock, block;
		uint64 *const finalInt64WhiteningValuesPtr = whiteningValuesPtr64 + sizeof (whiteningValues) / sizeof (*whiteningValuesPtr64) - 1;
		uint64 blockCount, dataUnitNo;

		startDataUnitNo += SectorOffset;

		/* The encrypted data unit number (i.e. the resultant ciphertext block) is to be multiplied in the
		finite field GF(2^128) by j-th power of n, where j is the sequential plaintext/ciphertext block
		number and n is 2, a primitive element of GF(2^128). This can be (and is) simplified and implemented
		as a left shift of the preceding whitening value by one bit (with carry propagating). In addition, if
		the shift of the highest byte results in a carry, 135 is XORed into the lowest byte. The value 135 is
		derived from the modulus of the Galois Field (x^128+x^7+x^2+x+1). */

		// Convert the 64-bit data unit number into a little-endian 16-byte array. 
		// Note that as we are converting a 64-bit number into a 16-byte array we can always zero the last 8 bytes.
		dataUnitNo = startDataUnitNo;
		*((uint64 *) byteBufUnitNo) = Endian::Little (dataUnitNo);
		*((uint64 *) byteBufUnitNo + 1) = 0;

		if (length % BYTES_PER_XTS_BLOCK)
			TC_THROW_FATAL_EXCEPTION;

		blockCount = length / BYTES_PER_XTS_BLOCK;

		// Process all blocks in the buffer
		while (blockCount > 0)
		{
			if (blockCount < BLOCKS_PER_XTS_DATA_UNIT)
				endBlock = startBlock + (unsigned int) blockCount;
			else
				endBlock = BLOCKS_PER_XTS_DATA_UNIT;

			whiteningValuesPtr64 = finalInt64WhiteningValuesPtr;
			whiteningValuePtr64 = (uint64 *) whiteningValue;

			// Encrypt the data unit number using the secondary key (in order to generate the first 
			// whitening value for this data unit)
			*whiteningValuePtr64 = *((uint64 *) byteBufUnitNo);
			*(whiteningValuePtr64 + 1) = 0;
			secondaryCipher.EncryptBlock (whiteningValue);

			// Generate subsequent whitening values for blocks in this data unit. Note that all generated 128-bit
			// whitening values are stored in memory as a sequence of 64-bit integers in reverse order.
			for (block = 0; block < endBlock; block++)
			{
				if (block >= startBlock)
				{
					*whiteningValuesPtr64-- = *whiteningValuePtr64++;
					*whiteningValuesPtr64-- = *whiteningValuePtr64;
				}
				else
					whiteningValuePtr64++;

				// Derive the next whitening value

#if BYTE_ORDER == LITTLE_ENDIAN

				// Little-endian platforms

				finalCarry = 
					(*whiteningValuePtr64 & 0x8000000000000000ULL) ?
					135 : 0;

				*whiteningValuePtr64-- <<= 1;

				if (*whiteningValuePtr64 & 0x8000000000000000ULL)
					*(whiteningValuePtr64 + 1) |= 1;	

				*whiteningValuePtr64 <<= 1;
#else

				// Big-endian platforms

				finalCarry = 
					(*whiteningValuePtr64 & 0x80) ?
					135 : 0;

				*whiteningValuePtr64 = Endian::Little (Endian::Little (*whiteningValuePtr64) << 1);

				whiteningValuePtr64--;

				if (*whiteningValuePtr64 & 0x80)
					*(whiteningValuePtr64 + 1) |= 0x0100000000000000ULL;	

				*whiteningValuePtr64 = Endian::Little (Endian::Little (*whiteningValuePtr64) << 1);
#endif

				whiteningValue[0] ^= finalCarry;
			}

			dataUnitBufPtr = bufPtr;
			whiteningValuesPtr64 = finalInt64WhiteningValuesPtr;

			// Encrypt all blocks in this data unit

			for (block = startBlock; block < endBlock; block++)
			{
				// Pre-whitening
				*bufPtr++ ^= *whiteningValuesPtr64--;
				*bufPtr++ ^= *whiteningValuesPtr64--;
			}

			// Actual encryption
			cipher.EncryptBlocks ((byte *) dataUnitBufPtr, endBlock - startBlock);

			bufPtr = dataUnitBufPtr;
			whiteningValuesPtr64 = finalInt64WhiteningValuesPtr;

			for (block = startBlock; block < endBlock; block++)
			{
				// Post-whitening
				*bufPtr++ ^= *whiteningValuesPtr64--;
				*bufPtr++ ^= *whiteningValuesPtr64--;
			}

			blockCount -= endBlock - startBlock;
			startBlock = 0;
			dataUnitNo++;
			*((uint64 *) byteBufUnitNo) = Endian::Little (dataUnitNo);
		}

		FAST_ERASE64 (whiteningValue, sizeof (whiteningValue));
		FAST_ERASE64 (whiteningValues, sizeof (whiteningValues));
	}
Ejemplo n.º 12
0
	void EncryptionModeXTS::DecryptBufferXTS (const Cipher &cipher, const Cipher &secondaryCipher, byte *buffer, uint64 length, uint64 startDataUnitNo, unsigned int startCipherBlockNo) const
	{
		byte finalCarry;
		byte whiteningValues [ENCRYPTION_DATA_UNIT_SIZE];
		byte whiteningValue [BYTES_PER_XTS_BLOCK];
		byte byteBufUnitNo [BYTES_PER_XTS_BLOCK];
		uint64 *whiteningValuesPtr64 = (uint64 *) whiteningValues;
		uint64 *whiteningValuePtr64 = (uint64 *) whiteningValue;
		uint64 *bufPtr = (uint64 *) buffer;
		uint64 *dataUnitBufPtr;
		unsigned int startBlock = startCipherBlockNo, endBlock, block;
		uint64 *const finalInt64WhiteningValuesPtr = whiteningValuesPtr64 + sizeof (whiteningValues) / sizeof (*whiteningValuesPtr64) - 1;
		uint64 blockCount, dataUnitNo;

		startDataUnitNo += SectorOffset;

		// Convert the 64-bit data unit number into a little-endian 16-byte array. 
		// Note that as we are converting a 64-bit number into a 16-byte array we can always zero the last 8 bytes.
		dataUnitNo = startDataUnitNo;
		*((uint64 *) byteBufUnitNo) = Endian::Little (dataUnitNo);
		*((uint64 *) byteBufUnitNo + 1) = 0;

		if (length % BYTES_PER_XTS_BLOCK)
			TC_THROW_FATAL_EXCEPTION;

		blockCount = length / BYTES_PER_XTS_BLOCK;

		// Process all blocks in the buffer
		while (blockCount > 0)
		{
			if (blockCount < BLOCKS_PER_XTS_DATA_UNIT)
				endBlock = startBlock + (unsigned int) blockCount;
			else
				endBlock = BLOCKS_PER_XTS_DATA_UNIT;

			whiteningValuesPtr64 = finalInt64WhiteningValuesPtr;
			whiteningValuePtr64 = (uint64 *) whiteningValue;

			// Encrypt the data unit number using the secondary key (in order to generate the first 
			// whitening value for this data unit)
			*whiteningValuePtr64 = *((uint64 *) byteBufUnitNo);
			*(whiteningValuePtr64 + 1) = 0;
			secondaryCipher.EncryptBlock (whiteningValue);

			// Generate subsequent whitening values for blocks in this data unit. Note that all generated 128-bit
			// whitening values are stored in memory as a sequence of 64-bit integers in reverse order.
			for (block = 0; block < endBlock; block++)
			{
				if (block >= startBlock)
				{
					*whiteningValuesPtr64-- = *whiteningValuePtr64++;
					*whiteningValuesPtr64-- = *whiteningValuePtr64;
				}
				else
					whiteningValuePtr64++;

				// Derive the next whitening value

#if BYTE_ORDER == LITTLE_ENDIAN

				// Little-endian platforms

				finalCarry = 
					(*whiteningValuePtr64 & 0x8000000000000000ULL) ?
					135 : 0;

				*whiteningValuePtr64-- <<= 1;

				if (*whiteningValuePtr64 & 0x8000000000000000ULL)
					*(whiteningValuePtr64 + 1) |= 1;	

				*whiteningValuePtr64 <<= 1;

#else
				// Big-endian platforms

				finalCarry = 
					(*whiteningValuePtr64 & 0x80) ?
					135 : 0;

				*whiteningValuePtr64 = Endian::Little (Endian::Little (*whiteningValuePtr64) << 1);

				whiteningValuePtr64--;

				if (*whiteningValuePtr64 & 0x80)
					*(whiteningValuePtr64 + 1) |= 0x0100000000000000ULL;	

				*whiteningValuePtr64 = Endian::Little (Endian::Little (*whiteningValuePtr64) << 1);
#endif

				whiteningValue[0] ^= finalCarry;
			}

			dataUnitBufPtr = bufPtr;
			whiteningValuesPtr64 = finalInt64WhiteningValuesPtr;

			// Decrypt blocks in this data unit

			for (block = startBlock; block < endBlock; block++)
			{
				*bufPtr++ ^= *whiteningValuesPtr64--;
				*bufPtr++ ^= *whiteningValuesPtr64--;
			}

			cipher.DecryptBlocks ((byte *) dataUnitBufPtr, endBlock - startBlock);

			bufPtr = dataUnitBufPtr;
			whiteningValuesPtr64 = finalInt64WhiteningValuesPtr;

			for (block = startBlock; block < endBlock; block++)
			{
				*bufPtr++ ^= *whiteningValuesPtr64--;
				*bufPtr++ ^= *whiteningValuesPtr64--;
			}

			blockCount -= endBlock - startBlock;
			startBlock = 0;
			dataUnitNo++;

			*((uint64 *) byteBufUnitNo) = Endian::Little (dataUnitNo);
		}

		FAST_ERASE64 (whiteningValue, sizeof (whiteningValue));
		FAST_ERASE64 (whiteningValues, sizeof (whiteningValues));
	}
Ejemplo n.º 13
0
int main()
{
	Cipher cipher;
	//unsigned char szPlainStr[] = "A^G~o`'X";
	unsigned char szPlainStr[] = "test1234567890abcdefghijkl";
	unsigned char szHexToStr[1024];
	unsigned char szStrToHex[1024];
	unsigned char szEncStr[1024];
	unsigned char szDecStr[1024];
	char *pszBase64EncBuf = 0;
	unsigned char pszBase64DecBuf[500] = "";
	int iRet=0, i;
	
	memset(szHexToStr, 0, sizeof(szHexToStr));
	memset(szStrToHex, 0, sizeof(szStrToHex));
	memset(szEncStr, 0, sizeof(szEncStr));
	memset(szDecStr, 0, sizeof(szDecStr));
	
	printf("===================\n");                           
	printf("Cipher Module Test\n");
	printf("===================\n");
	printf("Cipher Value Test\n");
	printf("Press Enter Key.\n");
	getchar();
	printf("=====================\n");
	printf("Function : cipher.StringToHex\n");
	printf("Plain Text : %s\n",szPlainStr);
	printf("Plain Text[0] : %d\n",szPlainStr[0]);	
	iRet = cipher.StringToHex(szStrToHex, szPlainStr);
	printf("StringToHex(Hex, Str) Result : %d\n",iRet);
	printf("Hex Text : %s\n", szStrToHex);
	printf("Press Enter Key.\n");
	getchar();
	printf("=====================\n");
	printf("Function : cipher.HexToString\n");
	printf("Hex Text : %s\n",szStrToHex);
	iRet = cipher.HexToString(szHexToStr, szStrToHex);
	printf("HexToString(Str, Hex) Result : %d\n",iRet);
	printf("String Text : %s\n", szHexToStr);
	printf("String Text[0] : %d\n", szHexToStr[0]);
	printf("Press Enter Key.\n");
	getchar();
	printf("=====================\n");
	printf("Function : cipher.Base64_Encode\n");
	printf("Plain Text : %s\n",szPlainStr);
	iRet = cipher.Base64_Encode(&pszBase64EncBuf, (char*)szPlainStr, strlen((char*)szPlainStr));
	printf("Base64_Encode(Buf, Plain, Len) Result : %d\n",iRet);
	printf("Base64_Encode Text : %s\n", pszBase64EncBuf);
	printf("Press Enter Key.\n");
	getchar();
	printf("=====================\n");
	printf("Function : cipher.Base64_Decode\n");
	printf("Base64_Encode Text : %s\n",pszBase64EncBuf);
	iRet = cipher.Base64_Decode(pszBase64DecBuf, pszBase64EncBuf, sizeof(pszBase64DecBuf));
	printf("Base64_Decode(Dec, Enc, Len) Result : %d\n",iRet);
	printf("Base64_Decode Text : %s\n", pszBase64DecBuf);
	printf("Press Enter Key.\n");
	getchar();
	printf("=====================\n");
	printf("Function : cipher.Encrypt2\n");
	printf("Plain Text : %s\n",szPlainStr);
	iRet = cipher.Encrypt2(szEncStr, szPlainStr, (unsigned char*)"1234567890123456");
	printf("Encrypt2(Enc, Plain, Key) Result : %d\n",iRet);
	printf("Encrypt Text : %s\n", szEncStr);
	printf("Press Enter Key.\n");
	getchar();
	printf("=====================\n");
	printf("Function : cipher.Decrypt2\n");
	printf("Encrypt Text : %s\n",szEncStr);
	iRet = cipher.Decrypt2(szDecStr, szEncStr, (unsigned char*)"1234567890123456");
	printf("Decrypt2(Dec, Enc, Key) Result : %d\n",iRet);
	printf("Decrypt Text : %s\n", szDecStr);
	printf("Press Enter Key.\n");
	getchar();
	printf("=====================\n");
	printf("End.\n\n");
	return 0;
}
int main(int argc, char *argv[])
{
    Cipher code; //creating object
    int c;

    c = getopt(argc, argv,"ed:");
    if(c == 'e'){//encrypt message

        code.setKey(atoi(optarg));
        int offSet = code.getKey();

        c = getopt(argc, argv,"f:");
        if(c == 'f'){
            code.setFileName(optarg);
            string fileOpen = code.getFileName();
            code.cipherDoc(fileOpen, offSet);
            code.printMsg();
        } else {
            cout<<"Did not provide a file"<<endl;
        }

    }
    if(c == 'd'){//decrypt message

        code.setKey(atoi(optarg));
        int offSet = code.getKey();

        c = getopt(argc, argv,"f:");
        if(c == 'f'){
            code.setFileName(optarg);
            string fileOpen = code.getFileName();
            code.decipherDoc(fileOpen, offSet);
            code.printMsg();
        } else {
            cout<<"Did not provide a file"<<endl;
        }
    }
    /* while((c = getopt(argc, argv,"f:")) != 'n'){ //Just in case other doesn't work
            if(c == 'f'){
                string fileOpen = Cipher.setFileName(optarg);
            } else {
                string z = optarg;
                z = 'n';
            }
        }*/
    return 0;
}