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

📄 dhkeytype.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 3 页
字号:
        keyInfo->subprimeQ.len = qLen;
        currentBuf += qLen;
        spaceAvailable -= qLen;
      }
      else
      {
        /* If not 0, then status should be BufferTooSmall.
         */
        if (status != VT_ERROR_BUFFER_TOO_SMALL)
          break;
        spaceAvailable = 0;
      }
      totalSize += qLen;
    }

    /* Try to get the base.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mpCtx->MpIntToOctetString (
      pubKey->baseG, &sign, currentBuf, spaceAvailable, &gLen);
    if (status == 0)
    {
      keyInfo->baseG.data = currentBuf;
      keyInfo->baseG.len = gLen;
      currentBuf += gLen;
      spaceAvailable -= gLen;
    }
    else
    {
      /* If not 0, then status should be BufferTooSmall.
       */
      if (status != VT_ERROR_BUFFER_TOO_SMALL)
        break;
      spaceAvailable = 0;
    }
    totalSize += gLen;

    /* Try to get the public value.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mpCtx->MpIntToOctetString (
      pubKey->pubValY, &sign, currentBuf, spaceAvailable, &pubValYLen);
    if (status == 0)
    {
      keyInfo->pubValY.data = currentBuf;
      keyInfo->pubValY.len = pubValYLen;
    }
    else
    {
      /* If not 0, then status should be BufferTooSmall.
       */
      if (status != VT_ERROR_BUFFER_TOO_SMALL)
        break;
    }
    totalSize += pubValYLen;

    *bufferSize = totalSize;

  } while (0);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, obj, status, 0, 0,
    (char *)0, "BuildPubKeyInfo", fnctLine, (char *)0)

  return (status);
}

static int BuildPriKeyInfo (
   VoltKeyObject *obj,
   VoltDHPrivateKey *priKey,
   unsigned char *buffer,
   unsigned int *bufferSize
   )
{
  int status;
  unsigned int totalSize, spaceAvailable;
  unsigned int sign, pLen, qLen, gLen, pubValYLen, priValXLen;
  VoltMpIntCtx *mpCtx = obj->mpCtx;
  VtDHPriKeyInfo *keyInfo = (VtDHPriKeyInfo *)buffer;
  unsigned char *currentBuf;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* spaceAvailable is how much space is currently available.
   */
  spaceAvailable = 0;
  totalSize = sizeof (VtDHPriKeyInfo);
  if (*bufferSize > sizeof (VtDHPriKeyInfo))
    spaceAvailable = *bufferSize - sizeof (VtDHPriKeyInfo);

  do
  {
    currentBuf = (unsigned char *)0;
    if (spaceAvailable != 0)
      currentBuf = (buffer + sizeof (VtDHPriKeyInfo));

    /* Try to get the prime.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mpCtx->MpIntToOctetString (
      priKey->primeP, &sign, currentBuf, spaceAvailable, &pLen);
    if (status == 0)
    {
      keyInfo->primeP.data = currentBuf;
      keyInfo->primeP.len = pLen;
      currentBuf += pLen;
      spaceAvailable -= pLen;
    }
    else
    {
      /* If not 0, then status should be BufferTooSmall.
       */
      if (status != VT_ERROR_BUFFER_TOO_SMALL)
        break;
      spaceAvailable = 0;
    }
    totalSize += pLen;

    /* Try to get the subprime.
     */
    if (priKey->subprimeQ != (VoltMpInt *)0)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      status = mpCtx->MpIntToOctetString (
        priKey->subprimeQ, &sign, currentBuf, spaceAvailable, &qLen);
      if (status == 0)
      {
        keyInfo->subprimeQ.data = currentBuf;
        keyInfo->subprimeQ.len = qLen;
        currentBuf += qLen;
        spaceAvailable -= qLen;
      }
      else
      {
        /* If not 0, then status should be BufferTooSmall.
         */
        if (status != VT_ERROR_BUFFER_TOO_SMALL)
          break;
        spaceAvailable = 0;
      }
      totalSize += qLen;
    }

    /* Try to get the base.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mpCtx->MpIntToOctetString (
      priKey->baseG, &sign, currentBuf, spaceAvailable, &gLen);
    if (status == 0)
    {
      keyInfo->baseG.data = currentBuf;
      keyInfo->baseG.len = gLen;
      currentBuf += gLen;
      spaceAvailable -= gLen;
    }
    else
    {
      /* If not 0, then status should be BufferTooSmall.
       */
      if (status != VT_ERROR_BUFFER_TOO_SMALL)
        break;
      spaceAvailable = 0;
    }
    totalSize += gLen;

    /* Try to get the public value.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mpCtx->MpIntToOctetString (
      priKey->pubValY, &sign, currentBuf, spaceAvailable, &pubValYLen);
    if (status == 0)
    {
      keyInfo->pubValY.data = currentBuf;
      keyInfo->pubValY.len = pubValYLen;
      currentBuf += pubValYLen;
      spaceAvailable -= pubValYLen;
    }
    else
    {
      /* If not 0, then status should be BufferTooSmall.
       */
      if (status != VT_ERROR_BUFFER_TOO_SMALL)
        break;
      spaceAvailable = 0;
    }
    totalSize += pubValYLen;

    /* Try to get the private value.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = mpCtx->MpIntToOctetString (
      priKey->priValX, &sign, currentBuf, spaceAvailable, &priValXLen);
    if (status == 0)
    {
      keyInfo->priValX.data = currentBuf;
      keyInfo->priValX.len = priValXLen;
    }
    else
    {
      /* If not 0, then status should be BufferTooSmall.
       */
      if (status != VT_ERROR_BUFFER_TOO_SMALL)
        break;
    }
    totalSize += priValXLen;

    *bufferSize = totalSize;

  } while (0);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, obj, status, 0, 0,
    (char *)0, "BuildPriKeyInfo", fnctLine, (char *)0)

  return (status);
}

