⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bbparamtype.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Copyright 2005-2006, Voltage Security, all rights reserved.
 */
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "paramobj.h"
#include "ibe.h"
#include "ibecache.h"
#include "mpint.h"
#include "errorctx.h"

/* Gets the system parameters out of a parameter object.
 *
 * @param paramObj The object from which the params are 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 VoltParameterGetBBType1IBEParams VOLT_PROTO_LIST ((
   VtParameterObject object,
   Pointer *getInfo
));

/* Free the data fields of the coordinates of the point, memset
 * everything to 0.
 */
static void VOLT_CALLING_CONV FreePointData VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VtBBType1IBEPoint *thePoint
));

/* Given a VtBBType1IBEPoint, copy it into the xCoord buffer. This will
 * copy into the xCoord as the prefix byte (y-coordinate bit) and the
 * x-coordinate data.
 * <p>If the point is infinity, error.
 * <p>If the x-coordinate (with extra byte) won't fit in the buffer
 * (bufferSize), error (not BUFFER_TOO_SMALL).
 * <p>If the point has no x-coordinate, place a fake value into xCoord
 * and set fake to 1 (true, yes).
 * <p>If the point copy works, set fake to 0.
 */
static int VOLT_CALLING_CONV CopyPointToXCoord VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VtBBType1IBEPoint *point,
   unsigned char *xCoord,
   unsigned int bufferSize,
   unsigned int *xCoordLen,
   unsigned int *fake
));

int VtParameterParamBBType1IBEParams (
   VtParameterObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltParameterObject *obj = (VoltParameterObject *)object;
  VtBBType1IBEParamInfo *paramInfo = (VtBBType1IBEParamInfo *)info;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    if (flag == VOLT_PARAM_GET_TYPE_FLAG)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltParameterGetBBType1IBEParams (object, (Pointer *)info);
      break;
    }

    /* Check the flag, it should be VOLT_PARAM_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_PARAM_SET_TYPE_FLAG)
      break;

    /* Check the paramData of the object. It should be empty.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_SET;
    if (obj->paramData != (Pointer)0)
      break;

    /* The associated info should be a pointer to VtBBType1IBEParamInfo.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    /* If using this ParameterParam, the object must already have an
     * mpCtx loaded.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_PARAM_OBJ;
    if (obj->mpCtx == (VoltMpIntCtx *)0)
      break;

    /* The Set function will check the params.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltSetBBParams ((VtParameterObject)obj, paramInfo);

  } while (0);

  /* If everything worked, return 0.
   */
  if (status == 0)
    return (0);

  /* If something went wrong, indicate that this object is not usable.
   */
  obj->paramType = 0;

  VOLT_LOG_ERROR_INFO (
    0, object, status, 0, errorType,
    (char *)0, "VtParameterParamBBType1IBEParams", fnctLine, (char *)0)

  return (status);
}

