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

📄 dsakeytype.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 3 页
字号:
   */
  spaceAvailable = 0;
  totalSize = sizeof (VtDSAPubKeyInfo);
  if (*bufferSize > sizeof (VtDSAPubKeyInfo))
    spaceAvailable = *bufferSize - sizeof (VtDSAPubKeyInfo);

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

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

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

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

    /* 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_COMPARE (
    status, libCtx, status, 0, fnctLine, "BuildPubKeyInfo", (char *)0)

  return (status);
}

static int BuildPriKeyInfo (
   VoltKeyObject *obj,
   VoltDsaPrivateKey *priKey,
   unsigned char *buffer,
   unsigned int *bufferSize
   )
{
  int status;
  unsigned int totalSize, spaceAvailable;
  unsigned int sign, primePLen, subprimeQLen, baseGLen, pubValYLen, priValXLen;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltMpIntCtx *mpCtx = obj->mpCtx;
  VtDSAPriKeyInfo *keyInfo = (VtDSAPriKeyInfo *)buffer;
  unsigned char *currentBuf;  
  VOLT_DECLARE_FNCT_LINE (fnctLine)

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

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

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

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

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

    /* 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;
      currentBuf += priValXLen;
      spaceAvailable -= priValXLen;
    }
    else
    {
      /* If not 0, then status should be BufferTooSmall.
       */
      if (status != VT_ERROR_BUFFER_TOO_SMALL)
        break;
      spaceAvailable = 0;
    }
    totalSize += priValXLen;

    /* Try to get the public value. We don't store the pubVal with
     * the DSA private key. So if the pubVal is NULL we need to compute
     * it from the other values.
     */
    if (priKey->pubValY == (VoltMpInt *)0)
    {
      status =  SetObjectDSAPubFromPrivate (libCtx, mpCtx, priKey);
      if (status != 0)
        break;
    }
    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;      
    }
    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_COMPARE (
    status, libCtx, status, 0, fnctLine, "BuildPriKeyInfo", (char *)0)

  return (status);
}

int VoltCloneDsaPubKey (
   Pointer sourceObject,
   Pointer *destObject
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)sourceObject;
  VtKeyObject newKey = (VtKeyObject)0;
  VtDSAPubKeyInfo *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 DSA 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_DSA)
      break;
    if ((obj->keyType & VOLT_KEY_TYPE_PUBLIC) == 0)
      break;

    /* Get the DSA key data out of the source object.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtGetKeyParam (
      (VtKeyObject)obj, VtKeyParamDSAPublic, (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, VtKeyParamDSAPublic, (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 (
    obj->voltObject.libraryCtx, status, 0, fnctLine, "VoltCloneDsaPubKey",
    (char *)0)

  return (status);
}

int VoltCloneDsaPriKey (
   Pointer sourceObject,
   Pointer *destObject
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)sourceObject;
  VtKeyObject newKey = (VtKeyObject)0;
  VtDSAPriKeyInfo *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 DSA 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_DSA)
      break;
    if ((obj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0)
      break;

    /* Get the DSA key data out of the source object.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtGetKeyParam (
      (VtKeyObject)obj, VtKeyParamDSAPrivate, (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, VtKeyParamDSAPrivate, (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 (
    obj->voltObject.libraryCtx, status, 0, fnctLine, "VoltCloneDsaPriKey",
    (char *)0)

  return (status);
}

void DSAKeyDataDestroy (
   Pointer obj,
   Pointer ctx
   )
{
  VoltLibCtx *libCtx = (VoltLibCtx *)(((VoltObject *)obj)->libraryCtx);
  VoltMpIntCtx *mpCtx = ((VoltKeyObject *)obj)->mpCtx;
  VoltDsaPublicKey *pubKey = (VoltDsaPublicKey *)ctx;
  VoltDsaPrivateKey *priKey = (VoltDsaPrivateKey *)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 != (VtDSAPubKeyInfo *)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 + -