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

📄 cbcimpl.c

📁 IBE是一种非对称密码技术
💻 C
字号:
/* Copyright 2003-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 "block.h"
#include "cbc.h"
#include "errorctx.h"

int CBCEncryptInit (
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
   )
{
  int status;
  VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBlockCipherCtx *blockCtx =
    (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
  VoltCbcCtx *cbcCtx = (VoltCbcCtx *)(blockCtx->feedbackCtx);
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Copy the initVector.
     */
    Z2Memcpy (
      cbcCtx->feedCtx.xorVector, cbcCtx->feedCtx.initVector,
      cbcCtx->feedCtx.blockSize);

    /* Call the underlying algorithm's Init.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = cbcCtx->AlgEncryptInit (algObj, keyObj);

  } while (0);

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

  return (status);
}

int CBCEncryptUpdate (
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToEncrypt,
   unsigned int dataToEncryptLen,
   unsigned char *encryptedData
   )
{
  unsigned int index;
  VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBlockCipherCtx *blockCtx =
    (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
  VoltCbcCtx *cbcCtx = (VoltCbcCtx *)(blockCtx->feedbackCtx);

  /* So long as we have blocks, operate on them.
   */
  while (dataToEncryptLen >= cbcCtx->feedCtx.blockSize)
  {
    dataToEncryptLen -= cbcCtx->feedCtx.blockSize;

    /* XOR each byte of input with the XOR vector.
     */
    for (index = 0; index < cbcCtx->feedCtx.blockSize; ++index)
    {
      cbcCtx->feedCtx.xorVector[index] ^= *dataToEncrypt;
      dataToEncrypt++;
    }

    /* Encrypt the data. This will also be the next XOR vector.
     */
    cbcCtx->AlgEncryptUpdate (
      algObj, random, cbcCtx->feedCtx.xorVector, cbcCtx->feedCtx.blockSize,
      cbcCtx->feedCtx.xorVector);
    Z2Memcpy (
      encryptedData, cbcCtx->feedCtx.xorVector, cbcCtx->feedCtx.blockSize);
    encryptedData += cbcCtx->feedCtx.blockSize;
  }

  return (0);
}

int CBCDecryptInit (
   VoltAlgorithmObject *algObj,
   VoltKeyObject *keyObj
   )
{
  int status;
  VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBlockCipherCtx *blockCtx =
    (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
  VoltCbcCtx *cbcCtx = (VoltCbcCtx *)(blockCtx->feedbackCtx);
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Copy the initVector.
     */
    Z2Memcpy (
      cbcCtx->feedCtx.xorVector, cbcCtx->feedCtx.initVector,
      cbcCtx->feedCtx.blockSize);

    /* Call the underlying algorithm's Init.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = cbcCtx->AlgDecryptInit (algObj, keyObj);

  } while (0);

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

  return (status);
}

int CBCDecryptUpdate (
   VoltAlgorithmObject *algObj,
   VtRandomObject random,
   unsigned char *dataToDecrypt,
   unsigned int dataToDecryptLen,
   unsigned char *decryptedData
   )
{
  unsigned int index;
  VoltLibCtx *libCtx = (VoltLibCtx *)(algObj->voltObject.libraryCtx);
  VoltCipherClassCtx *cipherCtx = (VoltCipherClassCtx *)(algObj->classCtx);
  VoltBlockCipherCtx *blockCtx =
    (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);
  VoltCbcCtx *cbcCtx = (VoltCbcCtx *)(blockCtx->feedbackCtx);

  /* So long as we have blocks, operate on them.
   */
  while (dataToDecryptLen >= cbcCtx->feedCtx.blockSize)
  {
    dataToDecryptLen -= cbcCtx->feedCtx.blockSize;

    /* Decrypt the block.
     */
    cbcCtx->AlgDecryptUpdate (
      algObj, random, dataToDecrypt, cbcCtx->feedCtx.blockSize, decryptedData);

    /* XOR each byte of decrypted data with the XOR vector.
     */
    for (index = 0; index < cbcCtx->feedCtx.blockSize; ++index)
    {
      *decryptedData ^= cbcCtx->feedCtx.xorVector[index];
      decryptedData++;
    }

    /* The ciphertext will be the next XOR vector.
     */
    Z2Memcpy (
      cbcCtx->feedCtx.xorVector, dataToDecrypt, cbcCtx->feedCtx.blockSize);
    dataToDecrypt += cbcCtx->feedCtx.blockSize;
  }

  return (0);
}

⌨️ 快捷键说明

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