size_t bits;
/**
- * Bit buffer for up to 16 bits
+ * Bit buffer for up to 32 bits
*/
- uint16_t bits_buf;
+ uint32_t bits_buf;
/**
* Bits left in the bit buffer
}
METHOD(bliss_bitpacker_t, write_bits, bool,
- private_bliss_bitpacker_t *this, uint16_t value, size_t bits)
+ private_bliss_bitpacker_t *this, uint32_t value, size_t bits)
{
- if (bits > 16)
+ if (bits > 32)
{
return FALSE;
}
value &= (1 << (bits - this->bits_left)) - 1;
bits -= this->bits_left;
- if (this->pos.len < 4)
+ if (this->pos.len < 8)
{
return FALSE;
}
- htoun16(this->pos.ptr, this->bits_buf);
- this->pos = chunk_skip(this->pos, 2);
+ htoun32(this->pos.ptr, this->bits_buf);
+ this->pos = chunk_skip(this->pos, 4);
this->bits_buf = 0;
- this->bits_left = 16;
+ this->bits_left = 32;
}
}
METHOD(bliss_bitpacker_t, read_bits, bool,
- private_bliss_bitpacker_t *this, uint16_t *value, size_t bits)
+ private_bliss_bitpacker_t *this, uint32_t *value, size_t bits)
{
- if (bits > 16)
+ if (bits > 32)
{
return FALSE;
}
{
if (this->bits_left == 0)
{
- if (this->pos.len < 2)
+ if (this->pos.len < 4)
{
return FALSE;
}
- this->bits_buf = untoh16(this->pos.ptr);
- this->pos = chunk_skip(this->pos, 2);
- this->bits_left = 16;
+ this->bits_buf = untoh32(this->pos.ptr);
+ this->pos = chunk_skip(this->pos, 4);
+ this->bits_left = 32;
}
if (bits <= this->bits_left)
{
{
chunk_t buf;
- htoun16(this->pos.ptr, this->bits_buf);
- this->pos.len -= 2;
+ htoun32(this->pos.ptr, this->bits_buf);
+ this->pos.len -= 4;
buf = this->buf;
buf.len = this->buf.len - this->pos.len - this->bits_left/8;
this->buf = this->pos = chunk_empty;
.extract_buf = _extract_buf,
.destroy = _destroy,
},
- .bits_left = 16,
- .buf = chunk_alloc(round_up(max_bits, 16)/8),
+ .bits_left = 32,
+ .buf = chunk_alloc(round_up(max_bits, 32)/8),
);
this->pos = this->buf;
.destroy = _destroy,
},
.bits = 8 * data.len,
- .buf = chunk_alloc(round_up(data.len, 2)),
+ .buf = chunk_alloc(round_up(data.len, 4)),
);
+ memset(this->buf.ptr + this->buf.len - 4, 0x00, 4);
memcpy(this->buf.ptr, data.ptr, data.len);
- if (this->buf.len > data.len)
- {
- *(this->buf.ptr + data.len) = 0x00;
- }
this->pos = this->buf;
return &this->public;
* @param bits Number of bits to be written
* @result TRUE if value could be written into buffer
*/
- bool (*write_bits)(bliss_bitpacker_t *this, uint16_t value, size_t bits);
+ bool (*write_bits)(bliss_bitpacker_t *this, uint32_t value, size_t bits);
/**
* @param bits Number of bits to be read
* @result TRUE if value could be read from buffer
*/
- bool (*read_bits)(bliss_bitpacker_t *this, uint16_t *value, size_t bits);
+ bool (*read_bits)(bliss_bitpacker_t *this, uint32_t *value, size_t bits);
/**
* Detach the internal octet buffer and return it
uint32_t **pubkey)
{
bliss_bitpacker_t *packer;
- uint16_t coefficient;
+ uint32_t coefficient;
int i;
/* skip initial bit string octet defining unused bits */
for (i = 0; i < set->n; i++)
{
packer->read_bits(packer, &coefficient, set->q_bits);
- (*pubkey)[i] = (uint32_t)coefficient;
+ (*pubkey)[i] = coefficient;
}
packer->destroy(packer);
{
private_bliss_signature_t *this;
bliss_bitpacker_t *packer;
- uint32_t z1_sign, z1_mask;
- uint16_t z2d_sign, z2d_mask, value, z1_bits, z2d_bits;
+ uint32_t z1_sign, z1_mask, value;
+ uint16_t z2d_sign, z2d_mask, z1_bits, z2d_bits;
int i;
z1_bits = set->z1_bits;
#include <bliss_bitpacker.h>
-static uint16_t bits[] = { 0, 1, 2, 3, 4, 7, 1, 14, 2, 29, 3, 28, 67};
+static uint32_t bits[] = { 0, 1, 2, 3, 4, 7, 1, 14, 2, 29, 3, 28, 67};
static chunk_t packed_bits = chunk_from_chars(0x6e, 0x71, 0xe1, 0x74,
0x37, 0x21, 0x80);
packer = bliss_bitpacker_create(49);
- ck_assert(!packer->write_bits(packer, 0, 17));
+ ck_assert(!packer->write_bits(packer, 0, 33));
for (i = 0; i < countof(bits); i++)
{
START_TEST(test_bliss_sign_bitpacker_read)
{
- uint16_t value;
+ uint32_t value;
bliss_bitpacker_t *packer;
int i;
packer = bliss_bitpacker_create_from_data(packed_bits);
- ck_assert(!packer->read_bits(packer, &value, 17));
+ ck_assert(!packer->read_bits(packer, &value, 33));
for (i = 0; i < countof(bits); i++)
{
ck_assert(packer->read_bits(packer, &value, 1 + i/2));
ck_assert_int_eq(value, bits[i]);
}
- ck_assert(!packer->read_bits(packer, &value, 16));
+ ck_assert(!packer->read_bits(packer, &value, 32));
packer->destroy(packer);
}
START_TEST(test_bliss_sign_bitpacker_fail)
{
bliss_bitpacker_t *packer;
- uint16_t value;
+ uint32_t value;
- packer = bliss_bitpacker_create(16);
- ck_assert(!packer->write_bits(packer, 0, 17));
- ck_assert( packer->write_bits(packer, 0x7f01, 15));
+ packer = bliss_bitpacker_create(32);
+ ck_assert(!packer->write_bits(packer, 0, 33));
+ ck_assert( packer->write_bits(packer, 0x7f2a3b01, 31));
ck_assert(!packer->write_bits(packer, 3, 2));
packer->destroy(packer);
- packer = bliss_bitpacker_create_from_data(chunk_from_chars(0x7f, 0x01));
- ck_assert(!packer->read_bits(packer, &value, 17));
- ck_assert( packer->read_bits(packer, &value, 15));
+ packer = bliss_bitpacker_create_from_data(
+ chunk_from_chars(0x7f, 0x2a, 0x3b, 0x01));
+ ck_assert(!packer->read_bits(packer, &value, 33));
+ ck_assert( packer->read_bits(packer, &value, 31));
ck_assert(!packer->read_bits(packer, &value, 2));
packer->destroy(packer);
}