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

📄 ibeencimpl.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2003-2006, Voltage Security, all rights reserved.
 */

#include "vibecrypto.h"
#include "environment.h"
#include "base.h"
#include "libctx.h"
#include "paramobj.h"
#include "algobj.h"
#include "keyobj.h"
#include "cipher.h"
#include "ibe.h"
#include "surrender.h"
#include "errorctx.h"

int BFType1IBEGetOutputSize (
   VoltAlgorithmObject *obj,
   unsigned int callFlag,
   unsigned char *input,
   unsigned int inputLen,
   unsigned int *outputSize,
   unsigned int *leftovers,
   VtRandomObject random
   )
{
  int status;
  unsigned int theSize;
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(obj->classCtx);
  VoltBFType1IBECtx *ibeCtx = (VoltBFType1IBECtx *)(cipherCtx->localCipherCtx);
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* Initialize result to 0. If this call works, we'll change it
   * (leftovers is always 0).
   */
  *outputSize = 0;
  *leftovers = 0;

  do
  {
    if (callFlag == VOLT_CALLER_ENCRYPT_FINAL)
    {
      VOLT_SET_FNCT_LINE (fnctLine)
      theSize = bfGetRawEncryptedSize (inputLen, ibeCtx->bfCtx);
      status = VT_ERROR_INVALID_INPUT;
      if (theSize == 0)
        break;

      /* We can now set the plain and cipher block sizes.
       */
      *outputSize = (unsigned int)theSize;
      cipherCtx->plainBlockSize = inputLen;
      cipherCtx->cipherBlockSize = (unsigned int)theSize;

      status = 0;
      break;
    }

    /* Only EncryptFinal or DecryptFinal can make this call.
     * If we reach this point, we know the caller wasn't EncryptFinal. If
     * not DecryptFinal, error.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_CALL_ORDER;
    if (callFlag != VOLT_CALLER_DECRYPT_FINAL)
      break;

    VOLT_SET_FNCT_LINE (fnctLine)
    theSize = bfGetRawDecryptedSize (inputLen, ibeCtx->bfCtx);
    status = VT_ERROR_INVALID_INPUT;
    if (theSize == 0)
      break;

    /* We can now set the plain and cipher block sizes.
     */
    *outputSize = (unsigned int)theSize;
    cipherCtx->plainBlockSize = (unsigned int)theSize;
    cipherCtx->cipherBlockSize = inputLen;

    status = 0;

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, obj->voltObject.libraryCtx, status, VT_ERROR_TYPE_PRIMARY,
    fnctLine, "BFType1IBEGetOutputSize", (char *)0)

  return (status);
}

int BFType1IBEPubEncryptInit (
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
   )
{
  int status;
  VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBFType1IBECtx *encCtx = (VoltBFType1IBECtx *)(cipherCtx->localCipherCtx);
  VoltBFType1IBEPubKeyData *keyData =
    (VoltBFType1IBEPubKeyData *)(keyObj->keyData);
  VtBFType1IBEParamInfo *paramData = (VtBFType1IBEParamInfo *)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Make sure the key matches the algorithm object.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) !=
        VOLT_KEY_ALG_IBE_TYPE_1)
      break;
    if ( ((keyObj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0) &&
         ((keyObj->keyType & VOLT_KEY_TYPE_PUBLIC) == 0) )
      break;

    /* We need an mpCtx from the key.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (keyObj->mpCtx == (VoltMpIntCtx *)0)
      break;

    /* Clone the mpCtx.
     */
    if (algObj->mpCtx != (VoltMpIntCtx *)0)
      VtDestroyMpIntCtx ((VtMpIntCtx *)&(algObj->mpCtx));

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCloneObject (
      (Pointer)(keyObj->mpCtx), (Pointer *)&(algObj->mpCtx));
    if (status != 0)
      break;

    /* We need data (status is still set to VT_ERROR_INVALID_KEY_OBJ if we
     * can't get the data out).
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
    {
      if (keyObj->GetKeyData == (VGetKeyData)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData);
      if (status != 0)
        break;
    }

    /* Get the IBE params out of the parameter object in the key data.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtGetParameterParam
      (keyData->ibeParams, VtParameterParamBFType1IBEParams,
      (Pointer *)&paramData);
    if (status != 0)
      break;

    /* Get the bfCtx in the encCtx.
     */
    VoltReleaseBfCtx (libCtx, &(encCtx->bfCtx));

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBfCtxFromIBEParams (
      libCtx, (VtMpIntCtx)(keyObj->mpCtx), &(paramData->curve),
      &(paramData->pubPointP), 0, (unsigned char *)0, &(encCtx->bfCtx));
    if (status != 0)
      break;

    /* Copy the encoded identity into this object.
     */
    if (encCtx->encodedId.data != (unsigned char *)0)
      Z2Free (encCtx->encodedId.data);

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    encCtx->encodedId.data = (unsigned char *)Z2Malloc (
      keyData->encodedId.len, 0);
    if (encCtx->encodedId.data == (unsigned char *)0)
      break;
    Z2Memcpy (
      encCtx->encodedId.data, keyData->encodedId.data,
      keyData->encodedId.len);
    encCtx->encodedId.len = keyData->encodedId.len;

    status = 0;
  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, errorType, fnctLine,
    "BFType1IBEPubEncryptInit", (char *)0)

  return (status);
}

int BFType1IBEPubEncryptUpdate (
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToEncrypt,
   unsigned int dataToEncryptLen,
   unsigned char *encryptedData
   )
{
  int status, count;
  unsigned int callNum, outputLen;
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBFType1IBECtx *ibeCtx = (VoltBFType1IBECtx *)(cipherCtx->localCipherCtx);
  VoltSurrenderCtx *surrCtx = (VoltSurrenderCtx *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If we reach this point in the code, the buffer is big enough.
   * Call bfRawEncrypt. If the return is not 0, try again, we may have
   * gotten unlucky with a random generation. But only try so many
   * times.
   */
  count = 0;
  do
  {
    /* If there's a surrender ctx, call the Surrender function.
     */
    if (count == 0)
    {
      VOLT_GET_OBJECT_SURR_CTX (surrCtx, algObj);
      VOLT_CALL_SURRENDER (
        surrCtx, VT_SURRENDER_FNCT_BF_TYPE1_IBE_ENCRYPT, 0, 1)
    }

    /* If the call to Encrypt is successful, we're done, break out.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    callNum = 2;
    status = (unsigned int)bfRawEncrypt (
      encryptedData, cipherCtx->cipherBlockSize, &outputLen,
      ibeCtx->encodedId.data, ibeCtx->encodedId.len,
      dataToEncrypt, dataToEncryptLen, surrCtx,
      VT_SURRENDER_FNCT_BF_TYPE1_IBE_ENCRYPT, &callNum, random, ibeCtx->bfCtx);
    if (status == 0)
    {
      /* If there's a surrender ctx, call it for the last time.
       */
      VOLT_CALL_SURRENDER (
        surrCtx, VT_SURRENDER_FNCT_BF_TYPE1_IBE_ENCRYPT, 0, 0)
      break;
    }

    if ( (count >= 10) || (status == VT_ERROR_RANDOM_OBJECT) ||
         (status == VT_ERROR_BUFFER_TOO_SMALL) )
      break;
    count++;

  } while (1);

  VOLT_LOG_ERROR_COMPARE (
    status, algObj->voltObject.libraryCtx, status, 0, fnctLine,
    "BFType1IBEPubEncryptUpdate", (char *)0)

  return (status);
}

