Fixed bad bit shift and sign extension errors

This commit is contained in:
Andreas Steffen
2014-12-23 15:40:01 +01:00
parent fd19feefa4
commit 6139c8e524
4 changed files with 34 additions and 10 deletions
@@ -79,13 +79,19 @@ METHOD(mgf1_bitspender_t, get_bits, bool,
private_mgf1_bitspender_t *this, int bits_needed, uint32_t *bits)
{
int bits_now;
*bits = 0x00000000;
if (bits_needed == 0)
{
/* trivial */
return TRUE;
}
if (bits_needed > 32)
{
/* too many bits requested */
return FALSE;
}
*bits = 0x00000000;
while (bits_needed)
{
@@ -113,17 +119,25 @@ METHOD(mgf1_bitspender_t, get_bits, bool,
bits_now = this->bits_left;
this->bits_left = 0;
bits_needed -= bits_now;
*bits <<= bits_now;
*bits |= this->bits;
}
else
{
bits_now = bits_needed;
this->bits_left -= bits_needed;
bits_needed = 0;
}
if (bits_now == 32)
{
*bits = this->bits;
}
else
{
*bits <<= bits_now;
*bits |= this->bits >> this->bits_left;
this->bits &= 0xffffffff >> (32 - this->bits_left);
if (this->bits_left)
{
this->bits &= 0xffffffff >> (32 - this->bits_left);
}
}
}
return TRUE;
@@ -151,7 +165,7 @@ METHOD(mgf1_bitspender_t, get_byte, bool,
}
*byte = this->bytes[4 - this->bytes_left--];
return TRUE;
return TRUE;
}
METHOD(mgf1_bitspender_t, destroy, void,
@@ -62,12 +62,19 @@ METHOD(bliss_bitpacker_t, get_bits, size_t,
METHOD(bliss_bitpacker_t, write_bits, bool,
private_bliss_bitpacker_t *this, uint32_t value, size_t bits)
{
if (bits == 0)
{
return TRUE;
}
if (bits > 32)
{
return FALSE;
}
if (bits < 32)
{
value &= (1 << bits) - 1;
}
this->bits += bits;
value &= (1 << bits) - 1;
while (TRUE)
{
@@ -152,7 +159,7 @@ METHOD(bliss_bitpacker_t, destroy, void,
/**
* See header.
*/
bliss_bitpacker_t *bliss_bitpacker_create(size_t max_bits)
bliss_bitpacker_t *bliss_bitpacker_create(uint16_t max_bits)
{
private_bliss_bitpacker_t *this;
@@ -73,7 +73,7 @@ struct bliss_bitpacker_t {
*
* @param max_bits Total number of bits to be stored
*/
bliss_bitpacker_t* bliss_bitpacker_create(size_t max_bits);
bliss_bitpacker_t* bliss_bitpacker_create(uint16_t max_bits);
/**
* Create a bliss_bitpacker_t object for reading
@@ -391,12 +391,15 @@ bool bliss_public_key_from_asn1(chunk_t object, bliss_param_set_t *set,
{
bliss_bitpacker_t *packer;
uint32_t coefficient;
uint16_t needed_bits;
int i;
/* skip initial bit string octet defining unused bits */
object = chunk_skip(object, 1);
if (8 * object.len < set->n * set->q_bits)
needed_bits = set->n * set->q_bits;
if (8 * object.len < needed_bits)
{
return FALSE;
}