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