📄 ibeparamtype.c
字号:
/* If something went wrong indicate that this object is not usable.
*/
obj->paramType = 0;
VOLT_LOG_ERROR (
(VtLibCtx)libCtx, status, errorType, fnctLine,
"VoltSetIBEParams", (char *)0)
return (status);
}
void BFType1IBEParamDataDestroy (
Pointer paramObj,
Pointer ctx
)
{
VoltParameterObject *obj = (VoltParameterObject *)paramObj;
VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
VoltBFType1IBEParams *theParams = (VoltBFType1IBEParams *)(obj->paramData);
/* Is there anything to destroy?
*/
if (obj->paramData == (Pointer)0)
return;
VoltReleaseBfCtx (libCtx, &(theParams->bfCtx));
/* The master secret, if present, was allocated separately.
*/
if (theParams->masterSecret.data != (unsigned char *)0)
Z2Free (theParams->masterSecret.data);
Z2Free (obj->paramData);
}
int BFType1IBECopyParams (
Pointer sourceParamObj,
Pointer destParamObj
)
{
int status;
VoltParameterObject *src = (VoltParameterObject *)sourceParamObj;
VoltParameterObject *dest = (VoltParameterObject *)destParamObj;
VoltLibCtx *libCtx = (VoltLibCtx *)(src->voltObject.libraryCtx);
VoltBFType1IBEParams *srcParams, *destParams;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
dest->paramType = src->paramType;
dest->CopyParams = src->CopyParams;
dest->GetParamData = src->GetParamData;
dest->ParamDataDestroy = src->ParamDataDestroy;
/* If there's no paramData, we're done.
*/
if (src->paramData == (Pointer)0)
break;
/* Is the paramData data or a handle?
* If a handle, we should not be in this code.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_GET;
if ((src->paramType & VOLT_PARAM_TYPE_MASK_DATA) != VOLT_PARAM_TYPE_DATA)
break;
/* Just call the Set function to copy the data.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltSetIBEParams (
(VtParameterObject)dest, (VtBFType1IBEParamInfo *)(src->paramData));
if (status != 0)
break;
/* If there's no master secret in the source, we're done. If not,
* copy it.
*/
srcParams = (VoltBFType1IBEParams *)(src->paramData);
if (srcParams->masterSecret.data == (unsigned char *)0)
break;
destParams = (VoltBFType1IBEParams *)(dest->paramData);
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
destParams->masterSecret.data = (unsigned char *)Z2Malloc (
srcParams->masterSecret.len, VOLT_MEMORY_SENSITIVE);
if (destParams->masterSecret.data == (unsigned char *)0)
break;
/* Copy src to dest.
*/
Z2Memcpy (
destParams->masterSecret.data, srcParams->masterSecret.data,
srcParams->masterSecret.len);
destParams->masterSecret.len = srcParams->masterSecret.len;
status = 0;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, src->voltObject.libraryCtx, status, errorType, fnctLine,
"BFType1IBECopyParams", (char *)0)
return (status);
}
int VoltCheckParams (
VoltLibCtx *libCtx,
VtBFType1IBEParamInfo *paramInfo
)
{
int status;
unsigned int primeLen;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* First, check the curve.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltCheckCurve (libCtx, &(paramInfo->curve));
if (status != 0)
break;
primeLen = paramInfo->curve.primeP.len;
/* The curve is fine, how about the pubPointP? First, it's not
* allowed to be infinity.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_ASSOCIATED_INFO;
if (paramInfo->pubPointP.isInfinity != 0)
break;
/* The length of the point's coordinates should be the same size as
* the primeP. Unless there is no y-coordinate, in which case the
* x-coordinate must have an extra byte.
*/
if (paramInfo->pubPointP.yCoord.data == (unsigned char *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
if (paramInfo->pubPointP.xCoord.data == (unsigned char *)0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (paramInfo->pubPointP.xCoord.len > (primeLen + 1))
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (paramInfo->pubPointP.yCoord.len != 0)
break;
}
else
{
VOLT_SET_FNCT_LINE (fnctLine)
if (paramInfo->pubPointP.yCoord.len > primeLen)
break;
if (paramInfo->pubPointP.xCoord.data != (unsigned char *)0)
{
VOLT_SET_FNCT_LINE (fnctLine)
if (paramInfo->pubPointP.xCoord.len > primeLen)
break;
}
else
{
VOLT_SET_FNCT_LINE (fnctLine)
if (paramInfo->pubPointP.xCoord.len != 0)
break;
}
}
status = 0;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, (VtLibCtx)libCtx, status, errorType, fnctLine,
"VoltCheckParams", (char *)0)
return (status);
}
int VoltBuildIBEParamsFromBfCtx (
VoltLibCtx *libCtx,
bf_context_t *bfCtx,
VtBFType1IBEParamInfo **theParams
)
{
int status;
unsigned int primePLen, subprimeQLen, bufferSize, offset;
unsigned char *buffer = (unsigned char *)0;
VtBFType1IBEParamInfo *localParams;
ic_parameters_simple_t *icParams = (ic_parameters_simple_t *)0;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
do
{
/* Get the data into an icParams struct, this is clearer than the
* bfCtx.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = bfIcParsNew (bfCtx, &icParams);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = bfContextExportToIcPars (icParams, bfCtx);
if (status != 0)
break;
/* Get the primePLen and subprimeQLen.
*/
primePLen = (unsigned int)zSizeInBase (icParams->prime, 2);
primePLen = (primePLen + 7) / 8;
subprimeQLen = (unsigned int)zSizeInBase (icParams->subprime, 2);
subprimeQLen = (subprimeQLen + 7) / 8;
/* Make sure the lengths are correct.
*/
VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_INVALID_INPUT;
if ((unsigned int)(icParams->baseSize) != (primePLen + 1))
break;
if ((unsigned int)(icParams->referenceSize) != (primePLen + 1))
break;
/* Now that we have the prime and subprime lengths, we can determine
* the total length. We need
* sizeof (VtBFType1IBEParamInfo)
* primeP (primePLen)
* subPrimeQ (subprimeQLen)
* basePointG (2 coordinates, 2 * primePLen)
* pubPointP (2 coordinates, 2 * primePlen)
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
bufferSize =
sizeof (VtBFType1IBEParamInfo) + (5 * primePLen) + subprimeQLen;
buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
if (buffer == (unsigned char *)0)
break;
Z2Memset (buffer, 0, bufferSize);
/* Locate the pointers.
*/
localParams = (VtBFType1IBEParamInfo *)buffer;
offset = sizeof (VtBFType1IBEParamInfo);
localParams->curve.primeP.data = buffer + offset;
localParams->curve.primeP.len = primePLen;
offset += primePLen;
localParams->curve.subprimeQ.data = buffer + offset;
localParams->curve.subprimeQ.len = subprimeQLen;
offset += subprimeQLen;
localParams->curve.basePointG.xCoord.data = buffer + offset;
offset += primePLen;
localParams->curve.basePointG.yCoord.data = buffer + offset;
offset += primePLen;
localParams->pubPointP.xCoord.data = buffer + offset;
offset += primePLen;
localParams->pubPointP.yCoord.data = buffer + offset;
/* Now get the values into the buffers.
*/
VOLT_SET_ERROR_TYPE (errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = zRawEncode (
(void *)(localParams->curve.primeP.data), primePLen,
icParams->prime);
if (status != 0)
break;
VOLT_SET_FNCT_LINE (fnctLine)
status = zRawEncode (
(void *)(localParams->curve.subprimeQ.data), subprimeQLen,
icParams->subprime);
if (status != 0)
break;
Z2Memcpy (
localParams->curve.basePointG.xCoord.data, icParams->base + 1,
icParams->baseSize - 1);
localParams->curve.basePointG.xCoord.len =
(unsigned int)(icParams->baseSize - 1);
/* The icParams base is actually only the x-coordinate. Call this
* routine to get y.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = bfCalculatePointY (
localParams->curve.basePointG.yCoord.data, primePLen,
&(localParams->curve.basePointG.yCoord.len),
icParams->base, icParams->baseSize, bfCtx);
if (status != 0)
break;
Z2Memcpy (
localParams->pubPointP.xCoord.data, icParams->reference + 1, primePLen);
localParams->pubPointP.xCoord.len =
(unsigned int)(icParams->referenceSize - 1);
/* The icParams pub point is actually only the x-coordinate. Call
* this routine to get y.
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = bfCalculatePointY (
localParams->pubPointP.yCoord.data, primePLen,
&(localParams->pubPointP.yCoord.len),
icParams->reference, icParams->referenceSize, bfCtx);
if (status != 0)
break;
/* Everything worked, set status to 0 and return the address of the
* params.
*/
status = 0;
*theParams = localParams;
} while (0);
if (icParams != (ic_parameters_simple_t *)0)
bfIcParsDel (icParams);
/* If no error, we're done.
*/
if (status == 0)
return (0);
/* If there was an error, free any memory we allocated.
*/
if (buffer != (unsigned char *)0)
Z2Free (buffer);
VOLT_LOG_ERROR (
(VtLibCtx)libCtx, status, errorType, fnctLine,
"VoltBuildIBEParamsFromBfCtx", (char *)0)
return (status);
}
void VoltDemolishIBEParams (
VoltLibCtx *libCtx,
VtBFType1IBEParamInfo **theParams
)
{
/* Is there anything to demolish?
*/
if (*theParams == (VtBFType1IBEParamInfo *)0)
return;
/* To build the struct, we allocated one block.
*/
Z2Free (*theParams);
}
int VoltGetBfCtxFromIBEParams (
VoltLibCtx *libCtx,
VtMpIntCtx mpIntCtx,
VtBFType1IBECurveInfo *curve,
VtBFType1IBEPoint *pubPointP,
unsigned int accelCount,
unsigned char *accelTable,
bf_context_t **bfCtx
)
{
int status, status2, saveFlag;
int cacheLockAcquired = 0;
unsigned int bufferSize, offset, pubYLen, refLen;
unsigned char *buffer = (unsigned char *)0;
unsigned char *refBuf = (unsigned char *)0;
VoltMpIntCtx *mpCtx = (VoltMpIntCtx *)mpIntCtx;
ic_parameters_simple_t icParams;
bf_context_t *newCtx = (bf_context_t *)0;
VoltIBECacheCtx *ibeCache = (VoltIBECacheCtx *)0;
VtBFType1IBEParamInfo bfParamInfo;
VOLT_DECLARE_ERROR_TYPE (errorType)
VOLT_DECLARE_FNCT_LINE (fnctLine)
Z2Memset (&icParams, 0, sizeof (ic_parameters_simple_t));
Z2Memset (&bfParamInfo, 0, sizeof (VtBFType1IBEParamInfo));
pubYLen = 0;
/* Initialize the saveFlag to 2. That means we have not yet seen the
* base point's x coordinate nor the public point's x coordinate.
* Each time we see one of those values we'll subtract 1 from
* saveFlag. If saveFlag is 0, save a created bfCtx in the cache. If
* not, don't.
*/
saveFlag = 2;
do
{
/* If there's no curve, the caller just wanted a new bfCtx.
*/
if (curve != (VtBFType1IBECurveInfo *)0)
{
/* Get the IBE cache, if there is one.
*/
ibeCache = (VoltIBECacheCtx *)VoltGetLibCtxInfo (
libCtx, VOLT_LIB_CTX_INFO_TYPE_IBE_CACHE);
/* Try to find the bfCtx in the cache.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -