Annotation of embedaddon/strongswan/src/libstrongswan/plugins/bliss/bliss_signature.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2014 Andreas Steffen
                      3:  * HSR Hochschule fuer Technik Rapperswil
                      4:  *
                      5:  * This program is free software; you can redistribute it and/or modify it
                      6:  * under the terms of the GNU General Public License as published by the
                      7:  * Free Software Foundation; either version 2 of the License, or (at your
                      8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
                      9:  *
                     10:  * This program is distributed in the hope that it will be useful, but
                     11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
                     13:  * for more details.
                     14:  */
                     15: 
                     16: #include "bliss_signature.h"
                     17: #include "bliss_bitpacker.h"
                     18: #include "bliss_huffman_coder.h"
                     19: 
                     20: 
                     21: typedef struct private_bliss_signature_t private_bliss_signature_t;
                     22: 
                     23: /**
                     24:  * Private data of a bliss_signature_t object.
                     25:  */
                     26: struct private_bliss_signature_t {
                     27:        /**
                     28:         * Public interface for this signer.
                     29:         */
                     30:        bliss_signature_t public;
                     31: 
                     32:        /**
                     33:         * BLISS signature parameter set
                     34:         */
                     35:        const bliss_param_set_t *set;
                     36: 
                     37:        /**
                     38:         * BLISS signature vector z1 of size n
                     39:         */
                     40:        int32_t *z1;
                     41: 
                     42:        /**
                     43:         * BLISS signature vector z2d of size n
                     44:         */
                     45:        int16_t *z2d;
                     46: 
                     47:        /**
                     48:         * Indices of sparse BLISS challenge vector c of size kappa
                     49:         */
                     50:        uint16_t *c_indices;
                     51: 
                     52: };
                     53: 
                     54: METHOD(bliss_signature_t, get_encoding, chunk_t,
                     55:        private_bliss_signature_t *this)
                     56: {
                     57:        bliss_bitpacker_t *packer;
                     58:        bliss_huffman_coder_t *coder;
                     59:        bliss_huffman_code_t *code;
                     60:        int32_t z1;
                     61:        uint32_t z1_sign;
                     62:        uint16_t z2d_bits;
                     63:        chunk_t encoding = chunk_empty;
                     64:        int i;
                     65: 
                     66:        z2d_bits = this->set->z1_bits - this->set->d;
                     67: 
                     68:        /* Get Huffman code for this BLISS parameter set */
                     69:        code = bliss_huffman_code_get_by_id(this->set->id);
                     70:        if (!code)
                     71:        {
                     72:                DBG1(DBG_LIB, "no Huffman code found for parameter set %N",
                     73:                         bliss_param_set_id_names, this->set->id);
                     74:                return chunk_empty;
                     75:        }
                     76: 
                     77:        packer = bliss_bitpacker_create(this->set->n * this->set->z1_bits +
                     78:                                                                        this->set->n * z2d_bits +
                     79:                                                                        this->set->kappa * this->set->n_bits);
                     80:        coder = bliss_huffman_coder_create(code, packer);
                     81: 
                     82:        for (i = 0; i < this->set->n; i++)
                     83:        {
                     84:                /* determine and remove the sign of z1[i]*/
                     85:                z1_sign = this->z1[i] < 0;
                     86:                z1 = z1_sign ? -this->z1[i] : this->z1[i];
                     87: 
                     88:                if (!packer->write_bits(packer, z1_sign, 1) ||
                     89:                        !packer->write_bits(packer, z1 & 0xff, 8) ||
                     90:                        !coder->encode(coder, z1 >> 8, this->z2d[i]))
                     91:                {
                     92:                        goto end;
                     93:                }
                     94:        }
                     95:        for (i = 0; i < this->set->kappa; i++)
                     96:        {
                     97:                if (!packer->write_bits(packer, this->c_indices[i], this->set->n_bits))
                     98:                {
                     99:                        goto end;
                    100:                }
                    101:        }
                    102:        encoding = packer->extract_buf(packer);
                    103: 
                    104:        DBG2(DBG_LIB, "efficiency of Huffman coder is %6.4f bits/tuple (%u bits)",
                    105:                                   coder->get_bits(coder)/(double)(this->set->n),
                    106:                                   coder->get_bits(coder));
                    107:        DBG2(DBG_LIB, "generated BLISS signature (%u bits encoded in %u bytes)",
                    108:                                   packer->get_bits(packer), encoding.len);
                    109: 
                    110:        end:
                    111:        coder->destroy(coder);
                    112:        packer->destroy(packer);
                    113:        return encoding;
                    114: }
                    115: 
                    116: METHOD(bliss_signature_t, get_parameters, void,
                    117:        private_bliss_signature_t *this, int32_t **z1, int16_t **z2d,
                    118:        uint16_t **c_indices)
                    119: {
                    120:        *z1 = this->z1;
                    121:        *z2d = this->z2d;
                    122:        *c_indices = this->c_indices;
                    123: }
                    124: 
                    125: METHOD(bliss_signature_t, destroy, void,
                    126:        private_bliss_signature_t *this)
                    127: {
                    128:        free(this->z1);
                    129:        free(this->z2d);
                    130:        free(this->c_indices);
                    131:        free(this);
                    132: }
                    133: 
                    134: /**
                    135:  * See header.
                    136:  */
                    137: bliss_signature_t *bliss_signature_create(const bliss_param_set_t *set)
                    138: {
                    139:        private_bliss_signature_t *this;
                    140: 
                    141:        INIT(this,
                    142:                .public = {
                    143:                        .get_encoding = _get_encoding,
                    144:                        .get_parameters = _get_parameters,
                    145:                        .destroy = _destroy,
                    146:                },
                    147:                .set = set,
                    148:                .z1  = malloc(set->n * sizeof(int32_t)),
                    149:                .z2d = malloc(set->n * sizeof(int16_t)),
                    150:                .c_indices = malloc(set->n * sizeof(uint16_t)),
                    151:        );
                    152: 
                    153:        return &this->public;
                    154: }
                    155: 
                    156: /**
                    157:  * See header.
                    158:  */
                    159: bliss_signature_t *bliss_signature_create_from_data(const bliss_param_set_t *set,
                    160:                                                                                                        chunk_t encoding)
                    161: {
                    162:        private_bliss_signature_t *this;
                    163:        bliss_bitpacker_t *packer;
                    164:        bliss_huffman_coder_t *coder;
                    165:        bliss_huffman_code_t *code;
                    166:        uint32_t z1_sign, z1_low, value;
                    167:        int32_t z1;
                    168:        int16_t z2;
                    169:        int i;
                    170: 
                    171:        /* Get Huffman code for this BLISS parameter set */
                    172:        code = bliss_huffman_code_get_by_id(set->id);
                    173:        if (!code)
                    174:        {
                    175:                DBG1(DBG_LIB, "no Huffman code found for parameter set %N",
                    176:                         bliss_param_set_id_names, set->id);
                    177:                return NULL;
                    178:        }
                    179: 
                    180:        if (encoding.len == 0)
                    181:        {
                    182:                DBG1(DBG_LIB, "zero length BLISS signature");
                    183:                return NULL;
                    184:        }
                    185: 
                    186:        INIT(this,
                    187:                .public = {
                    188:                        .get_encoding = _get_encoding,
                    189:                        .get_parameters = _get_parameters,
                    190:                        .destroy = _destroy,
                    191:                },
                    192:                .set = set,
                    193:                .z1  = malloc(set->n * sizeof(int32_t)),
                    194:                .z2d = malloc(set->n * sizeof(int16_t)),
                    195:                .c_indices = malloc(set->n * sizeof(uint16_t)),
                    196:        );
                    197: 
                    198:        packer = bliss_bitpacker_create_from_data(encoding);
                    199:        coder = bliss_huffman_coder_create(code, packer);
                    200: 
                    201:        for (i = 0; i < set->n; i++)
                    202:        {
                    203:                if (!packer->read_bits(packer, &z1_sign, 1) ||
                    204:                        !packer->read_bits(packer, &z1_low, 8) ||
                    205:                        !coder->decode(coder, &z1, &z2))
                    206:                {
                    207:                        DBG1(DBG_LIB, "truncated BLISS signature encoding of z1/z2");
                    208:                        coder->destroy(coder);
                    209:                        packer->destroy(packer);
                    210:                        destroy(this);
                    211:                        return NULL;
                    212:                }
                    213:                z1 = (z1 << 8) + z1_low;
                    214:                this->z1[i] = z1_sign ? -z1 : z1;
                    215:                this->z2d[i] = z2;
                    216:        }
                    217:        coder->destroy(coder);
                    218: 
                    219:        for (i = 0; i < set->kappa; i++)
                    220:        {
                    221:                if (!packer->read_bits(packer, &value, set->n_bits))
                    222:                {
                    223:                        DBG1(DBG_LIB, "truncated BLISS signature encoding of c_indices");
                    224:                        packer->destroy(packer);
                    225:                        destroy(this);
                    226:                        return NULL;
                    227:                }
                    228:                this->c_indices[i] = value;
                    229:        }
                    230:        packer->destroy(packer);
                    231: 
                    232:        return &this->public;
                    233: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>