Annotation of embedaddon/strongswan/src/libstrongswan/plugins/openssl/openssl_rng.c, revision 1.1.1.1
1.1 misho 1: /*
2: * Copyright (C) 2012-2018 Tobias Brunner
3: * HSR Hochschule fuer Technik Rapperswil
4: *
5: * Copyright (C) 2012 Aleksandr Grinberg
6: *
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:
13: *
14: * The above copyright notice and this permission notice shall be included in
15: * all copies or substantial portions of the Software.
16: *
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
23: * THE SOFTWARE.
24: */
25:
26: #include <library.h>
27: #include <utils/debug.h>
28:
29: #include <openssl/rand.h>
30:
31: #include "openssl_rng.h"
32:
33: typedef struct private_openssl_rng_t private_openssl_rng_t;
34:
35: /**
36: * Private data of openssl_rng_t
37: */
38: struct private_openssl_rng_t {
39:
40: /**
41: * Public part of this class.
42: */
43: openssl_rng_t public;
44:
45: /**
46: * Quality of randomness
47: */
48: rng_quality_t quality;
49: };
50:
51: METHOD(rng_t, get_bytes, bool,
52: private_openssl_rng_t *this, size_t bytes, uint8_t *buffer)
53: {
54: #if OPENSSL_VERSION_NUMBER >= 0x1010100fL
55: if (this->quality > RNG_WEAK)
56: { /* use a separate DRBG for data we want to keep private, compared
57: * to e.g. nonces */
58: return RAND_priv_bytes((char*)buffer, bytes) == 1;
59: }
60: #endif
61: return RAND_bytes((char*)buffer, bytes) == 1;
62: }
63:
64: METHOD(rng_t, allocate_bytes, bool,
65: private_openssl_rng_t *this, size_t bytes, chunk_t *chunk)
66: {
67: *chunk = chunk_alloc(bytes);
68: if (!get_bytes(this, chunk->len, chunk->ptr))
69: {
70: chunk_free(chunk);
71: return FALSE;
72: }
73: return TRUE;
74: }
75:
76: METHOD(rng_t, destroy, void,
77: private_openssl_rng_t *this)
78: {
79: free(this);
80: }
81:
82: /*
83: * Described in header.
84: */
85: openssl_rng_t *openssl_rng_create(rng_quality_t quality)
86: {
87: private_openssl_rng_t *this;
88:
89: INIT(this,
90: .public = {
91: .rng = {
92: .get_bytes = _get_bytes,
93: .allocate_bytes = _allocate_bytes,
94: .destroy = _destroy,
95: },
96: },
97: .quality = quality,
98: );
99:
100: return &this->public;
101: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>