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

📄 ibeparamtype.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 4 页
字号:
    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);
}

int VtLibCtxParamIBECache (
   VtLibCtx libraryCtx,
   Pointer info,
   unsigned int flag
   )
{
  int status;
#if VOLT_ALIGNMENT != 1
  unsigned int pad;
#endif
  unsigned int bufferSize, offset;
  unsigned char *buffer = (unsigned char *)0;
  VoltLibCtx *libCtx = (VoltLibCtx *)libraryCtx;
  VoltBfCtxCache *newCache;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the flag, it should be VOLT_LIB_CTX_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_LIB_CTX_SET_TYPE_FLAG)
      break;

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

    /* Allocate space for the BfCtxCache and the localCache.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize = sizeof (VoltBfCtxCache) + sizeof (VoltBfCtxLocalCache);
#if VOLT_ALIGNMENT != 1
    /* If the alignment is 1, there's no need to pad. If not, compute
     * the pad length.
     */
    VOLT_COMPUTE_ALIGN_PAD (VOLT_ALIGNMENT, sizeof (VoltBfCtxCache), pad)
      bufferSize += pad;
#endif
    buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the caches.
     */
    newCache = (VoltBfCtxCache *)buffer;
    offset = sizeof (VoltBfCtxCache);
#if VOLT_ALIGNMENT != 1
    offset += pad;
#endif
    newCache->localCache = (Pointer)(buffer + offset);
  
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = libCtx->threadCtx->CreateLock(libCtx->threadCtx, &newCache->lock);
    if (status != 0)
      break;
      
    /* Populate the cache.
     */
    newCache->SearchBfCtxCache = VoltSearchBfCtxCache;
    newCache->AddToBfCtxCache = VoltAddToBfCtxCache;
    newCache->ReleaseBfCtxFromCache = VoltReleaseBfCtxFromCache;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltAddLibCtxInfo (
      libCtx, VOLT_LIB_CTX_INFO_TYPE_BF_CTX_CACHE, 1,
      (Pointer)newCache, VoltBfCtxCacheDestroy);

  } while (0);

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

  /* If error, free anything we allocated.
   */
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR (
    libraryCtx, status, errorType, fnctLine,
    "VtLibCtxParamIBECache", (char *)0)

  return (status);
}

