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

📄 bbparamtype.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 3 页
字号:
      ibeCache = (VoltIBECacheCtx *)VoltGetLibCtxInfo (
        libCtx, VOLT_LIB_CTX_INFO_TYPE_IBE_CACHE);

      /* Try to find the bbCtx in the cache.
       */
      if (ibeCache != (VoltIBECacheCtx *)0)
      {
        /* Get the reference against which we'll seach the cache.
         */
        VOLT_SET_ERROR_TYPE (errorType, 0)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VoltBuildIBECacheRefAlloc (
          libCtx, (VtBFType1IBEParamInfo *)0, paramInfo, &refBuf, &refLen);
        if (status != 0)
          break;

        VOLT_SET_ERROR_TYPE (errorType, 0)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = ibeCache->LockIBECache (
          (Pointer)ibeCache);
        if (status != 0)
          break;
        
        cacheLockAcquired = 1;
        
        VOLT_SET_ERROR_TYPE (errorType, 0)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = ibeCache->SearchCacheForBfCtx (
          (Pointer)ibeCache, VOLT_IBE_CTX_TYPE_BB, refBuf, refLen,
          (Pointer *)bbCtx);
        if (status != 0)
          break;

        /* If not NULL, we found one.
         */
        if (*bbCtx != (bb1_context_t *)0)
          break;
      }
    }

    /* No ctx in the cache, create a new ctx.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = bb1New (libCtx, mpCtx, &newCtx);
    if (status != 0)
      break;

    *bbCtx = newCtx;

    /* If there's no param set, the caller just wanted a new bfCtx.
     * Skip the rest of the work.
     */
    if (paramInfo == (VtBBType1IBEParamInfo *)0)
      break;

    primeLen = paramInfo->primeP.len;

    /* Allocate space for the a, b, and base and pub points.
     * Points for this function include only the x-ccordinate plus one
     * byte indicating the low order bit of the y-coordinate.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize = (6 * primeLen) + 6;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    bbParams.type = 1;
    bbParams.a = buffer;
    bbParams.aSize = primeLen;
    offset = primeLen;
    bbParams.b = buffer + offset;
    bbParams.bSize = primeLen;
    bbParams.b[primeLen - 1] = 1;
    offset += primeLen;

    bbParams.g = buffer + offset;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = CopyPointToXCoord (
      libCtx, &(paramInfo->basePointG), bbParams.g, primeLen + 1,
      &(bbParams.gSize), &fake);
    if (status != 0)
      break;

    saveFlag += fake;
    offset += bbParams.gSize;
    bbParams.g1 = buffer + offset;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = CopyPointToXCoord (
      libCtx, &(paramInfo->pubPointAlpha), bbParams.g1, primeLen + 1,
      &(bbParams.g1Size), &fake);
    if (status != 0)
      break;

    saveFlag += fake;
    offset += bbParams.g1Size;
    bbParams.g2 = buffer + offset;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = CopyPointToXCoord (
      libCtx, &(paramInfo->pubPointBeta), bbParams.g2, primeLen + 1,
      &(bbParams.g2Size), &fake);
    if (status != 0)
      break;

    saveFlag += fake;
    offset += bbParams.g2Size;
    bbParams.g3 = buffer + offset;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = CopyPointToXCoord (
      libCtx, &(paramInfo->pubPointGamma), bbParams.g3, primeLen + 1,
      &(bbParams.g3Size), &fake);
    if (status != 0)
      break;

    saveFlag += fake;

    /* Create and set the prime and subprime.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = zNew (mpCtx, &(bbParams.p));
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = zRawDecode (
      bbParams.p, (void *)(paramInfo->primeP.data),
      paramInfo->primeP.len);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = zNew (mpCtx, &(bbParams.order));
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = zRawDecode (
      bbParams.order, (void *)(paramInfo->subprimeQ.data),
      paramInfo->subprimeQ.len);
    if (status != 0)
      break;

    /* Load up these icParams into the bfCtx.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = bb1ContextImportFromIcPars (
      newCtx, &bbParams, accelCount, accelG, accelG1, accelG3,
      (VoltSurrenderCtx *)0, 0, (unsigned int *)0);
    if (status != 0)
      break;

    /* If the saveFlag is 0, add this ctx to the cache (if there is
     * one).
     */
    if ( (saveFlag == 0) && (ibeCache != (VoltIBECacheCtx *)0) )
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = ibeCache->AddBfCtxToCache (
        (Pointer)ibeCache, VOLT_IBE_CTX_TYPE_BB, (Pointer)newCtx);
      if (status != 0)
        break;
    }

  } while (0);

  if (cacheLockAcquired)
  {
    status2 = ibeCache->UnlockIBECache (
      (Pointer)ibeCache);
    if (status == 0)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = status2;
    }
  }
  
  if (bbParams.p != (z_t *)0)
    zDel (bbParams.p);
  if (bbParams.order != (z_t *)0)
    zDel (bbParams.order);

  if (buffer != (unsigned char *)0)
    Z2Free (buffer);
  if (refBuf != (unsigned char *)0)
    Z2Free (refBuf);

  /* If success, we're done.
   */
  if (status == 0)
    return (0);

  /* If error, destroy the bfCtx we created, it won't be returned.
   */
  *bbCtx = (bb1_context_t *)0;
  VoltReleaseBbCtx (libCtx, &newCtx);

  VOLT_LOG_ERROR_INFO (
    libCtx, 0, status, 0, errorType,
    (char *)0, "VoltGetBBCtxFromIBEParams", fnctLine, (char *)0)

  return (status);
}

