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

📄 ibeparamtype.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Copyright 2003-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 "mpint.h"
#include "ibecache.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 VoltParameterGetBFType1IBEParams VOLT_PROTO_LIST ((
   VtParameterObject object,
   Pointer *getInfo
));

int VtParameterParamBFType1IBEParams (
   VtParameterObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltParameterObject *obj = (VoltParameterObject *)object;
  VtBFType1IBEParamInfo *paramInfo = (VtBFType1IBEParamInfo *)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 = VoltParameterGetBFType1IBEParams (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 VtBFType1IBEParamInfo.
     */
    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;

    /* Are the input params acceptable?
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCheckParams (
      (VoltLibCtx *)(obj->voltObject.libraryCtx), paramInfo);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltSetIBEParams ((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 (
    obj->voltObject.libraryCtx, status, errorType, fnctLine,
    "VtParameterParamBFType1IBEParams", (char *)0)

  return (status);
}

int VoltParameterGetBFType1IBEParams (
   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_IBE_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_IBE_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_COMPARE (
    status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
    "VoltParameterGetBFType1IBEParams", (char *)0)

  return (status);
}

int VoltSetIBEParams (
   VtParameterObject paramObj,
   VtBFType1IBEParamInfo *paramInfo
   )
{
  int status;
  unsigned int bufferSize, offset, baseYLen, baseXLen, pubYLen, pubXLen;
  VoltParameterObject *obj = (VoltParameterObject *)paramObj;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  unsigned char *baseY = (unsigned char *)0;
  unsigned char *baseX = (unsigned char *)0;
  unsigned char *pubY = (unsigned char *)0;
  unsigned char *pubX = (unsigned char *)0;
  VoltBFType1IBEParams *theParams;
  VtBFType1IBEParamInfo newParamInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* Use newParamInfo in case there are no y-coordinates.
   */
  Z2Memcpy (&newParamInfo, paramInfo, sizeof (newParamInfo));

  do
  {
    /* If the base point y-coordinate is not given, get it.
     */
    if (newParamInfo.curve.basePointG.yCoord.data == (unsigned char *)0)
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      baseYLen = newParamInfo.curve.primeP.len;
      baseY = (unsigned char *)Z2Malloc (baseYLen, 0);
      if (baseY == (unsigned char *)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetYCoordFromX (
        libCtx, obj->mpCtx, (bf_context_t *)0, &newParamInfo,
        newParamInfo.curve.basePointG.xCoord.data,
        newParamInfo.curve.basePointG.xCoord.len,
        baseY, baseYLen, &baseYLen);
      if (status != 0)
        break;

      newParamInfo.curve.basePointG.yCoord.data = baseY;
      newParamInfo.curve.basePointG.yCoord.len = baseYLen;
      newParamInfo.curve.basePointG.xCoord.data++;
      newParamInfo.curve.basePointG.xCoord.len--;
    }

    /* If the base point x-coordinate is not given, get it.
     */
    if (newParamInfo.curve.basePointG.xCoord.data == (unsigned char *)0)
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      baseXLen = newParamInfo.curve.primeP.len;
      baseX = (unsigned char *)Z2Malloc (baseXLen, 0);
      if (baseX == (unsigned char *)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetXCoordFromY (
        libCtx, obj->mpCtx, &newParamInfo,
        newParamInfo.curve.basePointG.yCoord.data,
        newParamInfo.curve.basePointG.yCoord.len,
        baseX, baseXLen, &baseXLen);
      if (status != 0)
        break;

      newParamInfo.curve.basePointG.xCoord.data = baseX;
      newParamInfo.curve.basePointG.xCoord.len = baseXLen;
    }

    /* If the pubPoint y-coordinate is not given, get it.
     */
    if (newParamInfo.pubPointP.yCoord.data == (unsigned char *)0)
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      pubYLen = newParamInfo.curve.primeP.len;
      pubY = (unsigned char *)Z2Malloc (pubYLen, 0);
      if (pubY == (unsigned char *)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetYCoordFromX (
        libCtx, obj->mpCtx, (bf_context_t *)0, &newParamInfo,
        newParamInfo.pubPointP.xCoord.data,
        newParamInfo.pubPointP.xCoord.len,
        pubY, pubYLen, &pubYLen);
      if (status != 0)
        break;

      newParamInfo.pubPointP.yCoord.data = pubY;
      newParamInfo.pubPointP.yCoord.len = pubYLen;
      newParamInfo.pubPointP.xCoord.data++;
      newParamInfo.pubPointP.xCoord.len--;
    }

    /* If the pubPoint x-coordinate is not given, get it.
     */
    if (newParamInfo.pubPointP.xCoord.data == (unsigned char *)0)
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_MEMORY;
      pubXLen = newParamInfo.curve.primeP.len;
      pubX = (unsigned char *)Z2Malloc (pubXLen, 0);
      if (pubX == (unsigned char *)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetXCoordFromY (
        libCtx, obj->mpCtx, &newParamInfo,
        newParamInfo.pubPointP.yCoord.data,
        newParamInfo.pubPointP.yCoord.len,
        pubX, pubXLen, &pubXLen);
      if (status != 0)
        break;

      newParamInfo.pubPointP.xCoord.data = pubX;
      newParamInfo.pubPointP.xCoord.len = pubXLen;
    }

    /* Allocate space to hold the param struct, the prime, subprime,
     * base point and pub point. 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 (VoltBFType1IBEParams) + newParamInfo.curve.primeP.len +
      newParamInfo.curve.subprimeQ.len +
      newParamInfo.curve.basePointG.xCoord.len +
      newParamInfo.curve.basePointG.yCoord.len +
      newParamInfo.pubPointP.xCoord.len + newParamInfo.pubPointP.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 = (VoltBFType1IBEParams *)buffer;
    offset = sizeof (VoltBFType1IBEParams);
    theParams->paramInfo.curve.primeP.data = buffer + offset;
    offset += newParamInfo.curve.primeP.len;
    theParams->paramInfo.curve.subprimeQ.data = buffer + offset;
    offset += newParamInfo.curve.subprimeQ.len;
    theParams->paramInfo.curve.basePointG.xCoord.data =  buffer + offset;
    offset += newParamInfo.curve.basePointG.xCoord.len;
    theParams->paramInfo.curve.basePointG.yCoord.data =  buffer + offset;
    offset += newParamInfo.curve.basePointG.yCoord.len;
    theParams->paramInfo.pubPointP.xCoord.data = buffer + offset;
    offset += newParamInfo.pubPointP.xCoord.len;
    theParams->paramInfo.pubPointP.yCoord.data = buffer + offset;

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

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

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

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

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

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

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

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

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

  } while (0);

  if (baseY != (unsigned char *)0)
    Z2Free (baseY);
  if (baseX != (unsigned char *)0)
    Z2Free (baseX);
  if (pubY != (unsigned char *)0)
    Z2Free (pubY);
  if (pubX != (unsigned char *)0)
    Z2Free (pubX);

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

⌨️ 快捷键说明

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