Annotation of embedaddon/strongswan/src/libstrongswan/crypto/signers/mac_signer.c, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2012 Tobias Brunner
        !             3:  * Copyright (C) 2005-2008 Martin Willi
        !             4:  * Copyright (C) 2005 Jan Hutter
        !             5:  * HSR Hochschule fuer Technik Rapperswil
        !             6:  *
        !             7:  * This program is free software; you can redistribute it and/or modify it
        !             8:  * under the terms of the GNU General Public License as published by the
        !             9:  * Free Software Foundation; either version 2 of the License, or (at your
        !            10:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !            11:  *
        !            12:  * This program is distributed in the hope that it will be useful, but
        !            13:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            14:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            15:  * for more details.
        !            16:  */
        !            17: 
        !            18: #include "mac_signer.h"
        !            19: 
        !            20: typedef struct private_signer_t private_signer_t;
        !            21: 
        !            22: /**
        !            23:  * Private data of a mac_signer_t object.
        !            24:  */
        !            25: struct private_signer_t {
        !            26: 
        !            27:        /**
        !            28:         * Public interface
        !            29:         */
        !            30:        signer_t public;
        !            31: 
        !            32:        /**
        !            33:         * MAC to use
        !            34:         */
        !            35:        mac_t *mac;
        !            36: 
        !            37:        /**
        !            38:         * Truncation of MAC output
        !            39:         */
        !            40:        size_t truncation;
        !            41: };
        !            42: 
        !            43: METHOD(signer_t, get_signature, bool,
        !            44:        private_signer_t *this, chunk_t data, uint8_t *buffer)
        !            45: {
        !            46:        if (buffer)
        !            47:        {
        !            48:                uint8_t mac[this->mac->get_mac_size(this->mac)];
        !            49: 
        !            50:                if (!this->mac->get_mac(this->mac, data, mac))
        !            51:                {
        !            52:                        return FALSE;
        !            53:                }
        !            54:                memcpy(buffer, mac, this->truncation);
        !            55:                return TRUE;
        !            56:        }
        !            57:        return this->mac->get_mac(this->mac, data, NULL);
        !            58: }
        !            59: 
        !            60: METHOD(signer_t, allocate_signature, bool,
        !            61:        private_signer_t *this, chunk_t data, chunk_t *chunk)
        !            62: {
        !            63:        if (chunk)
        !            64:        {
        !            65:                uint8_t mac[this->mac->get_mac_size(this->mac)];
        !            66: 
        !            67:                if (!this->mac->get_mac(this->mac, data, mac))
        !            68:                {
        !            69:                        return FALSE;
        !            70:                }
        !            71:                *chunk = chunk_alloc(this->truncation);
        !            72:                memcpy(chunk->ptr, mac, this->truncation);
        !            73:                return TRUE;
        !            74:        }
        !            75:        return this->mac->get_mac(this->mac, data, NULL);
        !            76: }
        !            77: 
        !            78: METHOD(signer_t, verify_signature, bool,
        !            79:        private_signer_t *this, chunk_t data, chunk_t signature)
        !            80: {
        !            81:        uint8_t mac[this->mac->get_mac_size(this->mac)];
        !            82: 
        !            83:        if (signature.len != this->truncation)
        !            84:        {
        !            85:                return FALSE;
        !            86:        }
        !            87:        return this->mac->get_mac(this->mac, data, mac) &&
        !            88:                   memeq_const(signature.ptr, mac, this->truncation);
        !            89: }
        !            90: 
        !            91: METHOD(signer_t, get_key_size, size_t,
        !            92:        private_signer_t *this)
        !            93: {
        !            94:        return this->mac->get_mac_size(this->mac);
        !            95: }
        !            96: 
        !            97: METHOD(signer_t, get_block_size, size_t,
        !            98:        private_signer_t *this)
        !            99: {
        !           100:        return this->truncation;
        !           101: }
        !           102: 
        !           103: METHOD(signer_t, set_key, bool,
        !           104:        private_signer_t *this, chunk_t key)
        !           105: {
        !           106:        return this->mac->set_key(this->mac, key);
        !           107: }
        !           108: 
        !           109: METHOD(signer_t, destroy, void,
        !           110:        private_signer_t *this)
        !           111: {
        !           112:        this->mac->destroy(this->mac);
        !           113:        free(this);
        !           114: }
        !           115: 
        !           116: /*
        !           117:  * Described in header
        !           118:  */
        !           119: signer_t *mac_signer_create(mac_t *mac, size_t len)
        !           120: {
        !           121:        private_signer_t *this;
        !           122: 
        !           123:        INIT(this,
        !           124:                .public = {
        !           125:                        .get_signature = _get_signature,
        !           126:                        .allocate_signature = _allocate_signature,
        !           127:                        .verify_signature = _verify_signature,
        !           128:                        .get_block_size = _get_block_size,
        !           129:                        .get_key_size = _get_key_size,
        !           130:                        .set_key = _set_key,
        !           131:                        .destroy = _destroy,
        !           132:                },
        !           133:                .truncation = min(len, mac->get_mac_size(mac)),
        !           134:                .mac = mac,
        !           135:        );
        !           136: 
        !           137:        return &this->public;
        !           138: }

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