Annotation of embedaddon/strongswan/src/libstrongswan/crypto/rngs/rng_tester.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2013 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 "rng_tester.h"
                     17: 
                     18: typedef struct private_rng_t private_rng_t;
                     19: 
                     20: /**
                     21:  * Private data.
                     22:  */
                     23: struct private_rng_t {
                     24: 
                     25:        /**
                     26:         * Public interface.
                     27:         */
                     28:        rng_t public;
                     29: 
                     30:        /**
                     31:         * Entropy string.
                     32:         */
                     33:        chunk_t entropy;
                     34: };
                     35: 
                     36: METHOD(rng_t, get_bytes, bool,
                     37:        private_rng_t *this, size_t bytes, uint8_t *buffer)
                     38: {
                     39:        if (bytes > this->entropy.len)
                     40:        {
                     41:                return FALSE;
                     42:        }
                     43:        memcpy(buffer, this->entropy.ptr, bytes);
                     44:        this->entropy = chunk_skip(this->entropy, bytes);
                     45:        return TRUE;
                     46: }
                     47: 
                     48: METHOD(rng_t, allocate_bytes, bool,
                     49:        private_rng_t *this, size_t bytes, chunk_t *chunk)
                     50: {
                     51:        if (bytes > this->entropy.len)
                     52:        {
                     53:                *chunk = chunk_empty;
                     54:                return FALSE;
                     55:        }
                     56: 
                     57:        *chunk = chunk_alloc(bytes);
                     58:        memcpy(chunk->ptr, this->entropy.ptr, bytes);
                     59:        this->entropy = chunk_skip(this->entropy, bytes);
                     60:        return TRUE;
                     61: }
                     62: 
                     63: METHOD(rng_t, destroy, void,
                     64:        private_rng_t *this)
                     65: {
                     66:        free(this);
                     67: }
                     68: 
                     69: /*
                     70:  * Described in header.
                     71:  */
                     72: rng_t *rng_tester_create(chunk_t entropy)
                     73: {
                     74:        private_rng_t *this;
                     75: 
                     76:        INIT(this,
                     77:                .public = {
                     78:                        .get_bytes = _get_bytes,
                     79:                        .allocate_bytes = _allocate_bytes,
                     80:                        .destroy = _destroy,
                     81:                },
                     82:                .entropy = entropy,
                     83:        );
                     84: 
                     85:        return &this->public;
                     86: }

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