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

📄 md5type.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 "md5.h"
#include "errorctx.h"

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

int VtAlgorithmImplMD5 (
   VtAlgorithmObject *object,
   Pointer info,
   unsigned int flag
   )
{
  int status;
  unsigned int algClass;
  VoltAlgorithmObject *obj = (VoltAlgorithmObject *)(*object);
  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;    

    /* Check the class of the object. It should be 0 (not yet set) or
     * a class that uses a digest as a subsidiary algorithm, such as
     * signature or HMAC.
     */
    algClass = (unsigned int)(obj->algClass);
    switch (algClass)
    {
      case 0:
        /* If the class is not set yet, this is being called to set
         * this as a simple digest.
         */
        /* Set the object, and if that is successful, set the class.
         */
        VOLT_SET_ERROR_TYPE (errorType, 0)
        VOLT_SET_FNCT_LINE (fnctLine)
        status = SetObjectMD5 (obj);
        if (status == 0)
          obj->algClass = VOLT_CLASS_DIGEST;
        break;

      case VOLT_CLASS_MAC:
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_UNIMPLEMENTED;
        break;

      default:
        VOLT_SET_FNCT_LINE (fnctLine)
        status = VT_ERROR_INVALID_ALG_OBJ;
    }
    if (status != 0)
      break;

  } while (0);

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

  return (status);
}

static int SetObjectMD5 (
   VoltAlgorithmObject *obj
   )
{
  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 *digestCtx;
  VoltMD5Ctx *md5Ctx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Allocate enough space for a DigestCtx and an MD5Ctx.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    bufferSize = sizeof (VoltDigestClassCtx) + sizeof (VoltMD5Ctx);
#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.
     */
    digestCtx = (VoltDigestClassCtx *)buffer;
    offset = sizeof (VoltDigestClassCtx);
#if VOLT_ALIGNMENT != 1
    offset += pad;
#endif
    md5Ctx = (VoltMD5Ctx *)(buffer + offset);

    /* Populate the contexts.
     */
    md5Ctx->MD5Transform = MD5Transform;

    digestCtx->algorithmImpl = VtAlgorithmImplMD5;
    digestCtx->algorithmImplInfo = (Pointer)0;
    digestCtx->DigestInit = MD5Init;
    digestCtx->DigestUpdate = MD5Update;
    digestCtx->DigestFinal = MD5Final;
    digestCtx->digestSize = 16;
    digestCtx->blockLen = 64;
    digestCtx->algorithm = VT_DIGEST_ALG_MD5;

    digestCtx->localDigestCtx = (Pointer)md5Ctx;
    /* No LocalDigestCtxDestroy, the ClassCtxDestroy we will load will
     * take care of it.
     */

    obj->classCtx = (Pointer)digestCtx;
    obj->ClassCtxDestroy = VoltSimpleCtxDestroy;

    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 (
    obj->voltObject.libraryCtx, status, VT_ERROR_TYPE_PRIMARY, fnctLine,
    "SetObjectMD5", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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