📄 ofbimpl.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 "ofb.h"
#include "errorctx.h"
int OFBEncryptInit (
VoltAlgorithmObject *algObj,
VoltKeyObject *keyObj
)
{
int status;
VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBlockCipherCtx *blockCtx =
(VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
VoltOFBCtx *ofbCtx = (VoltOFBCtx *)(blockCtx->feedbackCtx);
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Copy the initVector.
*/
Z2Memcpy (
ofbCtx->feedCtx.xorVector, ofbCtx->feedCtx.initVector,
ofbCtx->feedCtx.blockSize);
ofbCtx->xorUsed = ofbCtx->feedCtx.blockSize;
/* Call the underlying algorithm's Init.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = ofbCtx->AlgEncryptInit (algObj, keyObj);
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, algObj->voltObject.libraryCtx, status, 0, fnctLine,
"OFBEncryptInit", (char *)0)
return (status);
}
int OFBEncryptUpdate (
VoltAlgorithmObject *algObj,
VtRandomObject random,
unsigned char *dataToEncrypt,
unsigned int dataToEncryptLen,
unsigned char *encryptedData
)
{
unsigned int index, dataLen;
VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
VoltBlockCipherCtx *blockCtx =
(VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
VoltOFBCtx *ofbCtx = (VoltOFBCtx *)(blockCtx->feedbackCtx);
/* If we have some unused bytes in the XOR vector, use them.
*/
if (ofbCtx->xorUsed < ofbCtx->feedCtx.blockSize)
{
/* Are there enough dataToEncrypt bytes to finish out the block of
* XOR bytes we have available?
*/
dataLen = ofbCtx->feedCtx.blockSize - ofbCtx->xorUsed;
if (dataToEncryptLen < dataLen)
dataLen = dataToEncryptLen;
for (index = ofbCtx->xorUsed; index < (ofbCtx->xorUsed + dataLen) ; ++index)
{
/* Encrypted data is the input XOR'd with a byte from the XOR
* vector.
*/
*encryptedData = *dataToEncrypt ^ ofbCtx->feedCtx.xorVector[index];
dataToEncrypt++;
encryptedData++;
}
dataToEncryptLen -= dataLen;
ofbCtx->xorUsed += dataLen;
}
/* So long as we have blocks, operate on them.
*/
while (dataToEncryptLen >= ofbCtx->feedCtx.blockSize)
{
/* Encrypt the xorVector to get the next XOR vector.
*/
ofbCtx->AlgEncryptUpdate (
algObj, random, ofbCtx->feedCtx.xorVector, ofbCtx->feedCtx.blockSize,
ofbCtx->feedCtx.xorVector);
/* XOR each byte of input with the XOR vector. This will be the
* ciphertext.
*/
for (index = 0; index < ofbCtx->feedCtx.blockSize; ++index)
encryptedData[index] =
ofbCtx->feedCtx.xorVector[index] ^ dataToEncrypt[index];
dataToEncrypt += ofbCtx->feedCtx.blockSize;
encryptedData += ofbCtx->feedCtx.blockSize;
dataToEncryptLen -= ofbCtx->feedCtx.blockSize;
}
/* After processing full blocks, if there's no more input, we're done.
*/
if (dataToEncryptLen == 0)
return (0);
/* We have some leftovers, create the XOR vector.
*/
ofbCtx->AlgEncryptUpdate (
algObj, random, ofbCtx->feedCtx.xorVector, ofbCtx->feedCtx.blockSize,
ofbCtx->feedCtx.xorVector);
for (index = 0; index < dataToEncryptLen; ++index)
encryptedData[index] =
dataToEncrypt[index] ^ ofbCtx->feedCtx.xorVector[index];
ofbCtx->xorUsed = dataToEncryptLen;
return (0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -