📄 cfbtype.c
字号:
/* Copyright 2003-2004, Voltage Security, all rights reserved.
*/
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "algobj.h"
#include "cipher.h"
#include "block.h"
#include "cfb.h"
#include "errorctx.h"
/* This routine does the work. It allocates and fills in the contexts.
* @param obj The algorithm object to set.
* @param cipherCtx The Cipher Class context to set.
* @param blockCtx The BlockCipher context to set.
* @param cfbInfo The struct containing the IV and transferSize.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV SetObjectCFB VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
VoltCipherClassCtx *cipherCtx,
VoltBlockCipherCtx *blockCtx,
VtCFBInfo *cfbInfo
));
int VtFeedbackCFB (
VtAlgorithmObject object,
VtFeedbackInfo *feedInfo,
unsigned int flag
)
{
int status;
unsigned int blockSize;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)object;
VtCFBInfo *cfbInfo = (VtCFBInfo *)(feedInfo->info);
VoltCipherClassCtx *cipherCtx;
VoltBlockCipherCtx *blockCtx;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Check the flag, it should be VOLT_FEEDBACK_SET_TYPE_FLAG.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_FEEDBACK_SET_TYPE_FLAG)
break;
/* The associated info should be a pointer to a VtCFBInfo struct.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (feedInfo->info == (Pointer)0)
break;
/* Check the class of the object. It should be
* VOLT_CLASS_BLOCK_CIPHER.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (obj->algClass != VOLT_CLASS_BLOCK_CIPHER)
break;
/* We have a block cipher object, which means we have a CipherCtx
* and a BlockCtx.
*/
cipherCtx = (VoltCipherClassCtx *)(obj->classCtx);
blockCtx = (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
/* Make sure the setState in the CipherCtx is correct.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_SET;
if (cipherCtx->setState != VOLT_BLOCK_SET_STATE_ALG)
break;
/* Make sure there's data and that it is blockSize bytes long.
*/
VOLT_SET_FNCT_LINE (fnctLine)
blockSize = cipherCtx->plainBlockSize;
if ( (blockSize < 2) || (blockSize != cipherCtx->cipherBlockSize) )
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if ( (cfbInfo->initVector.data == (unsigned char *)0) ||
(cfbInfo->initVector.len != blockSize) )
break;
/* The transferSize must be 1, 8 or the blockSize.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if ( (cfbInfo->transferSizeBits != 1) &&
(cfbInfo->transferSizeBits != 8) &&
(cfbInfo->transferSizeBits != (blockSize * 8)) )
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = SetObjectCFB (obj, cipherCtx, blockCtx, cfbInfo);
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
"VtFeedbackCFB", (char *)0)
return (status);
}
static int SetObjectCFB (
VoltAlgorithmObject *obj,
VoltCipherClassCtx *cipherCtx,
VoltBlockCipherCtx *blockCtx,
VtCFBInfo *cfbInfo
)
{
int status;
unsigned int blockSize, bufferSize, offset;
#if VOLT_ALIGNMENT != 1
unsigned int pad;
#endif
unsigned char *buffer = (unsigned char *)0;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltCFBCtx *cfbCtx = (VoltCFBCtx *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Allocate enough space for a CFB Ctx and three blockSize buffers.
* Because the buffers will be pointers to byte arrays, we should
* not have to worry about alignment. However, we may want to
* dereference the pointer to the xorVector as a UInt32 or UInt64,
* so make sure that buffer is aligned.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
blockSize = cfbInfo->initVector.len;
bufferSize = sizeof (VoltCFBCtx) + (3 * blockSize);
#if VOLT_ALIGNMENT != 1
/* If the alignment is 1, there's no need to pad. If not, compute
* the pad length.
*/
VOLT_COMPUTE_ALIGN_PAD (VOLT_ALIGNMENT, sizeof (VoltCFBCtx), pad)
bufferSize += pad;
#endif
buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
/* Locate the pointers.
*/
cfbCtx = (VoltCFBCtx *)buffer;
offset = sizeof (VoltCFBCtx);
#if VOLT_ALIGNMENT != 1
offset += pad;
#endif
cfbCtx->feedCtx.xorVector = buffer + offset;
offset += blockSize;
cfbCtx->feedCtx.initVector = buffer + offset;
offset += blockSize;
cfbCtx->currentVector = buffer + offset;
/* Populate the contexts.
*/
Z2Memcpy (
cfbCtx->feedCtx.initVector, cfbInfo->initVector.data,
cfbInfo->initVector.len);
cfbCtx->feedCtx.blockSize = blockSize;
cfbCtx->transferSizeBits = cfbInfo->transferSizeBits;
cfbCtx->AlgEncryptInit = cipherCtx->EncryptInit;
cfbCtx->AlgEncryptUpdate = cipherCtx->EncryptUpdate;
blockCtx->feedbackCtx = (Pointer)cfbCtx;
blockCtx->FeedbackCtxDestroy = VoltSimpleCtxDestroy;
cipherCtx->EncryptInit = CFBEncryptInit;
cipherCtx->EncryptUpdate = CFBEncryptUpdateBlock;
if (cfbCtx->transferSizeBits == 1)
cipherCtx->EncryptUpdate = CFBEncryptUpdate1Bit;
if (cfbCtx->transferSizeBits == 8)
cipherCtx->EncryptUpdate = CFBEncryptUpdate8Bit;
cipherCtx->DecryptInit = CFBEncryptInit;
cipherCtx->DecryptUpdate = CFBDecryptUpdateBlock;
if (cfbCtx->transferSizeBits == 1)
cipherCtx->DecryptUpdate = CFBDecryptUpdate1Bit;
if (cfbCtx->transferSizeBits == 8)
cipherCtx->DecryptUpdate = CFBDecryptUpdate8Bit;
cipherCtx->plainBlockSize = 1;
cipherCtx->cipherBlockSize = 1;
cipherCtx->GetOutputSize = VoltStreamEncDecGetOutputSize;
cipherCtx->setState = VOLT_BLOCK_SET_STATE_FEED;
obj->subAlg2 = VOLT_SUB_ALG_CFB;
status = 0;
} while (0);
/* If everything worked, return 0.
*/
if (status == 0)
return (0);
/* If something went wrong, destroy anything we created and indicate
* that this object is not usable.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
obj->state = VOLT_STATE_ERROR;
VOLT_LOG_ERROR (
obj->voltObject.libraryCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
"SetObjectCFB", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -