📄 ofbtype.c
字号:
/* Copyright 2003-2006, 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 "ofb.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 iv The item containing the init vector.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV SetObjectOFB VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
VoltCipherClassCtx *cipherCtx,
VoltBlockCipherCtx *blockCtx,
VtItem *iv
));
int VtFeedbackOFB (
VtAlgorithmObject object,
VtFeedbackInfo *feedInfo,
unsigned int flag
)
{
int status;
unsigned int blockSize;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)object;
VtItem *iv = (VtItem *)(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 VoltageOFBInfo
* 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 ( (iv->data == (unsigned char *)0) || (iv->len != blockSize) )
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = SetObjectOFB (obj, cipherCtx, blockCtx, iv);
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
"VtFeedbackOFB", (char *)0)
return (status);
}
static int SetObjectOFB (
VoltAlgorithmObject *obj,
VoltCipherClassCtx *cipherCtx,
VoltBlockCipherCtx *blockCtx,
VtItem *iv
)
{
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);
VoltOFBCtx *ofbCtx = (VoltOFBCtx *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Allocate enough space for an OFB Ctx and two 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 = iv->len;
bufferSize = sizeof (VoltOFBCtx) + (2 * 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 (VoltOFBCtx), 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.
*/
ofbCtx = (VoltOFBCtx *)buffer;
offset = sizeof (VoltOFBCtx);
#if VOLT_ALIGNMENT != 1
offset += pad;
#endif
ofbCtx->feedCtx.xorVector = buffer + offset;
offset += blockSize;
ofbCtx->feedCtx.initVector = buffer + offset;
/* Populate the contexts.
*/
Z2Memcpy (ofbCtx->feedCtx.initVector, iv->data, iv->len);
ofbCtx->feedCtx.blockSize = blockSize;
ofbCtx->AlgEncryptInit = cipherCtx->EncryptInit;
ofbCtx->AlgEncryptUpdate = cipherCtx->EncryptUpdate;
blockCtx->feedbackCtx = (Pointer)ofbCtx;
blockCtx->FeedbackCtxDestroy = VoltSimpleCtxDestroy;
cipherCtx->EncryptInit = OFBEncryptInit;
cipherCtx->EncryptUpdate = OFBEncryptUpdate;
cipherCtx->DecryptInit = OFBEncryptInit;
cipherCtx->DecryptUpdate = OFBEncryptUpdate;
cipherCtx->plainBlockSize = 1;
cipherCtx->cipherBlockSize = 1;
cipherCtx->GetOutputSize = VoltStreamEncDecGetOutputSize;
cipherCtx->setState = VOLT_BLOCK_SET_STATE_FEED;
obj->subAlg2 = VOLT_SUB_ALG_OFB;
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,
"SetObjectOFB", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -