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

📄 dsakeytype.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Copyright 2003-2005, Voltage Security, all rights reserved.
 */
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "keyobj.h"
#include "dsa.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 VoltKeyGetDsaPublic 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 VoltKeyGetDsaPrivate 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 SetObjectDSAPublic VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VtDSAPubKeyInfo *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 SetObjectDSAPrivate VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VtDSAPriKeyInfo *keyInfo
));

/* This routine ccomputes the DSA public key given the DSA private key
 * Because we don't store the public key with the DSA private key, this
 * can be helpful if we want to rebuild the public key from the private
 * key. It also sets the key object with the public value.  
 *
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV SetObjectDSAPubFromPrivate VOLT_PROTO_LIST ((
   VoltLibCtx *libCtx,
   VoltMpIntCtx *mpCtx,
   VoltDsaPrivateKey *priKey
   ));

/* Fill in the buffer with the public key info as a
 * VtDSAPubKeyInfo 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 VoltDsaPublicKey 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,
   VoltDsaPublicKey *pubKey,
   unsigned char *buffer,
   unsigned int *bufferSize
));

/* Fill in the buffer with the private key info as a
 * VtDSAPriKeyInfo 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 VoltDsaPrivateKey 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,
   VoltDsaPrivateKey *priKey,
   unsigned char *buffer,
   unsigned int *bufferSize
));

int VtKeyParamDSAPublic (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtDSAPubKeyInfo *keyInfo = (VtDSAPubKeyInfo *)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 = VoltKeyGetDsaPublic (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 VtDSAPubKeyInfo.
     */
    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->primeP.len < 64 ) || 
         (keyInfo->primeP.len > 256) ||
         (keyInfo->subprimeQ.data == (unsigned char *)0) ||
         (keyInfo->subprimeQ.len != 20) ||
         (keyInfo->baseG.data == (unsigned char *)0) ||
         (keyInfo->pubValY.data == (unsigned char *)0) )
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectDSAPublic (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 (
    obj->voltObject.libraryCtx, status, errorType, fnctLine,
    "VtKeyParamDSAPublic", (char *)0)

  return (status);
}

int VtKeyParamDSAPrivate (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtDSAPriKeyInfo *keyInfo = (VtDSAPriKeyInfo *)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 = VoltKeyGetDsaPrivate (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 VtDSAPriKeyInfo.
     */
    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->primeP.len != 128) ||
         (keyInfo->subprimeQ.data == (unsigned char *)0) ||
         (keyInfo->subprimeQ.len != 20) ||
         (keyInfo->baseG.data == (unsigned char *)0) )
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectDSAPrivate (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 (
    obj->voltObject.libraryCtx, status, errorType, fnctLine,
    "VtKeyParamDSAPrivate", (char *)0)

  return (status);
}

static int VoltKeyGetDsaPublic (
   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;
  VoltDsaPublicKey *pubKey;
  VoltDsaKeyPair *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 DSA?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) != VOLT_KEY_ALG_DSA)
      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 = (VoltDsaKeyPair *)(obj->keyData);
      if (theKeyPair->pubKey == (VtKeyObject)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VtGetKeyParam (
        theKeyPair->pubKey, VtKeyParamDSAPublic, 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, VtKeyParamDSAPrivate, 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 = (VoltDsaPublicKey *)(obj->keyData);
    *getInfo = (Pointer)(pubKey->keyItems);
    if (pubKey->keyItems != (VtDSAPubKeyInfo *)0)
      break;

    /* Call the routine that builds the VtDSAPubKeyInfo 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;

    pubKey->keyItems = (VtDSAPubKeyInfo *)buffer;
    *getInfo = (Pointer)buffer;

  } while (0);

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

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

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

  return (status);
}

static int VoltKeyGetDsaPrivate (
   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;
  VoltDsaPrivateKey *priKey;
  VoltDsaKeyPair *theKeyPair;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Is there data?
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)

⌨️ 快捷键说明

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