2 * Copyright (C) 2019 Sean Parkinson, wolfSSL Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 #include <wolfssl_common.h>
28 #include <utils/debug.h>
30 #include <wolfssl/wolfcrypt/random.h>
32 #include "wolfssl_rng.h"
34 typedef struct private_wolfssl_rng_t private_wolfssl_rng_t
;
36 #ifndef SINGLE_THREADED
37 wolfSSL_Mutex globalRngMutex
;
39 static WC_RNG globalRng
;
40 static int globalRngInit
= 0;
43 * Private data of wolfssl_rng_t
45 struct private_wolfssl_rng_t
{
48 * Public part of this class.
53 * Random number generator to use
54 * Either own instance or reference to global.
59 METHOD(rng_t
, get_bytes
, bool,
60 private_wolfssl_rng_t
*this, size_t bytes
, uint8_t *buffer
)
64 #ifndef SINGLE_THREADED
65 if (this->rng
== &globalRng
)
67 ret
= wc_LockMutex(&globalRngMutex
);
70 DBG1(DBG_LIB
, "Locking failed, get bytes failed");
75 ret
= wc_RNG_GenerateBlock(this->rng
, buffer
, bytes
);
76 #ifndef SINGLE_THREADED
77 if (this->rng
== &globalRng
)
79 wc_UnLockMutex(&globalRngMutex
);
86 METHOD(rng_t
, allocate_bytes
, bool,
87 private_wolfssl_rng_t
*this, size_t bytes
, chunk_t
*chunk
)
89 *chunk
= chunk_alloc(bytes
);
90 if (!get_bytes(this, chunk
->len
, chunk
->ptr
))
98 METHOD(rng_t
, destroy
, void,
99 private_wolfssl_rng_t
*this)
101 if (this->rng
!= &globalRng
)
103 wc_FreeRng(this->rng
);
110 * Described in header.
112 wolfssl_rng_t
*wolfssl_rng_create(rng_quality_t quality
)
114 private_wolfssl_rng_t
*this;
119 .get_bytes
= _get_bytes
,
120 .allocate_bytes
= _allocate_bytes
,
127 if (quality
> RNG_WEAK
)
129 this->rng
= malloc(sizeof(*this->rng
));
130 if (wc_InitRng(this->rng
) != 0)
132 DBG1(DBG_LIB
, "Init RNG failed, rng create failed");
138 return &this->public;
142 * Described in header.
144 int wolfssl_rng_global_init()
150 ret
= wc_InitRng(&globalRng
);
153 DBG1(DBG_LIB
, "Init RNG failed, rng global init failed");
155 #ifndef SINGLE_THREADED
156 else if ((ret
= wc_InitMutex(&globalRngMutex
)) != 0)
158 DBG1(DBG_LIB
, "Init Mutex failed, rng global init failed");
171 * Described in header.
173 void wolfssl_rng_global_final()
177 #ifndef SINGLE_THREADED
178 wc_FreeMutex(&globalRngMutex
);
180 wc_FreeRng(&globalRng
);
185 #endif /* WC_NO_RNG */