Annotation of embedaddon/strongswan/src/libstrongswan/plugins/botan/botan_hmac.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_hmac.h"
                     25: 
                     26: #include <botan/build.h>
                     27: 
                     28: #ifdef BOTAN_HAS_HMAC
                     29: 
                     30: #include <crypto/mac.h>
                     31: #include <crypto/prfs/mac_prf.h>
                     32: #include <crypto/signers/mac_signer.h>
                     33: 
                     34: #include <botan/ffi.h>
                     35: 
                     36: typedef struct private_botan_mac_t private_botan_mac_t;
                     37: 
                     38: /**
                     39:  * Private data of a mac_t object.
                     40:  */
                     41: struct private_botan_mac_t {
                     42: 
                     43:        /**
                     44:         * Public interface
                     45:         */
                     46:        mac_t public;
                     47: 
                     48:        /**
                     49:         * HMAC
                     50:         */
                     51:        botan_mac_t hmac;
                     52: };
                     53: 
                     54: METHOD(mac_t, set_key, bool,
                     55:        private_botan_mac_t *this, chunk_t key)
                     56: {
                     57:        if (botan_mac_set_key(this->hmac, key.ptr, key.len))
                     58:        {
                     59:                return FALSE;
                     60:        }
                     61:        return TRUE;
                     62: }
                     63: 
                     64: METHOD(mac_t, get_mac, bool,
                     65:        private_botan_mac_t *this, chunk_t data, uint8_t *out)
                     66: {
                     67:        if (botan_mac_update(this->hmac, data.ptr, data.len))
                     68:        {
                     69:                return FALSE;
                     70:        }
                     71: 
                     72:        if (out && botan_mac_final(this->hmac, out))
                     73:        {
                     74:                return FALSE;
                     75:        }
                     76:        return TRUE;
                     77: }
                     78: 
                     79: METHOD(mac_t, get_mac_size, size_t,
                     80:        private_botan_mac_t *this)
                     81: {
                     82:        size_t len = 0;
                     83: 
                     84:        if (botan_mac_output_length(this->hmac, &len))
                     85:        {
                     86:                return 0;
                     87:        }
                     88:        return len;
                     89: }
                     90: 
                     91: METHOD(mac_t, destroy, void,
                     92:        private_botan_mac_t *this)
                     93: {
                     94:        botan_mac_destroy(this->hmac);
                     95:        free(this);
                     96: }
                     97: 
                     98: /*
                     99:  * Create a Botan-backed implementation of the mac_t interface
                    100:  */
                    101: static mac_t *hmac_create(hash_algorithm_t algo)
                    102: {
                    103:        private_botan_mac_t *this;
                    104:        const char* hmac_name;
                    105: 
                    106:        switch (algo)
                    107:        {
                    108:                case HASH_SHA1:
                    109:                        hmac_name = "HMAC(SHA-1)";
                    110:                        break;
                    111:                case HASH_SHA256:
                    112:                        hmac_name = "HMAC(SHA-256)";
                    113:                        break;
                    114:                case HASH_SHA384:
                    115:                        hmac_name = "HMAC(SHA-384)";
                    116:                        break;
                    117:                case HASH_SHA512:
                    118:                        hmac_name = "HMAC(SHA-512)";
                    119:                        break;
                    120:                default:
                    121:                        return NULL;
                    122:        }
                    123: 
                    124:        INIT(this,
                    125:                .public = {
                    126:                        .get_mac = _get_mac,
                    127:                        .get_mac_size = _get_mac_size,
                    128:                        .set_key = _set_key,
                    129:                        .destroy = _destroy,
                    130:                }
                    131:        );
                    132: 
                    133:        if (botan_mac_init(&this->hmac, hmac_name, 0))
                    134:        {
                    135:                free(this);
                    136:                return NULL;
                    137:        }
                    138:        return &this->public;
                    139: }
                    140: 
                    141: /*
                    142:  * Described in header
                    143:  */
                    144: prf_t *botan_hmac_prf_create(pseudo_random_function_t algo)
                    145: {
                    146:        mac_t *hmac;
                    147: 
                    148:        hmac = hmac_create(hasher_algorithm_from_prf(algo));
                    149:        if (hmac)
                    150:        {
                    151:                return mac_prf_create(hmac);
                    152:        }
                    153:        return NULL;
                    154: }
                    155: 
                    156: /*
                    157:  * Described in header
                    158:  */
                    159: signer_t *botan_hmac_signer_create(integrity_algorithm_t algo)
                    160: {
                    161:        mac_t *hmac;
                    162:        size_t trunc;
                    163: 
                    164:        hmac = hmac_create(hasher_algorithm_from_integrity(algo, &trunc));
                    165:        if (hmac)
                    166:        {
                    167:                return mac_signer_create(hmac, trunc);
                    168:        }
                    169:        return NULL;
                    170: }
                    171: 
                    172: #endif

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