Annotation of embedaddon/strongswan/src/libstrongswan/plugins/sha3/sha3_hasher.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2015-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 <string.h>
                     17: 
                     18: #include "sha3_hasher.h"
                     19: #include "sha3_keccak.h"
                     20: 
                     21: typedef struct private_sha3_hasher_t private_sha3_hasher_t;
                     22: 
                     23: /**
                     24:  * Private data structure with hashing context for SHA-3
                     25:  */
                     26: struct private_sha3_hasher_t {
                     27: 
                     28:        /**
                     29:         * Public interface for this hasher.
                     30:         */
                     31:        sha3_hasher_t public;
                     32: 
                     33:        /**
                     34:         * SHA-3 algorithm to be used
                     35:         */
                     36:        hash_algorithm_t algorithm;
                     37: 
                     38:        /**
                     39:         * SHA-3 Keccak state
                     40:         */
                     41:        sha3_keccak_t *keccak;
                     42: 
                     43: };
                     44: 
                     45: METHOD(hasher_t, reset, bool,
                     46:        private_sha3_hasher_t *this)
                     47: {
                     48:        this->keccak->reset(this->keccak);
                     49:        return TRUE;
                     50: }
                     51: 
                     52: METHOD(hasher_t, get_hash_size, size_t,
                     53:        private_sha3_hasher_t *this)
                     54: {
                     55:        switch (this->algorithm)
                     56:        {
                     57:                case HASH_SHA3_224:
                     58:                        return HASH_SIZE_SHA224;
                     59:                case HASH_SHA3_256:
                     60:                        return HASH_SIZE_SHA256;
                     61:                case HASH_SHA3_384:
                     62:                        return HASH_SIZE_SHA384;
                     63:                case HASH_SHA3_512:
                     64:                        return HASH_SIZE_SHA512;
                     65:                default:
                     66:                        return 0;
                     67:        }
                     68: }
                     69: 
                     70: 
                     71: METHOD(hasher_t, get_hash, bool,
                     72:        private_sha3_hasher_t *this, chunk_t chunk, uint8_t *buffer)
                     73: {
                     74:        this->keccak->absorb(this->keccak, chunk);
                     75: 
                     76:        if (buffer != NULL)
                     77:        {
                     78:                this->keccak->finalize(this->keccak);
                     79:                this->keccak->squeeze(this->keccak, get_hash_size(this), buffer);
                     80:                this->keccak->reset(this->keccak);
                     81:        }
                     82:        return TRUE;
                     83: }
                     84: 
                     85: METHOD(hasher_t, allocate_hash, bool,
                     86:        private_sha3_hasher_t *this, chunk_t chunk, chunk_t *hash)
                     87: {
                     88:        chunk_t allocated_hash;
                     89: 
                     90:        this->keccak->absorb(this->keccak, chunk);
                     91: 
                     92:        if (hash != NULL)
                     93:        {
                     94:                this->keccak->finalize(this->keccak);
                     95:                allocated_hash = chunk_alloc(get_hash_size(this));
                     96:                this->keccak->squeeze(this->keccak, allocated_hash.len,
                     97:                                                                                        allocated_hash.ptr);
                     98:                this->keccak->reset(this->keccak);
                     99:                *hash = allocated_hash;
                    100:        }
                    101:        return TRUE;
                    102: }
                    103: 
                    104: METHOD(hasher_t, destroy, void,
                    105:        private_sha3_hasher_t *this)
                    106: {
                    107:        this->keccak->destroy(this->keccak);
                    108:        free(this);
                    109: }
                    110: 
                    111: /*
                    112:  * Described in header.
                    113:  */
                    114: sha3_hasher_t *sha3_hasher_create(hash_algorithm_t algorithm)
                    115: {
                    116:        private_sha3_hasher_t *this;
                    117: 
                    118:        switch (algorithm)
                    119:        {
                    120:                case HASH_SHA3_224:
                    121:                case HASH_SHA3_256:
                    122:                case HASH_SHA3_384:
                    123:                case HASH_SHA3_512:
                    124:                        break;
                    125:                default:
                    126:                        return NULL;
                    127:        }
                    128: 
                    129:        INIT(this,
                    130:                .public = {
                    131:                        .hasher_interface = {
                    132:                                .reset = _reset,
                    133:                                .get_hash_size = _get_hash_size,
                    134:                                .get_hash = _get_hash,
                    135:                                .allocate_hash = _allocate_hash,
                    136:                                .destroy = _destroy,
                    137:                        },
                    138:                },
                    139:                .algorithm = algorithm,
                    140:        );
                    141: 
                    142:        this->keccak = sha3_keccak_create(2*get_hash_size(this), 0x06);
                    143:        if (!this->keccak)
                    144:        {
                    145:                free(this);
                    146:                return NULL;
                    147:        }
                    148: 
                    149:        return &this->public;
                    150: }

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