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

📄 ibeparamber.c

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

/* The IBE Parameter Data (IBEPublicParameters).
 */
typedef struct
{
  ASN1_INTEGER *version;
  Asn1ObjectId *oid;
  Asn1EcPoint *pubPoint;
  Asn1EcParameters *ecParams;
} Asn1IBEParams;

DECLARE_ASN1_FUNCTIONS (Asn1IBEParams)

/* Set up the OpenSSL ASN.1 templates.
 */
ASN1_SEQUENCE (Asn1IBEParams) =
{
  ASN1_OPT (Asn1IBEParams, version, ASN1_INTEGER),
  ASN1_SIMPLE (Asn1IBEParams, oid, Asn1ObjectId),
  ASN1_SIMPLE (Asn1IBEParams, pubPoint, Asn1EcPoint),
  ASN1_SIMPLE (Asn1IBEParams, ecParams, Asn1EcParameters)
} ASN1_SEQUENCE_END (Asn1IBEParams);

IMPLEMENT_ASN1_FUNCTIONS (Asn1IBEParams)

/* Encode the IBE params.
 * <p>This function will place the encoding into the paramsDer field of
 * the VoltParameterObject. That is, upon return (if no error), find the
 * encoding in the paramsDer VtItem.
 *
 * @param obj The object with the param data to encode.
 * @param paramData The param data (if hardware params, the data might
 * not be in the object).
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code (e.g., key and object don't match).
 */
static int VOLT_CALLING_CONV EncodeIBEParams VOLT_PROTO_LIST ((
   VoltParameterObject *obj,
   VoltBFType1IBEParams *params
));

/* Decode a IBE params. This function creates a new Asn1IBEParams
 * "object", it is the responsibility of the caller to destroy it (by
 * calling Asn1IBEParams_free).
 *
 * @param libCtx The library context to use.
 * @param encoding The buffer containing the alleged IBE params.
 * @param maxEncodingLen The max number of bytes to decode.
 * @param ibeParams The address where this function will deposit the
 * created Asn1IBEParams "object".
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV DecodeIBEParamsCreate VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   unsigned char *encoding,
   unsigned int maxEncodingLen,
   Asn1IBEParams **ibeParams
));

int VtDerCoderBFType1IBEParams (
   VtDerCoderInfo *coderInfo,
   Pointer object,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize;
  VoltLibCtx *libCtx;
  VoltParameterObject *obj = (VoltParameterObject *)object;
  VoltDerCoderEncodeData *encodeData = &(coderInfo->info.encodeData);
  VoltDerCoderGetAlgData *getAlgData = &(coderInfo->info.getAlgData);
  VoltDerCoderDecodeData *decodeData = &(coderInfo->info.decodeData);
  VoltBFType1IBEParams *paramData;
  VtSetParamBerInfo *setBerInfo;
  Asn1IBEParams *ibeParams = (Asn1IBEParams *)0;
  VtBFType1IBEParamInfo paramInfo;
  unsigned char curveTypeOid[VoltBFType1IBECurveOidBytesLen] =
    { VoltBFType1IBECurveOidBytes };
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  switch (flag)
  {
    default:
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_TYPE;
      break;

    case VOLT_DER_TYPE_ENCODE_FLAG:
      /* If the flag is ENCODE, return the IBEPublicParameters.
       */

      /* Check the args.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NON_NULL_ARG;
      if (object != (Pointer)0)
        break;

      /* We need a place to drop the length.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NULL_ARG;
      if (encodeData->encodingLen == (unsigned int *)0)
        break;

      /* The info should be a param object.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NULL_ARG;
      if (encodeData->info == (Pointer)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_INVALID_PARAM_OBJ;
      obj = (VoltParameterObject *)(encodeData->info);
      if (VOLT_OBJECT_TYPE_NOT_EQUAL (obj, VOLT_OBJECT_TYPE_PARAMETER))
        break;      
      if ((obj->paramType & VOLT_PARAM_TYPE_MASK_ALG) !=
          VOLT_PARAM_ALG_IBE_TYPE_1)
        break;

      /* We need the param data.
       */
      paramData = (VoltBFType1IBEParams *)(obj->paramData);
      if ((obj->paramType & VOLT_PARAM_TYPE_MASK_DATA) != VOLT_PARAM_TYPE_DATA)
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        if (obj->GetParamData == (VGetParamData)0)
          break;

        VOLT_SET_ERROR_TYPE (errorType, 0)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = obj->GetParamData (
          (VtParameterObject)obj, (Pointer *)&paramData);
        if (status != 0)
          break;
      }

      bufferSize = encodeData->bufferSize;
      if (encodeData->encoding == (unsigned char *)0)
        bufferSize = 0;

      /* Build the params.
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = EncodeIBEParams (obj, paramData);
      if (status != 0)
        break;

      /* Is the buffer big enough?
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_BUFFER_TOO_SMALL;
      *(encodeData->encodingLen) = obj->paramsDer.len;
      if (bufferSize < obj->paramsDer.len)
        break;

      libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
      Z2Memcpy (encodeData->encoding, obj->paramsDer.data, obj->paramsDer.len);
      status = 0;

      break;

    case VOLT_DER_TYPE_DECODE_FLAG:
      /* If the flag is decode, set the param object with the param
       * info.
       */

      /* Check the args, the type should be VOLT_DER_TYPE_PARAM_FLAG.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_UNKNOWN_BER;
      if (decodeData->type != VOLT_DER_TYPE_PARAM_FLAG)
        break;

      setBerInfo = (VtSetParamBerInfo *)(decodeData->info);

      libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = DecodeIBEParamsCreate (
        libCtx, setBerInfo->berEncoding, setBerInfo->maxEncodingLen,
        &ibeParams);
      if (status != 0)
        break;

      paramInfo.curve.primeP.data = ibeParams->ecParams->fieldId->prime->data;
      paramInfo.curve.primeP.len = ibeParams->ecParams->fieldId->prime->length;
      paramInfo.curve.subprimeQ.data = ibeParams->ecParams->subprime->data;
      paramInfo.curve.subprimeQ.len = ibeParams->ecParams->subprime->length;
      paramInfo.curve.basePointG.isInfinity = 0;
      paramInfo.curve.basePointG.xCoord.data =
        ibeParams->ecParams->basePoint->xCoord.data;
      paramInfo.curve.basePointG.xCoord.len =
        ibeParams->ecParams->basePoint->xCoord.len;
      paramInfo.curve.basePointG.yCoord.data =
        ibeParams->ecParams->basePoint->yCoord.data;
      paramInfo.curve.basePointG.yCoord.len =
        ibeParams->ecParams->basePoint->yCoord.len;
      paramInfo.pubPointP.isInfinity = 0;
      paramInfo.pubPointP.xCoord.data = ibeParams->pubPoint->xCoord.data;
      paramInfo.pubPointP.xCoord.len = ibeParams->pubPoint->xCoord.len;
      paramInfo.pubPointP.yCoord.data = ibeParams->pubPoint->yCoord.data;
      paramInfo.pubPointP.yCoord.len = ibeParams->pubPoint->yCoord.len;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtSetParameterParam (
        (VtParameterObject)obj, VtParameterParamBFType1IBEParams,
        (Pointer)&paramInfo);

      break;

    case VOLT_DER_TYPE_GET_ALG_FLAG:
      /* If the flag is get alg, check the input to see if it's the
       * IBE encryption OID.
       */

      /* Check the args.
       */
      VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NON_NULL_ARG;
      if (object != (Pointer)0)
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_NULL_ARG;
      if ( (getAlgData->algorithm == (unsigned int *)0) ||
           (getAlgData->oid == (unsigned char *)0) )
        break;

      VOLT_SET_FNCT_LINE (fnctLine)
      status = VT_ERROR_UNKNOWN_BER;
      if (getAlgData->oidLen != VoltBFType1IBECurveOidBytesLen)

⌨️ 快捷键说明

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