⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cciphercontext.cpp

📁 PGP8.0源码 请认真阅读您的文件包然后写出其具体功能
💻 CPP
字号:
/*____________________________________________________________________________
		Copyright (C) 2002 PGP Corporation
        All rights reserved.

        $Id: CCipherContext.cpp,v 1.6 2002/08/06 20:10:18 dallen Exp $
____________________________________________________________________________*/

#include "pgpClassesConfig.h"

#include "CSecureObject.h"
#include "CCipherContext.h"
#include "UCryptoFactory.h"

_USING_PGP

// Class CCipherContext member functions

CCipherContext::CCipherContext() : 

	mIsInitialized(FALSE), mIsKeyAttached(FALSE), 
		mAlgorithm(kPGPdiskInvalidAlgorithm), mKeyHandle(NULL)
{
	pgpClearMemory(&mVTable, sizeof(mVTable));
}

CCipherContext::CCipherContext(PGPdiskEncryptionAlgorithm algorithm)

	: mIsInitialized(FALSE), mIsKeyAttached(FALSE), 
		mAlgorithm(kPGPdiskInvalidAlgorithm), mKeyHandle(NULL)
{
	pgpClearMemory(&mVTable, sizeof(mVTable));

#if PGP_EXCEPTIONS
	Init(algorithm);
#else	// !PGP_EXCEPTIONS
	Status() = Init(algorithm);
#endif	// PGP_EXCEPTIONS
}

CCipherContext::CCipherContext(const CCipherContext& cipher) : 
	mIsInitialized(FALSE), mIsKeyAttached(FALSE), 
		mAlgorithm(kPGPdiskInvalidAlgorithm), mKeyHandle(NULL)
{
	pgpClearMemory(&mVTable, sizeof(mVTable));

#if PGP_EXCEPTIONS
	Assign(cipher);
#else	// !PGP_EXCEPTIONS
	Status() = Assign(cipher);
#endif	// PGP_EXCEPTIONS
}

CCipherContext::~CCipherContext()
{
#if PGP_EXCEPTIONS
	if (IsInitialized())
		Cleanup();
#else	// !PGP_EXCEPTIONS
	if (Status().IsntError() && IsInitialized())
		Cleanup();
#endif	// PGP_EXCEPTIONS
}

CCipherContext& 
CCipherContext::operator=(const CCipherContext& cipher)
{
#if PGP_EXCEPTIONS
	Assign(cipher);
#else	// !PGP_EXCEPTIONS
	Status() = Assign(cipher);
#endif	// PGP_EXCEPTIONS

	return *this;
}

PGPUInt32 
CCipherContext::GetEncryptSize() const
{
	pgpAssert(IsInitialized());
	return mVTable.pfnGetEncryptSize(mKeyHandle);
}

PGPUInt32 
CCipherContext::GetDecryptSize() const
{
	pgpAssert(IsInitialized());
	return mVTable.pfnGetDecryptSize(mKeyHandle);
}

PGPUInt32 
CCipherContext::GetKeySize() const
{
	pgpAssert(IsInitialized());
	return mVTable.pfnGetKeySize(mKeyHandle);
}

PGPUInt32 
CCipherContext::GetNeededRandomDataSize() const
{
	pgpAssert(IsInitialized());
	return mVTable.pfnGetNeededRandomDataSize(mKeyHandle);
}

PGPUInt32 
CCipherContext::GetSizeForExport() const
{
	pgpAssert(IsInitialized());
	return mVTable.pfnGetSizeForExport(mKeyHandle);
}

