engines/e_aep.c: make it BN_ULONG-size and endian "neutral".

This commit is contained in:
Andy Polyakov 2013-10-28 22:18:48 +01:00
parent 4eeb750d20
commit e6e29bc4c9

View File

@ -1052,13 +1052,10 @@ static AEP_RV GetBigNumSize(AEP_VOID_PTR ArbBigNum, AEP_U32* BigNumSize)
/*Cast the ArbBigNum pointer to our BIGNUM struct*/ /*Cast the ArbBigNum pointer to our BIGNUM struct*/
bn = (BIGNUM*) ArbBigNum; bn = (BIGNUM*) ArbBigNum;
#ifdef SIXTY_FOUR_BIT_LONG *BigNumSize = bn->top*BN_BYTES;
*BigNumSize = bn->top << 3;
#else if (BN_BYTES>sizeof(AEP_U32) && (bn->d[bn->top-1]>>BN_BITS4)==0)
/*Size of the bignum in bytes is equal to the bn->top (no of 32 bit *BigNumSize -= 4;
words) multiplies by 4*/
*BigNumSize = bn->top << 2;
#endif
return AEP_R_OK; return AEP_R_OK;
} }
@ -1067,31 +1064,42 @@ static AEP_RV MakeAEPBigNum(AEP_VOID_PTR ArbBigNum, AEP_U32 BigNumSize,
unsigned char* AEP_BigNum) unsigned char* AEP_BigNum)
{ {
BIGNUM* bn; BIGNUM* bn;
const union { long one; char little; } is_endian = {1};
#ifndef SIXTY_FOUR_BIT_LONG int i,j;
unsigned char* buf;
int i;
#endif
/*Cast the ArbBigNum pointer to our BIGNUM struct*/ /*Cast the ArbBigNum pointer to our BIGNUM struct*/
bn = (BIGNUM*) ArbBigNum; bn = (BIGNUM*) ArbBigNum;
#ifdef SIXTY_FOUR_BIT_LONG
memcpy(AEP_BigNum, bn->d, BigNumSize);
#else
/*Must copy data into a (monotone) least significant byte first format /*Must copy data into a (monotone) least significant byte first format
performing endian conversion if necessary*/ performing endian conversion if necessary*/
for(i=0;i<bn->top;i++) if (is_endian.little && sizeof(bn->d[0])==BN_BYTES)
memcpy(AEP_BigNum, bn->d, BigNumSize);
else
{ {
buf = (unsigned char*)&bn->d[i]; BN_ULONG di;
*((AEP_U32*)AEP_BigNum) = (AEP_U32) for (i=0; BigNumSize>=BN_BYTES; i++)
((unsigned) buf[1] << 8 | buf[0]) | {
((unsigned) buf[3] << 8 | buf[2]) << 16; di = bn->d[i];
for (j=0; j<BN_BYTES; j++)
AEP_BigNum += 4; {
AEP_BigNum[j] = (unsigned char)di;
di>>=8;
}
AEP_BigNum += BN_BYTES;
BigNumSize -= BN_BYTES;
}
if (BigNumSize)
{
di = bn->d[i];
for (j=0; j<BigNumSize; j++)
{
AEP_BigNum[j] = (unsigned char)di;
di>>=8;
}
}
} }
#endif
return AEP_R_OK; return AEP_R_OK;
} }
@ -1101,36 +1109,46 @@ static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
unsigned char* AEP_BigNum) unsigned char* AEP_BigNum)
{ {
BIGNUM* bn; BIGNUM* bn;
#ifndef SIXTY_FOUR_BIT_LONG const union { long one; char little; } is_endian = {1};
int i; int i,j,top;
#endif
bn = (BIGNUM*)ArbBigNum; bn = (BIGNUM*)ArbBigNum;
/*Expand the result bn so that it can hold our big num. /*Expand the result bn so that it can hold our big num.
Size is in bits*/ Size is in bits*/
bn_expand(bn, (int)(BigNumSize << 3)); top = (BigNumSize+BN_BYTES-1)/BN_BYTES;
bn_expand(bn, top);
#ifdef SIXTY_FOUR_BIT_LONG bn->top = top;
bn->top = BigNumSize >> 3; bn->d[top-1] = 0;
if((BigNumSize & 7) != 0)
bn->top++;
memset(bn->d, 0, bn->top << 3);
if (is_endian.little && sizeof(bn->d[0])==BN_BYTES)
memcpy(bn->d, AEP_BigNum, BigNumSize); memcpy(bn->d, AEP_BigNum, BigNumSize);
#else else
bn->top = BigNumSize >> 2;
for(i=0;i<bn->top;i++)
{ {
bn->d[i] = (AEP_U32) BN_ULONG di;
((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]); for (i=0; BigNumSize>=BN_BYTES; i++)
AEP_BigNum += 4; {
for (di=0,j=BN_BYTES; j!=0; )
{
di <<= 8;
di |= AEP_BigNum[--j];
}
bn->d[i] = di;
AEP_BigNum += BN_BYTES;
BigNumSize -= BN_BYTES;
}
if (BigNumSize)
{
for (di=0,j=BigNumSize; j!=0; )
{
di <<= 8;
di |= AEP_BigNum[--j];
}
bn->d[i] = di;
}
} }
#endif
return AEP_R_OK; return AEP_R_OK;
} }