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

📄 ibecurvetype.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "paramobj.h"
#include "ibe.h"
#include "mpint.h"
#include "errorctx.h"

/* Gets the curve out of a parameter object.
 *
 * @param paramObj The object from which the params are to be extracted.
 * @param getInfo The address where the function will deposit the
 * pointer to the info.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV VoltParameterGetBFType1IBECurve VOLT_PROTO_LIST ((
   VtParameterObject object,
   Pointer *getInfo
));

int VtParameterParamBFType1IBECurve (
   VtParameterObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltParameterObject *obj = (VoltParameterObject *)object;
  VtBFType1IBECurveInfo *curveInfo = (VtBFType1IBECurveInfo *)info;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    if (flag == VOLT_PARAM_GET_TYPE_FLAG)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltParameterGetBFType1IBECurve (object, (Pointer *)info);
      break;
    }

    /* Check the flag, it should be VOLT_PARAM_SET_TYPE_FLAG.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_PARAM_SET_TYPE_FLAG)
      break;

    /* Check the paramType of the object. It should be 0.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_SET;
    if (obj->paramType != 0)
      break;

    /* The associated info should be a pointer to VoltageBFType1IBECurve.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    /* Is the input curve acceptable?
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCheckCurve (
      (VoltLibCtx *)(obj->voltObject.libraryCtx), curveInfo);
    if (status != 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltSetIBECurve (obj, curveInfo);

  } while (0);

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

  /* If something went wrong, indicate that this object is not usable.
   */
  obj->paramType = 0;

  VOLT_LOG_ERROR (
    obj->voltObject.libraryCtx, status, errorType, fnctLine,
    "VtParameterParamBFType1IBECurve", (char *)0)

  return (status);
}

int VoltParameterGetBFType1IBECurve (
   VtParameterObject object,
   Pointer *getInfo
   )
{
  int status;
  VoltParameterObject *obj = (VoltParameterObject *)object;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the paramType in the object, it should contain
     * VOLT_PARAM_ALG_IBE_TYPE_1 and the either VOLT_PARAM_TYPE_CURVE
     * or VOLT_PARAM_TYPE_PARAMS.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_GET_INFO_UNAVAILABLE;
    if (obj->paramType == 0)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if ((obj->paramType & VOLT_PARAM_TYPE_MASK_ALG) !=
        VOLT_PARAM_ALG_IBE_TYPE_1)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if ( ((obj->paramType & VOLT_PARAM_TYPE_CURVE) == 0) &&
         ((obj->paramType & VOLT_PARAM_TYPE_PARAMS) == 0) )
      break;

    /* Are the parameters in data format? If so, we're done. (Set the
     * return values.)
     */
    *getInfo = obj->paramData;
    status = 0;
    if ((obj->paramType & VOLT_PARAM_TYPE_MASK_DATA) == VOLT_PARAM_TYPE_DATA)
      break;

    /* The data is not available, does the object have a GetData
     * function?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    *getInfo = (Pointer)0;
    status = VT_ERROR_INVALID_GET;
    if (obj->GetParamData == (VGetParamData)0)
      break;

    /* Call the Get function.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = obj->GetParamData ((VtParameterObject)obj, getInfo);

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, obj->voltObject.libraryCtx, status, errorType, fnctLine,
    "VoltParameterGetBFType1IBECurve", (char *)0)

  return (status);
}

int VoltSetIBECurve (
   VoltParameterObject *obj,
   VtBFType1IBECurveInfo *curveInfo
   )
{
  int status;
  unsigned int bufferSize, offset;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  VoltBFType1IBECurve *theCurve;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Allocate space to hold the curve struct, the prime, subprime and
     * base point. After the struct, all the data is byte arrays, so
     * there's no need to worry about alignment.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      sizeof (VoltBFType1IBECurve) + curveInfo->primeP.len +
      curveInfo->subprimeQ.len + curveInfo->basePointG.xCoord.len +
      curveInfo->basePointG.yCoord.len;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the structs and data.
     */
    theCurve = (VoltBFType1IBECurve *)buffer;
    offset = sizeof (VoltBFType1IBECurve);
    theCurve->curveData.primeP.data = buffer + offset;
    offset += curveInfo->primeP.len;
    theCurve->curveData.subprimeQ.data = buffer + offset;
    offset += curveInfo->subprimeQ.len;
    theCurve->curveData.basePointG.xCoord.data = buffer + offset;
    offset += curveInfo->basePointG.xCoord.len;
    theCurve->curveData.basePointG.yCoord.data = buffer + offset;

    /* Set these values "early" so we have the destructor in place if
     * something goes wrong.
     */
    obj->paramData = (Pointer)theCurve;
    obj->CopyParams = BFType1IBECopyCurve;
    obj->ParamDataDestroy = BFType1IBECurveDataDestroy;
    obj->paramType =
      VOLT_PARAM_ALG_IBE_TYPE_1 | VOLT_PARAM_TYPE_CURVE |
      VOLT_PARAM_TYPE_CONTENTS | VOLT_PARAM_TYPE_DATA;

    /* Fill in fields and copy data.
     */
    theCurve->curveData.basePointG.isInfinity = 0;

    Z2Memcpy (
      theCurve->curveData.primeP.data, curveInfo->primeP.data,
      curveInfo->primeP.len);
    theCurve->curveData.primeP.len = curveInfo->primeP.len;

    Z2Memcpy (
      theCurve->curveData.subprimeQ.data, curveInfo->subprimeQ.data,
      curveInfo->subprimeQ.len);
    theCurve->curveData.subprimeQ.len = curveInfo->subprimeQ.len;

    Z2Memcpy (
      theCurve->curveData.basePointG.xCoord.data,
      curveInfo->basePointG.xCoord.data, curveInfo->basePointG.xCoord.len);
    theCurve->curveData.basePointG.xCoord.len =
      curveInfo->basePointG.xCoord.len;

    Z2Memcpy (
      theCurve->curveData.basePointG.yCoord.data,
      curveInfo->basePointG.yCoord.data, curveInfo->basePointG.yCoord.len);
    theCurve->curveData.basePointG.yCoord.len =
      curveInfo->basePointG.yCoord.len;

    /* Now get the bfCtx.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBfCtxFromIBEParams (
      libCtx, (VtMpIntCtx)(obj->mpCtx), &(theCurve->curveData),
      (VtBFType1IBEPoint *)0, 0, (unsigned char *)0, &(theCurve->bfCtx));

  } while (0);

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

  /* If something went wrong, indicate that this object is not usable.
   */
  obj->paramType = 0;

  VOLT_LOG_ERROR (
    (VtLibCtx)libCtx, status, errorType, fnctLine,
    "VoltSetIBECurve", (char *)0)

  return (status);
}

void BFType1IBECurveDataDestroy (
   Pointer paramObj,
   Pointer ctx
   )
{
  VoltParameterObject *obj = (VoltParameterObject *)paramObj;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltBFType1IBECurve *theCurve = (VoltBFType1IBECurve *)(obj->paramData);

  /* Is there anything to destroy?
   */
  if (obj->paramData == (Pointer)0)
    return;

  VoltReleaseBfCtx (libCtx, &(theCurve->bfCtx));

  Z2Free (obj->paramData);
}

int BFType1IBECopyCurve (
   Pointer sourceParamObj,
   Pointer destParamObj
   )
{
  int status;
  VoltParameterObject *src = (VoltParameterObject *)sourceParamObj;
  VoltParameterObject *dest = (VoltParameterObject *)destParamObj;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    dest->paramType = src->paramType;
    dest->CopyParams = src->CopyParams;
    dest->GetParamData = src->GetParamData;
    dest->ParamDataDestroy = src->ParamDataDestroy;

    /* If there's no paramData, we're done.
     */
    if (src->paramData == (Pointer)0)
      break;

    /* Is the paramData data or a handle?
     * If a handle, we should not be in this code.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_GET;
    if ((src->paramType & VOLT_PARAM_TYPE_MASK_DATA) != VOLT_PARAM_TYPE_DATA)
      break;

    /* Just call the Set function to copy the data.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltSetIBECurve (
      dest, (VtBFType1IBECurveInfo *)(src->paramData));

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, src->voltObject.libraryCtx, status, errorType, fnctLine,
    "BFType1IBECopyCurve", (char *)0)

  return (status);
}

int VoltCheckCurve (
   VoltLibCtx *libCtx,
   VtBFType1IBECurveInfo *curveInfo
   )
{
  int status;
  unsigned int primeLen;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if ( (curveInfo->primeP.data == (unsigned char *)0) ||
         (curveInfo->subprimeQ.data == (unsigned char *)0) )
      break;

    primeLen = curveInfo->primeP.len;

    /* The base point cannot be infinity.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (curveInfo->basePointG.isInfinity != 0)
      break;

    /* The length of the point's coordinates should be the same size as
     * the primeP. Unless there is no y-coordinate, in which case the
     * x-coordinate must have an extra byte.
     */
    if (curveInfo->basePointG.yCoord.data == (unsigned char *)0)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      if (curveInfo->basePointG.xCoord.data == (unsigned char *)0)
        break;
      VOLT_SET_FNCT_LINE (fnctLine)
      if (curveInfo->basePointG.xCoord.len > (primeLen + 1))
        break;
      VOLT_SET_FNCT_LINE (fnctLine)
      if (curveInfo->basePointG.yCoord.len != 0)
        break;
    }
    else
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      if (curveInfo->basePointG.yCoord.len > primeLen)
        break;
      if (curveInfo->basePointG.xCoord.data != (unsigned char *)0)
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        if (curveInfo->basePointG.xCoord.len > primeLen)
          break;
      }
      else
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        if (curveInfo->basePointG.xCoord.len != 0)
          break;
      }
    }

    status = 0;

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "VoltCheckCurve", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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