SMART_ERROR 
CCipherContext::Validate() const
{
	pgpAssert(IsInitialized());

	CComboError	error;
	error = mVTable.pfnValidate(mKeyHandle);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::Init(PGPdiskEncryptionAlgorithm algorithm)
{
	pgpAssert(!IsInitialized());

	CComboError	error;

#if PGP_EXCEPTIONS
	UCryptoFactory::RegisterModuleClient(algorithm, mVTable);
#else	// !PGP_EXCEPTIONS
	error = UCryptoFactory::RegisterModuleClient(algorithm, mVTable);
#endif	// PGP_EXCEPTIONS

#if !PGP_EXCEPTIONS
	if (error.IsntError())
#endif	// !PGP_EXCEPTIONS
	{
		error = mVTable.pfnInit(&mKeyHandle, algorithm);

	#if PGP_EXCEPTIONS
		THROW_IF_ERROR(error);
	#else	// !PGP_EXCEPTIONS
		if (error.IsntError())
	#endif	// PGP_EXCEPTIONS
		{
			mAlgorithm = algorithm;
			mIsInitialized = TRUE;
		}

	#if !PGP_EXCEPTIONS
		if (error.IsError())
			UCryptoFactory::DeregisterModuleClient(algorithm);
	#endif	// !PGP_EXCEPTIONS
	}

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::Assign(const CCipherContext& cipher)
{
#if PGP_EXCEPTIONS

	if (&cipher == this)
		return;

	if (!cipher.IsInitialized())
		THROW_PGPERROR(kPGPError_BadParams);

	if (!cipher.IsKeyAttached())
		THROW_PGPERROR(kPGPError_BadParams);

	if (!IsInitialized())
		Init(cipher.Algorithm());

	CArray<PGPUInt8>	exportedCipher(cipher.GetSizeForExport());

	cipher.Export(exportedCipher.Get(), cipher.GetSizeForExport());
	Import(exportedCipher.Get(), cipher.GetSizeForExport());

#else	// !PGP_EXCEPTIONS

	CComboError	error;

	if (&cipher == this)
		return error;

	if (!cipher.IsInitialized())
		error.pgpErr = kPGPError_BadParams;

	if (error.IsntError())
	{
		if (!cipher.IsKeyAttached())
			error.pgpErr = kPGPError_BadParams;
	}

	if (error.IsntError())
	{
		if (!IsInitialized())
			error = Init(cipher.Algorithm());
	}

	if (error.IsntError())
	{
		CArray<PGPUInt8>	exportedCipher(cipher.GetSizeForExport());
		error = exportedCipher.Status();

		if (error.IsntError())
		{
			error = cipher.Export(exportedCipher.Get(), 
				cipher.GetSizeForExport());
		}

		if (error.IsntError())
			error = Import(exportedCipher.Get(), cipher.GetSizeForExport());
	}

	return error;

#endif
}

void 
CCipherContext::Cleanup()
{
	pgpAssert(IsInitialized());

	mVTable.pfnCleanup(mKeyHandle);
	UCryptoFactory::DeregisterModuleClient(mAlgorithm);

	mIsInitialized = FALSE;
	mAlgorithm = kPGPdiskInvalidAlgorithm;
}

SMART_ERROR 
CCipherContext::FlipKeyBytes()
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());

	CComboError	error;
	error = mVTable.pfnFlipKeyBytes(mKeyHandle);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::AttachSymmetricKey(
	const Crypto::SymmetricKey&		key, 
	const Crypto::PassphraseSalt&	salt)
{
	pgpAssert(IsInitialized());

	CComboError	error;
	error = mVTable.pfnAttachSymmetricKey(mKeyHandle, &key, &salt);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#else	// !PGP_EXCEPTIONS
	if (error.IsntError())
#endif	// PGP_EXCEPTIONS
		mIsKeyAttached = TRUE;

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::GetSymmetricKey(Crypto::SymmetricKey& key) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());

	CComboError	error;
	error = mVTable.pfnGetSymmetricKey(mKeyHandle, &key);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::GetSalt(Crypto::PassphraseSalt& salt) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());

	CComboError	error;
	error = mVTable.pfnGetSalt(mKeyHandle, &salt);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::Encrypt(const void *in, void *out, PGPUInt32 numBlocks) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());
	pgpAssertAddrValid(in, VoidAlign);
	pgpAssertAddrValid(out, VoidAlign);

	CComboError	error;
	error = mVTable.pfnEncrypt(mKeyHandle, in, out, numBlocks);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::Decrypt(const void *in, void *out, PGPUInt32 numBlocks) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());
	pgpAssertAddrValid(in, VoidAlign);
	pgpAssertAddrValid(out, VoidAlign);

	CComboError	error;
	error = mVTable.pfnDecrypt(mKeyHandle, in, out, numBlocks);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::EncryptCFB(
	PGPUInt64	startBlockIndex, 
	PGPUInt32	numBlocks, 
	const void	*inBlocks, 
	void		*outBlocks) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());
	pgpAssertAddrValid(inBlocks, VoidAlign);
	pgpAssertAddrValid(outBlocks, VoidAlign);

	CComboError	error;

	error = mVTable.pfnEncryptCFB(mKeyHandle, startBlockIndex, numBlocks, 
		inBlocks, outBlocks);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::DecryptCFB(
	PGPUInt64	startBlockIndex, 
	PGPUInt32	numBlocks, 
	const void	*inBlocks, 
	void		*outBlocks) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());
	pgpAssertAddrValid(inBlocks, VoidAlign);
	pgpAssertAddrValid(outBlocks, VoidAlign);

	CComboError	error;

	error = mVTable.pfnDecryptCFB(mKeyHandle, startBlockIndex, numBlocks, 
		inBlocks, outBlocks);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::EncryptPassphraseKey(
	const char					*passphrase, 
	Crypto::PassphraseKeyInfo&	keyInfo) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());
	pgpAssertStrValid(passphrase);

	CComboError	error;
	error = mVTable.pfnEncryptPassphraseKey(mKeyHandle, passphrase, &keyInfo);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::DecryptPassphraseKey(
	const char							*passphrase, 
	const Crypto::PassphraseSalt&		salt, 
	const Crypto::PassphraseKeyInfo&	keyInfo)
{
	pgpAssert(IsInitialized());
	pgpAssertStrValid(passphrase);

	CComboError	error;

	error = mVTable.pfnDecryptPassphraseKey(mKeyHandle, 
		passphrase, &salt, &keyInfo);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#else	// !PGP_EXCEPTIONS
	if (error.IsntError())
#endif	// PGP_EXCEPTIONS
		mIsKeyAttached = TRUE;

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::GenerateNewSymmetricKey(
	PGPUInt8						*randomData1, 
	PGPUInt8						*randomData2, 
	PGPUInt32						bytesRandomData, 
	const Crypto::PassphraseSalt&	salt)
{
	pgpAssert(IsInitialized());
	pgpAssertAddrValid(randomData1, PGPUInt8);
	pgpAssertAddrValid(randomData2, PGPUInt8);

	CComboError	error;

	error = mVTable.pfnGenerateNewSymmetricKey(mKeyHandle, randomData1, 
		randomData2, bytesRandomData, &salt);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#else	// !PGP_EXCEPTIONS
	if (error.IsntError())
#endif	// PGP_EXCEPTIONS
		mIsKeyAttached = TRUE;

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::Export(void *buffer, PGPUInt32 sizeBuffer) const
{
	pgpAssert(IsInitialized());
	pgpAssert(IsKeyAttached());
	pgpAssertAddrValid(buffer, VoidAlign);

	CComboError	error;
	error = mVTable.pfnExport(mKeyHandle, buffer, sizeBuffer);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#endif	// PGP_EXCEPTIONS

	SMART_ERROR_RETURN
}

SMART_ERROR 
CCipherContext::Import(const void *buffer, PGPUInt32 sizeBuffer)
{
	pgpAssert(IsInitialized());
	pgpAssertAddrValid(buffer, VoidAlign);

	CComboError	error;
	error = mVTable.pfnImport(mKeyHandle, buffer, sizeBuffer);

#if PGP_EXCEPTIONS
	THROW_IF_ERROR(error);
#else	// !PGP_EXCEPTIONS
	if (error.IsntError())
#endif	// PGP_EXCEPTIONS
		mIsKeyAttached = TRUE;

	SMART_ERROR_RETURN
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -