📄 dhkgtype.c
字号:
/* Copyright 2005-2006, Voltage Security, all rights reserved.
*/
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "keyobj.h"
#include "dh.h"
#include "mpint.h"
#include "random.h"
#include "errorctx.h"
/* This routine does the work. It allocates and fills in the contexts.
*
* @param obj The algorithm object to set.
* @param primeSizeBits The size of the primeP in bits.
* @param paramInfo Contains the params from which the keys will be
* generated.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV SetObjectDHKeyGen VOLT_PROTO_LIST ((
VoltKeyObject *obj,
unsigned int primeSizeBits,
VtDHParamInfo *paramInfo
));
int VtKeyPairGenDH (
VtKeyObject object,
Pointer info,
unsigned int flag,
VtRandomObject random
)
{
int status;
unsigned int msByte, primeSizeBits;
VoltKeyObject *obj = (VoltKeyObject *)object;
VtParameterObject pObj = (VtParameterObject)info;
VtDHParamInfo *paramInfo;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Check the flag, it should be VOLT_KEY_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_KEY_SET_TYPE_FLAG)
break;
/* Check the keyType of the object. It should be 0.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_SET;
if (obj->keyType != 0)
break;
/* The associated info should be a parameter object.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
/* Get the params.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtGetParameterParam (
pObj, VtParameterParamDHParams, (Pointer *)¶mInfo);
if (status != 0)
{
if (status == VT_ERROR_GET_INFO_UNAVAILABLE)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
break;
}
/* How big is the prime?
*/
primeSizeBits = 8;
msByte = (unsigned int)(paramInfo->primeP.data[0]) & 0xff;
while ((msByte & 0x80) == 0)
{
msByte <<= 1;
primeSizeBits--;
}
primeSizeBits += (paramInfo->primeP.len - 1) * 8;
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = SetObjectDHKeyGen (obj, primeSizeBits, paramInfo);
} while (0);
/* If everything worked, return 0.
*/
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 something went wrong, indicate that this object is not usable.
*/
obj->keyType = 0;
VOLT_LOG_ERROR_INFO (
0, object, status, 0, errorType,
(char *)0, "VtKeyPairGenDH", fnctLine, (char *)0)
return (status);
}
static int SetObjectDHKeyGen (
VoltKeyObject *obj,
unsigned int primeSizeBits,
VtDHParamInfo *paramInfo
)
{
int status;
unsigned int bufferSize, pLen, qLen, gLen;
unsigned char *buffer = (unsigned char *)0;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltDHKeyGenCtx *dhGenCtx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Get the lengths from the params.
*/
pLen = paramInfo->primeP.len;
qLen = paramInfo->subprimeQ.len;
gLen = paramInfo->baseG.len;
#if VOLT_BUILD == VOLT_BUILD_FIPS_SHARED
/* If this is the FIPS build, there must be a subprimeQ.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (qLen == 0)
break;
#endif
/* Allocate enough space for a DHKeyGenCtx. The buffers after the
* struct will be byte arrays, so no need to worry about alignment.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize = sizeof (VoltDHKeyGenCtx) + pLen + qLen + gLen;
buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
/* Locate the context.
*/
dhGenCtx = (VoltDHKeyGenCtx *)buffer;
/* Populate the context.
*/
dhGenCtx->mpCtx = (VtMpIntCtx)(obj->mpCtx);
dhGenCtx->primeSizeBits = primeSizeBits;
dhGenCtx->primeP.data = buffer + sizeof (VoltDHKeyGenCtx);
if (qLen != 0)
dhGenCtx->subprimeQ.data = dhGenCtx->primeP.data + pLen;
dhGenCtx->baseG.data = dhGenCtx->primeP.data + pLen + qLen;
/* Copy the params.
*/
Z2Memcpy (dhGenCtx->primeP.data, paramInfo->primeP.data, pLen);
dhGenCtx->primeP.len = pLen;
if (qLen != 0)
{
Z2Memcpy (dhGenCtx->subprimeQ.data, paramInfo->subprimeQ.data, qLen);
dhGenCtx->subprimeQ.len = qLen;
}
Z2Memcpy (dhGenCtx->baseG.data, paramInfo->baseG.data, gLen);
dhGenCtx->baseG.len = gLen;
obj->keyType = VOLT_KEY_ALG_DH | VOLT_KEY_TYPE_GEN_PAIR;
obj->GenerateKey = DHGenerateKeyPair;
obj->localGenerateCtx = (Pointer)dhGenCtx;
obj->LocalGenerateCtxDestroy = 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->keyType = 0;
VOLT_LOG_ERROR_INFO (
0, obj, status, 0, VT_ERROR_TYPE_PRIMARY,
(char *)0, "SetObjectDHKeyGen", fnctLine, (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -