📄 sha1type.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 "digest.h"
#include "sha1.h"
#include "fipsmodule.h"
#include "errorctx.h"
/* This routine does the work. It allocates and fills in the contexts.
* @param obj The algorithm object to set.
* @param initState The initial state, an array of 5 UInt32's.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV SetObjectSHA1 VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
unsigned int padding,
UInt32 *initState
));
int VtAlgorithmImplSHA1 (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
unsigned char regularSHA1InitState[20] =
{
0x67, 0x45, 0x23, 0x01,
0xEF, 0xCD, 0xAB, 0x89,
0x98, 0xBA, 0xDC, 0xFE,
0x10, 0x32, 0x54, 0x76,
0xC3, 0xD2, 0xE1, 0xF0
};
VtGeneralSHA1Info genInfo;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* The associated info should be NULL pointer.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info != (Pointer)0)
break;
/* Call the general purpose SHA-1 with the regular SHA-1 initial
* values and padding.
*/
genInfo.padding = VT_SHA1_STD_PAD;
genInfo.initState.data = regularSHA1InitState;
genInfo.initState.len = 20;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtAlgorithmImplGeneralSHA1 (object, (Pointer)&genInfo, flag);
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, ((VoltObject *)(*object))->libraryCtx, status, errorType,
fnctLine, "VtAlgorithmImplSHA1", (char *)0)
return (status);
}
int VtAlgorithmImplGeneralSHA1 (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
unsigned int algClass;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
VtGeneralSHA1Info *genInfo = (VtGeneralSHA1Info *)info;
unsigned char *buf;
UInt32 initState[5];
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;
/* The associated info should be a VtItem with data that is 20 bytes
* long.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if ( (genInfo->initState.data == (unsigned char *)0) ||
(genInfo->initState.len != 20) )
break;
VOLT_SET_FNCT_LINE (fnctLine)
if ( (genInfo->padding != VT_SHA1_STD_PAD) &&
(genInfo->padding != VT_SHA1_FIPS_186_PAD) )
break;
/* Get the initial value as an array of 5 UInt32's.
*/
buf = genInfo->initState.data;
SHA1_GET_UINT32 (buf, initState[0])
buf += 4;
SHA1_GET_UINT32 (buf, initState[1])
buf += 4;
SHA1_GET_UINT32 (buf, initState[2])
buf += 4;
SHA1_GET_UINT32 (buf, initState[3])
buf += 4;
SHA1_GET_UINT32 (buf, initState[4])
/* Check the class of the object. It should be 0 (not yet set) or
* a class that uses a digest as a subsidiary algorithm, such as
* signature or HMAC.
*/
algClass = (unsigned int)(obj->algClass);
switch (algClass)
{
case 0:
/* If the class is not set yet, this is being called to set
* this as a simple digest.
*/
/* Set the object, and if that is successful, set the class.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = SetObjectSHA1 (obj, genInfo->padding, initState);
if (status == 0)
obj->algClass = VOLT_CLASS_DIGEST;
break;
case VOLT_CLASS_MAC:
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_UNIMPLEMENTED;
break;
default:
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ALG_OBJ;
}
} while (0);
/* If successful, set the FIPS bit in the object type, this object
* is a FIPS object.
*/
if (status == 0)
obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;
VOLT_LOG_ERROR_COMPARE (
status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
"VtAlgorithmImplGeneralSHA1", (char *)0)
return (status);
}
static int SetObjectSHA1 (
VoltAlgorithmObject *obj,
unsigned int padding,
UInt32 *initState
)
{
int status;
#if VOLT_ALIGNMENT != 1
unsigned int pad;
#endif
unsigned int bufferSize, offset;
unsigned char *buffer = (unsigned char *)0;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltDigestClassCtx *digestCtx;
VoltSHA1Ctx *sha1Ctx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Allocate enough space for a DigestCtx and a SHA1Ctx.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = sizeof (VoltDigestClassCtx) + sizeof (VoltSHA1Ctx);
#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 (VoltDigestClassCtx), pad)
bufferSize += pad;
#endif
buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
/* Locate the contexts.
*/
digestCtx = (VoltDigestClassCtx *)buffer;
offset = sizeof (VoltDigestClassCtx);
#if VOLT_ALIGNMENT != 1
offset += pad;
#endif
sha1Ctx = (VoltSHA1Ctx *)(buffer + offset);
/* Populate the contexts.
*/
sha1Ctx->initState[0] = initState[0];
sha1Ctx->initState[1] = initState[1];
sha1Ctx->initState[2] = initState[2];
sha1Ctx->initState[3] = initState[3];
sha1Ctx->initState[4] = initState[4];
sha1Ctx->SHA1Transform = SHA1Transform;
sha1Ctx->padding = padding;
digestCtx->algorithmImpl = VtAlgorithmImplSHA1;
digestCtx->algorithmImplInfo = (Pointer) 0;
digestCtx->DigestInit = SHA1Init;
digestCtx->DigestUpdate = SHA1Update;
digestCtx->DigestFinal = SHA1Final;
digestCtx->digestSize = 20;
digestCtx->blockLen = 64;
digestCtx->algorithm = VT_DIGEST_ALG_SHA1;
digestCtx->localDigestCtx = (Pointer)sha1Ctx;
/* No LocalDigestCtxDestroy, the ClassCtxDestroy we will load will
* take care of it.
*/
obj->classCtx = (Pointer)digestCtx;
obj->ClassCtxDestroy = VoltSimpleCtxDestroy;
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,
"SetObjectSHA1", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -