Annotation of embedaddon/strongswan/src/libtpmtss/plugins/tpm/tpm_rng.c, revision 1.1.1.1

1.1       misho       1: /*
                      2:  * Copyright (C) 2017 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 "tpm_rng.h"
                     17: 
                     18: #include <tpm_tss.h>
                     19: #include <utils/debug.h>
                     20: 
                     21: typedef struct private_tpm_rng_t private_tpm_rng_t;
                     22: 
                     23: /**
                     24:  * Private data of an tpm_rng_t object.
                     25:  */
                     26: struct private_tpm_rng_t {
                     27: 
                     28:        /**
                     29:         * Public interface.
                     30:         */
                     31:        tpm_rng_t public;
                     32: 
                     33:        /**
                     34:         * Trusted Platform Module
                     35:         */
                     36:        tpm_tss_t *tpm;
                     37: 
                     38: };
                     39: 
                     40: METHOD(rng_t, get_bytes, bool,
                     41:        private_tpm_rng_t *this, size_t bytes, uint8_t *buffer)
                     42: {
                     43:        return this->tpm->get_random(this->tpm, bytes, buffer);
                     44: }
                     45: 
                     46: METHOD(rng_t, allocate_bytes, bool,
                     47:        private_tpm_rng_t *this, size_t bytes, chunk_t *chunk)
                     48: {
                     49:        *chunk = chunk_alloc(bytes);
                     50:        if (!get_bytes(this, chunk->len, chunk->ptr))
                     51:        {
                     52:                chunk_clear(chunk);
                     53:                return FALSE;
                     54:        }
                     55:        return TRUE;
                     56: }
                     57: 
                     58: METHOD(rng_t, destroy, void,
                     59:        private_tpm_rng_t *this)
                     60: {
                     61:        this->tpm->destroy(this->tpm);
                     62:        free(this);
                     63: }
                     64: 
                     65: /*
                     66:  * Described in header.
                     67:  */
                     68: tpm_rng_t *tpm_rng_create(rng_quality_t quality)
                     69: {
                     70:        private_tpm_rng_t *this;
                     71:        tpm_tss_t *tpm;
                     72: 
                     73:        /* try to find a TPM 2.0 */
                     74:        tpm = tpm_tss_probe(TPM_VERSION_2_0);
                     75:        if (!tpm)
                     76:        {
                     77:                DBG1(DBG_LIB, "no TPM 2.0 found");
                     78:                return NULL;
                     79:        }
                     80: 
                     81:        INIT(this,
                     82:                .public = {
                     83:                        .rng = {
                     84:                                .get_bytes = _get_bytes,
                     85:                                .allocate_bytes = _allocate_bytes,
                     86:                                .destroy = _destroy,
                     87:                        },
                     88:                },
                     89:                .tpm = tpm,
                     90:        );
                     91: 
                     92:        return &this->public;
                     93: }
                     94: 

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