int BFType1IBEPriDecryptInit (
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
   )
{
  int status;
  unsigned int index, bufferSize;
  unsigned char *buffer;
  VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBFType1IBECtx *decCtx = (VoltBFType1IBECtx *)(cipherCtx->localCipherCtx);
  VoltIBEPriKeyData *keyData = (VoltIBEPriKeyData *)(keyObj->keyData);
  VtBFType1IBEParamInfo *paramData = (VtBFType1IBEParamInfo *)0;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Make sure the key matches the algorithm object.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_KEY_OBJ;
    if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_ASYM_ALG) !=
        VOLT_KEY_ALG_IBE_TYPE_1)
      break;
    if ((keyObj->keyType & VOLT_KEY_TYPE_PRIVATE) == 0)
      break;

    /* We need an mpCtx from the key.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (keyObj->mpCtx == (VoltMpIntCtx *)0)
      break;

    /* Clone the mpCtx.
     */
    if (algObj->mpCtx != (VoltMpIntCtx *)0)
      VtDestroyMpIntCtx ((VtMpIntCtx *)&(algObj->mpCtx));

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtCloneObject (
      (Pointer)(keyObj->mpCtx), (Pointer *)&(algObj->mpCtx));
    if (status != 0)
      break;

    /* We need data (status is still set to VT_ERROR_INVALID_KEY_OBJ if we
     * can't get the data out).
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    if ((keyObj->keyType & VOLT_KEY_TYPE_MASK_DATA) != VOLT_KEY_TYPE_DATA)
    {
      if (keyObj->GetKeyData == (VGetKeyData)0)
        break;

      VOLT_SET_ERROR_TYPE (errorType, 0)
      VOLT_SET_FNCT_LINE (fnctLine)
      status = keyObj->GetKeyData ((VtKeyObject)keyObj, (Pointer *)&keyData);
      if (status != 0)
        break;
    }

    /* Get the IBE params out of the parameter object in the key data.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtGetParameterParam (
      keyData->bfKeyInfo.ibeParams, VtParameterParamBFType1IBEParams,
      (Pointer *)&paramData);
    if (status != 0)
      break;

    /* Build the bfCtx in the decCtx.
     */
    VoltReleaseBfCtx (libCtx, &(decCtx->bfCtx));

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltGetBfCtxFromIBEParams (
      libCtx, (VtMpIntCtx)(keyObj->mpCtx), &(paramData->curve),
      &(paramData->pubPointP), 0, (unsigned char *)0, &(decCtx->bfCtx));
    if (status != 0)
      break;

    /* Build the private value.
     */
    if (decCtx->privateValue.data != (unsigned char *)0)
      Z2Free (decCtx->privateValue.data);

    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize =
      paramData->curve.primeP.len +
      keyData->bfKeyInfo.privatePoint.xCoord.len + 1;
    decCtx->privateValue.data = (unsigned char *)Z2Malloc (
      bufferSize, VOLT_MEMORY_SENSITIVE);
    if (decCtx->privateValue.data == (unsigned char *)0)
      break;

    /* Build the xCoord as a compressed point.
     */
    buffer = decCtx->privateValue.data + paramData->curve.primeP.len;
    index = keyData->bfKeyInfo.privatePoint.yCoord.len - 1;
    buffer[0] = keyData->bfKeyInfo.privatePoint.yCoord.data[index];
    buffer[0] &= 1;
    buffer[0] += 2;
    Z2Memcpy (
      buffer + 1, keyData->bfKeyInfo.privatePoint.xCoord.data,
      keyData->bfKeyInfo.privatePoint.xCoord.len);

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = bfPrivateKeyImportFromPoint (
      decCtx->privateValue.data, paramData->curve.primeP.len,
      &(decCtx->privateValue.len), buffer,
      (int)(keyData->bfKeyInfo.privatePoint.xCoord.len + 1), decCtx->bfCtx);

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, (VtLibCtx)libCtx, status, errorType, fnctLine,
    "BFType1IBEPriDecryptInit", (char *)0)

  return (status);
}

int BFType1IBEPriDecryptUpdate (
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToDecrypt,
   unsigned int dataToDecryptLen,
   unsigned char *decryptedData
   )
{
  int status;
  unsigned int callNum, outputLen;
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBFType1IBECtx *ibeCtx = (VoltBFType1IBECtx *)(cipherCtx->localCipherCtx);
  VoltSurrenderCtx *surrCtx = (VoltSurrenderCtx *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* If there's a surrender ctx, call the Surrender function.
     */
    VOLT_GET_OBJECT_SURR_CTX (surrCtx, algObj);
    VOLT_CALL_SURRENDER (
      surrCtx, VT_SURRENDER_FNCT_BF_TYPE1_IBE_DECRYPT, 0, 1)

    /* If we reach this point in the code, the buffer is big enough.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    callNum = 1;
    status = bfRawDecrypt (
      decryptedData, cipherCtx->plainBlockSize, &outputLen,
      ibeCtx->privateValue.data, ibeCtx->privateValue.len,
      dataToDecrypt, dataToDecryptLen, surrCtx,
      VT_SURRENDER_FNCT_BF_TYPE1_IBE_DECRYPT, &callNum, ibeCtx->bfCtx);

    /* If there's a surrender ctx, call it for the last time.
     */
    if (status == 0)
    {
      VOLT_CALL_SURRENDER (
        surrCtx, VT_SURRENDER_FNCT_BF_TYPE1_IBE_DECRYPT, 0, 0)
    }

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, algObj->voltObject.libraryCtx, status, 0, fnctLine,
    "BFType1IBEPriDecryptUpdate", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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