Annotation of embedaddon/strongswan/src/libstrongswan/plugins/botan/botan_rng.c, revision 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_rng.h"
        !            25: 
        !            26: #include <botan/build.h>
        !            27: 
        !            28: #ifdef BOTAN_HAS_HMAC_DRBG
        !            29: 
        !            30: #include <botan/ffi.h>
        !            31: 
        !            32: typedef struct private_botan_random_t private_botan_random_t;
        !            33: 
        !            34: /**
        !            35:  * Private data of an botan_rng_t object.
        !            36:  */
        !            37: struct private_botan_random_t {
        !            38: 
        !            39:        /**
        !            40:         * Public botan_rnd_t interface.
        !            41:         */
        !            42:        botan_random_t public;
        !            43: 
        !            44:        /**
        !            45:         * RNG quality of this instance
        !            46:         */
        !            47:        rng_quality_t quality;
        !            48: 
        !            49:        /**
        !            50:         * RNG instance
        !            51:         */
        !            52:        botan_rng_t rng;
        !            53: };
        !            54: 
        !            55: METHOD(rng_t, get_bytes, bool,
        !            56:        private_botan_random_t *this, size_t bytes, uint8_t *buffer)
        !            57: {
        !            58:        return botan_rng_get(this->rng, buffer, bytes) == 0;
        !            59: }
        !            60: 
        !            61: METHOD(rng_t, allocate_bytes, bool,
        !            62:        private_botan_random_t *this, size_t bytes, chunk_t *chunk)
        !            63: {
        !            64:        *chunk = chunk_alloc(bytes);
        !            65:        if (!get_bytes(this, chunk->len, chunk->ptr))
        !            66:        {
        !            67:                chunk_free(chunk);
        !            68:                return FALSE;
        !            69:        }
        !            70:        return TRUE;
        !            71: }
        !            72: 
        !            73: METHOD(rng_t, destroy, void,
        !            74:        private_botan_random_t *this)
        !            75: {
        !            76:        botan_rng_destroy(this->rng);
        !            77:        free(this);
        !            78: }
        !            79: 
        !            80: /*
        !            81:  * Described in header
        !            82:  */
        !            83: botan_random_t *botan_rng_create(rng_quality_t quality)
        !            84: {
        !            85:        private_botan_random_t *this;
        !            86:        const char* rng_name;
        !            87: 
        !            88:        switch (quality)
        !            89:        {
        !            90:                case RNG_WEAK:
        !            91:                case RNG_STRONG:
        !            92:                        /* some rng_t instances of this class (e.g. in the ike-sa-manager)
        !            93:                         * may be called concurrently by different threads. the Botan RNGs
        !            94:                         * are not reentrant, by default, so use the threadsafe version.
        !            95:                         * because we build without threading support when running tests
        !            96:                         * with leak-detective (lots of reports of frees of unknown memory)
        !            97:                         * there is a fallback to the default */
        !            98: #ifdef BOTAN_TARGET_OS_HAS_THREADS
        !            99:                        rng_name = "user-threadsafe";
        !           100: #else
        !           101:                        rng_name = "user";
        !           102: #endif
        !           103:                        break;
        !           104:                case RNG_TRUE:
        !           105:                        rng_name = "system";
        !           106:                        break;
        !           107:                default:
        !           108:                        return NULL;
        !           109:        }
        !           110: 
        !           111:        INIT(this,
        !           112:                .public = {
        !           113:                        .rng = {
        !           114:                                .get_bytes = _get_bytes,
        !           115:                                .allocate_bytes = _allocate_bytes,
        !           116:                                .destroy = _destroy,
        !           117:                        },
        !           118:                },
        !           119:                .quality = quality,
        !           120:        );
        !           121: 
        !           122:        if (botan_rng_init(&this->rng, rng_name))
        !           123:        {
        !           124:                free(this);
        !           125:                return NULL;
        !           126:        }
        !           127:        return &this->public;
        !           128: }
        !           129: 
        !           130: #endif

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