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

📄 dhkeytype.c

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

/* 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.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV VoltKeyGetDHPublic VOLT_PROTO_LIST ((
   VtKeyObject object,
   Pointer *getInfo
));

/* 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.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV VoltKeyGetDHPrivate VOLT_PROTO_LIST ((
   VtKeyObject object,
   Pointer *getInfo
));

/* This routine does the work. It allocates and fills in the contexts.
 *
 * @param obj The key object to set.
 * @param keyInfo The data, params and pub value.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV SetObjectDHPublic VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VtDHPubKeyInfo *keyInfo
));

/* This routine does the work. It allocates and fills in the contexts.
 *
 * @param obj The key object to set.
 * @param keyInfo The data, params and pub and pri values.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV SetObjectDHPrivate VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VtDHPriKeyInfo *keyInfo
));

/* Fill in the buffer with the public key info as a
 * VtDHPubKeyInfo struct.
 * <p>This routine does no argument checking, it is the responsibility
 * of the caller not to make mistakes. If buffer is NULL, *bufferSize
 * must be 0.
 *
 * @param obj The key object.
 * @param pubKey The VoltDHPublicKey struct with the MpInt version of
 * the key data.
 * @param The buffer to fill.
 * @param bufferSize As input, the current size of the buffer, as
 * output, the size the buffer needs to be or the number of bytes paced
 * into the buffer.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV BuildPubKeyInfo VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VoltDHPublicKey *pubKey,
   unsigned char *buffer,
   unsigned int *bufferSize
));

/* Fill in the buffer with the private key info as a
 * VtDHPriKeyInfo struct.
 * <p>This routine does no argument checking, it is the responsibility
 * of the caller not to make mistakes. If buffer is NULL, *bufferSize
 * must be 0.
 *
 * @param obj The key object.
 * @param pubKey The VoltDHPrivateKey struct with the MpInt version of
 * the key data.
 * @param The buffer to fill.
 * @param bufferSize As input, the current size of the buffer, as
 * output, the size the buffer needs to be or the number of bytes paced
 * into the buffer.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV BuildPriKeyInfo VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VoltDHPrivateKey *priKey,
   unsigned char *buffer,
   unsigned int *bufferSize
));

int VtKeyParamDHPublic (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtDHPubKeyInfo *keyInfo = (VtDHPubKeyInfo *)info;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    if (flag == VOLT_KEY_GET_TYPE_FLAG)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltKeyGetDHPublic (object, (Pointer *)info);
      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;

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

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

    /* Make sure the key data is there.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if ( (keyInfo->primeP.data == (unsigned char *)0) ||
         (keyInfo->baseG.data == (unsigned char *)0) ||
         (keyInfo->pubValY.data == (unsigned char *)0) )
      break;

#if VOLT_BUILD == VOLT_BUILD_FIPS_SHARED
    /* If this is the FIPS build, there must be a subprimeQ.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (keyInfo->subprimeQ.data == (unsigned char *)0)
      break;
#endif

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectDHPublic (obj, keyInfo);
    if (status != 0)
      break;

    /* Set the FIPS bit in the object type, this object is a FIPS
     * object.
     */
    obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;

  } while (0);

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

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

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, errorType,
    (char *)0, "VtKeyParamDHPublic", fnctLine, (char *)0)

  return (status);
}

int VtKeyParamDHPrivate (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtDHPriKeyInfo *keyInfo = (VtDHPriKeyInfo *)info;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    if (flag == VOLT_KEY_GET_TYPE_FLAG)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltKeyGetDHPrivate (object, (Pointer *)info);
      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;

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

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

    /* Make sure the key data is there.
     * This check demands a 1024-bit primeP and a 160-bit subprimeQ.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if ( (keyInfo->primeP.data == (unsigned char *)0) ||
         (keyInfo->baseG.data == (unsigned char *)0) ||
         (keyInfo->pubValY.data == (unsigned char *)0) ||
         (keyInfo->priValX.data == (unsigned char *)0) )
      break;

#if VOLT_BUILD == VOLT_BUILD_FIPS_SHARED
    /* If this is the FIPS build, there must be a subprimeQ.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (keyInfo->subprimeQ.data == (unsigned char *)0)
      break;
#endif

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectDHPrivate (obj, keyInfo);
    if (status != 0)
      break;

    /* Set the FIPS bit in the object type, this object is a FIPS
     * object.
     */
    obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;

  } while (0);

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

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

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, errorType,
    (char *)0, "VtKeyParamDHPrivate", fnctLine, (char *)0)

  return (status);
}

static int VoltKeyGetDHPublic (
   VtKeyObject object,
   Pointer *getInfo
   )
{
  int status;
  unsigned int bufferSize;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  VoltDHPublicKey *pubKey;
  VoltDHKeyPair *theKeyPair;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Is there data?
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_GET_INFO_UNAVAILABLE;
    if (obj->keyData == (Pointer)0)
      break;

    /* Is the algorithm DH?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) != VOLT_KEY_ALG_DH)
      break;

    /* Check the keyType in the object, if it contains
     * VOLT_KEY_ALG_ASYM_PAIR, the contents are a key pair.
     */
    if ((obj->keyType & VOLT_KEY_TYPE_ASYM_PAIR) != 0)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      theKeyPair = (VoltDHKeyPair *)(obj->keyData);
      if (theKeyPair->pubKey == (VtKeyObject)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtGetKeyParam (
        theKeyPair->pubKey, VtKeyParamDHPublic, getInfo);
      break;
    }

    /* It's not a key pair.
     * We can get the public key data out of a private key object as well.
     */
    if ((obj->keyType & VOLT_KEY_TYPE_PRIVATE) != 0)
    {
      /* If this is a private key, we want to build the private keyItems
       * struct internally. Externally it looks just like the public.
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtGetKeyParam (
        (VtKeyObject)obj, VtKeyParamDHPrivate, getInfo);
      break;
    }

    /* It's not a pair, it's not private, if it's not public, error.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if ((obj->keyType & VOLT_KEY_TYPE_PUBLIC) == 0)
      break;

    /* Is the key in data form?
     */
    if ((obj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
    {
      /* The data is not available, does the object have a GetData
       * function?
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      *getInfo = (Pointer)0;
      if (obj->GetKeyData == (VGetKeyData)0)
        break;

      /* Call the Get function.
       */
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = obj->GetKeyData ((VtKeyObject)obj, getInfo);
      break;
    }

    /* Do we have the data in the appropriate format already?
     */
    status = 0;
    pubKey = (VoltDHPublicKey *)(obj->keyData);
    *getInfo = (Pointer)(pubKey->keyItems);
    if (pubKey->keyItems != (VtDHPubKeyInfo *)0)
      break;

    /* Call the routine that builds the VtDHPubKeyInfo struct.
     * This routine actually will return BufferTooSmall and set
     * bufferSize to the space needed. We'll allocate the space and
     * call it again.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    bufferSize = 0;
    status = BuildPubKeyInfo (obj, pubKey, buffer, &bufferSize);
    if (status == 0)
      status = VT_ERROR_INVALID_KEY_OBJ;
    if (status != VT_ERROR_BUFFER_TOO_SMALL)
      break;

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    buffer = (unsigned char *)Z2Malloc (bufferSize, 0);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = BuildPubKeyInfo (obj, pubKey, buffer, &bufferSize);
    if (status != 0)
      break;

⌨️ 快捷键说明

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