a74cab42f828f14758e94c8bff04a4de1a5eaa4e
[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
29 /**
30 * Default random device used when no device is given.
31 */
32 #define DEFAULT_RANDOM_DEVICE "/dev/random"
33
34 /**
35 * Pseudo random device used when no device is given.
36 */
37 #define DEFAULT_PSEUDO_RANDOM_DEVICE "/dev/urandom"
38
39 typedef struct randomizer_t randomizer_t;
40
41 /**
42 * @brief Class used to get random and pseudo random values.
43 *
44 * This class is thread save as file system read calls are thread save.
45 *
46 * @todo fix FEHLVERHALTEN
47 *
48 * @ingroup utils
49 */
50 struct randomizer_t {
51
52 /**
53 * @brief Reads a specific number of bytes from random device.
54 *
55 * @param this calling randomizer_t object
56 * @param bytes number of bytes to read
57 * @param[out] buffer pointer to buffer where to write the data in.
58 * Size of buffer has to be at least bytes.
59 */
60 void (*get_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
61
62 /**
63 * @brief Allocates space and writes in random bytes.
64 *
65 * @param this calling randomizer_t object
66 * @param bytes number of bytes to allocate
67 * @param[out] chunk chunk which will hold the allocated random bytes
68 */
69 void (*allocate_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
70
71 /**
72 * @brief Reads a specific number of bytes from pseudo random device.
73 *
74 * @param this calling randomizer_t object
75 * @param bytes number of bytes to read
76 * @param[out] buffer pointer to buffer where to write the data in.
77 * size of buffer has to be at least bytes.
78 */
79 void (*get_pseudo_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
80
81 /**
82 * @brief Allocates space and writes in pseudo random bytes.
83 *
84 * @param this calling randomizer_t object
85 * @param bytes number of bytes to allocate
86 * @param[out] chunk chunk which will hold the allocated random bytes
87 */
88 void (*allocate_pseudo_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
89
90 /**
91 * @brief Destroys a randomizer_t object.
92 *
93 * @param this randomizer_t object to destroy
94 */
95 void (*destroy) (randomizer_t *this);
96 };
97
98 /**
99 * @brief Creates a randomizer_t object
100 *
101 * @return
102 * - created randomizer_t, or
103 * - NULL if failed
104 *
105 * @ingroup utils
106 */
107 randomizer_t *randomizer_create();
108
109 /**
110 * @brief Creates an randomizer_t object with specific random device names.
111 *
112 * @param random_dev_name device name for random values, etc /dev/random
113 * @param prandom_dev_name device name for pseudo random values, etc /dev/urandom
114 * @return randomizer_t object
115 *
116 * @ingroup utils
117 */
118 randomizer_t *randomizer_create_on_devices(char * random_dev_name,char * prandom_dev_name);
119
120 #endif /*RANDOMIZER_H_*/