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

📄 cipherserver.c

📁 一个加密库代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#endif
				if (keybuf == NULL)
					return CIPHERSERVER_ERROR_OUTOFMEMORY;
			}
			else
			{
				keybuf = NULL;
			}

			for (lI = 0; lI < lKeySize; lI++)
				keybuf[lI] = (WORD8) (lI & 0x0ff);

			// allocate the two block buffer
			nTestBlockSize = (int)pCtx->infoblock.lBlockSize << 1;
#ifdef KERNEL_COMPILE
			twoblocks = ( WORD8* )ExAllocatePool( NonPagedPool, nTestBlockSize  );
#else
			twoblocks = (WORD8*) malloc(nTestBlockSize);
#endif
			
			if (twoblocks == NULL)
			{
#ifdef KERNEL_COMPILE
				ExFreePool( keybuf );
#else
				free(keybuf);
#endif
				return CIPHERSERVER_ERROR_OUTOFMEMORY;
			}

			// we use this creation for checking the right decryption later
			for (nI = 0; nI < nTestBlockSize; nI++)
				twoblocks[nI] = (WORD8) (nI & 0x0ff);

			// allocate the init. data memory
			if (pCtx->infoblock.lInitDataSize)
			{
#ifdef KERNEL_COMPILE
				pInitData = ExAllocatePool( NonPagedPool, pCtx->infoblock.lInitDataSize  );
#else
				pInitData = malloc(pCtx->infoblock.lInitDataSize);
#endif
			
				if (pInitData == NULL)
				{
#ifdef KERNEL_COMPILE
					ExFreePool( twoblocks );
#else
					free(twoblocks);
#endif
#ifdef KERNEL_COMPILE
					ExFreePool( keybuf );
#else
					free(keybuf);
#endif

					return CIPHERSERVER_ERROR_OUTOFMEMORY;
				}
			}
			else
			{
				pInitData = NULL;
			}

			// open an encryption session
			lResult = CipherServer_OpenSession(
				CIPHERSERVER_MODE_ENCRYPT | lModeFlags,
				keybuf,
				lKeySize,
				pCtx,
				pInitData,
				&chandle);

			if ((lResult != CIPHER_ERROR_NOERROR) && (lResult != CIPHER_ERROR_WEAKKEY))
			{
#ifdef KERNEL_COMPILE
				ExFreePool( pInitData );
#else
				free(pInitData);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( twoblocks );
#else
				free(twoblocks);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( keybuf );
#else
				free(keybuf);
#endif
				return CIPHERSERVER_ERROR_INVALIDCIPHER;
			}

			// encrypt the blocks
			CipherServer_EncryptBlocks(chandle,
				twoblocks,
				twoblocks,
				2);

			// close the session
			lResult = CipherServer_CloseSession(chandle);
			if (lResult != CIPHER_ERROR_NOERROR)
			{
#ifdef KERNEL_COMPILE
				ExFreePool( pInitData );
#else
				free(pInitData);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( twoblocks );
#else
				free(twoblocks);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( keybuf );
#else
				free(keybuf);
#endif

				return CIPHERSERVER_ERROR_INVALIDCIPHER;
			}

			// open a decryption session
			lResult = CipherServer_OpenSession(
				CIPHERSERVER_MODE_DECRYPT | lModeFlags,
				keybuf,
				lKeySize,
				pCtx,
				pInitData,
				&chandle);

			if ((lResult != CIPHER_ERROR_NOERROR) && (lResult != CIPHER_ERROR_WEAKKEY))
			{
#ifdef KERNEL_COMPILE
				ExFreePool( pInitData );
#else
				free(pInitData);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( twoblocks );
#else
				free(twoblocks);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( keybuf );
#else
				free(keybuf);
#endif

				return CIPHERSERVER_ERROR_INVALIDCIPHER;
			}

			// decrypt the blocks
			CipherServer_DecryptBlocks(chandle,
				twoblocks,
				twoblocks,
				2,
				CIPHER_NULL);

			// close the session
			lResult = CipherServer_CloseSession(chandle);
			if (lResult != CIPHER_ERROR_NOERROR)
			{
#ifdef KERNEL_COMPILE
				ExFreePool( pInitData );
#else
				free(pInitData);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( twoblocks );
#else
				free(twoblocks);
#endif
#ifdef KERNEL_COMPILE
				ExFreePool( keybuf );
#else
				free(keybuf);
#endif

				return CIPHERSERVER_ERROR_INVALIDCIPHER;
			}

			// correct decryption?
			for (nI = 0; nI < nTestBlockSize; nI++)
			{
				if (twoblocks[nI] != (nI & 0x0ff))
				{
#ifdef KERNEL_COMPILE
					ExFreePool( pInitData );
#else
					free(pInitData);
#endif
#ifdef KERNEL_COMPILE
					ExFreePool( twoblocks );
#else
					free(twoblocks);
#endif
#ifdef KERNEL_COMPILE
					ExFreePool( keybuf );
#else
					free(keybuf);
#endif

					return CIPHERSERVER_ERROR_INVALIDCIPHER;
				}
			}

			// free all memory
