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

📄 ibeparamber.c

📁 IBE是一种非对称密码技术
💻 C
📖 第 1 页 / 共 2 页
字号:
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      libCtx = getAlgData->libCtx;
      if (Z2Memcmp (
        getAlgData->oid, curveTypeOid, VoltBFType1IBECurveOidBytesLen) != 0 )
        break;

      /* The OID matches, the algorithm is IBE encryption.
       */
      *(getAlgData->algorithm) = VT_OID_BF_TYPE1_IBE_PARAMS;
      getAlgData->SymKeyParam = (VtKeyParam *)0;
      getAlgData->DigestImpl = (VtAlgorithmImpl *)0;

      status = 0;
  }

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, object, status, 0, errorType,
    (char *)0, "VtDerCoderBFType1IBEParams", fnctLine, (char *)0)

  return (status);
}

static int EncodeIBEParams (
   VoltParameterObject *obj,
   VoltBFType1IBEParams *params
   )
{
  int status;
  unsigned int primeLen, encodingLen;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  Asn1IBEParams *ibeParams =(Asn1IBEParams *)0;
  unsigned char *buffer = (unsigned char *)0;
  unsigned char *temp;
  unsigned char curveTypeOid[VoltBFType1IBECurveOidBytesLen] =
    { VoltBFType1IBECurveOidBytes };
  unsigned char primeFieldOid[VoltPrimeFieldOidBytesLen] =
    { VoltPrimeFieldOidBytes };
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* Is the encoding already in the object?
   */
  if (obj->paramsDer.data != (unsigned char *)0)
    return (0);

  do
  {
    primeLen = params->paramInfo.curve.primeP.len;

    /* Build the Asn1 "object".
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    ibeParams = Asn1IBEParams_new ();
    if (ibeParams == (Asn1IBEParams *)0)
      break;

    /* Set the fields. Don't set the version in the ibeParams, it's
     * DEFAULT. Set the version in the ECParameters, though.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (Asn1ObjectId_set (
      ibeParams->oid, curveTypeOid, VoltBFType1IBECurveOidBytesLen) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (ASN1_INTEGER_set (
      ibeParams->ecParams->version, 1) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (Asn1ObjectId_set (
      ibeParams->ecParams->fieldId->oid, primeFieldOid,
      VoltPrimeFieldOidBytesLen) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (ASN1_STRING_set (
      ibeParams->ecParams->fieldId->prime,
      params->paramInfo.curve.primeP.data, primeLen) != 1)
      break;

    /* Set coeffA = 0 and coeffB = 1. They are OCTET STRING's the same
     * size as the prime.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    buffer = (unsigned char *)Z2Malloc (primeLen, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, primeLen);

    VOLT_SET_FNCT_LINE (fnctLine)
    if (ASN1_OCTET_STRING_set (
      ibeParams->ecParams->curve->coeffA, buffer, primeLen) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    buffer[primeLen - 1] = 1;
    if (ASN1_OCTET_STRING_set (
      ibeParams->ecParams->curve->coeffB, buffer, primeLen) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (Asn1EcPoint_set (
      ibeParams->ecParams->basePoint, primeLen,
      params->paramInfo.curve.basePointG.xCoord.data,
      params->paramInfo.curve.basePointG.yCoord.len,
      params->paramInfo.curve.basePointG.yCoord.data,
      params->paramInfo.curve.basePointG.yCoord.len) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (ASN1_STRING_set (
      ibeParams->ecParams->subprime,
      params->paramInfo.curve.subprimeQ.data,
      params->paramInfo.curve.subprimeQ.len) != 1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (Asn1EcPoint_set (
      ibeParams->pubPoint, primeLen,
      params->paramInfo.pubPointP.xCoord.data,
      params->paramInfo.pubPointP.xCoord.len,
      params->paramInfo.pubPointP.yCoord.data,
      params->paramInfo.pubPointP.yCoord.len) != 1)
      break;

    /* How big does the buffer need to be?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    encodingLen = i2d_Asn1IBEParams (ibeParams, (unsigned char **)0);
    if (encodingLen == 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    Z2Free (buffer);
    buffer = (unsigned char *)Z2Malloc (encodingLen, 0);
    if (buffer == (unsigned char *)0)
      break;

    /* Now encode into the buffer.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT;
    temp = buffer;
    encodingLen = i2d_Asn1IBEParams (ibeParams, &temp);
    if (encodingLen == 0)
      break;

    /* If everything worked, store the encoding in the object.
     */
    obj->paramsDer.data = buffer;
    obj->paramsDer.len = encodingLen;
    status = 0;

  } while (0);

  if (ibeParams != (Asn1IBEParams *)0)
    Asn1IBEParams_free (ibeParams);

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

  /* If there was an error, free the buffer.
   */
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "EncodeIBEParams", fnctLine, (char *)0)

  return (status);
}

static int DecodeIBEParamsCreate (
   VoltLibCtx *libCtx,
   unsigned char *encoding,
   unsigned int maxEncodingLen,
   Asn1IBEParams **ibeParams
   )
{
  int status;
  unsigned char *temp;
  Asn1IBEParams *newIBEParams = (Asn1IBEParams *)0;
  unsigned char curveTypeOid[VoltBFType1IBECurveOidBytesLen] =
    { VoltBFType1IBECurveOidBytes };
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Create the object.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    newIBEParams = Asn1IBEParams_new ();
    if (newIBEParams == (Asn1IBEParams *)0)
      break;

    /* Decode.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_UNKNOWN_BER;
    temp = encoding;
    d2i_Asn1IBEParams (&newIBEParams, &temp, maxEncodingLen);

    /* Did it work?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (newIBEParams == (Asn1IBEParams *)0)
      break;

    /* Check the OID, expected?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_UNKNOWN_BER;
    if (newIBEParams->oid->base.length != VoltBFType1IBECurveOidBytesLen)
      break;
    if (Z2Memcmp (
      newIBEParams->oid->base.data, curveTypeOid,
      VoltBFType1IBECurveOidBytesLen) != 0)
      break;

    /* If successful, return the object.
     */
    *ibeParams = newIBEParams;
    status = 0;

  } while (0);

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

  /* If error, destroy what we created.
   */
  if (newIBEParams != (Asn1IBEParams *)0)
    Asn1IBEParams_free (newIBEParams);

  VOLT_LOG_ERROR_INFO (
    libCtx, 0, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "DecodeIBEParamsCreate", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

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