📄 dsasigntype.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 "sign.h"
#include "dsa.h"
#include "mpint.h"
#include "errorctx.h"
int VtAlgorithmImplDSASign (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
#if VOLT_ALIGNMENT != 1
unsigned int pad;
#endif
unsigned int sigFormat, bufferSize, offset;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
unsigned char *buffer = (unsigned char *)0;
VoltSignClassCtx *ctx;
VoltDsaSignCtx *dsaCtx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Check the flag, it should be VOLT_ALG_SET_TYPE_FLAG.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_ALG_SET_TYPE_FLAG)
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;
/* The associated info should be a pointer to an unsigned int, one
* of the allowed signature formats.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
sigFormat = *((unsigned int *)info);
if ( (sigFormat != VT_DSA_SIGNATURE_R_S) &&
(sigFormat != VT_DSA_SIGNATURE_DER_ENCODED) )
break;
/* Allocate space for the VoltSignClassCtx and the local context.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = sizeof (VoltSignClassCtx) + sizeof (VoltDsaSignCtx);
#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 (VoltSignClassCtx), pad)
bufferSize += pad;
#endif
buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
ctx = (VoltSignClassCtx *)buffer;
offset = sizeof (VoltSignClassCtx);
#if VOLT_ALIGNMENT != 1
offset += pad;
#endif
dsaCtx = (VoltDsaSignCtx *)(buffer + offset);
dsaCtx->format = sigFormat;
ctx->GetDigestObject = DSAGetDigestObject;
ctx->CheckSignatureInput = DSACheckSignatureInput;
ctx->SignData = DSASignData;
ctx->algorithm |= VOLT_SIGNATURE_ALG_DSA;
ctx->localSignCtx = (Pointer)dsaCtx;
ctx->LocalSignCtxDestroy = DSASignCtxDestroy;
obj->algClass = VOLT_CLASS_SIGNATURE;
obj->classCtx = (Pointer)ctx;
obj->ClassCtxDestroy = VoltSignCtxDestroy;
status = 0;
} while (0);
/* If success, we're done.
*/
if (status == 0)
{
/* Set the FIPS bit in the object type, this object is a FIPS
* object.
*/
obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;
return (0);
}
/* If there was an error, free up any memory this function allocated.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR (
(VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
"VtAlgorithmImplDSASign", (char *)0)
return (status);
}
int VtAlgorithmImplDSAVerify (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
#if VOLT_ALIGNMENT != 1
unsigned int pad;
#endif
unsigned int sigFormat, bufferSize, offset;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
unsigned char *buffer = (unsigned char *)0;
VoltSignClassCtx *ctx;
VoltDsaSignCtx *dsaCtx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Check the flag, it should be VOLT_ALG_SET_TYPE_FLAG.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_TYPE;
if (flag != VOLT_ALG_SET_TYPE_FLAG)
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;
/* The associated info should be a pointer to an unsigned int, one
* of the allowed signature formats.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
sigFormat = *((unsigned int *)info);
if ( (sigFormat != VT_DSA_SIGNATURE_R_S) &&
(sigFormat != VT_DSA_SIGNATURE_DER_ENCODED) )
break;
/* Allocate space for the VoltSignClassCtx and the local context.
* The local context is simply the sig format, which can be
* represented in an unsigned int.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = sizeof (VoltSignClassCtx) + sizeof (VoltDsaSignCtx);
#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 (VoltSignClassCtx), pad)
bufferSize += pad;
#endif
buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
ctx = (VoltSignClassCtx *)buffer;
offset = sizeof (VoltSignClassCtx);
#if VOLT_ALIGNMENT != 1
offset += pad;
#endif
dsaCtx = (VoltDsaSignCtx *)(buffer + offset);
dsaCtx->format = sigFormat;
ctx->GetDigestObject = DSAGetDigestObject;
ctx->VerifyData = DSAVerifyData;
ctx->algorithm |= VOLT_SIGNATURE_ALG_DSA;
ctx->localSignCtx = (Pointer)dsaCtx;
ctx->LocalSignCtxDestroy = DSASignCtxDestroy;
obj->algClass = VOLT_CLASS_VERIFY;
obj->classCtx = (Pointer)ctx;
obj->ClassCtxDestroy = VoltSignCtxDestroy;
status = 0;
} while (0);
/* If success, we're done.
*/
if (status == 0)
{
/* Set the FIPS bit in the object type, this object is a FIPS
* object.
*/
obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;
return (0);
}
/* If there was an error, free up any memory this function allocated.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR (
(VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
"VtAlgorithmImplDSAVerify", (char *)0)
return (status);
}
int VtAlgorithmImplDSASignVerify (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
VoltSignClassCtx *ctx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
/* Set this up to sign, then add verify capabilities.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VtAlgorithmImplDSASign (object, info, flag);
if (status == 0)
{
ctx = (VoltSignClassCtx *)(obj->classCtx);
ctx->VerifyData = DSAVerifyData;
obj->algClass |= VOLT_CLASS_VERIFY;
}
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, *object, status, 0, 0,
(char *)0, "VtAlgorithmImplDSASignVerify", fnctLine, (char *)0)
return (status);
}
int DSAGetDigestObject (
VoltAlgorithmObject *obj,
VtAlgorithmObject *digestObject
)
{
int status;
VoltSignClassCtx *ctx = (VoltSignClassCtx *)(obj->classCtx);
VtAlgorithmObject newObject = (VtAlgorithmObject)0;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* If we don't have an object built, build it.
*/
if (ctx->digestObject == (VtAlgorithmObject)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCreateAlgorithmObject (
obj->voltObject.libraryCtx, VtAlgorithmImplSHA1, (Pointer)0,
&newObject);
if (status != 0)
break;
ctx->digestObject = newObject;
}
*digestObject = ctx->digestObject;
status = 0;
} while (0);
if (status == 0)
return (0);
VtDestroyAlgorithmObject (&newObject);
VOLT_LOG_ERROR (
obj->voltObject.libraryCtx, status, 0, fnctLine,
"DSAGetDigestObject", (char *)0)
return (status);
}
void DSASignCtxDestroy (
Pointer obj,
Pointer ctx
)
{
VoltObject *voltObj;
VoltLibCtx *libCtx;
VoltDsaSignCtx *dsaCtx;
/* Anything to destroy?
*/
if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
return;
voltObj = (VoltObject *)obj;
dsaCtx = (VoltDsaSignCtx *)ctx;
libCtx = (VoltLibCtx *)(voltObj->libraryCtx);
VtDestroyKeyObject (&(dsaCtx->tempKey));
/* Don't free the memory, it's part of the full signCtx.
*/
}
void VoltSignCtxDestroy (
Pointer obj,
Pointer ctx
)
{
VoltObject *voltObj;
VoltLibCtx *libCtx;
VoltSignClassCtx *signCtx;
/* Anything to destroy?
*/
if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
return;
voltObj = (VoltObject *)obj;
signCtx = (VoltSignClassCtx *)ctx;
libCtx = (VoltLibCtx *)(voltObj->libraryCtx);
VtDestroyAlgorithmObject (&(signCtx->digestObject));
if (signCtx->LocalSignCtxDestroy != (VCtxDestroy)0)
signCtx->LocalSignCtxDestroy (obj, signCtx->localSignCtx);
if (signCtx->PadCtxDestroy != (VCtxDestroy)0)
signCtx->PadCtxDestroy (obj, signCtx->padCtx);
Z2Free (ctx);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -