rsakgtype.c

来自「IBE是一种非对称密码技术」· C语言 代码 · 共 207 行

C
207
字号
/* 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 "random.h"
#include "errorctx.h"

/* This routine does the work. It allocates and fills in the contexts.
 *
 * @param obj The algorithm object to set.
 * @param keyGenInfo Contains the modLen and pubExpo.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV SetObjectRSAKeyGen VOLT_PROTO_LIST ((
   VoltKeyObject *obj,
   VoltRSAKeyPairGenInfo *keyGenInfo
));

int VtKeyPairGenRSA (
   VtKeyObject object,
   Pointer info,
   unsigned int flag,
   VtRandomObject random
   )
{
  int status;
  VoltKeyObject *obj = (VoltKeyObject *)object;
  VtRSAKeyPairGenInfo *genInfo;
  VoltRSAKeyPairGenInfo keyGenInfo;
  unsigned char pubExpo[3] = { 0x01, 0x00, 0x01 };
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* 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 of 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 an unsigned int, the
     * modulus length, in bytes. The modulus must be 1024 or 2048 bits.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    genInfo = (VtRSAKeyPairGenInfo *)info;

    keyGenInfo.modLenBits = genInfo->modulusBits;
    keyGenInfo.usageFlag = genInfo->usageFlag;
    keyGenInfo.pubExpo.data = pubExpo;
    keyGenInfo.pubExpo.len = 3;

    VOLT_SET_FNCT_LINE (fnctLine)
    if ( (keyGenInfo.modLenBits != 1024) &&
         (keyGenInfo.modLenBits != 2048) )
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    if ( (keyGenInfo.usageFlag != VT_RSA_KEY_USAGE_SIGN_VERIFY) &&
         (keyGenInfo.usageFlag != VT_RSA_KEY_USAGE_ENCRYPT_DECRYPT) )
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectRSAKeyGen (obj, &keyGenInfo);

  } while (0);

  /* If everything worked, return 0.
   */
  if (status == 0)
  {
    /* Set the FIPS bit in the object type, this object is a FIPS
     * object.
     */
    obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;
    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, "VtKeyPairGenRSA", fnctLine, (char *)0)

  return (status);
}

static int SetObjectRSAKeyGen (
   VoltKeyObject *obj,
   VoltRSAKeyPairGenInfo *keyGenInfo
   )
{
  int status;
  unsigned int bufferSize, modLen, prime1Len, prime2Len;
  unsigned char *buffer = (unsigned char *)0;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltRsaKeyGenCtx *rsaGenCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* This function assumes the modLenBits is a multiple of 16 (such
     * as 1024 or 2048).
     */
    prime1Len = keyGenInfo->modLenBits / 16;
    prime2Len = prime1Len;
    modLen = keyGenInfo->modLenBits / 8;

    /* Allocate enough space for an RsaKeyGenCtx. The buffers after the
     * struct will be byte arrays, so no need to worry about alignment.
     * These buffers will contain the primes, prime exponents, CRT
     * coefficient, pubExpo, priExpo, and modulus.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      sizeof (VoltRsaKeyGenCtx) + (3 * prime1Len) + (2 * prime2Len) +
      (2 * modLen) + keyGenInfo->pubExpo.len;
    buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the context.
     */
    rsaGenCtx = (VoltRsaKeyGenCtx *)buffer;

    /* Populate the context.
     */
    rsaGenCtx->mpCtx = (VtMpIntCtx)(obj->mpCtx);
    rsaGenCtx->modBits = keyGenInfo->modLenBits;
    rsaGenCtx->usageFlag = keyGenInfo->usageFlag;
    rsaGenCtx->prime1Bits = rsaGenCtx->modBits / 2;
    rsaGenCtx->prime2Bits = rsaGenCtx->prime1Bits;
    rsaGenCtx->prime1.data = buffer + sizeof (VoltRsaKeyGenCtx);
    rsaGenCtx->prime2.data = rsaGenCtx->prime1.data + prime1Len;
    rsaGenCtx->expo1.data = rsaGenCtx->prime2.data + prime2Len;
    rsaGenCtx->expo2.data = rsaGenCtx->expo1.data + prime1Len;
    rsaGenCtx->coeff.data = rsaGenCtx->expo2.data + prime2Len;
    rsaGenCtx->modulus.data = rsaGenCtx->coeff.data + prime1Len;
    rsaGenCtx->priExpo.data = rsaGenCtx->modulus.data + modLen;
    rsaGenCtx->pubExpo.data = rsaGenCtx->priExpo.data + modLen;

    rsaGenCtx->prime1.len = prime1Len;
    rsaGenCtx->prime2.len = prime2Len;
    rsaGenCtx->expo1.len = prime1Len;
    rsaGenCtx->expo2.len = prime2Len;
    rsaGenCtx->coeff.len = prime1Len;
    rsaGenCtx->modulus.len = modLen;
    rsaGenCtx->priExpo.len = modLen;

    Z2Memcpy (
      rsaGenCtx->pubExpo.data, keyGenInfo->pubExpo.data,
      keyGenInfo->pubExpo.len);
    rsaGenCtx->pubExpo.len = keyGenInfo->pubExpo.len;

    obj->keyType = VOLT_KEY_ALG_RSA | VOLT_KEY_TYPE_GEN_PAIR;
    obj->GenerateKey = RSAGenerateKeyPair;
    obj->localGenerateCtx = (Pointer)rsaGenCtx;
    obj->LocalGenerateCtxDestroy = VoltSimpleCtxDestroy;

    status = 0;

  } while (0);

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

  /* If something went wrong, destroy anything we created and indicate
   * that this object is not usable.
   */
  if (buffer != (unsigned char *)0)
    Z2Free (buffer);

  obj->keyType = 0;

  VOLT_LOG_ERROR_INFO (
    0, obj, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "SetObjectRSAKeyGen", fnctLine, (char *)0)

  return (status);
}

⌨️ 快捷键说明

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