📄 rsasigntype.c
字号:
/* Copyright 2005-2006, Voltage Security, all rights reserved.
*/
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "algobj.h"
#include "sign.h"
#include "rsa.h"
#include "mpint.h"
#include "errorctx.h"
int VtAlgorithmImplRSASignVerify (
VtAlgorithmObject *object,
Pointer info,
unsigned int flag
)
{
int status;
#if VOLT_ALIGNMENT != 1
unsigned int pad;
#endif
unsigned int bufferSize, offset;
VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
unsigned char *buffer = (unsigned char *)0;
VoltSignClassCtx *ctx = (VoltSignClassCtx *)0;
VoltRsaSignCtx *rsaCtx;
VtRSAInfo *rsaInfo;
VoltPaddingInfo paddingInfo;
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 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 a VtRSAInfo struct.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
rsaInfo = (VtRSAInfo *)info;
/* RSA Sign does not work without a padding scheme.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if (rsaInfo->padding == (VtPaddingScheme *)0)
break;
/* Allocate space for the VoltSignClassCtx and the localCtx.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = sizeof (VoltSignClassCtx) + sizeof (VoltRsaSignCtx);
#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
rsaCtx = (VoltRsaSignCtx *)(buffer + offset);
ctx->GetDigestObject = RSAGetDigestObject;
ctx->CheckSignatureInput = RSACheckSignatureInput;
ctx->SignData = RSASignData;
ctx->VerifyData = RSAVerifyData;
ctx->algorithm |= VOLT_SIGNATURE_ALG_RSA;
ctx->localSignCtx = (Pointer)rsaCtx;
ctx->LocalSignCtxDestroy = RSASignCtxDestroy;
obj->algClass = VOLT_CLASS_SIGNATURE | VOLT_CLASS_VERIFY;
obj->classCtx = (Pointer)ctx;
obj->ClassCtxDestroy = VoltSignCtxDestroy;
/* Load the padding.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
paddingInfo.info = rsaInfo->paddingInfo;
status = rsaInfo->padding (
(VtAlgorithmObject)obj, (VtPaddingInfo *)&paddingInfo,
VOLT_PADDING_SET_TYPE_FLAG);
} while (0);
if (status == 0)
return (0);
/* If there was an error, destroy what we created but did not return.
*/
if (ctx != (VoltSignClassCtx *)0)
VoltSignCtxDestroy ((Pointer)(*object), (Pointer)ctx);
/* Make sure the object does not have a reference to the ctx.
*/
obj->classCtx = (Pointer)0;
obj->ClassCtxDestroy = (VCtxDestroy)0;
VOLT_LOG_ERROR_INFO (
0, *object, status, 0, errorType,
(char *)0, "VtAlgorithmImplRSASignVerify", fnctLine, (char *)0)
return (status);
}
int RSAGetDigestObject (
VoltAlgorithmObject *obj,
VtAlgorithmObject *digestObj
)
{
int status;
VoltSignClassCtx *ctx = (VoltSignClassCtx *)(obj->classCtx);
VoltRsaSignCtx *rsaCtx = (VoltRsaSignCtx *)(ctx->localSignCtx);
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Ther DER decoder should have built a digest object in the
* rsaCtx. If not, there's nothing we can do.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_GET_INFO_UNAVAILABLE;
if (rsaCtx->digester == (VtAlgorithmObject)0)
break;
*digestObj = rsaCtx->digester;
status = 0;
} while (0);
VOLT_LOG_ERROR_INFO_COMPARE (
status, 0, obj, status, 0, VT_ERROR_TYPE_PRIMARY,
(char *)0, "RSAGetDigestObject", fnctLine, (char *)0)
return (status);
}
void RSASignCtxDestroy (
Pointer obj,
Pointer ctx
)
{
VoltObject *voltObj;
VoltLibCtx *libCtx;
VoltRsaSignCtx *rsaCtx;
/* Anything to destroy?
*/
if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
return;
voltObj = (VoltObject *)obj;
rsaCtx = (VoltRsaSignCtx *)ctx;
libCtx = (VoltLibCtx *)(voltObj->libraryCtx);
VtDestroyKeyObject (&(rsaCtx->tempKey));
/* Don't free the memory, it's part of the full signCtx.
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -