📄 tdestype.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 "des.h"
#include "errorctx.h"
/* Implements VoltBlockAlgSetter.
*/
int VOLT_CALLING_CONV VoltBlockAlgSetTripleDesEde VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
Pointer info,
unsigned int flag
));
/* 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.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV SetObjectTripleDesEde VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
VoltCipherClassCtx *cipherCtx,
VoltBlockCipherCtx *blockCtx
));
int VtAlgorithmImpl3DESEDE (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
VtBlockCipherInfo *blockInfo = (VtBlockCipherInfo *)info;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Check the flag, it should be VOLT_ALG_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_ALG_SET_TYPE_FLAG)
break;
/* Check the args.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
/* Check the class of the object. It should be 0 (not yet set)
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_SET;
if (obj->algClass != 0)
break;
/* Call the general purpose block cipher Setter.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltAlgorithmSetBlockCipher (
obj, VoltBlockAlgSetTripleDesEde, blockInfo);
if (status != 0)
break;
obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
"VtAlgorithmImpl3DESEDE", (char *)0)
return (status);
}
int VoltBlockAlgSetTripleDesEde (
VoltAlgorithmObject *obj,
Pointer info,
unsigned int flag
)
{
int status;
VoltCipherClassCtx *cipherCtx;
VoltBlockCipherCtx *blockCtx;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Check the class of the object. It should be
* VOLT_CLASS_BLOCK_CIPHER.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
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_BLOCK)
break;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = SetObjectTripleDesEde (obj, cipherCtx, blockCtx);
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
"VoltBlockAlgSetTripleDesEde", (char *)0)
return (status);
}
static int SetObjectTripleDesEde (
VoltAlgorithmObject *obj,
VoltCipherClassCtx *cipherCtx,
VoltBlockCipherCtx *blockCtx
)
{
int status;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltTripleDesCtx *tdesCtx = (VoltTripleDesCtx *)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Allocate enough space for a Triple DES Ctx.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
tdesCtx = (VoltTripleDesCtx *)Z2Malloc (sizeof (VoltTripleDesCtx), 0);
if (tdesCtx == (VoltTripleDesCtx *)0)
break;
Z2Memset (tdesCtx, 0, sizeof (VoltTripleDesCtx));
/* Populate the contexts.
*/
blockCtx->algCtx = (Pointer)tdesCtx;
blockCtx->AlgCtxDestroy = VoltSimpleCtxDestroy;
cipherCtx->SymKeyParam = VtKeyParamTripleDES;
cipherCtx->EncryptInit = TripleDesEdeEncryptInit;
cipherCtx->EncryptUpdate = TripleDesEdeEncryptUpdate;
cipherCtx->DecryptInit = TripleDesEdeDecryptInit;
cipherCtx->DecryptUpdate = TripleDesEdeDecryptUpdate;
cipherCtx->plainBlockSize = 8;
cipherCtx->cipherBlockSize = 8;
cipherCtx->setState = VOLT_BLOCK_SET_STATE_ALG;
obj->subAlg1 = VOLT_SUB_ALG_3DES_EDE;
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 (tdesCtx != (VoltTripleDesCtx *)0)
Z2Free (tdesCtx);
obj->state = VOLT_STATE_ERROR;
VOLT_LOG_ERROR (
obj->voltObject.libraryCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
"SetObjectTripleDesEde", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -