Annotation of embedaddon/strongswan/src/libstrongswan/plugins/botan/botan_hasher.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2018 RenĂ© Korthaus
                      3:  * Rohde & Schwarz Cybersecurity GmbH
                      4:  *
                      5:  * Permission is hereby granted, free of charge, to any person obtaining a copy
                      6:  * of this software and associated documentation files (the "Software"), to deal
                      7:  * in the Software without restriction, including without limitation the rights
                      8:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
                      9:  * copies of the Software, and to permit persons to whom the Software is
                     10:  * furnished to do so, subject to the following conditions:
                     11:  *
                     12:  * The above copyright notice and this permission notice shall be included in
                     13:  * all copies or substantial portions of the Software.
                     14:  *
                     15:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
                     16:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
                     17:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
                     18:  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
                     19:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
                     20:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
                     21:  * THE SOFTWARE.
                     22:  */
                     23: 
                     24: #include "botan_hasher.h"
                     25: #include "botan_util.h"
                     26: 
                     27: #include <utils/debug.h>
                     28: 
                     29: #include <botan/ffi.h>
                     30: 
                     31: typedef struct private_botan_hasher_t private_botan_hasher_t;
                     32: 
                     33: /**
                     34:  * Private data of botan_hasher_t
                     35:  */
                     36: struct private_botan_hasher_t {
                     37: 
                     38:        /**
                     39:         * Public part of this class.
                     40:         */
                     41:        botan_hasher_t public;
                     42: 
                     43:        /**
                     44:         * botan hash instance
                     45:         */
                     46:        botan_hash_t hash;
                     47: };
                     48: 
                     49: METHOD(hasher_t, get_hash_size, size_t,
                     50:        private_botan_hasher_t *this)
                     51: {
                     52:        size_t len = 0;
                     53: 
                     54:        if (botan_hash_output_length(this->hash, &len))
                     55:        {
                     56:                return 0;
                     57:        }
                     58:        return len;
                     59: }
                     60: 
                     61: METHOD(hasher_t, reset, bool,
                     62:        private_botan_hasher_t *this)
                     63: {
                     64:        if (botan_hash_clear(this->hash))
                     65:        {
                     66:                return FALSE;
                     67:        }
                     68:        return TRUE;
                     69: }
                     70: 
                     71: METHOD(hasher_t, get_hash, bool,
                     72:        private_botan_hasher_t *this, chunk_t chunk, uint8_t *hash)
                     73: {
                     74:        if (botan_hash_update(this->hash, chunk.ptr, chunk.len))
                     75:        {
                     76:                return FALSE;
                     77:        }
                     78: 
                     79:        if (hash && botan_hash_final(this->hash, hash))
                     80:        {
                     81:                return FALSE;
                     82:        }
                     83:        return TRUE;
                     84: }
                     85: 
                     86: METHOD(hasher_t, allocate_hash, bool,
                     87:        private_botan_hasher_t *this, chunk_t chunk, chunk_t *hash)
                     88: {
                     89:        if (hash)
                     90:        {
                     91:                *hash = chunk_alloc(get_hash_size(this));
                     92:                return get_hash(this, chunk, hash->ptr);
                     93:        }
                     94:        return get_hash(this, chunk, NULL);
                     95: }
                     96: 
                     97: METHOD(hasher_t, destroy, void,
                     98:        private_botan_hasher_t *this)
                     99: {
                    100:        botan_hash_destroy(this->hash);
                    101:        free(this);
                    102: }
                    103: 
                    104: /*
                    105:  * Described in header
                    106:  */
                    107: botan_hasher_t *botan_hasher_create(hash_algorithm_t algo)
                    108: {
                    109:        private_botan_hasher_t *this;
                    110:        const char* hash_name;
                    111: 
                    112:        hash_name = botan_get_hash(algo);
                    113:        if (!hash_name)
                    114:        {
                    115:                return FALSE;
                    116:        }
                    117: 
                    118:        INIT(this,
                    119:                .public = {
                    120:                        .hasher = {
                    121:                                .get_hash = _get_hash,
                    122:                                .allocate_hash = _allocate_hash,
                    123:                                .get_hash_size = _get_hash_size,
                    124:                                .reset = _reset,
                    125:                                .destroy = _destroy,
                    126:                        },
                    127:                },
                    128:        );
                    129: 
                    130:        if (botan_hash_init(&this->hash, hash_name, 0))
                    131:        {
                    132:                free(this);
                    133:                return NULL;
                    134:        }
                    135:        return &this->public;
                    136: }

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