int VoltParameterGetBBType1IBEParams (
   VtParameterObject object,
   Pointer *getInfo
   )
{
  int status;
  VoltParameterObject *obj = (VoltParameterObject *)object;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the paramType in the object, it should contain
     * VOLT_PARAM_TYPE_PARAMS and VOLT_PARAM_ALG_BB_TYPE_1.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_GET_INFO_UNAVAILABLE;
    if (obj->paramType == 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if ((obj->paramType & VOLT_PARAM_TYPE_MASK_ALG) !=
        VOLT_PARAM_ALG_BB_TYPE_1)
      break;
    if ((obj->paramType & VOLT_PARAM_TYPE_PARAMS) == 0)
      break;

    /* Are the parameters in data format? If so, we're done. (Set the
     * return values.)
     */
    *getInfo = obj->paramData;
    status = 0;
    if ((obj->paramType & VOLT_PARAM_TYPE_MASK_DATA) == VOLT_PARAM_TYPE_DATA)
      break;

    /* The data is not available, does the object have a GetData
     * function?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    *getInfo = (Pointer)0;
    status = VT_ERROR_INVALID_GET;
    if (obj->GetParamData == (VGetParamData)0)
      break;

    /* Call the Get function.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = obj->GetParamData ((VtParameterObject)obj, getInfo);

  } while (0);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, object, status, 0, errorType,
    (char *)0, "VoltParameterGetBBType1IBEParams", fnctLine, (char *)0)

  return (status);
}

int VoltSetBBParams (
   VtParameterObject paramObj,
   VtBBType1IBEParamInfo *paramInfo
   )
{
  int status;
  unsigned int bufferSize, offset;
  VoltParameterObject *obj = (VoltParameterObject *)paramObj;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  VoltBBType1IBEParams *theParams;
  VtBBType1IBEParamInfo newParamInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  Z2Memset (&newParamInfo, 0, sizeof (newParamInfo));

  do
  {
    /* Copy each of the points.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltBBCopyPoint (
      libCtx, obj->mpCtx, paramInfo, &(paramInfo->basePointG),
      &(newParamInfo.basePointG));
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltBBCopyPoint (
      libCtx, obj->mpCtx, paramInfo, &(paramInfo->pubPointAlpha),
      &(newParamInfo.pubPointAlpha));
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltBBCopyPoint (
      libCtx, obj->mpCtx, paramInfo, &(paramInfo->pubPointBeta),
      &(newParamInfo.pubPointBeta));
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltBBCopyPoint (
      libCtx, obj->mpCtx, paramInfo, &(paramInfo->pubPointGamma),
      &(newParamInfo.pubPointGamma));
    if (status != 0)
      break;

    /* Copy references to the prime and subprime.
     */
    newParamInfo.primeP.data = paramInfo->primeP.data;
    newParamInfo.primeP.len = paramInfo->primeP.len;
    newParamInfo.subprimeQ.data = paramInfo->subprimeQ.data;
    newParamInfo.subprimeQ.len = paramInfo->subprimeQ.len;

    /* Allocate space to hold the param struct, the prime, subprime,
     * base point and pub points. After the struct, all the data is byte
     * arrays, so there's no need to worry about alignment.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      sizeof (VoltBBType1IBEParams) + newParamInfo.primeP.len +
      newParamInfo.subprimeQ.len +
      newParamInfo.basePointG.xCoord.len +
      newParamInfo.basePointG.yCoord.len +
      newParamInfo.pubPointAlpha.xCoord.len +
      newParamInfo.pubPointAlpha.yCoord.len +
      newParamInfo.pubPointBeta.xCoord.len +
      newParamInfo.pubPointBeta.yCoord.len +
      newParamInfo.pubPointGamma.xCoord.len +
      newParamInfo.pubPointGamma.yCoord.len;
    buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the structs and data.
     */
    theParams = (VoltBBType1IBEParams *)buffer;
    offset = sizeof (VoltBBType1IBEParams);
    theParams->paramInfo.primeP.data = buffer + offset;
    offset += newParamInfo.primeP.len;
    theParams->paramInfo.subprimeQ.data = buffer + offset;
    offset += newParamInfo.subprimeQ.len;
    theParams->paramInfo.basePointG.xCoord.data =  buffer + offset;
    offset += newParamInfo.basePointG.xCoord.len;
    theParams->paramInfo.basePointG.yCoord.data =  buffer + offset;
    offset += newParamInfo.basePointG.yCoord.len;
    theParams->paramInfo.pubPointAlpha.xCoord.data = buffer + offset;
    offset += newParamInfo.pubPointAlpha.xCoord.len;
    theParams->paramInfo.pubPointAlpha.yCoord.data = buffer + offset;
    offset += newParamInfo.pubPointAlpha.yCoord.len;
    theParams->paramInfo.pubPointBeta.xCoord.data = buffer + offset;
    offset += newParamInfo.pubPointBeta.xCoord.len;
    theParams->paramInfo.pubPointBeta.yCoord.data = buffer + offset;
    offset += newParamInfo.pubPointBeta.yCoord.len;
    theParams->paramInfo.pubPointGamma.xCoord.data = buffer + offset;
    offset += newParamInfo.pubPointGamma.xCoord.len;
    theParams->paramInfo.pubPointGamma.yCoord.data = buffer + offset;
    offset += newParamInfo.pubPointGamma.yCoord.len;

    /* Fill in fields and copy data.
     */
    Z2Memcpy (
      theParams->paramInfo.primeP.data, newParamInfo.primeP.data,
      newParamInfo.primeP.len);
    theParams->paramInfo.primeP.len = newParamInfo.primeP.len;

    Z2Memcpy (
      theParams->paramInfo.subprimeQ.data, newParamInfo.subprimeQ.data,
      newParamInfo.subprimeQ.len);
    theParams->paramInfo.subprimeQ.len = newParamInfo.subprimeQ.len;

    theParams->paramInfo.basePointG.isInfinity =
      newParamInfo.basePointG.isInfinity;

    Z2Memcpy (
      theParams->paramInfo.basePointG.xCoord.data,
      newParamInfo.basePointG.xCoord.data,
      newParamInfo.basePointG.xCoord.len);
    theParams->paramInfo.basePointG.xCoord.len =
      newParamInfo.basePointG.xCoord.len;

    Z2Memcpy (
      theParams->paramInfo.basePointG.yCoord.data,
      newParamInfo.basePointG.yCoord.data,
      newParamInfo.basePointG.yCoord.len);
    theParams->paramInfo.basePointG.yCoord.len =
      newParamInfo.basePointG.yCoord.len;

    theParams->paramInfo.pubPointAlpha.isInfinity =
      newParamInfo.pubPointAlpha.isInfinity;

    Z2Memcpy (
      theParams->paramInfo.pubPointAlpha.xCoord.data,
      newParamInfo.pubPointAlpha.xCoord.data,
      newParamInfo.pubPointAlpha.xCoord.len);
    theParams->paramInfo.pubPointAlpha.xCoord.len =
      newParamInfo.pubPointAlpha.xCoord.len;

    Z2Memcpy (
      theParams->paramInfo.pubPointAlpha.yCoord.data,
      newParamInfo.pubPointAlpha.yCoord.data,
      newParamInfo.pubPointAlpha.yCoord.len);
    theParams->paramInfo.pubPointAlpha.yCoord.len =
      newParamInfo.pubPointAlpha.yCoord.len;

    theParams->paramInfo.pubPointBeta.isInfinity =
      newParamInfo.pubPointBeta.isInfinity;

    Z2Memcpy (
      theParams->paramInfo.pubPointBeta.xCoord.data,
      newParamInfo.pubPointBeta.xCoord.data,
      newParamInfo.pubPointBeta.xCoord.len);
    theParams->paramInfo.pubPointBeta.xCoord.len =
      newParamInfo.pubPointBeta.xCoord.len;

    Z2Memcpy (
      theParams->paramInfo.pubPointBeta.yCoord.data,
      newParamInfo.pubPointBeta.yCoord.data,
      newParamInfo.pubPointBeta.yCoord.len);
    theParams->paramInfo.pubPointBeta.yCoord.len =
      newParamInfo.pubPointBeta.yCoord.len;

    theParams->paramInfo.pubPointGamma.isInfinity =
      newParamInfo.pubPointGamma.isInfinity;

    Z2Memcpy (
      theParams->paramInfo.pubPointGamma.xCoord.data,
      newParamInfo.pubPointGamma.xCoord.data,
      newParamInfo.pubPointGamma.xCoord.len);
    theParams->paramInfo.pubPointGamma.xCoord.len =
      newParamInfo.pubPointGamma.xCoord.len;

    Z2Memcpy (
      theParams->paramInfo.pubPointGamma.yCoord.data,
      newParamInfo.pubPointGamma.yCoord.data,
      newParamInfo.pubPointGamma.yCoord.len);
    theParams->paramInfo.pubPointGamma.yCoord.len =
      newParamInfo.pubPointGamma.yCoord.len;

    /* Set these values "early" so we have the destructor in place if
     * something goes wrong.
     */
    obj->paramType =
      VOLT_PARAM_ALG_BB_TYPE_1 | VOLT_PARAM_TYPE_PARAMS |
      VOLT_PARAM_TYPE_CONTENTS | VOLT_PARAM_TYPE_DATA;
    obj->paramData = (Pointer)theParams;
    obj->CopyParams = BBType1IBECopyParams;
    obj->ParamDataDestroy = BBType1IBEParamDataDestroy;

    /* Now get the bbCtx.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBbCtxFromIBEParams (
      libCtx, (VtMpIntCtx)(obj->mpCtx), &(theParams->paramInfo),
      0, (unsigned char *)0,  (unsigned char *)0,  (unsigned char *)0,
      &(theParams->bbCtx));

  } while (0);

  FreePointData (libCtx, &(newParamInfo.basePointG));
  FreePointData (libCtx, &(newParamInfo.pubPointAlpha));
  FreePointData (libCtx, &(newParamInfo.pubPointBeta));
  FreePointData (libCtx, &(newParamInfo.pubPointGamma));

  /* If everything worked, return 0.
   */
  if (status == 0)
    return (0);

  /* If something went wrong indicate that this object is not usable.
   */
  obj->paramType = 0;

  VOLT_LOG_ERROR_INFO (
    0, paramObj, status, 0, errorType,
    (char *)0, "VoltSetBBParams", fnctLine, (char *)0)

  return (status);
}

int VoltBBCopyPoint (
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   VtBBType1IBEParamInfo *paramInfo,
   VtBBType1IBEPoint *oldPoint,
   VtBBType1IBEPoint *newPoint
   )
{
  int status;
  unsigned int primeLen, theXLen;
  unsigned char *theX;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    primeLen = paramInfo->primeP.len;

    /* Because the x-coordinate might contain an extra byte, copy the
     * pointer to a variable we can adjust.
     */
    theX = oldPoint->xCoord.data;
    theXLen = oldPoint->xCoord.len;

    /* This function rejects infinity.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (oldPoint->isInfinity != 0)
      break;

    /* Allocate space to hold the computed or copied x and y.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newPoint->yCoord.data = (unsigned char *)Z2Malloc (
      primeLen, VOLT_MEMORY_SENSITIVE);
    if (newPoint->yCoord.data == (unsigned char *)0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    newPoint->xCoord.data = (unsigned char *)Z2Malloc (
      primeLen, VOLT_MEMORY_SENSITIVE);
    if (newPoint->xCoord.data == (unsigned char *)0)
      break;

    /* If there is no y-coordinate, compute it.
     */
    if (oldPoint->yCoord.data == (unsigned char *)0)
    {
      /* We can't compute y without the x. And it must be in the
       * appropriate format.
       */
      VOLT_SET_FNCT_LINE (fnctLine)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -