Expanded bliss_bitpacker to 32 bits
authorAndreas Steffen <andreas.steffen@strongswan.org>
Tue, 9 Dec 2014 06:31:59 +0000 (07:31 +0100)
committerAndreas Steffen <andreas.steffen@strongswan.org>
Tue, 9 Dec 2014 10:58:17 +0000 (11:58 +0100)
src/libstrongswan/plugins/bliss/bliss_bitpacker.c
src/libstrongswan/plugins/bliss/bliss_bitpacker.h
src/libstrongswan/plugins/bliss/bliss_public_key.c
src/libstrongswan/plugins/bliss/bliss_signature.c
src/libstrongswan/plugins/bliss/suites/test_bliss_bitpacker.c

index 6bd480f..295c5a2 100644 (file)
@@ -32,9 +32,9 @@ struct private_bliss_bitpacker_t {
        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
@@ -60,9 +60,9 @@ METHOD(bliss_bitpacker_t, get_bits, size_t,
 }
 
 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;
        }
@@ -82,21 +82,21 @@ METHOD(bliss_bitpacker_t, write_bits, bool,
                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;
        }
@@ -106,13 +106,13 @@ METHOD(bliss_bitpacker_t, read_bits, bool,
        {
                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)
                {
@@ -133,8 +133,8 @@ METHOD(bliss_bitpacker_t, extract_buf, chunk_t,
 {
        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;
@@ -164,8 +164,8 @@ bliss_bitpacker_t *bliss_bitpacker_create(size_t max_bits)
                        .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;
@@ -189,14 +189,11 @@ bliss_bitpacker_t *bliss_bitpacker_create_from_data(chunk_t data)
                        .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;
index 76accc2..7fcf05d 100644 (file)
@@ -45,7 +45,7 @@ struct bliss_bitpacker_t {
         * @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);
 
 
        /**
@@ -55,7 +55,7 @@ struct bliss_bitpacker_t {
         * @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
index f1ea685..569ad8d 100644 (file)
@@ -388,7 +388,7 @@ bool bliss_public_key_from_asn1(chunk_t object, bliss_param_set_t *set,
                                                                uint32_t **pubkey)
 {
        bliss_bitpacker_t *packer;
-       uint16_t coefficient;
+       uint32_t coefficient;
        int i;
 
        /* skip initial bit string octet defining unused bits */
@@ -405,7 +405,7 @@ bool bliss_public_key_from_asn1(chunk_t object, bliss_param_set_t *set,
        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);
 
index 4d575c1..861d4ce 100644 (file)
@@ -133,8 +133,8 @@ bliss_signature_t *bliss_signature_create_from_data(bliss_param_set_t *set,
 {
        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;
index ad885e8..f040ba7 100644 (file)
@@ -17,7 +17,7 @@
 
 #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);
@@ -30,7 +30,7 @@ START_TEST(test_bliss_sign_bitpacker_write)
 
        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++)
        {
@@ -47,20 +47,20 @@ END_TEST
 
 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);
 }
@@ -69,17 +69,18 @@ END_TEST
 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);
 }