void VoltReleaseBbCtx (
   VoltLibCtx *libCtx,
   bb1_context_t **bbCtx
   )
{
  VoltIBECacheCtx *ibeCache;

  /* Anything to release?
   */
  if (bbCtx == (bb1_context_t **)0)
    return;
  if (*bbCtx == (bb1_context_t *)0)
    return;

  /* Get the IBE cache, if there is one.
   */
  ibeCache = (VoltIBECacheCtx *)VoltGetLibCtxInfo (
    libCtx, VOLT_LIB_CTX_INFO_TYPE_IBE_CACHE);

  /* See if this bbCtx is in the cache.
   */
  if (ibeCache != (VoltIBECacheCtx *)0)
  {
    ibeCache->ReleaseBfCtxFromCache ((Pointer)ibeCache, (Pointer *)bbCtx);
    /* Did that call release it?
     */
    if (*bbCtx == (bb1_context_t *)0)
      return;
  }

  /* The ctx was not released, destroy it.
   */
  bb1Del (*bbCtx);
  *bbCtx = (bb1_context_t *)0;
}

int VoltBBGetYCoordFromX (
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   VtBBType1IBEParamInfo *paramInfo,
   unsigned char *xCoord,
   unsigned int xCoordLen,
   unsigned char *yCoord,
   unsigned int bufferSize,
   unsigned int *yCoordLen
   )
{
  int status;
  unsigned int primeLen;
  bb1_context_t *bbCtx = (bb1_context_t *)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Is the buffer big enough?
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_BUFFER_TOO_SMALL;
    primeLen = paramInfo->primeP.len;
    *yCoordLen = primeLen;
    if (bufferSize < primeLen)
      break;

    Z2Memset (yCoord, 0, bufferSize);

    /* Get a bbCtx from the params.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBbCtxFromIBEParams (
      libCtx, (VtMpIntCtx)mpCtx, paramInfo,
      0, (unsigned char *)0,  (unsigned char *)0,  (unsigned char *)0,
      &bbCtx);
    if (status != 0)
      break;

    /* Call the ICTk function that finds the coordinate.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = bb1CalculatePointY (
      yCoord, bufferSize, yCoordLen, xCoord, xCoordLen, bbCtx);

  } while (0);

  VoltReleaseBbCtx (libCtx, &bbCtx);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, libCtx, 0, status, 0, errorType,
    (char *)0, "VoltBBGetYCoordFromX", fnctLine, (char *)0)

  return (status);
}

int VoltBBGetXCoordFromY (
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   VtBBType1IBEParamInfo *paramInfo,
   unsigned char *yCoord,
   unsigned int yCoordLen,
   unsigned char *xCoord,
   unsigned int bufferSize,
   unsigned int *xCoordLen
   )
{
  int status;
  unsigned int primeLen, actualLen;
  bb1_context_t *bbCtx = (bb1_context_t *)0;
  unsigned char *buffer = (unsigned char *)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Is the buffer big enough?
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_BUFFER_TOO_SMALL;
    primeLen = paramInfo->primeP.len;
    *xCoordLen = primeLen;
    if (bufferSize < primeLen)
      break;

    Z2Memset (xCoord, 0, bufferSize);

    /* Get a bbCtx from the params.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBbCtxFromIBEParams (
      libCtx, (VtMpIntCtx)mpCtx, paramInfo,
      0, (unsigned char *)0,  (unsigned char *)0,  (unsigned char *)0,
      &bbCtx);
    if (status != 0)
      break;

    /* Create an intermediate buffer, the return from the bb function
     * will contain an extra byte.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (primeLen + 1, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;

    /* Call the ictk function that finds the coordinate.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = bb1CalculatePointX (
      buffer, primeLen + 1, &actualLen, yCoord, yCoordLen, bbCtx);
    if (status != 0)
      break;

    /* The return value has an extra byte at the beginning we don't
     * want.
     */
    Z2Memcpy (xCoord, buffer + 1, actualLen - 1);
    *xCoordLen = actualLen - 1;

  } while (0);

  VoltReleaseBbCtx (libCtx, &bbCtx);
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, libCtx, 0, status, 0, errorType,
    (char *)0, "VoltBBGetXCoordFromY", fnctLine, (char *)0)

  return (status);
}

static int CopyPointToXCoord (
   VoltLibCtx *libCtx,
   VtBBType1IBEPoint *point,
   unsigned char *xCoord,
   unsigned int bufferSize,
   unsigned int *xCoordLen,
   unsigned int *fake
   )
{
  int status;
  unsigned int xLen, lowBit;
  unsigned char *xBuf;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *fake = 0;

  do
  {
    /* This function does not accept infinity.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    if (point->isInfinity != 0)
      break;

    /* If there's no x-coordinate, use a fake point.
     */
    if (point->xCoord.data == (unsigned char *)0)
    {
      status = 0;
      Z2Memset (xCoord, 0, bufferSize);
      xCoord[bufferSize - 1] = 7;
      xCoord[0] = 3;
      *xCoordLen = bufferSize;
      *fake = 1;
      break;
    }

    /* How big does the output buffer need to be? This long or this
     * long plus 1.
     */
    xLen = point->xCoord.len;
    xBuf = xCoord;

    /* If lowBit is 0, we copy xCoord as given. Otherwise it will be 2
     * or 3, the first byte of the copied xCoord.
     */
    lowBit = 0;
    if (point->yCoord.data != (unsigned char *)0)
    {
      lowBit = (unsigned int)(point->yCoord.data[point->yCoord.len - 1]);
      lowBit &= 1;
      lowBit += 2;

      xLen++;
      xBuf++;
      xCoord[0] = (unsigned char)lowBit;
    }

    /* If the buffer is not big enough, error, not BUFFER_TOO_SMALL.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (bufferSize < xLen)
      break;

    Z2Memcpy (xBuf, point->xCoord.data, point->xCoord.len);
    *xCoordLen = xLen;

    status = 0;

  } while (0);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, libCtx, 0, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "CopyPointToXCoord", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

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