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

📄 sha224type.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 "digest.h"
#include "sha224.h"
#include "fipsmodule.h"
#include "errorctx.h"

/* Build the digestCtx that has a SHA224Ctx as the localCtx.
 * <p>This function will allocate space for the DigestClassCtx and the
 * 224Ctx. The caller must free that space (it will be one buffer).
 *
 * @param obj The object that will hold the context.
 * @param digestCtx The address where the function will deposit the
 * created ctx.
 * @return an int, 0 if the function completed successfully or a
 * non-zero error code.
 */
static int VOLT_CALLING_CONV BuildSHA224Ctx VOLT_PROTO_LIST ((
   VoltAlgorithmObject *obj,
   VoltDigestClassCtx **digestCtx
));

/* Implements VCtxDestroy.
 */
void VOLT_CALLING_CONV SHA224CtxDestroy VOLT_PROTO_LIST ((
   Pointer obj,
   Pointer ctx
));

int VtAlgorithmImplSHA224 (
   VtAlgorithmObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
  VoltSHA224Ctx *ctx;
  VoltDigestClassCtx *digestCtx = (VoltDigestClassCtx *)0;
  VoltDigestClassCtx *sha256DigestCtx;
  VtItem newState;
  unsigned char initState[32] = { SHA224_INIT_DATA };
  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;

    /* The associated info should be a NULL pointer.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_ASSOCIATED_INFO;
    if (info != (Pointer)0)
      break;

    /* Build the DigestCtx and SHA-224 ctx.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    status = BuildSHA224Ctx (obj, &digestCtx);
    if (status != 0)
      break;

    ctx = (VoltSHA224Ctx *)(digestCtx->localDigestCtx);

    /* Build the subordinate SHA-256 object.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VoltCreateObject (
      obj->voltObject.libraryCtx, (VoltObject **)&(ctx->sha256Obj),
      sizeof (VoltAlgorithmObject), VOLT_OBJECT_TYPE_ALGORITHM);
    if (status != 0)
      break;

    ctx->sha256Obj->state = VOLT_STATE_CREATE_ALG_OBJ;

    /* Set this object with the support Impl.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VtAlgorithmImplSHA256 (
      (VtAlgorithmObject *)&(ctx->sha256Obj), (Pointer)0,
      VOLT_ALG_SET_TYPE_FLAG);
    if (status != 0)
      break;

    /* Reset the initial state.
     */
    VOLT_SET_ERROR_TYPE (errorType, 0)
    VOLT_SET_FNCT_LINE (fnctLine)
    newState.data = initState;
    newState.len = 32;
    status = VtSetAlgorithmParam (
      (VtAlgorithmObject)(ctx->sha256Obj), VtAlgorithmParamDigestInitialState,
      (Pointer)&newState);
    if (status != 0)
      break;

    /* Set the fields of the contexts.
     */
    sha256DigestCtx = (VoltDigestClassCtx *)(ctx->sha256Obj->classCtx);
    ctx->sha256Impl = sha256DigestCtx->algorithmImpl;
    ctx->sha256ImplInfo = sha256DigestCtx->algorithmImplInfo;
    digestCtx->algorithmImpl = VtAlgorithmImplSHA224;
    digestCtx->algorithmImplInfo = (Pointer)0;
    digestCtx->DigestInit = SHA224Init;
    digestCtx->DigestUpdate = SHA224Update;
    digestCtx->DigestFinal = SHA224Final;
    digestCtx->algorithm = VT_DIGEST_ALG_SHA224;
    digestCtx->digestSize = 28;
    digestCtx->blockLen = 64;
    /* Don't load a special localCtxDestroy, the DigestCtxDestroy will
     * take care of it.
     */

    obj->algClass = VOLT_CLASS_DIGEST;
    obj->classCtx = (Pointer)digestCtx;
    obj->ClassCtxDestroy = SHA224CtxDestroy;

  } while (0);

  /* If no error, we're done.
   */
  if (status == 0)
    return (0);

  /* If there was an error, destroy anything we created.
   */
  SHA224CtxDestroy ((Pointer)obj, (Pointer)digestCtx);

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, obj, status, 0, errorType,
    (char *)0, "VtAlgorithmImplSHA224", fnctLine, (char *)0)

  return (status);
}

static int BuildSHA224Ctx (
   VoltAlgorithmObject *obj,
   VoltDigestClassCtx **digestCtx
   )
{
  int status;
#if VOLT_ALIGNMENT != 1
  unsigned int pad;
#endif
  unsigned int bufferSize, offset;
  unsigned char *buffer = (unsigned char *)0;
  VoltLibCtx *libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  VoltDigestClassCtx *newDigestCtx;
  VoltSHA224Ctx *ctx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* We want to allocate space for a digestCtx and a SHA224Ctx.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize = sizeof (VoltDigestClassCtx) + sizeof (VoltSHA224Ctx);
#if VOLT_ALIGNMENT != 1
    /* If the alignment is 1, there's no need to pad. If not, compute
     * the pad length.
     */
    VOLT_COMPUTE_ALIGN_PAD (VOLT_ALIGNMENT, sizeof (VoltDigestClassCtx), pad)
      bufferSize += pad;
#endif
    buffer = (unsigned char *)Z2Malloc (bufferSize, VOLT_MEMORY_SENSITIVE);
    if (buffer == (unsigned char *)0)
      break;
    Z2Memset (buffer, 0, bufferSize);

    /* Locate the contexts.
     */
    newDigestCtx = (VoltDigestClassCtx *)buffer;
    offset = sizeof (VoltDigestClassCtx);
#if VOLT_ALIGNMENT != 1
    offset += pad;
#endif
    ctx = (VoltSHA224Ctx *)(buffer + offset);

    /* Populate the contexts.
     */
    newDigestCtx->localDigestCtx = (Pointer)ctx;

    *digestCtx = newDigestCtx;

    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->state = VOLT_STATE_ERROR;

  VOLT_LOG_ERROR_INFO_COMPARE (
    status, 0, obj, status, 0, VT_ERROR_TYPE_PRIMARY,
    (char *)0, "BuildSHA224Ctx", fnctLine, (char *)0)

  return (status);
}

void SHA224CtxDestroy (
   Pointer object,
   Pointer ctx
   )
{
  VoltAlgorithmObject *obj;
  VoltLibCtx *libCtx;
  VoltDigestClassCtx *digestCtx;
  VoltSHA224Ctx *sha224Ctx;

  if ( (object == (Pointer)0) || (ctx == (Pointer)0) )
    return;

  obj = (VoltAlgorithmObject *)object;
  libCtx = (VoltLibCtx *)(obj->voltObject.libraryCtx);
  digestCtx = (VoltDigestClassCtx *)ctx;
  sha224Ctx = (VoltSHA224Ctx *)(digestCtx->localDigestCtx);

  if (sha224Ctx->sha256Obj != (VoltAlgorithmObject *)0)
  {
    obj = sha224Ctx->sha256Obj;
    if (obj->ClassCtxDestroy != (VCtxDestroy)0)
      obj->ClassCtxDestroy ((Pointer)obj, obj->classCtx);
    if (obj->mpCtx != (VoltMpIntCtx *)0)
      VtDestroyMpIntCtx ((VtMpIntCtx *)&(obj->mpCtx));
    VoltDestroyObject ((VoltObject **)&(sha224Ctx->sha256Obj));
  }

  Z2Free (ctx);
}

⌨️ 快捷键说明

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