📄 opensslbnwrap.c
字号:
{
int status;
VoltMpIntCtx *mpCtx;
BN_CTX *bnCtx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
VOLT_DECLARE_ERROR_TYPE(errorType)
/* 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);
do
{
mpCtx = (VoltMpIntCtx *)(multiplicand->mpCtx);
/* Get the bnCtx from the mpCtx. This will be stored
* in thread-local data for multi-thread thread context
*/
VOLT_SET_ERROR_TYPE(errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBnCtxFromMpCtx(mpCtx, &bnCtx);
if (status != 0)
break;
/* This function returns 1 for success, 0 for failure.
* If there was an error, it was probably memory.
*/
VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
if (BN_mul (
(BIGNUM *)(product->mpInt), (BIGNUM *)(multiplicand->mpInt),
(BIGNUM *)(multiplier->mpInt), bnCtx) == 0)
{
status = VT_ERROR_MEMORY;
break;
}
}
while (0);
VOLT_LOG_ERROR_COMPARE (
status, product->mpCtx->voltObject.libraryCtx, status,
errorType, fnctLine, "OpenSSLBnWrapMultiply", (char *)0)
return (status);
}
int OpenSSLBnWrapSquare (
VoltMpInt *operand,
VoltMpInt *product
)
{
int status;
VoltMpIntCtx *mpCtx;
BN_CTX *bnCtx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
VOLT_DECLARE_ERROR_TYPE(errorType)
/* 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);
do
{
mpCtx = (VoltMpIntCtx *)(operand->mpCtx);
/* Get the bnCtx from the mpCtx. This will be stored
* in thread-local data for multi-thread thread context
*/
VOLT_SET_ERROR_TYPE(errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBnCtxFromMpCtx(mpCtx, &bnCtx);
if (status != 0)
break;
/* This function returns 1 for success, 0 for failure.
* If there was an error, it was probably memory.
*/
VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
if (BN_sqr (
(BIGNUM *)(product->mpInt), (BIGNUM *)(operand->mpInt), bnCtx) == 0)
{
status = VT_ERROR_MEMORY;
break;
}
}
while (0);
VOLT_LOG_ERROR_COMPARE (
status, product->mpCtx->voltObject.libraryCtx, status,
errorType, fnctLine, "OpenSSLBnWrapSquare", (char *)0)
return status;
}
int OpenSSLBnWrapDivide (
VoltMpInt *dividend,
VoltMpInt *divisor,
VoltMpInt *quotient,
VoltMpInt *remainder
)
{
int status;
VoltMpIntCtx *mpCtx;
BN_CTX *bnCtx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
VOLT_DECLARE_ERROR_TYPE(errorType)
/* 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);
do
{
mpCtx = (VoltMpIntCtx *)(dividend->mpCtx);
/* Get the bnCtx from the mpCtx. This will be stored
* in thread-local data for multi-thread thread context
*/
VOLT_SET_ERROR_TYPE(errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBnCtxFromMpCtx(mpCtx, &bnCtx);
if (status != 0)
break;
/* Can't divide by zero. If is_zero returns 1, then the value is zero.
*/
VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
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,
errorType, fnctLine, "OpenSSLBnWrapDivide", (char *)0)
return (status);
}
int OpenSSLBnWrapGCD (
VoltMpInt *operand1,
VoltMpInt *operand2,
VoltMpInt *result
)
{
int status;
VoltMpIntCtx *mpCtx;
BIGNUM *resultBn;
BN_CTX *bnCtx;
VOLT_DECLARE_FNCT_LINE (fnctLine)
VOLT_DECLARE_ERROR_TYPE(errorType)
/* 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 ( (operand1 == (VoltMpInt *)0) ||
(operand2 == (VoltMpInt *)0) ||
(result == (VoltMpInt *)0) )
return (VT_ERROR_NULL_ARG);
do
{
mpCtx = (VoltMpIntCtx *)(operand1->mpCtx);
/* Get the bnCtx from the mpCtx. This will be stored
* in thread-local data for multi-thread thread context
*/
VOLT_SET_ERROR_TYPE(errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBnCtxFromMpCtx(mpCtx, &bnCtx);
if (status != 0)
break;
/* Can't find GCD if one of the ops is zero. If is_zero returns 1,
* then the value is zero.
*/
VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY)
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_DIVIDE_BY_ZERO;
if (BN_is_zero ((BIGNUM *)(operand1->mpInt)) == 1)
break;
VOLT_SET_FNCT_LINE (fnctLine)
if (BN_is_zero ((BIGNUM *)(operand2->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 0 operand).
*/
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
resultBn = (BIGNUM *)(result->mpInt);
if (BN_gcd (
resultBn, (BIGNUM *)(operand1->mpInt), (BIGNUM *)(operand2->mpInt),
bnCtx) == 0)
break;
status = 0;
} while (0);
VOLT_LOG_ERROR_COMPARE (
status, result->mpCtx->voltObject.libraryCtx, status,
errorType, fnctLine, "OpenSSLBnWrapGCD", (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)
VOLT_DECLARE_ERROR_TYPE(errorType)
/* 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);
do
{
mpCtx = (VoltMpIntCtx *)(operand->mpCtx);
/* Get the bnCtx from the mpCtx. This will be stored
* in thread-local data for multi-thread thread context
*/
VOLT_SET_ERROR_TYPE(errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBnCtxFromMpCtx(mpCtx, &bnCtx);
if (status != 0)
break;
/* 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,
errorType, 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)
VOLT_DECLARE_ERROR_TYPE(errorType)
/* 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);
do
{
mpCtx = (VoltMpIntCtx *)(operand->mpCtx);
/* Get the bnCtx from the mpCtx. This will be stored
* in thread-local data for multi-thread thread context
*/
VOLT_SET_ERROR_TYPE(errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBnCtxFromMpCtx(mpCtx, &bnCtx);
if (status != 0)
break;
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;
}
while (0);
VOLT_LOG_ERROR (
result->mpCtx->voltObject.libraryCtx, status,
errorType, 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)
VOLT_DECLARE_ERROR_TYPE(errorType)
/* 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);
do
{
mpCtx = (VoltMpIntCtx *)(base->mpCtx);
/* Get the bnCtx from the mpCtx. This will be stored
* in thread-local data for multi-thread thread context
*/
VOLT_SET_ERROR_TYPE(errorType, 0)
VOLT_SET_FNCT_LINE (fnctLine)
status = VoltGetBnCtxFromMpCtx(mpCtx, &bnCtx);
if (status != 0)
break;
/* 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;
}
while (0);
VOLT_LOG_ERROR (
result->mpCtx->voltObject.libraryCtx, status,
errorType, fnctLine, "OpenSSLBnWrapModExp", (char *)0)
return (status);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -