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

📄 ibeprikey.c

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

/* Both BF and BB private keys use this code to set and get key info.
 * <p>If BF, algorithm must be VOLT_KEY_ALG_IBE_TYPE_1, if BB,
 * algorithm must be VOLT_KEY_ALG_BB_TYPE_1.
 * <p>Pass the ParameterParm used to get the parameter info out of the
 * object. We'll need the prime length. So pass in a ParameterParam
 * which will return a data struct whose first field is a VtItem (not a
 * pointer to a VtItem) that is primeP.
 */
static int VOLT_CALLING_CONV VoltKeyParamIBEPrivate VOLT_PROTO_LIST ((
   VtKeyObject object,
   Pointer info,
   unsigned int flag,
   unsigned int algorithm,
   VtParameterParam *ParameterParam
));

/* Gets the key data out of a key object.
 *
 * @param object The object from which the data is to be extracted.
 * @param getInfo The address where the function will deposit the
 * pointer to the info.
 * @param algorithm Either VOLT_KEY_ALG_IBE_TYPE_1 or
 * VOLT_KEY_ALG_BB_TYPE_1, indicating which type of key is to be
 * retrieved.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV VoltKeyGetIBEPrivate VOLT_PROTO_LIST ((
   VtKeyObject object,
   Pointer *getInfo,
   unsigned int algorithm
));

int VtKeyParamBBType1IBEPrivate (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  status = VoltKeyParamIBEPrivate (
    object, info, flag, VOLT_KEY_ALG_BB_TYPE_1,
    VtParameterParamBBType1IBEParams);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, object, status, 0, 0,
    (char *)0, "VtKeyParamBBType1IBEPrivate", fnctLine, (char *)0)

  return (status);
}

int VtKeyParamBFType1IBEPrivate (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  VOLT_SET_FNCT_LINE (fnctLine)
  status = VoltKeyParamIBEPrivate (
    object, info, flag, VOLT_KEY_ALG_IBE_TYPE_1,
    VtParameterParamBFType1IBEParams);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, object, status, 0, 0,
    (char *)0, "VtKeyParamBFType1IBEPrivate", fnctLine, (char *)0)

  return (status);
}

static int VoltKeyParamIBEPrivate (
   VtKeyObject object,
   Pointer info,
   unsigned int flag,
   unsigned int algorithm,
   VtParameterParam *ParameterParam
   )
{
  int status;
  unsigned int paramAlg, primeLen, bufferSize, offset, pointDataLen;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtBFType1IBEPriKeyInfo *bfInfo = (VtBFType1IBEPriKeyInfo *)info;
  VtBBType1IBEPriKeyInfo *bbInfo = (VtBBType1IBEPriKeyInfo *)info;
  VtBFType1IBEParamInfo *bfParams;
  VtBBType1IBEParamInfo *bbParams;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltParameterObject *pObj;
  VtParameterObject paramClone = (VtParameterObject)0;
  VoltIBEPriKeyData *localData;
  VtItem *encodedId;
  unsigned char *buffer = (unsigned char *)0;
  VtBFType1IBEPoint newPrivatePoint;
  VtBBType1IBEPoint newPrivatePointD0;
  VtBBType1IBEPoint newPrivatePointD1;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  Z2Memset (&newPrivatePoint, 0, sizeof (newPrivatePoint));
  Z2Memset (&newPrivatePointD0, 0, sizeof (newPrivatePointD0));
  Z2Memset (&newPrivatePointD1, 0, sizeof (newPrivatePointD1));

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

    /* Check the flag, it should be VOLT_KEY_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_KEY_SET_TYPE_FLAG)
      break;

    /* Check the keyType in the object, it should be 0.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_SET;
    if (obj->keyType != 0)
      break;

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

    /* Valid param object?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    pObj = (VoltParameterObject *)(bbInfo->ibeParams);
    paramAlg = VOLT_PARAM_ALG_BB_TYPE_1;
    if (algorithm == VOLT_KEY_ALG_IBE_TYPE_1)
    {
      pObj = (VoltParameterObject *)(bfInfo->ibeParams);
      paramAlg = VOLT_PARAM_ALG_IBE_TYPE_1;
    }

    if (pObj == (VoltParameterObject *)0)
      break;
    if (VOLT_OBJECT_TYPE_NOT_EQUAL (pObj, VOLT_OBJECT_TYPE_PARAMETER))
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if (pObj->paramData == (Pointer)0)
      break;
    if ((pObj->paramType & VOLT_PARAM_TYPE_MASK_ALG) != paramAlg)
      break;
    if ((pObj->paramType & VOLT_PARAM_TYPE_PARAMS) == 0)
      break;
    if ((pObj->paramType & VOLT_PARAM_TYPE_CONTENTS) == 0)
      break;

    /* If using this KeyParam, the object must already have an
     * mpCtx loaded.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if (obj->mpCtx == (VoltMpIntCtx *)0)
      break;

    /* Is there one private point? Or two?
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (algorithm == VOLT_KEY_ALG_IBE_TYPE_1)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      if (VtGetParameterParam (
        bfInfo->ibeParams, ParameterParam, (Pointer *)&bfParams) != 0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      if (VoltBFCopyPoint (
        libCtx, obj->mpCtx, bfParams, &(bfInfo->privatePoint),
        &newPrivatePoint) != 0)
        break;

      primeLen = bfParams->curve.primeP.len;
      pointDataLen = 2 * primeLen;
      encodedId = &(bfInfo->encodedId);
    }
    else
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      if (VtGetParameterParam (
        bbInfo->ibeParams, ParameterParam, (Pointer *)&bbParams) != 0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      if (VoltBBCopyPoint (
        libCtx, obj->mpCtx, bbParams, &(bbInfo->privatePointD0),
        &newPrivatePointD0))
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      if (VoltBBCopyPoint (
        libCtx, obj->mpCtx, bbParams, &(bbInfo->privatePointD1),
        &newPrivatePointD1))
        break;

      primeLen = bbParams->primeP.len;
      pointDataLen = 4 * primeLen;
      encodedId = &(bbInfo->encodedId);
    }

    /* Create a VoltIBEPriKeyData struct.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      sizeof (VoltIBEPriKeyData) + encodedId->len + pointDataLen;
    buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);
    localData = (VoltIBEPriKeyData *)buffer;
    localData->primeLen = primeLen;
    offset = sizeof (VoltIBEPriKeyData);
    localData->bfKeyInfo.encodedId.data = buffer + offset;
    localData->bbKeyInfo.encodedId.data = buffer + offset;
    offset += encodedId->len;
    localData->bfKeyInfo.privatePoint.xCoord.data = buffer + offset;
    localData->bbKeyInfo.privatePointD0.xCoord.data = buffer + offset;
    offset += primeLen;
    localData->bfKeyInfo.privatePoint.yCoord.data = buffer + offset;
    localData->bbKeyInfo.privatePointD0.yCoord.data = buffer + offset;

    if (algorithm == VOLT_KEY_ALG_BB_TYPE_1)
    {
      offset += primeLen;
      localData->bbKeyInfo.privatePointD1.xCoord.data = buffer + offset;
      offset += primeLen;
      localData->bbKeyInfo.privatePointD1.yCoord.data = buffer + offset;
    }

    /* Clone the parameter object.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCloneObject (
      (Pointer)pObj, (Pointer *)&(localData->bfKeyInfo.ibeParams));
    if (status != 0)
      break;

    localData->bbKeyInfo.ibeParams = localData->bfKeyInfo.ibeParams;

    /* Copy the encoding into the local buffer.
     */
    Z2Memcpy (
      localData->bfKeyInfo.encodedId.data, encodedId->data, encodedId->len);
    localData->bfKeyInfo.encodedId.len = encodedId->len;
    localData->bbKeyInfo.encodedId.len = encodedId->len;

    /* If BF, copy the private point.
     * If BB, copy the two points.
     */
    if (algorithm == VOLT_KEY_ALG_IBE_TYPE_1)
    {
      localData->bfKeyInfo.privatePoint.isInfinity =
        bfInfo->privatePoint.isInfinity;
      Z2Memcpy (
        localData->bfKeyInfo.privatePoint.xCoord.data,
        newPrivatePoint.xCoord.data, newPrivatePoint.xCoord.len);
      localData->bfKeyInfo.privatePoint.xCoord.len =
        newPrivatePoint.xCoord.len;
      Z2Memcpy (
        localData->bfKeyInfo.privatePoint.yCoord.data,

⌨️ 快捷键说明

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