📄 ibeprikey.c
字号:
/* Copyright 2003-2004, Voltage Security, all rights reserved.
*/
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "keyobj.h"
#include "ibe.h"
#include "errorctx.h"
/* Gets the key data out of a key object.
*
* @param object The object from which the data is to be extracted.
* @param getInfo The address where the function will deposit the
* pointer to the info.
* @return an int, 0 if the function completed successfully or a
* non-zero error code.
*/
static int VOLT_CALLING_CONV VoltKeyGetBFType1IBEPrivate VOLT_PROTO_LIST ((
VtKeyObject object,
Pointer *getInfo
));
int VtKeyParamBFType1IBEPrivate (
VtKeyObject object,
Pointer info,
unsigned int flag
)
{
int status;
unsigned int primeLen, bufferSize, offset;
VoltKeyObject *obj = (VoltKeyObject *)object;
VtBFType1IBEPriKeyInfo *inputInfo = (VtBFType1IBEPriKeyInfo *)info;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltParameterObject *pObj;
VtBFType1IBEParamInfo *paramData;
VoltBFType1IBEPriKeyData *localData;
unsigned char *buffer = (unsigned char *)0;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
if (flag == VOLT_KEY_GET_TYPE_FLAG)
{
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltKeyGetBFType1IBEPrivate (object, (Pointer *)info);
break;
}
/* 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 in 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 pointer to a
* VtBFType1IBEPriKeyInfo struct.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (info == (Pointer)0)
break;
/* Valid param object?
*/
VOLT_SET_FNCT_LINE (fnctLine)
pObj = (VoltParameterObject *)(inputInfo->ibeParams);
if (pObj == (VoltParameterObject *)0)
break;
if (VOLT_OBJECT_TYPE_NOT_EQUAL (pObj, VOLT_OBJECT_TYPE_PARAMETER))
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (pObj->paramData == (Pointer)0)
break;
if ((pObj->paramType & VOLT_PARAM_TYPE_MASK_ALG) !=
VOLT_PARAM_ALG_IBE_TYPE_1)
break;
if ((pObj->paramType & VOLT_PARAM_TYPE_PARAMS) == 0)
break;
if ((pObj->paramType & VOLT_PARAM_TYPE_CONTENTS) == 0)
break;
/* We need the params out of the param object.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtGetParameterParam (
inputInfo->ibeParams, VtParameterParamBFType1IBEParams,
(Pointer *)¶mData);
if (status != 0)
break;
primeLen = paramData->curve.primeP.len;
/* If using this KeyParam, the object must already have an
* mpCtx loaded.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_KEY_OBJ;
if (obj->mpCtx == (VoltMpIntCtx *)0)
break;
/* Is there a private point?
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (inputInfo->privatePoint.isInfinity != 0)
break;
if (inputInfo->privatePoint.xCoord.data == (unsigned char *)0)
{
/* If there's no xCoord, there had better be a yCoord.
*/
if (inputInfo->privatePoint.yCoord.data == (unsigned char *)0)
break;
if (inputInfo->privatePoint.yCoord.len > (primeLen + 1))
break;
}
else
{
/* There is an xCoord.
*/
if (inputInfo->privatePoint.xCoord.len > (primeLen + 1))
break;
if (inputInfo->privatePoint.yCoord.data != (unsigned char *)0)
{
if (inputInfo->privatePoint.yCoord.len > primeLen)
break;
}
}
/* Create a VoltIBEPriKeyData struct.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize =
sizeof (VoltBFType1IBEPriKeyData) + inputInfo->encodedId.len +
(2 * primeLen) + 1;
buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
localData = (VoltBFType1IBEPriKeyData *)buffer;
offset = sizeof (VoltBFType1IBEPriKeyData);
localData->keyInfo.encodedId.data = buffer + offset;
offset += inputInfo->encodedId.len;
localData->keyInfo.privatePoint.xCoord.data = buffer + offset;
offset += (primeLen + 1);
localData->keyInfo.privatePoint.yCoord.data = buffer + offset;
localData->primeLen = primeLen;
/* Clone the parameter object.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VtCloneObject (
(Pointer)pObj, (Pointer *)&(localData->keyInfo.ibeParams));
if (status != 0)
break;
/* Copy the encoding into the local buffer.
*/
Z2Memcpy (
localData->keyInfo.encodedId.data, inputInfo->encodedId.data,
inputInfo->encodedId.len);
localData->keyInfo.encodedId.len = inputInfo->encodedId.len;
/* Copy the private point.
*/
localData->keyInfo.privatePoint.isInfinity =
inputInfo->privatePoint.isInfinity;
if (inputInfo->privatePoint.xCoord.data != (unsigned char *)0)
{
Z2Memcpy (
localData->keyInfo.privatePoint.xCoord.data,
inputInfo->privatePoint.xCoord.data,
inputInfo->privatePoint.xCoord.len);
localData->keyInfo.privatePoint.xCoord.len =
inputInfo->privatePoint.xCoord.len;
if (inputInfo->privatePoint.yCoord.data != (unsigned char *)0)
{
Z2Memcpy (
localData->keyInfo.privatePoint.yCoord.data,
inputInfo->privatePoint.yCoord.data,
inputInfo->privatePoint.yCoord.len);
localData->keyInfo.privatePoint.yCoord.len =
inputInfo->privatePoint.yCoord.len;
}
else
{
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetYCoordFromX (
libCtx, obj->mpCtx, paramData,
localData->keyInfo.privatePoint.xCoord.data,
localData->keyInfo.privatePoint.xCoord.len,
localData->keyInfo.privatePoint.yCoord.data, primeLen,
&(localData->keyInfo.privatePoint.yCoord.len));
if (status != 0)
break;
/* If there was no yCoord, the xCoord had an extra byte.
*/
localData->keyInfo.privatePoint.xCoord.data++;
localData->keyInfo.privatePoint.xCoord.len--;
}
}
else
{
/* There's no xCoord.
*/
Z2Memcpy (
localData->keyInfo.privatePoint.yCoord.data,
inputInfo->privatePoint.yCoord.data,
inputInfo->privatePoint.yCoord.len);
localData->keyInfo.privatePoint.yCoord.len =
inputInfo->privatePoint.yCoord.len;
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetXCoordFromY (
libCtx, obj->mpCtx, paramData,
localData->keyInfo.privatePoint.yCoord.data,
localData->keyInfo.privatePoint.yCoord.len,
localData->keyInfo.privatePoint.xCoord.data, primeLen,
&(localData->keyInfo.privatePoint.xCoord.len));
if (status != 0)
break;
}
obj->keyType =
VOLT_KEY_ALG_IBE_TYPE_1 | VOLT_KEY_TYPE_PRIVATE |
VOLT_KEY_TYPE_ENCRYPT | VOLT_KEY_TYPE_DATA;
obj->keyData = (Pointer)localData;
obj->KeyDataDestroy = IBEPriKeyDataDestroy;
status = 0;
} while (0);
/* If everything worked, return 0.
*/
if (status == 0)
return (0);
/* If something went wrong, destroy anything we created.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR (
libCtx, status, errorType, fnctLine, "VtKeyParamBFType1IBEPrivate",
(char *)0)
return (status);
}
static int VoltKeyGetBFType1IBEPrivate (
VtKeyObject object,
Pointer *getInfo
)
{
int status;
VoltKeyObject *obj = (VoltKeyObject *)object;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Is there data?
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_GET_INFO_UNAVAILABLE;
if (obj->keyData == (Pointer)0)
break;
/* Make sure the key data is appropriate.
*/
VOLT_SET_FNCT_LINE (fnctLine)
if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) !=
VOLT_KEY_ALG_IBE_TYPE_1)
break;
if ((obj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0)
break;
/* Is the key data the actual data? If so, we're done.
*/
status = 0;
*getInfo = obj->keyData;
if ((obj->keyType & VOLT_KEY_TYPE_MASK_DATA) == VOLT_KEY_TYPE_DATA)
break;
/* The data is not available, does the object have a GetData
* function?
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_GET_INFO_UNAVAILABLE;
*getInfo = (Pointer)0;
if (obj->GetKeyData == (VGetKeyData)0)
break;
/* Call the Get function.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = obj->GetKeyData ((VtKeyObject)obj, getInfo);
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, libCtx, status, errorType, fnctLine,
"VoltKeyGetBFType1IBEPrivate", (char *)0)
return (status);
}
void IBEPriKeyDataDestroy (
Pointer obj,
Pointer ctx
)
{
VoltObject *voltObj = (VoltObject *)obj;
VoltLibCtx *libCtx;
VoltBFType1IBEPriKeyData *localData = (VoltBFType1IBEPriKeyData *)ctx;
if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
return;
libCtx = (VoltLibCtx *)(voltObj->libraryCtx);
VtDestroyParameterObject (&(localData->keyInfo.ibeParams));
Z2Free (localData);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -