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

📄 ibecacheimpl.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 4 页
字号:
  unsigned int count;
  VoltIBECacheCtx *ctx = (VoltIBECacheCtx *)ibeCacheCtx;
  VoltIBELocalCache *cache = (VoltIBELocalCache *)(ctx->localCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VoltIBECacheEntry *entry;
  VoltIBECacheEntry *previous;
  VoltIBECacheEntry *next;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Acquire the cache lock to syncronize access */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = ctx->LockIBECache(ibeCacheCtx);
    if (status != 0)
      break;
    
    lockAcquired = 1;
    
    /* Run through the list, counting the entries until the count equals
     * the index.
     */
    entry = cache->cacheList;
    count = 0;
    while (entry != (VoltIBECacheEntry *)0)
    {
      if (index == count)
        break;
      entry = (VoltIBECacheEntry *)(entry->nextEntry);
      count++;
    }

    /* If entry is NULL, there is no entry at the requested index.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_NO_ELEMENT_AT_INDEX;
    if (entry == (VoltIBECacheEntry *)0)
      break;

    /* If the referenceCount is not 0, someone still has this "checked
     * out".
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_DELETE;
    if (entry->referenceCount != 0)
      break;

    /* Link the two entries that surrounded this one. If this is the
     * first in the list, make sure the next in the list becomes the
     * first.
     */
    previous = (VoltIBECacheEntry *)(entry->previousEntry);
    next = (VoltIBECacheEntry *)(entry->nextEntry);

    if (previous != (VoltIBECacheEntry *)0)
      previous->nextEntry = (Pointer)next;
    else
      cache->cacheList = next;

    if (next != (VoltIBECacheEntry *)0)
      next->previousEntry = (Pointer)previous;

    /* Call the appropriate Del.
     */
    if (entry->ctxType == VOLT_IBE_CTX_TYPE_BF)
      bfDel ((bf_context_t *)(entry->theCtx));
    else
      bb1Del ((bb1_context_t *)(entry->theCtx));

    if (entry->element.data != (unsigned char *)0)
      Z2Free (entry->element.data);
    if (entry->reference.data != (unsigned char *)0)
      Z2Free (entry->reference.data);

    Z2Free (entry);

    status = 0;

  } while (0);

  if (lockAcquired)
  {
    status2 = ctx->UnlockIBECache(ibeCacheCtx);
    if (status == 0)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = status2;
    }
  }

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, ibeCacheCtx, status, 0, errorType,
    (char *)0, "VoltDeleteIBECacheEntry", fnctLine, (char *)0)

  return (status);
}

int VoltLockIBECache(
   Pointer ibeCacheCtx
)
{
  int status;
  VoltIBECacheCtx *ctx = (VoltIBECacheCtx *)ibeCacheCtx;
  VoltIBELocalCache *cache = (VoltIBELocalCache *)(ctx->localCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VOLT_DECLARE_FNCT_LINE (fnctLine)
  
  VOLT_SET_FNCT_LINE (fnctLine)
  status = Z2AcquireLock(cache->lock);
  
  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, ibeCacheCtx, status, 0, 0,
    (char *)0, "VoltLockIBECache", fnctLine, (char *)0)

  return (status);
}

int VoltUnlockIBECache(
   Pointer ibeCacheCtx
)
{
  int status;
  VoltIBECacheCtx *ctx = (VoltIBECacheCtx *)ibeCacheCtx;
  VoltIBELocalCache *cache = (VoltIBELocalCache *)(ctx->localCtx);
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  VOLT_DECLARE_FNCT_LINE (fnctLine)
  
  VOLT_SET_FNCT_LINE (fnctLine)
  status = Z2ReleaseLock(cache->lock);
  
  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, ibeCacheCtx, status, 0, 0,
    (char *)0, "VoltUnlockIBECache", fnctLine, (char *)0)

  return (status);
}

void VoltIBECacheCtxDestroy (
   Pointer obj,
   Pointer ctx
   )
{
  VoltIBECacheCtx *cacheCtx;
  VoltIBELocalCache *cache;
  VoltLibCtx *libCtx;
  VoltIBECacheEntry *entry;
  VoltIBECacheEntry *nextEntry;

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

  cacheCtx = (VoltIBECacheCtx *)obj;
  libCtx = (VoltLibCtx *)(cacheCtx->voltObject.libraryCtx);
  cache = (VoltIBELocalCache *)(cacheCtx->localCtx);

  /* For each of the entries, free the memory allocated.
   */
  nextEntry = cache->cacheList;
  while (nextEntry != (VoltIBECacheEntry *)0)
  {
    entry = nextEntry;
    nextEntry = (VoltIBECacheEntry *)(entry->nextEntry);

    if (entry->ctxType == VOLT_IBE_CTX_TYPE_BF)
      bfDel ((bf_context_t *)(entry->theCtx));
    else
      bb1Del ((bb1_context_t *)(entry->theCtx));

    if (entry->element.data != (unsigned char *)0)
      Z2Free (entry->element.data);
    if (entry->reference.data != (unsigned char *)0)
      Z2Free (entry->reference.data);

    Z2Free (entry);
  }

  /* Free the cache lock.
   */
  Z2DestroyLock(&cache->lock);
  
  /* Free the shell.
   */
  Z2Free (ctx);
}

static int VoltBuildCacheElementBF (
   VoltIBECacheCtx *ctx,
   VoltIBELocalCache *cache,
   VoltIBECacheEntry *entry,
   unsigned char *element,
   unsigned int bufferSize,
   unsigned int *elementLen
   )
{
  int status;
  unsigned int index, primeSize, subprimeSize, accelCount, sizeNeeded, offset;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  bf_context_t *bfCtx = (bf_context_t *)(entry->theCtx);
  VtBFType1IBEParamInfo *theParams = (VtBFType1IBEParamInfo *)0;
  z_t **zTableX, **zTableY;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* We'll need the params and the acceleration table. Here are the
     * params.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltBuildIBEParamsFromBfCtx (libCtx, bfCtx, &theParams);
    if (status != 0)
      break;

    /* The buffer we build will contain the following.
     *   flag || primeSize || subPrimeSize || accelCount ||
     *   prime || subPrime || baseX || baseY || pubX || pubY ||
     *   accelTable
     * The flag indicates whether this is BF or BB.
     * Each element, except for the subPrime, will be primeSize bytes
     * big, with prepended 00 bytes if necessary.
     * The accelCount is the number of pairs, not a byte length
     */
    primeSize = theParams->curve.primeP.len;
    subprimeSize = theParams->curve.subprimeQ.len;
    accelCount = (unsigned int)(bfCtx->common->mul->m);

    /* The buffer will need to contain 1 subprimeSize value plus
     * (accelCount * 2) + 5  primeSize values, plus 3 4-byte values
     * (the sizes and count) plus two bytes for the flag.
     */
    sizeNeeded = (accelCount * 2) * primeSize;
    sizeNeeded += (5 * primeSize);
    sizeNeeded += subprimeSize;
    sizeNeeded += 14;

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_BUFFER_TOO_SMALL;
    *elementLen = sizeNeeded;
    if (bufferSize < sizeNeeded)
      break;

    /* Write the
     *   flag || primeSize || subPrimeSize || accelCount ||
     */
    PlaceIntegerBE (element, VT_IBE_CACHE_ENTRY_BF_TYPE1, 2);
    PlaceIntegerBE (element + 2, primeSize, 4);
    PlaceIntegerBE (element + 6, subprimeSize, 4);
    PlaceIntegerBE (element + 10, accelCount, 4);

    /* Now start placing the values into the buffer.
     */
    offset = 14;
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->curve.primeP.data,
      theParams->curve.primeP.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->curve.subprimeQ.data,
      theParams->curve.subprimeQ.len, subprimeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->curve.basePointG.xCoord.data,
      theParams->curve.basePointG.xCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->curve.basePointG.yCoord.data,
      theParams->curve.basePointG.yCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointP.xCoord.data,
      theParams->pubPointP.xCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointP.yCoord.data,
      theParams->pubPointP.yCoord.len, primeSize, element + offset);

    /* Place the acceleration table.
     */
    zTableX = bfCtx->common->mul->prex;
    zTableY = bfCtx->common->mul->prey;
    for (index = 0; index < accelCount; ++index)
    {
      offset += PlaceBuffer (
        libCtx, zTableX[index], (unsigned char *)0, 0, primeSize,
        element + offset);
      offset += PlaceBuffer (
        libCtx, zTableY[index], (unsigned char *)0, 0, primeSize,
        element + offset);
    }

    status = 0;

  } while (0);

  VoltDemolishIBEParams (libCtx, &theParams);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, ctx, status, 0, errorType,
    (char *)0, "VoltBuildCacheElementBF", fnctLine, (char *)0)

  return (status);
}

static int VoltBuildCacheElementBB (
   VoltIBECacheCtx *ctx,
   VoltIBELocalCache *cache,
   VoltIBECacheEntry *entry,
   unsigned char *element,
   unsigned int bufferSize,
   unsigned int *elementLen
   )
{
  int status;
  unsigned int index, indexT, sizeNeeded, offset;
  unsigned int primeSize, subprimeSize, accelCount;
  VoltLibCtx *libCtx = (VoltLibCtx *)(ctx->voltObject.libraryCtx);
  bb1_context_t *bbCtx = (bb1_context_t *)(entry->theCtx);
  VtBBType1IBEParamInfo *theParams = (VtBBType1IBEParamInfo *)0;
  mul1_t *theMul;
  z_t **zTableX, **zTableY;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* We'll need the params and the acceleration table. Here are the
     * params.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltBuildIBEParamsFromBbCtx (libCtx, bbCtx, &theParams);
    if (status != 0)
      break;

    /* The buffer we build will contain the following.
     *   flag || primeSize || subPrimeSize || accelCount ||
     *   prime || subPrime || baseX || baseY ||
     *   pubAlphaX || pubAlphaY || pubBetaX || pubBetaY ||
     *   pubGammaX || pubGammaY ||
     *   accelTableG || accelTableG1 || accelTableG3
     * The flag indicates whether this is BF or BB.
     * Each element, except for the subPrime, will be primeSize bytes
     * big, with prepended 00 bytes if necessary.
     * The accelCount is the number of pairs, not a byte length.
     * There are three acceleration tables, each with accelCount pairs.
     */
    primeSize = theParams->primeP.len;
    subprimeSize = theParams->subprimeQ.len;
    accelCount = (unsigned int)(bbCtx->common->mulg->m);

    /* The buffer will need to contain 1 subprimeSize value plus
     * (accelCount * 2  * 3) + 9  primeSize values, plus 3 4-byte values
     * (the sizes and count) plus two bytes for the flag.
     */
    sizeNeeded = (accelCount * 6) * primeSize;
    sizeNeeded += (9 * primeSize);
    sizeNeeded += subprimeSize;
    sizeNeeded += 14;

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_BUFFER_TOO_SMALL;
    *elementLen = sizeNeeded;
    if (bufferSize < sizeNeeded)
      break;

    /* Write the
     *   flag || primeSize || subPrimeSize || accelCount ||
     */
    PlaceIntegerBE (element, VT_IBE_CACHE_ENTRY_BB_TYPE1, 2);
    PlaceIntegerBE (element + 2, primeSize, 4);
    PlaceIntegerBE (element + 6, subprimeSize, 4);
    PlaceIntegerBE (element + 10, accelCount, 4);

    /* Now start placing the values into the buffer.
     */
    offset = 14;
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->primeP.data,
      theParams->primeP.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->subprimeQ.data,
      theParams->subprimeQ.len, subprimeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->basePointG.xCoord.data,
      theParams->basePointG.xCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->basePointG.yCoord.data,
      theParams->basePointG.yCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointAlpha.xCoord.data,
      theParams->pubPointAlpha.xCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointAlpha.yCoord.data,
      theParams->pubPointAlpha.yCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointBeta.xCoord.data,
      theParams->pubPointBeta.xCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointBeta.yCoord.data,
      theParams->pubPointBeta.yCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointGamma.xCoord.data,
      theParams->pubPointGamma.xCoord.len, primeSize, element + offset);
    offset += PlaceBuffer (
      libCtx, (z_t *)0, theParams->pubPointGamma.yCoord.data,
      theParams->pubPointGamma.yCoord.len, primeSize, element + offset);

    /* Place the acceleration tables.
     */
    for (indexT = 0; indexT < 3; ++indexT)

⌨️ 快捷键说明

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