#ifdef KERNEL_COMPILE
			ExFreePool( pInitData );
#else
			free(pInitData);
#endif
#ifdef KERNEL_COMPILE
			ExFreePool( twoblocks );
#else
			free(twoblocks);
#endif
#ifdef KERNEL_COMPILE
			ExFreePool( keybuf );
#else
			free(keybuf);
#endif

		}
	}

	// quit with success
	return CIPHERSERVER_ERROR_NOERROR;
}

//////////////////////////////////////////////////////////////////////////////

WORD32 CRYPTPAK_API CipherServer_GetInfoBlock
(PCIPHERCTX pCtx,
 CIPHERINFOBLOCK* pInfoBlock)
{
	// call the retrieved function to get the information block
	pInfoBlock->lSizeOf = sizeof(CIPHERINFOBLOCK);
	switch ((*pCtx->pGetCipherInfo)(pInfoBlock))
	{
	case CIPHER_ERROR_NOERROR :
		return CIPHERSERVER_ERROR_NOERROR;
	case CIPHER_ERROR_INVALID :
		return CIPHERSERVER_ERROR_INVALIDCIPHER;
	default:
		// unknown return value
		return CIPHERSERVER_ERROR_ERROR;
	}
}

//////////////////////////////////////////////////////////////////////////////

WORD32 CRYPTPAK_API CipherServer_OpenSession
(WORD32 lMode,
 const WORD8* pKey,
 WORD32 lKeyLen,
 PCIPHERCTX pCtx,
 void* pInitData,
 PCIPHERSESSION* pSessionHandlePtr)
{
	PCIPHERSESSION pNewCryptSession;
	WORD32 lRetCode;

	// allocate memory for a new session handle and the work
	// context (the algorithm's runtime memory area) together
#ifdef KERNEL_COMPILE
	pNewCryptSession = (PCIPHERSESSION)ExAllocatePool( NonPagedPool, sizeof(CIPHERSESSION) + pCtx->infoblock.lContextSize  );
#else
	pNewCryptSession = (PCIPHERSESSION) malloc(sizeof(CIPHERSESSION) + pCtx->infoblock.lContextSize);
#endif

	//
	if (pNewCryptSession == NULL)
		return CIPHERSERVER_ERROR_OUTOFMEMORY;

	// calculate the pointer for the work context
	pNewCryptSession->pWorkContext = (WORD8*)pNewCryptSession + sizeof(CIPHERSESSION);

	memset(pNewCryptSession->pWorkContext,
		0,
		pCtx->infoblock.lContextSize);

	// call the cipher's CreateWorkContext() function
	switch ((*pCtx->pCreateWorkContext)(
		pNewCryptSession->pWorkContext,
		pKey,
		lKeyLen,
		lMode,
		pInitData,
		pCtx->pRandomGenerator,
		pCtx->pRndGenData))
	{
	case CIPHER_ERROR_NOERROR: lRetCode = CIPHERSERVER_ERROR_NOERROR; break;
	case CIPHER_ERROR_WEAKKEY: lRetCode = CIPHERSERVER_ERROR_WEAKKEY; break;
	default:
		// unknown return value (including case CIPHER_ERROR_KEYSETUPERROR)
		lRetCode = CIPHERSERVER_ERROR_ERROR;
	}
	if (lRetCode != CIPHERSERVER_ERROR_NOERROR)
	{
#ifdef KERNEL_COMPILE
		ExFreePool( pNewCryptSession );
#else
		free(pNewCryptSession);
#endif

		return lRetCode;
	}

	// copy the cipher handle to the session handle
	pNewCryptSession->pCipherContext = pCtx;

	// copy the crypt mode to the session handle (flags included)
	pNewCryptSession->lCryptMode = lMode;

	// return the cryptsession handle
	*pSessionHandlePtr = pNewCryptSession;

	// success
	return CIPHERSERVER_ERROR_NOERROR;
}

//////////////////////////////////////////////////////////////////////////////

void CRYPTPAK_API CipherServer_ResetSession
(PCIPHERSESSION pSessionHandle,
 void* pInitData)
{
	// call the ciphers ResetWorkContext() function
	(*pSessionHandle->pCipherContext->pResetWorkContext)(
		pSessionHandle->pWorkContext,
		pSessionHandle->lCryptMode,
		pInitData,
		pSessionHandle->pCipherContext->pRandomGenerator,
		pSessionHandle->pCipherContext->pRndGenData);
}

//////////////////////////////////////////////////////////////////////////////

WORD32 CRYPTPAK_API CipherServer_CloseSession
(PCIPHERSESSION pSessionHandle)
{
	WORD32 lRetCode;

	// call the cipher's DestroyContext() function
	if ((*pSessionHandle->pCipherContext->pDestroyWorkContext)(
		pSessionHandle->pWorkContext) == CIPHER_ERROR_NOERROR)
		lRetCode = CIPHERSERVER_ERROR_NOERROR;
	else
		lRetCode = CIPHERSERVER_ERROR_ERROR;

	// even if a (fatal) error has occured we clear (all in one)
	// and free all the session memory to quit properly
	memset(
		pSessionHandle,
		0,
		sizeof(CIPHERSESSION) + pSessionHandle->pCipherContext->infoblock.lContextSize);
#ifdef KERNEL_COMPILE
	ExFreePool( pSessionHandle );
#else
	free(pSessionHandle);
#endif

	// deliver the error code retrieved above
	return lRetCode;
}

//////////////////////////////////////////////////////////////////////////////

void CRYPTPAK_API CipherServer_EncryptBlocks
(PCIPHERSESSION pSessionHandle,
 const void* pSource,
 void* pTarget,
 WORD32 lNumOfBlocks)
{
	WORD32 lNumOfBytes;

	// calculate the number of bytes to encrypt
	lNumOfBytes = lNumOfBlocks * pSessionHandle->pCipherContext->infoblock.lBlockSize;

	// call the cipher's EncryptBuffer() function
	(*pSessionHandle->pCipherContext->pEncryptBuffer)(
		pSessionHandle->pWorkContext,
		pSource,
		pTarget,
		lNumOfBytes);
}

//////////////////////////////////////////////////////////////////////////////

void CRYPTPAK_API CipherServer_DecryptBlocks
(PCIPHERSESSION pSessionHandle,
 const void* pSource,
 void* pTarget,
 WORD32 lNumOfBlocks,
 const void* pPreviousBlock)
{
	WORD32 lNumOfBytes;

	// calculate the number of bytes to encrypt
	lNumOfBytes = lNumOfBlocks * pSessionHandle->pCipherContext->infoblock.lBlockSize;

	// call the cipher's DecryptBuffer() function
	(*pSessionHandle->pCipherContext->pDecryptBuffer)(
		pSessionHandle->pWorkContext,
		pSource,
		pTarget,
		lNumOfBytes,
		pPreviousBlock);
}

//////////////////////////////////////////////////////////////////////////////

void CRYPTPAK_API CipherServer_GetRandomData
(PCIPHERCTX pCtx,
 void* pTarget,
 WORD32 lNumOfBytes)
{
	// just map the call
	(*pCtx->pRandomGenerator)((WORD8*)pTarget,
		lNumOfBytes,
		pCtx->pRndGenData);
}

⌨️ 快捷键说明

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