- changed allocation behavior
[strongswan.git] / Source / charon / utils / randomizer.h
1 /**
2 * @file randomizer.h
3 *
4 * @brief Interface of randomizer_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #ifndef RANDOMIZER_H_
24 #define RANDOMIZER_H_
25
26 #include <types.h>
27
28 typedef struct randomizer_t randomizer_t;
29
30 /**
31 * @brief Class used to get random and pseudo random values.
32 *
33 * This class is thread save as file system read calls are thread save.
34 *
35 * @todo fix FEHLVERHALTEN
36 *
37 * @ingroup utils
38 */
39 struct randomizer_t {
40
41 /**
42 * @brief Reads a specific number of bytes from random device.
43 *
44 * @param this calling randomizer_t object
45 * @param bytes number of bytes to read
46 * @param[out] buffer pointer to buffer where to write the data in.
47 * Size of buffer has to be at least bytes.
48 * @return
49 * - SUCCESS
50 * - FAILED if random device could not be opened
51 */
52 status_t (*get_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
53
54 /**
55 * @brief Allocates space and writes in random bytes.
56 *
57 * @param this calling randomizer_t object
58 * @param bytes number of bytes to allocate
59 * @param[out] chunk chunk which will hold the allocated random bytes
60 * @return
61 * - SUCCESS
62 * - FAILED if random device could not be opened
63 */
64 status_t (*allocate_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
65
66 /**
67 * @brief Reads a specific number of bytes from pseudo random device.
68 *
69 * @param this calling randomizer_t object
70 * @param bytes number of bytes to read
71 * @param[out] buffer pointer to buffer where to write the data in.
72 * size of buffer has to be at least bytes.
73 * @return
74 * - SUCCESS
75 * - FAILED if random device could not be opened
76 */
77 status_t (*get_pseudo_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
78
79 /**
80 * @brief Allocates space and writes in pseudo random bytes.
81 *
82 * @param this calling randomizer_t object
83 * @param bytes number of bytes to allocate
84 * @param[out] chunk chunk which will hold the allocated random bytes
85 * @return
86 * - SUCCESS
87 * - FAILED if random device could not be opened
88 */
89 status_t (*allocate_pseudo_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
90
91 /**
92 * @brief Destroys a randomizer_t object.
93 *
94 * @param this randomizer_t object to destroy
95 */
96 void (*destroy) (randomizer_t *this);
97 };
98
99 /**
100 * @brief Creates a randomizer_t object
101 *
102 * @return
103 * - created randomizer_t, or
104 * - NULL if failed
105 *
106 * @ingroup utils
107 */
108 randomizer_t *randomizer_create();
109
110 /**
111 * @brief Creates an randomizer_t object with specific random device names.
112 *
113 * @param random_dev_name device name for random values, etc /dev/random
114 * @param prandom_dev_name device name for pseudo random values, etc /dev/urandom
115 * @return
116 * - created randomizer_t
117 * - NULL if failed
118 *
119 * @ingroup utils
120 */
121 randomizer_t *randomizer_create_on_devices(char * random_dev_name,char * prandom_dev_name);
122
123 #endif /*RANDOMIZER_H_*/