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

📄 rsaenctype.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2005-2006, Voltage Security, all rights reserved.
 */
#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "algobj.h"
#include "cipher.h"
#include "rsa.h"
#include "mpint.h"
#include "errorctx.h"

int VtAlgorithmImplRSAEncrypt (
   VtAlgorithmObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int bufferSize;
  VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  unsigned char *buffer = (unsigned char *)0;
  VoltCipherClassCtx *cipherCtx;
  VtRSAInfo rsaInfo;
  VoltPaddingInfo paddingInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  rsaInfo.padding = VtPaddingNoPad;
  rsaInfo.paddingInfo = (Pointer)0;

  do
  {
    /* Check the flag, it should be VOLT_ALG_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_ALG_SET_TYPE_FLAG)
      break;

    if (info != (Pointer)0)
      rsaInfo = *((VtRSAInfo *)info);

    /* Allocate space for the cipherCtx.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize = sizeof (VoltCipherClassCtx);
    buffer = (unsigned char *)Z3Malloc (bufferSize);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Fill in the fields.
     */
    cipherCtx = (VoltCipherClassCtx *)buffer;
    cipherCtx->GetOutputSize = RSAEncryptGetOutputSize;
    cipherCtx->EncryptInit = RSAEncryptInit;
    cipherCtx->EncryptUpdate = RSAEncryptUpdate;
    cipherCtx->WrapKey = RSAWrapKey;
    cipherCtx->DecryptInit = RSADecryptInit;
    cipherCtx->DecryptUpdate = RSADecryptUpdate;
    cipherCtx->UnwrapKey = RSAUnwrapKey;
    cipherCtx->setState = VOLT_CIPHER_SET_STATE_RSA;

    obj->algClass = VOLT_CLASS_ASYMMETRIC_CIPHER;
    obj->classCtx = (Pointer)cipherCtx;
    obj->ClassCtxDestroy = RSACipherClassCtxDestroy;

    /* If there's a padding scheme, call it.
     */
    if (rsaInfo.padding != (VtPaddingScheme *)0)
    {
      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      paddingInfo.info = rsaInfo.paddingInfo;
      status = rsaInfo.padding (
        *object, &paddingInfo, VOLT_PADDING_SET_TYPE_FLAG);
      if (status != 0)
        break;
    }
    else
    {
      obj->subAlg2 |= VOLT_SUB_ALG_NO_PAD;
    }

    status = 0;

  } while (0);

  if (status == 0)
    return (0);

  /* If there was an error, destroy what we created but did not return.
   */
  if (cipherCtx != (VoltCipherClassCtx *)0)
  {
    RSACipherClassCtxDestroy ((Pointer)(*object), (Pointer)cipherCtx);
    obj->classCtx = (Pointer)0;
    obj->ClassCtxDestroy = (VCtxDestroy)0;
  }

  obj->state = VOLT_STATE_ERROR;

  VOLT_LOG_ERROR_INFO (
    0, *object, status, 0, errorType,
    (char *)0, "VtAlgorithmImplRSAEncrypt", fnctLine, (char *)0)

  return (status);
}

void RSACipherClassCtxDestroy (
   Pointer object,
   Pointer ctx
   )
{
  VoltAlgorithmObject *obj;
  VoltLibCtx *libCtx;
  VoltCipherClassCtx *cipherCtx;

  /* Anything to destroy?
   */
  if ( (object == (Pointer)0) || (ctx == (Pointer)0) )
    return;

  obj = (VoltAlgorithmObject *)object;
  libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  cipherCtx = (VoltCipherClassCtx *)(obj->classCtx);

  if (cipherCtx->unprocessedData != (unsigned char *)0)
    Z2Free (cipherCtx->unprocessedData);

  /* If there's a localCipherCtx to destroy, destroy it.
   */
  if (cipherCtx->LocalCipherCtxDestroy != (VCtxDestroy)0)
    cipherCtx->LocalCipherCtxDestroy (
      (Pointer)obj, (Pointer)(cipherCtx->localCipherCtx));

  /* If there's a padCtx to destroy, destroy it.
   */
  if (cipherCtx->PadCtxDestroy != (VCtxDestroy)0)
    cipherCtx->PadCtxDestroy ((Pointer)obj, (Pointer)(cipherCtx->padCtx));

  Z2Free (ctx);
}

⌨️ 快捷键说明

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