int VoltSearchBfCtxCache (
   VoltLibCtx *libCtx,
   VoltBfCtxCache *bfCtxCache,
   VtBFType1IBECurveInfo *curve,
   bf_context_t **bfCtx
   )
{
  unsigned int lowBit, xLen;
  unsigned char *xBuf;
  VoltBfCtxLocalCache *cache = (VoltBfCtxLocalCache *)(bfCtxCache->localCache);
  VoltBfCtxCacheEntry *entry;

  *bfCtx = (bf_context_t *)0;

  if (curve == (VtBFType1IBECurveInfo *)0)
    return (0);

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

  /* Keep searching entries until we find a match or we run out of
   * entries.
   */
  while (entry != (VoltBfCtxCacheEntry *)0)
  {
    /* Do the primes match?
     */
    if (curve->primeP.len == entry->primeP.len)
    {
      if (Z2Memcmp (
        curve->primeP.data, entry->primeP.data, entry->primeP.len) == 0)
      {
        /* Do the subprimes match?
         */
        if (curve->subprimeQ.len == entry->subprimeQ.len)
        {
          if (Z2Memcmp (
            curve->subprimeQ.data, entry->subprimeQ.data,
            entry->subprimeQ.len) == 0)
          {
            /* Do the base points match? The yCoord might not be given.
             */
            xBuf = curve->basePointG.xCoord.data;
            xLen = curve->basePointG.xCoord.len;
            if (curve->basePointG.yCoord.data == (unsigned char *)0)
            {
              lowBit = (unsigned int)(xBuf[0]);
              lowBit &= 1;
              xBuf++;
              xLen--;
            }
            else
            {
              lowBit = curve->basePointG.yCoord.len - 1;
              lowBit = (unsigned int)
                (curve->basePointG.yCoord.data[lowBit]);
              lowBit &= 1;
            }
            if (xLen == entry->xCoordBasePointG.len)
            {
              if (Z2Memcmp (xBuf, entry->xCoordBasePointG.data, xLen) == 0)
              {
                /* We only need to check the low bit of the y
                 * coordinate.
                 */
                if (lowBit == entry->yCoordLowBit)
                {
                  /* We have a match, return the entry's bfCtx and
                   * increment the reference count.
                   */
                  *bfCtx = entry->bfCtx;
                  entry->referenceCount++;
                  break;
                }
              }
            }
          }
        }
      }
    }

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

  return (0);
}

int VoltAddToBfCtxCache (
   VoltLibCtx *libCtx,
   VoltBfCtxCache *bfCtxCache,
   VtBFType1IBECurveInfo *curveInfo,
   bf_context_t *bfCtx
   )
{
  int status;
  unsigned int bufferSize, offset;
  unsigned char *buffer = (unsigned char *)0;
  Pointer nextEntry;
  VoltBfCtxLocalCache *cache = (VoltBfCtxLocalCache *)(bfCtxCache->localCache);
  VoltBfCtxCacheEntry *newEntry, *previousEntry;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Build a buffer big enough to hold a cache entry plus prime,
     * subprime and xCoord of base point. The things after the struct
     * are byte arrays, no need to worry about alignment.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      sizeof (VoltBfCtxCacheEntry) + curveInfo->primeP.len +
      curveInfo->subprimeQ.len + curveInfo->basePointG.xCoord.len;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the pointers.
     */
    newEntry = (VoltBfCtxCacheEntry *)buffer;
    offset = sizeof (VoltBfCtxCacheEntry);
    newEntry->primeP.data = buffer + offset;
    offset += curveInfo->primeP.len;
    newEntry->subprimeQ.data = buffer + offset;
    offset += curveInfo->subprimeQ.len;
    newEntry->xCoordBasePointG.data = buffer + offset;

    /* Copy the curve values into the buffers.
     */
    Z2Memcpy (
      newEntry->primeP.data, curveInfo->primeP.data, curveInfo->primeP.len);
    newEntry->primeP.len = curveInfo->primeP.len;
    Z2Memcpy (
      newEntry->subprimeQ.data, curveInfo->subprimeQ.data,
      curveInfo->subprimeQ.len);
    newEntry->subprimeQ.len = curveInfo->subprimeQ.len;

    /* If the yCoord is not given, the xCoord contains the low bit of
     * the yCoord as the first byte. If it is given the xCoord in the
     * curveInfo is just what we want.
     */
    if (curveInfo->basePointG.yCoord.data != (unsigned char *)0)
    {
      Z2Memcpy (
        newEntry->xCoordBasePointG.data, curveInfo->basePointG.xCoord.data,
        curveInfo->basePointG.xCoord.len);
      newEntry->xCoordBasePointG.len = curveInfo->basePointG.xCoord.len;

      /* Get the low bit of the yCoord. Use offset as a temp variable.
       */
      offset = curveInfo->basePointG.yCoord.len - 1;
      offset = (unsigned int)(curveInfo->basePointG.yCoord.data[offset]);
      newEntry->yCoordLowBit = offset & 1;
    }
    else
    {
      Z2Memcpy (
        newEntry->xCoordBasePointG.data, curveInfo->basePointG.xCoord.data + 1,
        curveInfo->basePointG.xCoord.len - 1);
      newEntry->xCoordBasePointG.len = curveInfo->basePointG.xCoord.len - 1;

      /* Get the low bit of the yCoord. Use offset as a temp variable.
       */
      offset = (unsigned int)(curveInfo->basePointG.xCoord.data[0]);
      newEntry->yCoordLowBit = offset & 1;
    }

    /* Place the bfCtx.
     */
    newEntry->bfCtx = bfCtx;

    /* This is a new entry, the reference count is 1 (the caller).
     */
    newEntry->referenceCount = 1;

    status = 0;

    /* Place this new entry onto the end of the link list.
     */
    nextEntry = (Pointer)(cache->cacheList);
    if (nextEntry == (Pointer)0)
    {
      /* If this is the first, there is no linking.
       */
      cache->cacheList = newEntry;
      cache->count = 1;
      break;
    }

    /* Find the entry with no nextEntry.
     */
    do
    {
      previousEntry = (VoltBfCtxCacheEntry *)nextEntry;
      nextEntry = previousEntry->nextEntry;
    } while (nextEntry != (Pointer)0);

    /* The entry previousEntry is the last.
     */
    cache->count++;
    previousEntry->nextEntry = (Pointer)newEntry;
    newEntry->previousEntry = (Pointer)previousEntry;

  } while (0);

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

  /* If error, free the buffer, it will not be placed anywhere.
   */
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "VoltAddToBfCtxCache", (char *)0)

  return (status);
}

void VoltBfCtxCacheDestroy (
   Pointer obj,
   Pointer ctx
   )
{
  VoltLibCtx *libCtx;
  VoltBfCtxCache *bfCtxCache;
  VoltBfCtxLocalCache *cache;
  VoltBfCtxCacheEntry *entry, *nextEntry;

  /* Anything to destroy?
   */
  if ( (obj == (Pointer)0) || (ctx == (Pointer)0) )
    return;

  libCtx = (VoltLibCtx *)obj;
  bfCtxCache = (VoltBfCtxCache *)ctx;
  cache = (VoltBfCtxLocalCache *)(bfCtxCache->localCache);

  /* Run through the list, calling bfDel on each of the cached bfCtx's.
   * Then free the entry shell.
   */

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

  while (entry != (VoltBfCtxCacheEntry *)0)
  {
    nextEntry = (VoltBfCtxCacheEntry *)(entry->nextEntry);
    bfDel (entry->bfCtx);
    Z2Free (entry);
    entry = nextEntry;
  }

  libCtx->threadCtx->DestroyLock(libCtx->threadCtx, &bfCtxCache->lock);
      
  /* Now free the shell.
   */
  Z2Free (ctx);
}

⌨️ 快捷键说明

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