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

📄 ibecacheimpl.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 4 页
字号:
    {
      if (indexT == 0)
        theMul = bbCtx->common->mulg;
      else if (indexT == 1)
        theMul = bbCtx->common->mulg1;
      else
        theMul = bbCtx->common->mulg3;

      zTableX = theMul->prex;
      zTableY = theMul->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);

  VoltDemolishBBIBEParams (libCtx, &theParams);

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

  return (status);
}

static void PlaceIntegerBE (
   unsigned char *buffer,
   unsigned int value,
   unsigned int byteCount
   )
{
  int index;

  index = (int)(byteCount -1);
  do
  {
    buffer[index] = (unsigned char)value;
    index--;
    value >>= 8;
  } while (index >= 0);
}

static unsigned int GetIntegerBE (
   unsigned char *buffer,
   unsigned int integerSize
   )
{
  unsigned int index, value;

  value = 0;
  for (index = 0; index < integerSize; ++index)
  {
    value <<= 8;
    value += (unsigned int)(buffer[index]);
  }

  return (value);
}

static unsigned int PlaceBuffer (
   VoltLibCtx *libCtx,
   z_t *zValue,
   unsigned char *inputBuffer,
   unsigned int inputLen,
   unsigned int size,
   unsigned char *outputBuffer
   )
{
  unsigned int sign, outLen;
  VoltMpIntCtx *ctx;
  VoltMpInt *mpInt;

  if (zValue != (z_t *)0)
  {
    ctx = zValue->mpCtx;
    mpInt = (VoltMpInt *)(zValue->mpInt);
    ctx->MpIntToOctetString (mpInt, &sign, outputBuffer, size, &outLen);
    if (outLen == size)
      return (size);

    Z2Memmove (outputBuffer + (size - outLen), outputBuffer, outLen);
    Z2Memset (outputBuffer, 0, size - outLen);
    return (size);
  }

  Z2Memcpy (outputBuffer + (size - inputLen), inputBuffer, inputLen);
  if (inputLen == size)
    return (size);

  Z2Memset (outputBuffer, 0, size - inputLen);
  return (size);
}

static int BuildBfCtxFromCacheEntry (
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   unsigned int primeLen,
   unsigned int subprimeLen,
   unsigned int accelCount,
   unsigned char *element,
   bf_context_t **bfCtx
   )
{
  int status;
  unsigned char *prime, *subprime, *baseX, *baseY, *pubX, *pubY, *accelTable;
  bf_context_t *newBf = (bf_context_t *)0;
  VtBFType1IBECurveInfo curve;
  VtBFType1IBEPoint pubPoint;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Find the prime, subprime, etc.
     */
    prime = element + 14;
    subprime = prime + primeLen;
    baseX = subprime + subprimeLen;
    baseY = baseX + primeLen;
    pubX = baseY + primeLen;
    pubY = pubX + primeLen;
    accelTable = pubY + primeLen;

    curve.primeP.data = prime;
    curve.primeP.len = primeLen;
    curve.subprimeQ.data = subprime;
    curve.subprimeQ.len = subprimeLen;
    curve.basePointG.isInfinity = 0;
    curve.basePointG.xCoord.data = baseX;
    curve.basePointG.xCoord.len = primeLen;
    curve.basePointG.yCoord.data = baseY;
    curve.basePointG.yCoord.len = primeLen;
    pubPoint.isInfinity = 0;
    pubPoint.xCoord.data = pubX;
    pubPoint.xCoord.len = primeLen;
    pubPoint.yCoord.data = pubY;
    pubPoint.yCoord.len = primeLen;
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBfCtxFromIBEParams (
      libCtx, (VtMpIntCtx)mpCtx, &curve, &pubPoint, accelCount, accelTable,
      &newBf);
    if (status != 0)
      break;

    *bfCtx = newBf;

  } while (0);

  if (status == 0)
    return (0);

  /* If there was an error, destroy anything we created.
   */
  VoltReleaseBfCtx (libCtx, &newBf);

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

  return (status);
}

static int BuildBbCtxFromCacheEntry (
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   unsigned int primeLen,
   unsigned int subprimeLen,
   unsigned int accelCount,
   unsigned char *element,
   bb1_context_t **bbCtx
   )
{
  int status;
  unsigned char *prime, *subprime, *baseX, *baseY, *pubAlphaX, *pubAlphaY;
  unsigned char *pubBetaX, *pubBetaY, *pubGammaX, *pubGammaY;
  unsigned char *accelG, *accelG1, *accelG3;
  bb1_context_t *newBb = (bb1_context_t *)0;
  VtBBType1IBEParamInfo paramInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Find the prime, subprime, etc.
     */
    prime = element + 14;
    subprime = prime + primeLen;
    baseX = subprime + subprimeLen;
    baseY = baseX + primeLen;
    pubAlphaX = baseY + primeLen;
    pubAlphaY = pubAlphaX + primeLen;
    pubBetaX = pubAlphaY + primeLen;
    pubBetaY = pubBetaX + primeLen;
    pubGammaX = pubBetaY + primeLen;
    pubGammaY = pubGammaX + primeLen;
    accelG = pubGammaY + primeLen;
    accelG1 = accelG + (accelCount * primeLen * 2);
    accelG3 = accelG1 + (accelCount * primeLen * 2);

    paramInfo.primeP.data = prime;
    paramInfo.primeP.len = primeLen;
    paramInfo.subprimeQ.data = subprime;
    paramInfo.subprimeQ.len = subprimeLen;
    paramInfo.basePointG.isInfinity = 0;
    paramInfo.basePointG.xCoord.data = baseX;
    paramInfo.basePointG.xCoord.len = primeLen;
    paramInfo.basePointG.yCoord.data = baseY;
    paramInfo.basePointG.yCoord.len = primeLen;
    paramInfo.pubPointAlpha.isInfinity = 0;
    paramInfo.pubPointAlpha.xCoord.data = pubAlphaX;
    paramInfo.pubPointAlpha.xCoord.len = primeLen;
    paramInfo.pubPointAlpha.yCoord.data = pubAlphaY;
    paramInfo.pubPointAlpha.yCoord.len = primeLen;
    paramInfo.pubPointBeta.isInfinity = 0;
    paramInfo.pubPointBeta.xCoord.data = pubBetaX;
    paramInfo.pubPointBeta.xCoord.len = primeLen;
    paramInfo.pubPointBeta.yCoord.data = pubBetaY;
    paramInfo.pubPointBeta.yCoord.len = primeLen;
    paramInfo.pubPointGamma.isInfinity = 0;
    paramInfo.pubPointGamma.xCoord.data = pubGammaX;
    paramInfo.pubPointGamma.xCoord.len = primeLen;
    paramInfo.pubPointGamma.yCoord.data = pubGammaY;
    paramInfo.pubPointGamma.yCoord.len = primeLen;

    /* This function will build the bbCtx and load it into the IBE
     * cache.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBbCtxFromIBEParams (
      libCtx, (VtMpIntCtx)mpCtx, &paramInfo,
      accelCount, accelG, accelG1, accelG3, &newBb);
    if (status != 0)
      break;

    *bbCtx = newBb;

  } while (0);

  if (status == 0)
    return (0);

  /* If there was an error, destroy anything we created.
   */
  VoltReleaseBbCtx (libCtx, &newBb);

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

  return (status);
}

int VoltBuildIBECacheRefAlloc (
   VoltLibCtx *libCtx,
   VtBFType1IBEParamInfo *bfParams,
   VtBBType1IBEParamInfo *bbParams,
   unsigned char **reference,
   unsigned int *referenceLen
   )
{
  int status;
  unsigned int primeLen, bufLen, offset;
  unsigned char *refBuf;
  VtItem baseX, baseY, pubX, pubY;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  *reference = (unsigned char *)0;
  *referenceLen = 0;
  baseX.data = (unsigned char *)0;
  baseX.len = 0;
  baseY.data = (unsigned char *)0;
  baseY.len = 0;
  pubX.data = (unsigned char *)0;
  pubX.len = 0;
  pubY.data = (unsigned char *)0;
  pubY.len = 0;

  /* The reference is the base point and public point. If some
   * coordinates are not available, just leave them blank (00 bytes).
   */
  do
  {
    status = 0;
    if (bfParams != (VtBFType1IBEParamInfo *)0)
    {
      primeLen = bfParams->curve.primeP.len;
      if (bfParams->curve.basePointG.xCoord.data != (unsigned char *)0)
        baseX = bfParams->curve.basePointG.xCoord;
      if (bfParams->curve.basePointG.yCoord.data == (unsigned char *)0)
      {
        if (bfParams->curve.basePointG.xCoord.data == (unsigned char *)0)
          break;
        baseX.data += 1;
        baseX.len -= 1;
      }
      else
      {
        baseY = bfParams->curve.basePointG.yCoord;
      }

      if (bfParams->pubPointP.xCoord.data != (unsigned char *)0)
        pubX = bfParams->pubPointP.xCoord;
      if (bfParams->pubPointP.yCoord.data == (unsigned char *)0)
      {
        if (bfParams->pubPointP.xCoord.data == (unsigned char *)0)
          break;
        pubX.data += 1;
        pubX.len -= 1;
      }
      else
      {
        pubY = bfParams->pubPointP.yCoord;
      }
    }
    else
    {
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NULL_ARG;
      if (bbParams == (VtBBType1IBEParamInfo *)0)
        break;

      primeLen = bbParams->primeP.len;
      if (bbParams->basePointG.xCoord.data != (unsigned char *)0)
        baseX = bbParams->basePointG.xCoord;
      if (bbParams->basePointG.xCoord.data == (unsigned char *)0)
      {
        if (bbParams->basePointG.xCoord.data == (unsigned char *)0)
          break;
        baseX.data += 1;
        baseX.len -= 1;
      }
      else
      {
        baseY = bbParams->basePointG.yCoord;
      }

      if (bbParams->pubPointAlpha.xCoord.data != (unsigned char *)0)
        pubX = bbParams->pubPointAlpha.xCoord;
      if (bbParams->pubPointAlpha.yCoord.data == (unsigned char *)0)
      {
        if (bbParams->pubPointAlpha.xCoord.data == (unsigned char *)0)
          break;
        pubX.data += 1;
        pubX.len -= 1;
      }
      else
      {
        pubY = bbParams->pubPointAlpha.yCoord;
      }
    }

    bufLen = primeLen * 4;
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    refBuf = (unsigned char *)Z2Malloc (bufLen, 0);
    if (refBuf == (unsigned char *)0)
      break;

    Z2Memset (refBuf, 0, bufLen);
    if (baseX.data != (unsigned char *)0)
      Z2Memcpy (refBuf, baseX.data, baseX.len);
    if (baseY.data != (unsigned char *)0)
      Z2Memcpy (refBuf + primeLen, baseY.data, baseY.len);
    offset = 2 * primeLen;
    if (pubX.data != (unsigned char *)0)
      Z2Memcpy (refBuf + offset, pubX.data, pubX.len);
    offset += primeLen;
    if (pubY.data != (unsigned char *)0)
      Z2Memcpy (refBuf + offset, pubY.data, pubY.len);

    *reference = refBuf;
    *referenceLen = bufLen;

    status = 0;

  } while (0);

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

  /* If there was an error, free any memory we would have returned but
   * didn't because of the error.
   */
  if (refBuf != (unsigned char *)0)
    Z2Free (refBuf);

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

  return (status);
}

⌨️ 快捷键说明

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