Annotation of embedaddon/strongswan/src/libstrongswan/plugins/sha3/sha3_shake.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 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 "sha3_shake.h"
        !            17: #include "sha3_keccak.h"
        !            18: 
        !            19: typedef struct private_sha3_shake_t private_sha3_shake_t;
        !            20: 
        !            21: 
        !            22: /**
        !            23:  * Private data structure with hashing context for SHA-3
        !            24:  */
        !            25: struct private_sha3_shake_t {
        !            26: 
        !            27:        /**
        !            28:         * Public interface for this hasher.
        !            29:         */
        !            30:        sha3_shake_t public;
        !            31: 
        !            32:        /**
        !            33:         * XOF algorithm to be used (XOF_SHAKE_128 or XOF_SHAKE_256)
        !            34:         */
        !            35:        ext_out_function_t algorithm;
        !            36: 
        !            37:        /**
        !            38:         * SHA-3 Keccak state
        !            39:         */
        !            40:        sha3_keccak_t *keccak;
        !            41: 
        !            42:        /**
        !            43:         * Capacity in bytes of the SHA-3 Keccak state
        !            44:         */
        !            45:        u_int capacity;
        !            46: 
        !            47: };
        !            48: 
        !            49: METHOD(xof_t, get_type, ext_out_function_t,
        !            50:        private_sha3_shake_t *this)
        !            51: {
        !            52:        return this->algorithm;
        !            53: }
        !            54: 
        !            55: METHOD(xof_t, get_bytes, bool,
        !            56:        private_sha3_shake_t *this, size_t out_len, uint8_t *buffer)
        !            57: {
        !            58:        this->keccak->squeeze(this->keccak, out_len, buffer);
        !            59:        return TRUE;
        !            60: }
        !            61: 
        !            62: METHOD(xof_t, allocate_bytes, bool,
        !            63:        private_sha3_shake_t *this, size_t out_len, chunk_t *chunk)
        !            64: {
        !            65:        *chunk = chunk_alloc(out_len);
        !            66:        this->keccak->squeeze(this->keccak, out_len, chunk->ptr);
        !            67:        return TRUE;
        !            68: }
        !            69: 
        !            70: METHOD(xof_t, get_block_size, size_t,
        !            71:        private_sha3_shake_t *this)
        !            72: {
        !            73:        return this->keccak->get_rate(this->keccak);
        !            74: }
        !            75: 
        !            76: METHOD(xof_t, get_seed_size, size_t,
        !            77:        private_sha3_shake_t *this)
        !            78: {
        !            79:        return this->capacity;
        !            80: }
        !            81: 
        !            82: METHOD(xof_t, set_seed, bool,
        !            83:        private_sha3_shake_t *this, chunk_t seed)
        !            84: {
        !            85:        this->keccak->reset(this->keccak);
        !            86:        this->keccak->absorb(this->keccak, seed);
        !            87:        this->keccak->finalize(this->keccak);
        !            88:        return TRUE;
        !            89: }
        !            90: 
        !            91: 
        !            92: METHOD(xof_t, destroy, void,
        !            93:        private_sha3_shake_t *this)
        !            94: {
        !            95:        this->keccak->destroy(this->keccak);
        !            96:        free(this);
        !            97: }
        !            98: 
        !            99: /*
        !           100:  * Described in header.
        !           101:  */
        !           102: sha3_shake_t* sha3_shake_create(ext_out_function_t algorithm)
        !           103: {
        !           104:        private_sha3_shake_t *this;
        !           105:        u_int capacity = 0;
        !           106: 
        !           107:        switch (algorithm)
        !           108:        {
        !           109:                case XOF_SHAKE_128:
        !           110:                        capacity = 32;
        !           111:                        break;
        !           112:                case XOF_SHAKE_256:
        !           113:                        capacity = 64;
        !           114:                        break;
        !           115:                default:
        !           116:                        return NULL;
        !           117:        }
        !           118: 
        !           119:        INIT(this,
        !           120:                .public = {
        !           121:                        .xof_interface = {
        !           122:                                .get_type = _get_type,
        !           123:                                .get_bytes = _get_bytes,
        !           124:                                .allocate_bytes = _allocate_bytes,
        !           125:                                .get_block_size = _get_block_size,
        !           126:                                .get_seed_size = _get_seed_size,
        !           127:                                .set_seed = _set_seed,
        !           128:                                .destroy = _destroy,
        !           129:                        },
        !           130:                },
        !           131:                .algorithm = algorithm,
        !           132:                .capacity = capacity,
        !           133:        );
        !           134: 
        !           135:        this->keccak = sha3_keccak_create(capacity, 0x1f);
        !           136:        if (!this->keccak)
        !           137:        {
        !           138:                free(this);
        !           139:                return NULL;
        !           140:        }
        !           141: 
        !           142:        return &this->public;
        !           143: }

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