int VoltCloneDHPubKey (
   Pointer sourceObject,
   Pointer *destObject
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)sourceObject;
  VtKeyObject newKey = (VtKeyObject)0;
  VtDHPubKeyInfo *getInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* We know the source is an object, is it a key object?
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if (VOLT_OBJECT_TYPE_NOT_EQUAL (sourceObject, VOLT_OBJECT_TYPE_KEY))
      break;

    /* Is the object a DH public key?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) != VOLT_KEY_ALG_DH)
      break;
    if ((obj->keyType & VOLT_KEY_TYPE_PUBLIC) == 0)
      break;

    /* Get the DH key data out of the source object.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtGetKeyParam (
      (VtKeyObject)obj, VtKeyParamDHPublic, (Pointer *)&getInfo);
    if (status != 0)
      break;

    /* Create and set a key object with this data.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreateKeyObject (
      obj->voltObject.libraryCtx, VtKeyImplMpCtx, (Pointer)(obj->mpCtx),
      &newKey);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetKeyParam (newKey, VtKeyParamDHPublic, (Pointer)getInfo);
    if (status != 0)
      break;

    /* If all that worked, we have our clone.
     */
    *destObject = (Pointer)newKey;

  } while (0);

  /* If successful, return 0.
   */
  if (status == 0)
    return (0);

  /* If there was an error, destroy anything we created.
   */
  VtDestroyKeyObject (&newKey);

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, 0,
    (char *)0, "VoltCloneDHPubKey", fnctLine, (char *)0)

  return (status);
}

int VoltCloneDHPriKey (
   Pointer sourceObject,
   Pointer *destObject
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)sourceObject;
  VtKeyObject newKey = (VtKeyObject)0;
  VtDHPriKeyInfo *getInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* We know the source is an object, is it a key object?
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if (VOLT_OBJECT_TYPE_NOT_EQUAL (sourceObject, VOLT_OBJECT_TYPE_KEY))
      break;

    /* Is the object a DH private key.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) != VOLT_KEY_ALG_DH)
      break;
    if ((obj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0)
      break;

    /* Get the DH key data out of the source object.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtGetKeyParam (
      (VtKeyObject)obj, VtKeyParamDHPrivate, (Pointer *)&getInfo);
    if (status != 0)
      break;

    /* Create and set a key object with this data.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCreateKeyObject (
      obj->voltObject.libraryCtx, VtKeyImplMpCtx, (Pointer)(obj->mpCtx),
      &newKey);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtSetKeyParam (
      newKey, VtKeyParamDHPrivate, (Pointer)getInfo);
    if (status != 0)
      break;

    /* If all that worked, we have our clone.
     */
    *destObject = (Pointer)newKey;

  } while (0);

  /* If successful, return 0.
   */
  if (status == 0)
    return (0);

  /* If there was an error, destroy anything we created.
   */
  VtDestroyKeyObject (&newKey);

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, errorType,
    (char *)0, "VoltCloneDHPriKey", fnctLine, (char *)0)

  return (status);
}

void DHKeyDataDestroy (
   Pointer obj,
   Pointer ctx
   )
{
  VoltLibCtx *libCtx = (VoltLibCtx *)(((VoltObject *)obj)->libraryCtx);
  VoltMpIntCtx *mpCtx = ((VoltKeyObject *)obj)->mpCtx;
  VoltDHPublicKey *pubKey = (VoltDHPublicKey *)ctx;
  VoltDHPrivateKey *priKey = (VoltDHPrivateKey *)ctx;

  /* Is there anything to destroy?
   */
  if (ctx == (Pointer)0)
    return;

  /* If this is a private key, there's the private value to destroy.
   */
  if (priKey->type == VOLT_KEY_TYPE_PRIVATE)
  {
    if (priKey->priValX != (VoltMpInt *)0)
      mpCtx->DestroyMpInt (&(priKey->priValX));
  }
  else if (pubKey->type != VOLT_KEY_TYPE_PUBLIC)
  {
    /* It's not private, if it's not public, it's not something this
     * destructor can destroy.
     */
    return;
  }

  if (pubKey->primeP != (VoltMpInt *)0)
    mpCtx->DestroyMpInt (&(priKey->primeP));
  if (pubKey->subprimeQ != (VoltMpInt *)0)
    mpCtx->DestroyMpInt (&(priKey->subprimeQ));
  if (pubKey->baseG != (VoltMpInt *)0)
    mpCtx->DestroyMpInt (&(priKey->baseG));
  if (pubKey->pubValY != (VoltMpInt *)0)
    mpCtx->DestroyMpInt (&(priKey->pubValY));

  if (pubKey->keyItems != (VtDHPubKeyInfo *)0)
    Z2Free (pubKey->keyItems);

  /* Now free up the memory of the key data struct.
   */
  Z2Free (ctx);
}

⌨️ 快捷键说明

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