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

📄 rsakeytype.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 "rsa.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 usageFlag VERIFY or ENCRYPT, make sure the object matches.
 * @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 VoltKeyGetRsaPublic VOLT_PROTO_LIST ((
   VtKeyObject object,
   unsigned int usageFlag,
   Pointer *getInfo
));

/* Gets the key data out of a key object.
 *
 * @param object The object from which the data is to be extracted.
 * @param usageFlag VERIFY or ENCRYPT, make sure the object matches.
 * @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 VoltKeyGetRsaPrivate VOLT_PROTO_LIST ((
   VtKeyObject object,
   unsigned int usageFlag,
   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 SetObjectRSAPublic VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   unsigned int usageFlag,
   VtRSAPubKeyInfo *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.
 * @param flag If the 1 bit is set, copy the modulus and private
 * exponent. If the 2 bit is set, copy the CRT info. If the 4 bit is
 * set, copy the public exponent.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV SetObjectRSAPrivate VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VtRSAPriKeyInfo *keyInfo,
   unsigned int flag,
   unsigned int usageFlag
));

/* Fill in the buffer with the public key info as a VtRSAPubKeyInfo
 * 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 VoltRsaPublicKey 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 BuildRsaPubKeyInfo VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VoltRsaPublicKey *pubKey,
   unsigned char *buffer,
   unsigned int *bufferSize
));

/* Fill in the buffer with the private key info as a VtRSAPriKeyInfo
 * 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 VoltRsaPrivateKey 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 BuildRsaPriKeyInfo VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VoltRsaPrivateKey *priKey,
   unsigned char *buffer,
   unsigned int *bufferSize
));

int VtKeyParamRSAPublicVerify (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  return (VoltKeyParamRSAPublic (
    object, info, flag, VT_RSA_KEY_USAGE_SIGN_VERIFY));
}

int VtKeyParamRSAPublicEncrypt (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  return (VoltKeyParamRSAPublic (
    object, info, flag, VT_RSA_KEY_USAGE_ENCRYPT_DECRYPT));
}

int VoltKeyParamRSAPublic (
   VtKeyObject object,
   Pointer info,
   unsigned int flag,
   unsigned int usageFlag
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtRSAPubKeyInfo *keyInfo = (VtRSAPubKeyInfo *)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 = VoltKeyGetRsaPublic (object, usageFlag, (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 VtRSAPubKeyInfo.
     */
    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- or 2048-bit modulus.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if ( (keyInfo->modulus.data == (unsigned char *)0) ||
         (keyInfo->pubExpo.data == (unsigned char *)0) ||
         (keyInfo->pubExpo.len == 0) ||
         (keyInfo->pubExpo.len > keyInfo->modulus.len) )
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if ( (keyInfo->modulus.len != 128) && (keyInfo->modulus.len != 256) )
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectRSAPublic (obj, usageFlag, 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, "VoltKeyParamRSAPublic", fnctLine, (char *)0)

  return (status);
}

int VtKeyParamRSAPrivateSign (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  return (VoltKeyParamRSAPrivate (
    object, info, flag, VT_RSA_KEY_USAGE_SIGN_VERIFY));
}

int VtKeyParamRSAPrivateDecrypt (
   VtKeyObject object,
   Pointer info,
   unsigned int flag
   )
{
  return (VoltKeyParamRSAPrivate (
    object, info, flag, VT_RSA_KEY_USAGE_ENCRYPT_DECRYPT));
}

int VoltKeyParamRSAPrivate (
   VtKeyObject object,
   Pointer info,
   unsigned int flag,
   unsigned int usageFlag
   )
{
  int status;
  unsigned int infoFlag;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtRSAPriKeyInfo *keyInfo = (VtRSAPriKeyInfo *)info;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* Set infoFlag to 0, meaning we don't have modulus and private exponent,
   * nor do we have the CRT info. Later on, we'll check to see if the
   * info is correct and reset the flag. If we have the modulus and
   * private exponent, set the 1 bit in the flag. If we have the CRT
   * info, set the 2 bit. If the public exponent is available, set the
   * 4 bit.
   */
  infoFlag = 0;

  do
  {
    if (flag == VOLT_KEY_GET_TYPE_FLAG)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = VoltKeyGetRsaPrivate (object, usageFlag, (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 VtRSAPriKeyInfo.
     */
    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- or 2048-bit bit modulus.
     */
    if (keyInfo->modulus.data != (unsigned char *)0)
    {
      if ( (keyInfo->modulus.len == 128) || (keyInfo->modulus.len == 256) )
      {
        if ( (keyInfo->priExpo.data != (unsigned char *)0) &&
             (keyInfo->priExpo.len != 0) &&
             (keyInfo->priExpo.len <= keyInfo->modulus.len) )
          infoFlag |= 1;
      }
    }

    if ( (keyInfo->prime1.data != (unsigned char *)0) &&
         (keyInfo->prime2.data != (unsigned char *)0) &&
         (keyInfo->exponent1.data != (unsigned char *)0) &&
         (keyInfo->exponent2.data != (unsigned char *)0) &&
         (keyInfo->coefficient.data != (unsigned char *)0) &&
         (keyInfo->prime1.len != 0) &&
         (keyInfo->prime2.len != 0) &&
         (keyInfo->exponent1.len != 0) &&
         (keyInfo->exponent2.len != 0) &&
         (keyInfo->coefficient.len != 0) )
      infoFlag |= 2;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (infoFlag == 0)
      break;

    if (keyInfo->pubExpo.data != (unsigned char *)0)
      infoFlag |= 4;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectRSAPrivate (obj, keyInfo, infoFlag, usageFlag);
    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, "VoltKeyParamRSAPrivate", fnctLine, (char *)0)

  return (status);
}

static int VoltKeyGetRsaPublic (
   VtKeyObject object,
   unsigned int usageFlag,
   Pointer *getInfo
   )
{
  int status;
  unsigned int bufferSize, keyType;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  VoltRsaPublicKey *pubKey;
  VoltRsaKeyPair *theKeyPair;
  VtRSAPriKeyInfo *getPriInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    keyType = VOLT_KEY_TYPE_ENCRYPT;
    if (usageFlag = VT_RSA_KEY_USAGE_SIGN_VERIFY)
      keyType = VOLT_KEY_TYPE_SIGN;

    /* 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 RSA?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if ((obj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) != VOLT_KEY_ALG_RSA)
      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 = (VoltRsaKeyPair *)(obj->keyData);
      if (theKeyPair->pubKey == (VtKeyObject)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      if (usageFlag == VT_RSA_KEY_USAGE_SIGN_VERIFY)
      {
        status = VtGetKeyParam (
          theKeyPair->pubKey, VtKeyParamRSAPublicVerify, getInfo);
      }
      else
      {
        status = VtGetKeyParam (
          theKeyPair->pubKey, VtKeyParamRSAPublicEncrypt, getInfo);
      }
      break;
    }

    /* Same usage?
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if ((obj->keyType & keyType) != keyType)
      break;

    /* It's not a key pair.
     * We might be able to 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)
      if (usageFlag == VT_RSA_KEY_USAGE_SIGN_VERIFY)
      {
        status = VtGetKeyParam (
          (VtKeyObject)obj, VtKeyParamRSAPrivateSign,
          (Pointer *)&getPriInfo);
      }
      else
      {
        status = VtGetKeyParam (
          (VtKeyObject)obj, VtKeyParamRSAPrivateDecrypt,
          (Pointer *)&getPriInfo);
      }
      if (status != 0)
        break;

      if ( (getPriInfo->modulus.data == (unsigned char *)0) &&
           (getPriInfo->pubExpo.data == (unsigned char *)0) )
        status = VT_ERROR_GET_INFO_UNAVAILABLE;

      *getInfo = (Pointer)getPriInfo;
      break;
    }

    /* It's not a pair, it's not private, if it's not public, error.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_GET_INFO_UNAVAILABLE;
    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 = (VoltRsaPublicKey *)(obj->keyData);
    *getInfo = (Pointer)(pubKey->keyItems);
    if (pubKey->keyItems != (VtRSAPubKeyInfo *)0)
      break;

⌨️ 快捷键说明

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