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

📄 opensslbnwrap.c

📁 voltage 公司提供的一个开发Ibe的工具包
💻 C
📖 第 1 页 / 共 2 页
字号:
   */
  if (theInt == (VoltMpInt *)0)
    return (VT_ERROR_NULL_ARG);

  do
  {
    status = 0;

    /* If the value is 0, clear the bit.
     */
    if (value == 0)
    {
      /* This function returns 1 for success, 0 for failure. However, the
       * failure is if the index is out of range, which the MpInt chooses
       * to ignore (the bit is already clear).
       */
      BN_clear_bit ((BIGNUM *)(theInt->mpInt), (int)bitIndex);
      break;
    }

    /* If value is not 0, set the bit.
     * This function returns 1 for success, 0 for failure.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (BN_set_bit ((BIGNUM *)(theInt->mpInt), (int)bitIndex) != 0)
      break;

    /* If there was an error, it was likely memory.
     */
    status = VT_ERROR_MEMORY;

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, theInt->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapSetBit", (char *)0)

  return (status);
}

int OpenSSLBnWrapGetBit (
   VoltMpInt *theInt,
   unsigned int bitIndex,
   unsigned int *value
   )
{
  int status;
  unsigned int bitLen;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   */
  if (theInt == (VoltMpInt *)0)
    return (VT_ERROR_NULL_ARG);

  do
  {
    /* Check the arguments.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_NULL_ARG;
    if (value == (unsigned int *)0)
      break;
    status = 0;

    /* How big is the integer?
     */
    bitLen = (unsigned int)BN_num_bits ((BIGNUM *)(theInt->mpInt));
    if (bitLen == 0)
      bitLen = 1;

    /* If the index requested is beyond the end, set the value to 0.
     * We could return VT_ERROR_MP_INT_RANGE, but it is true that the
     * bit is 0. (Think of the decimal number 3,142, it is 03,142).
     */
    *value = 0;
    if (bitIndex >= bitLen)
      break;

    *value = (unsigned int)BN_is_bit_set (
      (BIGNUM *)(theInt->mpInt), (int)bitIndex);

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, theInt->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapGetBit", (char *)0)

  return (status);
}

int OpenSSLBnWrapShiftLeftBits (
   VoltMpInt *theInt,
   unsigned int shiftCount
   )
{
  int status;
  BIGNUM *temp = (BIGNUM *)0;
  BIGNUM *theBn, *retVal;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   */
  if (theInt == (VoltMpInt *)0)
    return (VT_ERROR_NULL_ARG);

  theBn = (BIGNUM *)(theInt->mpInt);

  do
  {
    /* The OpenSSL shift function needs two bn's.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    temp = BN_new ();
    if (temp == (BIGNUM *)0)
      break;

    /* Copy the input and then shift the temp into the input arg.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    retVal = BN_copy (temp, theBn);
    if (retVal == (BIGNUM *)0)
      break;

    /* This function returns 1 for success, 0 for failure.
     * If there was an error, it was probably memory.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (BN_lshift (theBn, temp, (int)shiftCount) != 0)
      status = 0;

  } while (0);

  if (temp != (BIGNUM *)0)
    BN_free (temp);

  VOLT_LOG_ERROR_COMPARE (
    status, theInt->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapShiftLeftBits", (char *)0)

  return (status);
}

int OpenSSLBnWrapShiftRightBits (
   VoltMpInt *theInt,
   unsigned int shiftCount
   )
{
  int status;
  BIGNUM *temp = (BIGNUM *)0;
  BIGNUM *theBn, *retVal;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   */
  if (theInt == (VoltMpInt *)0)
    return (VT_ERROR_NULL_ARG);

  theBn = (BIGNUM *)(theInt->mpInt);

  do
  {
    /* The OpenSSL shift function needs two bn's.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    temp = BN_new ();
    if (temp == (BIGNUM *)0)
      break;

    /* Copy the input and then shift the temp into the input arg.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    retVal = BN_copy (temp, theBn);
    if (retVal == (BIGNUM *)0)
      break;

    /* This function returns 1 for success, 0 for failure.
     * If there was an error, it was probably memory.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    if (BN_rshift (theBn, temp, (int)shiftCount) != 0)
      status = 0;

  } while (0);

  if (temp != (BIGNUM *)0)
    BN_free (temp);

  VOLT_LOG_ERROR_COMPARE (
    status, theInt->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapShiftRightBits", (char *)0)

  return (status);
}

int OpenSSLBnWrapSubtract (
   VoltMpInt *minuend,
   VoltMpInt *subtrahend,
   VoltMpInt *difference
   )
{
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (minuend == (VoltMpInt *)0) ||
       (subtrahend == (VoltMpInt *)0) ||
       (difference == (VoltMpInt *)0) )
    return (VT_ERROR_NULL_ARG);

  /* This function returns 1 for success, 0 for failure.
   * If there was an error, it was probably memory.
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  if (BN_sub (
    (BIGNUM *)(difference->mpInt), (BIGNUM *)(minuend->mpInt),
    (BIGNUM *)(subtrahend->mpInt)) != 0)
    return (0);

  VOLT_LOG_ERROR (
    difference->mpCtx->voltObject.libraryCtx, VT_ERROR_MEMORY,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapSubtract", (char *)0)

  return (VT_ERROR_MEMORY);
}

int OpenSSLBnWrapAdd (
   VoltMpInt *addend1,
   VoltMpInt *addend2,
   VoltMpInt *sum
   )
{
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (addend1 == (VoltMpInt *)0) ||
       (addend2 == (VoltMpInt *)0) ||
       (sum == (VoltMpInt *)0) )
    return (VT_ERROR_NULL_ARG);

  /* This function returns 1 for success, 0 for failure.
   * If there was an error, it was probably memory.
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  if (BN_add (
    (BIGNUM *)(sum->mpInt), (BIGNUM *)(addend1->mpInt),
    (BIGNUM *)(addend2->mpInt)) != 0)
    return (0);

  VOLT_LOG_ERROR (
    sum->mpCtx->voltObject.libraryCtx, VT_ERROR_MEMORY,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapAdd", (char *)0)

  return (VT_ERROR_MEMORY);
}

int OpenSSLBnWrapMultiply (
   VoltMpInt *multiplicand,
   VoltMpInt *multiplier,
   VoltMpInt *product
   )
{
  VoltMpIntCtx *mpCtx;
  BN_CTX *bnCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (multiplicand == (VoltMpInt *)0) ||
       (multiplier == (VoltMpInt *)0) ||
       (product == (VoltMpInt *)0) )
    return (VT_ERROR_NULL_ARG);

  /* The localCtx is the bnCtx.
   */
  mpCtx = (VoltMpIntCtx *)(multiplicand->mpCtx);
  bnCtx = (BN_CTX *)(mpCtx->localCtx);

  /* This function returns 1 for success, 0 for failure.
   * If there was an error, it was probably memory.
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  if (BN_mul (
    (BIGNUM *)(product->mpInt), (BIGNUM *)(multiplicand->mpInt),
    (BIGNUM *)(multiplier->mpInt), bnCtx) != 0)
    return (0);

  VOLT_LOG_ERROR (
    product->mpCtx->voltObject.libraryCtx, VT_ERROR_MEMORY,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapMultiply", (char *)0)

  return (VT_ERROR_MEMORY);
}

int OpenSSLBnWrapSquare (
   VoltMpInt *operand,
   VoltMpInt *product
   )
{
  VoltMpIntCtx *mpCtx;
  BN_CTX *bnCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (operand == (VoltMpInt *)0) ||
       (product == (VoltMpInt *)0) )
    return (VT_ERROR_NULL_ARG);

  /* The localCtx is the bnCtx.
   */
  mpCtx = (VoltMpIntCtx *)(operand->mpCtx);
  bnCtx = (BN_CTX *)(mpCtx->localCtx);

  /* This function returns 1 for success, 0 for failure.
   * If there was an error, it was probably memory.
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  if (BN_sqr (
    (BIGNUM *)(product->mpInt), (BIGNUM *)(operand->mpInt), bnCtx) != 0)
    return (0);

  VOLT_LOG_ERROR (
    product->mpCtx->voltObject.libraryCtx, VT_ERROR_MEMORY,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapSquare", (char *)0)

  return (VT_ERROR_MEMORY);
}

int OpenSSLBnWrapDivide (
   VoltMpInt *dividend,
   VoltMpInt *divisor,
   VoltMpInt *quotient,
   VoltMpInt *remainder
   )
{
  int status;
  VoltMpIntCtx *mpCtx;
  BN_CTX *bnCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (dividend == (VoltMpInt *)0) ||
       (divisor == (VoltMpInt *)0) ||
       (quotient == (VoltMpInt *)0) ||
       (remainder == (VoltMpInt *)0) ) 
    return (VT_ERROR_NULL_ARG);

  /* The localCtx is the bnCtx.
   */
  mpCtx = (VoltMpIntCtx *)(dividend->mpCtx);
  bnCtx = (BN_CTX *)(mpCtx->localCtx);

  do
  {
    /* Can't divide by zero. If is_zero returns 1, then the value is zero.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_DIVIDE_BY_ZERO;
    if (BN_is_zero ((BIGNUM *)(divisor->mpInt)) == 1)
      break;

    /* This function returns 1 for success, 0 for failure.
     * If there was an error, it was probably memory (we already checked
     * for divide by 0).
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    if (BN_div (
      (BIGNUM *)(quotient->mpInt), (BIGNUM *)(remainder->mpInt),
      (BIGNUM *)(dividend->mpInt), (BIGNUM *)(divisor->mpInt), bnCtx) != 0)
      status = 0;

  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, quotient->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapDivide", (char *)0)

  return (status);
}

int OpenSSLBnWrapModReduce (
   VoltMpInt *operand,
   VoltMpInt *modulus,
   VoltMpInt *result
   )
{
  int status;
  VoltMpIntCtx *mpCtx;
  BIGNUM *resultBn;
  BN_CTX *bnCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (operand == (VoltMpInt *)0) ||
       (modulus == (VoltMpInt *)0) ||
       (result == (VoltMpInt *)0) )
    return (VT_ERROR_NULL_ARG);

  /* The localCtx is the bnCtx.
   */
  mpCtx = (VoltMpIntCtx *)(operand->mpCtx);
  bnCtx = (BN_CTX *)(mpCtx->localCtx);

  do
  {
    /* Can't divide by zero. If is_zero returns 1, then the value is zero.
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_DIVIDE_BY_ZERO;
    if (BN_is_zero ((BIGNUM *)(modulus->mpInt)) == 1)
      break;

    /* This function returns 1 for success, 0 for failure.
     * If there was an error, it was probably memory (we already checked
     * for divide by 0).
     */
    VOLT_SET_FNCT_LINE (fnctLine)
    status = VT_ERROR_MEMORY;
    resultBn = (BIGNUM *)(result->mpInt);
    if (BN_mod (
      resultBn, (BIGNUM *)(operand->mpInt), (BIGNUM *)(modulus->mpInt),
      bnCtx) != 0)
    {
      /* We need to account for negative operands.
       * To get the sign, we can't treat the bn as an opaque type. So
       * long as the definition of BIGNUM does not change, this will
       * work. Inside a BIGNUM, if neg is 0, the number is positive,
       * if neg is 1, the number is negative.
       * If BN_add returns 0, error.
       */
      if (resultBn->neg != 0)
      {
        VOLT_SET_FNCT_LINE (fnctLine)
        if (BN_add (resultBn, resultBn, (BIGNUM *)(modulus->mpInt)) == 0)
          break;
      }

      status = 0;
    }
  } while (0);

  VOLT_LOG_ERROR_COMPARE (
    status, result->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapModReduce", (char *)0)

  return (status);
}

int OpenSSLBnWrapModInvert (
   VoltMpInt *operand,
   VoltMpInt *modulus,
   VoltMpInt *result
   )
{
  int status;
  unsigned long bnErr;
  VoltMpIntCtx *mpCtx;
  BIGNUM *retVal;
  BN_CTX *bnCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (operand == (VoltMpInt *)0) ||
       (modulus == (VoltMpInt *)0) ||
       (result == (VoltMpInt *)0) )
    return (VT_ERROR_NULL_ARG);

  /* The localCtx is the bnCtx.
   */
  mpCtx = (VoltMpIntCtx *)(operand->mpCtx);
  bnCtx = (BN_CTX *)(mpCtx->localCtx);

  VOLT_SET_FNCT_LINE (fnctLine)
  retVal = BN_mod_inverse (
    (BIGNUM *)(result->mpInt), (BIGNUM *)(operand->mpInt),
    (BIGNUM *)(modulus->mpInt), bnCtx);

  /* The return value is NULL for error. If not NULL, no error.
   */
  if (retVal != (BIGNUM *)0)
    return (0);

  /* If there was an error, what was it? If not MEMORY, then there must
   * be no inverse.
   */
  bnErr = ERR_get_error ();

  status = VT_ERROR_MEMORY;
  if (bnErr != ERR_R_MALLOC_FAILURE)
    status = VT_ERROR_NO_INVERSE;

  VOLT_LOG_ERROR (
    result->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapModInvert", (char *)0)

  return (status);
}

int OpenSSLBnWrapModExp (
   VoltMpInt *base,
   VoltMpInt *exponent,
   VoltMpInt *modulus,
   VoltMpInt *result
   )
{
  int status;
  unsigned long bnErr;
  VoltMpIntCtx *mpCtx;
  BN_CTX *bnCtx;
  VOLT_DECLARE_FNCT_LINE (fnctLine)

  /* If NULL, we can't log an error, no libCtx.
   * We could check for one NULL, the other not, but this is the kind
   * of error an app expects not to log.
   */
  if ( (base == (VoltMpInt *)0) ||
       (exponent == (VoltMpInt *)0) ||
       (modulus == (VoltMpInt *)0) ||
       (result == (VoltMpInt *)0) ) 
    return (VT_ERROR_NULL_ARG);

  /* The localCtx is the bnCtx.
   */
  mpCtx = (VoltMpIntCtx *)(base->mpCtx);
  bnCtx = (BN_CTX *)(mpCtx->localCtx);

  /* This function returns 1 for success, 0 for failure.
   */
  if (BN_mod_exp (
    (BIGNUM *)(result->mpInt), (BIGNUM *)(base->mpInt),
    (BIGNUM *)(exponent->mpInt), (BIGNUM *)(modulus->mpInt), bnCtx) != 0)
    return (0);

  /* If there was an error, what was it?
   */
  VOLT_SET_FNCT_LINE (fnctLine)
  status = VT_ERROR_MP_PROVIDER;
  bnErr = ERR_get_error ();
  if (bnErr == ERR_R_MALLOC_FAILURE)
    status = VT_ERROR_MEMORY;

  VOLT_LOG_ERROR (
    result->mpCtx->voltObject.libraryCtx, status,
    VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapModExp", (char *)0)

  return (status);
}

⌨️ 快捷键说明

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