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

📄 aestype.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 "aes.h"
#include "errorctx.h"

/* Implements VoltBlockAlgSetter.
 */
int VOLT_CALLING_CONV VoltBlockAlgSetAES VOLT_PROTO_LIST ((
   VoltAlgorithmObject *obj,
   Pointer info,
   unsigned int flag
));

/* This routine does the work. It allocates and fills in the contexts.
 * @param obj The algorithm object to set.
 * @param cipherCtx The Cipher Class context to set.
 * @param blockCtx The BlockCipher context to set.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV SetObjectAES VOLT_PROTO_LIST ((
   VoltAlgorithmObject *obj,
   VoltCipherClassCtx *cipherCtx,
   VoltBlockCipherCtx *blockCtx
));

int VtAlgorithmImplAES (
   VtAlgorithmObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
  VtBlockCipherInfo *blockInfo;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  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;

    /* Check the args.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info == (Pointer)0)
      break;

    blockInfo = (VtBlockCipherInfo *)info;

    /* Check the class of the object. It should be 0 (not yet set)
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_SET;
    if (obj->algClass != 0)
      break;

    /* Call the general purpose block cipher Setter.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltAlgorithmSetBlockCipher (obj, VoltBlockAlgSetAES, blockInfo);
    if (status != 0)
      break;

    /* If successful, set the FIPS bit in the object type, this object
     * is a FIPS object.
     */
    obj->voltObject.objectType |= VOLT_OBJECT_TYPE_FIPS;

  } while (0);

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

  return (status);
}

int VoltBlockAlgSetAES (
   VoltAlgorithmObject *obj,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltCipherClassCtx *cipherCtx;
  VoltBlockCipherCtx *blockCtx;
  VOLT_DECLARE_ERROR_TYPE (errorType)
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the class of the object. It should be
     * VOLT_CLASS_BLOCK_CIPHER.
     */
    VOLT_SET_ERROR_TYPE (errorType, VT_ERROR_TYPE_PRIMARY)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (obj->algClass != VOLT_CLASS_BLOCK_CIPHER)
      break;

    /* We have a block cipher object, which means we have a CipherCtx
     * and a BlockCtx.
     */
    cipherCtx = (VoltCipherClassCtx *)(obj->classCtx);
    blockCtx = (VoltBlockCipherCtx *)(cipherCtx->localCipherCtx);

    /* Make sure the setState in the CipherCtx is correct.
     * status is currently VT_ERROR_INVALID_TYPE;
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (cipherCtx->setState != VOLT_BLOCK_SET_STATE_BLOCK)
      break;

    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = SetObjectAES (obj, cipherCtx, blockCtx);

  } while (0);

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

  return (status);
}

static int SetObjectAES (
   VoltAlgorithmObject *obj,
   VoltCipherClassCtx *cipherCtx,
   VoltBlockCipherCtx *blockCtx
   )
{
  int status;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltAESCtx *aesCtx = (VoltAESCtx *)0;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Allocate enough space for an AES Ctx.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    aesCtx = (VoltAESCtx *)Z2Malloc (
      sizeof (VoltAESCtx), VOLT_MEMORY_SENSITIVE);
    if (aesCtx == (VoltAESCtx *)0)
      break;
    Z2Memset (aesCtx, 0, sizeof (VoltAESCtx));

    /* Populate the contexts.
     */
    blockCtx->algCtx = (Pointer)aesCtx;
    blockCtx->AlgCtxDestroy = VoltSimpleCtxDestroy;

    cipherCtx->SymKeyParam = VtKeyParamAES;
    cipherCtx->EncryptInit = AESEncryptInit;
    cipherCtx->EncryptUpdate = AESEncryptUpdate;
    cipherCtx->DecryptInit = AESDecryptInit;
    cipherCtx->DecryptUpdate = AESDecryptUpdate;
    cipherCtx->plainBlockSize = 16;
    cipherCtx->cipherBlockSize = 16;
    cipherCtx->setState = VOLT_BLOCK_SET_STATE_ALG;

    obj->subAlg1 = VOLT_SUB_ALG_AES;

    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 (aesCtx != (VoltAESCtx *)0)
    Z2Free (aesCtx);

  obj->state = VOLT_STATE_ERROR;

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

  return (status);
}

⌨️ 快捷键说明

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