📄 sha256type.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 "sha256.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 8 UInt32's.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV SetObjectSHA256 VOLT_PROTO_LIST ((
VoltAlgorithmObject *obj,
UInt32 *initState
));
int VtAlgorithmImplSHA256 (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
unsigned int algClass;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
UInt32 initState[8] =
{
SHA256_INIT_H0, SHA256_INIT_H1, SHA256_INIT_H2, SHA256_INIT_H3,
SHA256_INIT_H4, SHA256_INIT_H5, SHA256_INIT_H6, SHA256_INIT_H7
};
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 NULL.
*/
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) 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 = SetObjectSHA256 (obj, 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;
}
if (status != 0)
break;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
"VtAlgorithmImplSHA256", (char *)0)
return (status);
}
static int SetObjectSHA256 (
VoltAlgorithmObject *obj,
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;
VoltSHA256Ctx *sha256Ctx;
UInt32 kArray[SHA256_K_ARRAY_COUNT] = SHA256_K_ARRAY;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Allocate enough space for a DigestCtx and a SHA256Ctx.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = sizeof (VoltDigestClassCtx) + sizeof (VoltSHA256Ctx);
#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
sha256Ctx = (VoltSHA256Ctx *)(buffer + offset);
/* Populate the contexts.
*/
sha256Ctx->initState[0] = initState[0];
sha256Ctx->initState[1] = initState[1];
sha256Ctx->initState[2] = initState[2];
sha256Ctx->initState[3] = initState[3];
sha256Ctx->initState[4] = initState[4];
sha256Ctx->initState[5] = initState[5];
sha256Ctx->initState[6] = initState[6];
sha256Ctx->initState[7] = initState[7];
sha256Ctx->SHA256Transform = SHA256Transform;
Z2Memcpy (sha256Ctx->K, kArray, sizeof (kArray));
digestCtx->algorithmImpl = VtAlgorithmImplSHA256;
digestCtx->algorithmImplInfo = (Pointer) 0;
digestCtx->DigestInit = SHA256Init;
digestCtx->DigestUpdate = SHA256Update;
digestCtx->DigestFinal = SHA256Final;
digestCtx->SetDigestInitState = SHA256SetDigestInitState;
digestCtx->digestSize = 32;
digestCtx->blockLen = 64;
digestCtx->algorithm = VT_DIGEST_ALG_SHA256;
digestCtx->localDigestCtx = (Pointer)sha256Ctx;
/* 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,
"SetObjectSHA256", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -