/src/strongswan/src/libstrongswan/crypto/rngs/rng_tester.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2013 Andreas Steffen |
3 | | * |
4 | | * Copyright (C) secunet Security Networks AG |
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_tester.h" |
18 | | |
19 | | typedef struct private_rng_t private_rng_t; |
20 | | |
21 | | /** |
22 | | * Private data. |
23 | | */ |
24 | | struct private_rng_t { |
25 | | |
26 | | /** |
27 | | * Public interface. |
28 | | */ |
29 | | rng_t public; |
30 | | |
31 | | /** |
32 | | * Entropy string. |
33 | | */ |
34 | | chunk_t entropy; |
35 | | }; |
36 | | |
37 | | METHOD(rng_t, get_bytes, bool, |
38 | | private_rng_t *this, size_t bytes, uint8_t *buffer) |
39 | 0 | { |
40 | 0 | if (bytes > this->entropy.len) |
41 | 0 | { |
42 | 0 | return FALSE; |
43 | 0 | } |
44 | 0 | memcpy(buffer, this->entropy.ptr, bytes); |
45 | 0 | this->entropy = chunk_skip(this->entropy, bytes); |
46 | 0 | return TRUE; |
47 | 0 | } |
48 | | |
49 | | METHOD(rng_t, allocate_bytes, bool, |
50 | | private_rng_t *this, size_t bytes, chunk_t *chunk) |
51 | 0 | { |
52 | 0 | if (bytes > this->entropy.len) |
53 | 0 | { |
54 | 0 | *chunk = chunk_empty; |
55 | 0 | return FALSE; |
56 | 0 | } |
57 | | |
58 | 0 | *chunk = chunk_alloc(bytes); |
59 | 0 | memcpy(chunk->ptr, this->entropy.ptr, bytes); |
60 | 0 | this->entropy = chunk_skip(this->entropy, bytes); |
61 | 0 | return TRUE; |
62 | 0 | } |
63 | | |
64 | | METHOD(rng_t, destroy, void, |
65 | | private_rng_t *this) |
66 | 0 | { |
67 | 0 | free(this); |
68 | 0 | } |
69 | | |
70 | | /* |
71 | | * Described in header. |
72 | | */ |
73 | | rng_t *rng_tester_create(chunk_t entropy) |
74 | 0 | { |
75 | 0 | private_rng_t *this; |
76 | |
|
77 | 0 | INIT(this, |
78 | 0 | .public = { |
79 | 0 | .get_bytes = _get_bytes, |
80 | 0 | .allocate_bytes = _allocate_bytes, |
81 | 0 | .destroy = _destroy, |
82 | 0 | }, |
83 | 0 | .entropy = entropy, |
84 | 0 | ); |
85 | |
|
86 | 0 | return &this->public; |
87 | 0 | } |