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

📄 p5pad.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
字号:
/* Copyright 2003-2004, 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 "p5pad.h"
#include "errorctx.h"

int VtPaddingPkcs5 (
   VtAlgorithmObject object,
   VtPaddingInfo *padInfo,
   unsigned int flag
   )
{
  int status;
  VoltAlgorithmObject *obj = (VoltAlgorithmObject *)object;
  VoltCipherClassCtx *cipherCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Check the flag, it should be VOLT_PADDING_SET_TYPE_FLAG.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_TYPE;
    if (flag != VOLT_PADDING_SET_TYPE_FLAG)
      break;

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

    /* Check the class of the object. It should be
     * VOLT_CLASS_BLOCK_CIPHER.
     */
    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.
     */
    cipherCtx = (VoltCipherClassCtx *)(obj->classCtx);

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

    /* P5 Padding is only used if there is a block size > 1.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (cipherCtx->plainBlockSize < 2)
      break;

    /* All we have to do is set the Pad and Unpad fields in the Cipher
     * context. There is no context and no need to have a destructor.
     */
    cipherCtx->Pad = P5Pad;
    cipherCtx->Unpad = P5Unpad;
    cipherCtx->setState = VOLT_BLOCK_SET_STATE_PAD;

    obj->subAlg2 |= VOLT_SUB_ALG_P5_PAD;
    
    status = 0;

  } while (0);

  /* If everything worked, return 0.
   */
  if (status == 0)
    return (0);

  /* If something went wrong, indicate that this object is not usable.
   */
  obj->state = VOLT_STATE_ERROR;

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

  return (status);
}

int P5Pad (
   VoltAlgorithmObject *obj,
   VtRandomObject random,
   Pointer padCtx,
   unsigned char *block,
   unsigned int inputLen,
   unsigned int blockSize
   )
{
  int status;
  unsigned int padLen;
  unsigned char padByte;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* Make sure the inputLen is less than the block size.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_INPUT_LENGTH;
    if (inputLen >= blockSize)
      break;

    /* P5 padding places the number of pad bytes into each of the bytes
     * following the input data.
     */
    padLen = blockSize - inputLen;
    padByte = (unsigned char)padLen;
    /* Move to the end of the input data.
     */
    block += inputLen;
    do
    {
      /* Place a pad byte, then move on to the next byte in the buffer.
       */
      *block = padByte;
      padLen--;
      block++;
    } while (padLen > 0);

    status = 0;
  } while (0);

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

  return (status);
}

int P5Unpad (
   VoltAlgorithmObject *obj,
   Pointer padCtx,
   unsigned char *block,
   unsigned int blockSize,
   unsigned int *outputLen
   )
{
  int status;
  unsigned int padLen;
  unsigned char padByte;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  do
  {
    /* P5 unpadding simply tells how many of the blocks bytes are not
     * pad, checking to make sure all the purported pad bytes are indeed
     * pad bytes.
     */
    padByte = *(block + blockSize - 1);
    padLen = (unsigned int)padByte;

    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_INVALID_PAD;
    if ( (padLen > blockSize) || (padLen == 0) )
      break;

    *outputLen = blockSize - padLen;

    /* Move to the end of the actual data. That is, set block to point to
     * the first byte of pad.
     */
    block += (*outputLen);
    do
    {
      /* Check a pad byte, then move on to the next byte in the buffer.
       * If the current byte is not the padBytes, error. status is set
       * to INVALID_PAD.
       */
      VOLT_SET_FNCT_LINE (fnctLine)
      if (*block != padByte)
        break;
      padLen--;
      block++;
    } while (padLen > 0);

    /* If we went through the entire loop, there was no error. If
     * padLen is not 0, we broke out early because of an error.
     */
    if (padLen == 0)
      status = 0;

  } while (0);

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

  return (status);
}

⌨️ 快捷键说明

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