Annotation of embedaddon/strongswan/src/libstrongswan/plugins/ntru/ntru_trits.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013-2016 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 "ntru_trits.h"
        !            17: #include "ntru_convert.h"
        !            18: 
        !            19: #include <crypto/xofs/xof_bitspender.h>
        !            20: #include <utils/debug.h>
        !            21: #include <utils/test.h>
        !            22: 
        !            23: typedef struct private_ntru_trits_t private_ntru_trits_t;
        !            24: 
        !            25: /**
        !            26:  * Private data of an ntru_trits_t object.
        !            27:  */
        !            28: struct private_ntru_trits_t {
        !            29: 
        !            30:        /**
        !            31:         * Public ntru_trits_t interface.
        !            32:         */
        !            33:        ntru_trits_t public;
        !            34: 
        !            35:        /**
        !            36:         * Size of the trits array
        !            37:         */
        !            38:        size_t trits_len;
        !            39: 
        !            40:        /**
        !            41:         * Array containing a trit per octet
        !            42:         */
        !            43:        uint8_t *trits;
        !            44: 
        !            45: };
        !            46: 
        !            47: METHOD(ntru_trits_t, get_size, size_t,
        !            48:        private_ntru_trits_t *this)
        !            49: {
        !            50:        return this->trits_len;
        !            51: }
        !            52: 
        !            53: METHOD(ntru_trits_t, get_trits, uint8_t*,
        !            54:        private_ntru_trits_t *this)
        !            55: {
        !            56:        return this->trits;
        !            57: }
        !            58: 
        !            59: METHOD(ntru_trits_t, destroy, void,
        !            60:        private_ntru_trits_t *this)
        !            61: {
        !            62:        memwipe(this->trits, this->trits_len);
        !            63:        free(this->trits);
        !            64:        free(this);
        !            65: }
        !            66: 
        !            67: /*
        !            68:  * Described in header.
        !            69:  */
        !            70: ntru_trits_t *ntru_trits_create(size_t len, ext_out_function_t alg,
        !            71:                                                                chunk_t seed)
        !            72: {
        !            73:        private_ntru_trits_t *this;
        !            74:        uint8_t octet, buf[5], *trits;
        !            75:        size_t trits_needed;
        !            76:        xof_bitspender_t *bitspender;
        !            77: 
        !            78:        bitspender = xof_bitspender_create(alg, seed, TRUE);
        !            79:        if (!bitspender)
        !            80:        {
        !            81:            return NULL;
        !            82:        }
        !            83: 
        !            84:        INIT(this,
        !            85:                .public = {
        !            86:                        .get_size = _get_size,
        !            87:                        .get_trits = _get_trits,
        !            88:                        .destroy = _destroy,
        !            89:                },
        !            90:                .trits_len = len,
        !            91:                .trits = malloc(len),
        !            92:        );
        !            93: 
        !            94:        trits = this->trits;
        !            95:        trits_needed = this->trits_len;
        !            96: 
        !            97:        while (trits_needed > 0)
        !            98:        {
        !            99:                if (!bitspender->get_byte(bitspender, &octet))
        !           100:                {
        !           101:                        bitspender->destroy(bitspender);
        !           102:                        destroy(this);
        !           103:                        return NULL;
        !           104:                }
        !           105:                if (octet < 243)  /* 243 = 3^5 */
        !           106:                {
        !           107:                        ntru_octet_2_trits(octet, (trits_needed < 5) ? buf : trits);
        !           108:                        if (trits_needed < 5)
        !           109:                        {
        !           110:                                memcpy(trits, buf, trits_needed);
        !           111:                                break;
        !           112:                        }
        !           113:                        trits += 5;
        !           114:                        trits_needed -= 5;
        !           115:                }
        !           116:        }
        !           117:        bitspender->destroy(bitspender);
        !           118: 
        !           119:        return &this->public;
        !           120: }
        !           121: 
        !           122: EXPORT_FUNCTION_FOR_TESTS(ntru, ntru_trits_create);

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