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

📄 ibeparamtype.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 3 页
字号:
      if (ibeCache != (VoltIBECacheCtx *)0)
      {
        bfParamInfo.curve = *curve;
        bfParamInfo.pubPointP = *pubPointP;

        /* Get the reference against which we'll seach the cache.
         */
        VOLT_SET_ERROR_TYPE (errorType, 0)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VoltBuildIBECacheRefAlloc (
          libCtx, &bfParamInfo, (VtBBType1IBEParamInfo *)0, &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_BF, refBuf, refLen,
          (Pointer *)bfCtx);
        if (status != 0)
          break;

        /* If not NULL, we found one.
         */
        if (*bfCtx != (bf_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 = bfNew (libCtx, mpCtx, &newCtx);
    if (status != 0)
      break;

    *bfCtx = newCtx;

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

    /* 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 = (4 * (curve->primeP.len)) + 2;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    icParams.type = 1;
    icParams.a = buffer;
    icParams.aSize = curve->primeP.len;
    offset = curve->primeP.len;
    icParams.b = buffer + offset;
    icParams.bSize = curve->primeP.len;
    icParams.b[icParams.bSize - 1] = 1;
    offset += curve->primeP.len;

    /* The base is the basePointG. If both coordinates are given,
     * isolate the low order bit of y. If only the x-coordinate is given,
     * it is assumed the low order bit of y is represented in the
     * leading octet of x.
     */
    icParams.base = buffer + offset;
    if (curve->basePointG.yCoord.data != (unsigned char *)0)
    {
      icParams.base[0] =
        curve->basePointG.yCoord.data[curve->basePointG.yCoord.len - 1];
      icParams.base[0] &= 1;
      icParams.base[0] += 2;
      Z2Memcpy (
        icParams.base + 1, curve->basePointG.xCoord.data,
        curve->basePointG.xCoord.len);
      icParams.baseSize = curve->basePointG.xCoord.len + 1;
      offset += (curve->basePointG.xCoord.len + 1);
    }
    else
    {
      Z2Memcpy (
        icParams.base, curve->basePointG.xCoord.data,
        curve->basePointG.xCoord.len);
      icParams.baseSize = curve->basePointG.xCoord.len;
      offset += curve->basePointG.xCoord.len;
    }

    /* Do we actually have an x-coordinate?
     */
    if (icParams.baseSize > 1)
      saveFlag--;

    /* If the caller passed in a pub point, use it, if not, set the
     * reference to the base point as a "filler".
     */
    icParams.reference = icParams.base;
    icParams.referenceSize = icParams.baseSize;
    if (pubPointP != (VtBFType1IBEPoint *)0)
    {
      if (pubPointP->xCoord.data != (unsigned char *)0)
      {
        saveFlag--;
        icParams.reference = buffer + offset;
        if (pubPointP->yCoord.data != (unsigned char *)0)
        {
          icParams.reference[0] =
            pubPointP->yCoord.data[pubPointP->yCoord.len - 1];
          icParams.reference[0] &= 1;
          icParams.reference[0] += 2;
          Z2Memcpy (
            icParams.reference + 1, pubPointP->xCoord.data,
            pubPointP->xCoord.len);
          icParams.referenceSize = pubPointP->xCoord.len + 1;
        }
        else
        {
          Z2Memcpy (
            icParams.reference, pubPointP->xCoord.data,
            pubPointP->xCoord.len);
          icParams.referenceSize = pubPointP->xCoord.len;
        }
      }
    }

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

    VOLT_SET_FNCT_LINE (fnctLine)
    status = zRawDecode (
      icParams.prime, (void *)(curve->primeP.data),
      curve->primeP.len);
    if (status != 0)
      break;

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

    VOLT_SET_FNCT_LINE (fnctLine)
    status = zRawDecode (
      icParams.subprime, (void *)(curve->subprimeQ.data),
      curve->subprimeQ.len);
    if (status != 0)
      break;

    /* Load up these icParams into the bfCtx.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = bfContextImportFromIcPars (
      newCtx, &icParams, accelCount, accelTable,
      (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_BF, (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 (icParams.prime != (z_t *)0)
    zDel (icParams.prime);
  if (icParams.subprime != (z_t *)0)
    zDel (icParams.subprime);

  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.
   */
  *bfCtx = (bf_context_t *)0;
  VoltReleaseBfCtx (libCtx, &newCtx);

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, errorType, fnctLine,
    "VoltGetBfCtxFromIBEParams", (char *)0)

  return (status);
}

void VoltReleaseBfCtx (
   VoltLibCtx *libCtx,
   bf_context_t **bfCtx
   )
{
  VoltIBECacheCtx *ibeCache;

  /* Anything to release?
   */
  if (bfCtx == (bf_context_t **)0)
    return;
  if (*bfCtx == (bf_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 bfCtx is in the cache.
   */
  if (ibeCache != (VoltIBECacheCtx *)0)
  {
    ibeCache->ReleaseBfCtxFromCache ((Pointer)ibeCache, (Pointer *)bfCtx);
    /* Did that call release it?
     */
    if (*bfCtx == (bf_context_t *)0)
      return;
  }

  /* The ctx was not released, destroy it.
   */
  bfDel (*bfCtx);
  *bfCtx = (bf_context_t *)0;
}

int VoltGetYCoordFromX (
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   bf_context_t *inputCtx,
   VtBFType1IBEParamInfo *paramInfo,
   unsigned char *xCoord,
   unsigned int xCoordLen,
   unsigned char *yCoord,
   unsigned int bufferSize,
   unsigned int *yCoordLen
   )
{
  int status;
  unsigned int primeLen;
  bf_context_t *bfCtx;
  bf_context_t *newCtx = (bf_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->curve.primeP.len;
    *yCoordLen = primeLen;
    if (bufferSize < primeLen)
      break;

    Z2Memset (yCoord, 0, bufferSize);

    /* If there's no bfCtx input, get one from the params.
     */
    bfCtx = inputCtx;
    if (inputCtx == (bf_context_t *)0)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltGetBfCtxFromIBEParams (
        libCtx, (VtMpIntCtx)mpCtx, &(paramInfo->curve),
        &(paramInfo->pubPointP), 0, (unsigned char *)0, &newCtx);
      if (status != 0)
        break;

      bfCtx = newCtx;
    }

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

  } while (0);

  VoltReleaseBfCtx (libCtx, &newCtx);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, errorType, fnctLine,
    "VoltGetYCoordFromX", (char *)0)

  return (status);
}

int VoltGetXCoordFromY (
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   VtBFType1IBEParamInfo *paramInfo,
   unsigned char *yCoord,
   unsigned int yCoordLen,
   unsigned char *xCoord,
   unsigned int bufferSize,
   unsigned int *xCoordLen
   )
{
  int status;
  unsigned int primeLen, actualLen;
  bf_context_t *bfCtx = (bf_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->curve.primeP.len;
    *xCoordLen = primeLen;
    if (bufferSize < primeLen)
      break;

    Z2Memset (xCoord, 0, bufferSize);

    /* Get a bfCtx from the params.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBfCtxFromIBEParams (
      libCtx, (VtMpIntCtx)mpCtx, &(paramInfo->curve),
      &(paramInfo->pubPointP),  0, (unsigned char *)0, &bfCtx);
    if (status != 0)
      break;

    /* Create an intermediate buffer, the return from the bf 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.
     * Although the name specifies private key, it's really any point.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = bfCalculatePointX (
      buffer, primeLen + 1, &actualLen, yCoord, yCoordLen, bfCtx);
    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);

  VoltReleaseBfCtx (libCtx, &bfCtx);
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, errorType, fnctLine,
    "VoltGetXCoordFromY", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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