📄 opensslbnwrap.c
字号:
/* 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 ( (leftInt == (VoltMpInt *)0) || (rightInt == (VoltMpInt *)0) )
return (VT_ERROR_NULL_ARG);
VOLT_SET_FNCT_LINE (fnctLine)
if (result != (int *)0)
{
bnResult = BN_cmp (
(BIGNUM *)(leftInt->mpInt), (BIGNUM *)(rightInt->mpInt));
*result = 0;
if (bnResult < 0)
*result = -1;
else if (bnResult > 0)
*result = 1;
return (0);
}
VOLT_LOG_ERROR (
leftInt->mpCtx->voltObject.libraryCtx, VT_ERROR_NULL_ARG,
VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapCompare", (char *)0)
return (VT_ERROR_NULL_ARG);
}
int OpenSSLBnWrapEvenOddZeroPositiveNegative (
VoltMpInt *theInt,
int *result
)
{
int retVal;
BIGNUM *theBn;
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);
VOLT_SET_FNCT_LINE (fnctLine)
if (result != (int *)0)
{
/* BN_is_zero returns 0 if the number is not zero and 1 if the number
* is zero. So init the result to 0 (for this function, if the number
* is zero, the result is 0) and if the return from BN_is_zero is not
* 0, then the number is zero, result is what it should be so return.
*/
*result = 0;
retVal = BN_is_zero (theBn);
if (retVal != 0)
return (0);
/* BN_is_odd returns 1 if the number is odd and 0 if the number is
* not odd.
*/
retVal = BN_is_odd (theBn);
if (retVal != 1)
retVal = 2;
/* retVal is now 1 or 2. If the source is negative, negate the retVal.
* 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 (theBn->neg != 0)
retVal = -retVal;
*result = retVal;
return (0);
}
VOLT_LOG_ERROR (
theInt->mpCtx->voltObject.libraryCtx, VT_ERROR_NULL_ARG,
VT_ERROR_TYPE_PRIMARY, fnctLine,
"OpenSSLBnWrapEvenOddZeroPositiveNegative", (char *)0)
return (VT_ERROR_NULL_ARG);
}
int OpenSSLBnWrapGetBitLength (
VoltMpInt *theInt,
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);
VOLT_SET_FNCT_LINE (fnctLine)
if (bitLen != (unsigned int *)0)
{
/* How big is the integer?
*/
*bitLen = (unsigned int)BN_num_bits ((BIGNUM *)(theInt->mpInt));
if (*bitLen == 0)
*bitLen = 1;
return (0);
}
VOLT_LOG_ERROR (
theInt->mpCtx->voltObject.libraryCtx, VT_ERROR_NULL_ARG,
VT_ERROR_TYPE_PRIMARY, fnctLine, "OpenSSLBnWrapGetBitLength", (char *)0)
return (VT_ERROR_NULL_ARG);
}
int OpenSSLBnWrapSetBit (
VoltMpInt *theInt,
unsigned int bitIndex,
unsigned int value
)
{
int status;
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
{
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);
}
static int VoltGetBnCtxFromMpCtx(VoltMpIntCtx* mpCtx, BN_CTX** bnCtx)
{
int status = 0;
VtLibCtx libCtx;
Pointer threadLocalKey;
Pointer threadLocalData;
VOLT_DECLARE_FNCT_LINE(fnctLine)
VOLT_DECLARE_ERROR_TYPE(errorType)
do
{
libCtx = mpCtx->voltObject.libraryCtx;
threadLocalKey = mpCtx->localCtx;
VOLT_SET_ERROR_TYPE(errorType, 0);
VOLT_SET_FNCT_LINE (fnctLine)
status = libCtx->threadCtx->GetThreadLocalData(libCtx->threadCtx,
threadLocalKey, &threadLocalData);
if (status != 0)
break;
if (threadLocalData == (Pointer)0)
{
VOLT_SET_ERROR_TYPE(errorType, VT_ERROR_TYPE_PRIMARY);
VOLT_SET_FNCT_LINE (fnctLine)
status = VT_ERROR_MEMORY;
threadLocalData = (Pointer) BN_CTX_new();
if (threadLocalData == (Pointer)0)
break;
VOLT_SET_ERROR_TYPE(errorType, 0);
VOLT_SET_FNCT_LINE (fnctLine)
status = libCtx->threadCtx->SetThreadLocalData(libCtx->threadCtx,
threadLocalKey, threadLocalData);
if (status != 0)
break;
}
*bnCtx = (BN_CTX*)threadLocalData;
}
while (0);
VOLT_LOG_ERROR_COMPARE (
status, libCtx, status, errorType, fnctLine,
"GetBnCtxFromMpCtx", (char *)0)
return status;
}
int OpenSSLBnWrapMultiply (
VoltMpInt *multiplicand,
VoltMpInt *multiplier,
VoltMpInt *product
)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -