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

📄 ibeparamtype.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 4 页
字号:
      status = libCtx->threadCtx->AcquireLock(libCtx->threadCtx,
        bfCtxCache->lock);
      if (status != 0)
        break;
      
      lockAcquired = 1;
      
      VOLT_SET_FNCT_LINE (fnctLine)
      status = bfCtxCache->SearchBfCtxCache (
        libCtx, bfCtxCache, curve, 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.
     * This function assumes
     */
    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);
    if (status != 0)
      break;

    /* If the saveFlag is 0, add this ctx to the cache (if there is
     * one).
     */
    if ( (saveFlag == 0) && (bfCtxCache != (VoltBfCtxCache  *)0) )
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = bfCtxCache->AddToBfCtxCache (
        libCtx, bfCtxCache, curve, newCtx);
      if (status != 0)
        break;
    }

  } while (0);

  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 (lockAcquired)
  {
      status2 = libCtx->threadCtx->ReleaseLock(libCtx->threadCtx,
        bfCtxCache->lock);
      if (status == 0)
      {
        /* We may be cleaning up after some other error occurred with
         * the params and we don't want to clobber the real error, so
         * we only set the real status if it's 0.
         */
        VOLT_SET_FNCT_LINE (fnctLine)
        status = status2;
      }
  }
  
  /* 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 VoltReleaseBfCtxFromCache (
   VoltLibCtx *libCtx,
   VoltBfCtxCache *bfCtxCache,
   bf_context_t **bfCtx
   )
{
  VoltBfCtxLocalCache *cache = (VoltBfCtxLocalCache *)(bfCtxCache->localCache);
  VoltBfCtxCacheEntry *entry, *nextEntry;

  /* Start with the first entry in the list.
   */
  nextEntry = cache->cacheList;

  /* Keep searching entries until we find a match or we run out of
   * entries.
   */
  while (nextEntry != (VoltBfCtxCacheEntry *)0)
  {
    entry = nextEntry;
    /* Same bfCtx?
     */
    if (entry->bfCtx == *bfCtx)
    {
      if (entry->referenceCount != 0)
        entry->referenceCount--;
      *bfCtx = (bf_context_t *)0;
      return;
    }

    /* No match, move on to the next entry.
     */
    nextEntry = (VoltBfCtxCacheEntry *)(entry->nextEntry);
  }
}

void VoltReleaseBfCtx (
   VoltLibCtx *libCtx,
   bf_context_t **bfCtx
   )
{
  VoltBfCtxCache *bfCtxCache;
  int status;
  
  /* Anything to release?
   */
  if (bfCtx == (bf_context_t **)0)
    return;
  if (*bfCtx == (bf_context_t *)0)
    return;

  /* Get the cache, if there is one.
   */
  bfCtxCache = (VoltBfCtxCache *)VoltGetLibCtxInfo (
    libCtx, VOLT_LIB_CTX_INFO_TYPE_BF_CTX_CACHE);

  /* See if this bfCtx is in the cache.
   */
  if (bfCtxCache != (VoltBfCtxCache *)0)
  {
    status = libCtx->threadCtx->AcquireLock(libCtx->threadCtx,
      bfCtxCache->lock);
    
    bfCtxCache->ReleaseBfCtxFromCache (libCtx, bfCtxCache, bfCtx);

    status = libCtx->threadCtx->ReleaseLock(libCtx->threadCtx,
      bfCtxCache->lock);

    /* 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,
   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 *)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);

    /* 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), &bfCtx);
    if (status != 0)
      break;

    /* 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, &bfCtx);

  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), &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);

⌨️ 快捷键说明

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