2 * Copyright (C) 2012-2018 Tobias Brunner
3 * HSR Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2012 Aleksandr Grinberg
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 #include <utils/debug.h>
29 #include <openssl/rand.h>
31 #include "openssl_rng.h"
33 typedef struct private_openssl_rng_t private_openssl_rng_t
;
36 * Private data of openssl_rng_t
38 struct private_openssl_rng_t
{
41 * Public part of this class.
46 * Quality of randomness
48 rng_quality_t quality
;
51 METHOD(rng_t
, get_bytes
, bool,
52 private_openssl_rng_t
*this, size_t bytes
, uint8_t *buffer
)
54 #if OPENSSL_VERSION_NUMBER >= 0x1010100fL
55 if (this->quality
> RNG_WEAK
)
56 { /* use a separate DRBG for data we wan't to keep private, compared
58 return RAND_priv_bytes((char*)buffer
, bytes
) == 1;
61 return RAND_bytes((char*)buffer
, bytes
) == 1;
64 METHOD(rng_t
, allocate_bytes
, bool,
65 private_openssl_rng_t
*this, size_t bytes
, chunk_t
*chunk
)
67 *chunk
= chunk_alloc(bytes
);
68 if (!get_bytes(this, chunk
->len
, chunk
->ptr
))
76 METHOD(rng_t
, destroy
, void,
77 private_openssl_rng_t
*this)
83 * Described in header.
85 openssl_rng_t
*openssl_rng_create(rng_quality_t quality
)
87 private_openssl_rng_t
*this;
92 .get_bytes
= _get_bytes
,
93 .allocate_bytes
= _allocate_bytes
,
100 return &this->public;