Add a return value to hasher_t.get_hash()
[strongswan.git] / src / libstrongswan / crypto / rngs / rng.c
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "rng.h"
18
19 ENUM(rng_quality_names, RNG_WEAK, RNG_TRUE,
20 "RNG_WEAK",
21 "RNG_STRONG",
22 "RNG_TRUE",
23 );
24
25 /*
26 * Described in header.
27 */
28 bool rng_get_bytes_not_zero(rng_t *rng, size_t len, u_int8_t *buffer, bool all)
29 {
30 u_int8_t *pos = buffer, *check = buffer + (all ? len : min(1, len));
31
32 if (!rng->get_bytes(rng, len, pos))
33 {
34 return FALSE;
35 }
36
37 for (; pos < check; pos++)
38 {
39 while (*pos == 0)
40 {
41 if (!rng->get_bytes(rng, 1, pos))
42 {
43 return FALSE;
44 }
45 }
46 }
47 return TRUE;
48 }
49
50 /*
51 * Described in header.
52 */
53 bool rng_allocate_bytes_not_zero(rng_t *rng, size_t len, chunk_t *chunk,
54 bool all)
55 {
56 *chunk = chunk_alloc(len);
57 if (!rng_get_bytes_not_zero(rng, len, chunk->ptr, all))
58 {
59 chunk_clear(chunk);
60 return FALSE;
61 }
62 return TRUE;
63 }