Skip to content

Commit

Permalink
Fix signed compare of byte string.
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidWooten committed Jun 4, 2021
1 parent 9950301 commit f16879b
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions TPMCmd/tpm/src/support/MathOnByteBuffers.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ UnsignedCompareB(
return (a[i] > b[i]) ? 1 : -1;
}
}
// Will return == if sizes are both zero
return 0;
}

Expand All @@ -87,26 +88,13 @@ SignedCompareB(
const BYTE *b // IN: b buffer
)
{
int signA, signB; // sign of a and b

// For positive or 0, sign_a is 1
// for negative, sign_a is 0
signA = ((a[0] & 0x80) == 0) ? 1 : 0;

// For positive or 0, sign_b is 1
// for negative, sign_b is 0
signB = ((b[0] & 0x80) == 0) ? 1 : 0;

if(signA != signB)
{
return signA - signB;
}
if(signA == 1)
// do unsigned compare function
return UnsignedCompareB(aSize, a, bSize, b);
// are the signs different ?
if(((a[0] ^ b[0]) & 0x80) > 0)
// if the signs are different, then a is less than b if a is negative.
return a[0] & 0x80 ? -1 : 1;
else
// do unsigned compare the other way
return 0 - UnsignedCompareB(aSize, a, bSize, b);
// do unsigned compare function
return UnsignedCompareB(aSize, a, bSize, b);
}

//*** ModExpB
Expand Down Expand Up @@ -203,9 +191,9 @@ DivideB(
}

//*** AdjustNumberB()
// Remove/add leading zeros from a number in a TPM2B. Will try to make the number
// by adding or removing leading zeros. If the number is larger than the requested
// size, it will make the number as small as possible. Setting 'requestedSize' to
// Remove/add leading zeros from a number in a TPM2B. Will try to make the number
// by adding or removing leading zeros. If the number is larger than the requested
// size, it will make the number as small as possible. Setting 'requestedSize' to
// zero is equivalent to requesting that the number be normalized.
UINT16
AdjustNumberB(
Expand Down Expand Up @@ -233,7 +221,7 @@ AdjustNumberB(
}
}
// This is a request to shift the number to the right (add leading zeros)
else
else
{
MemoryCopy(&num->buffer[requestedSize - num->size], num->buffer, num->size);
MemorySet(num->buffer, 0, requestedSize- num->size);
Expand All @@ -243,7 +231,7 @@ AdjustNumberB(
}

//*** ShiftLeft()
// This function shifts a byte buffer (a TPM2B) one byte to the left. That is,
// This function shifts a byte buffer (a TPM2B) one byte to the left. That is,
// the most significant bit of the most significant byte is lost.
TPM2B *
ShiftLeft(
Expand Down

0 comments on commit f16879b

Please sign in to comment.