4 * @brief Implementation of randomizer_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
24 #include <sys/types.h>
29 #include "randomizer.h"
31 #include <utils/allocator.h>
33 typedef struct private_randomizer_t private_randomizer_t
;
36 * Private data of an randomizer_t object
38 struct private_randomizer_t
{
45 * @brief Reads a specific number of bytes from random or pseudo random device.
47 * @param this calling object
48 * @param pseudo_random TRUE, if pseudo random bytes should be read,
49 * FALSE for true random bytes
50 * @param bytes number of bytes to read
51 * @param[out] buffer pointer to buffer where to write the data in.
52 * Size of buffer has to be at least bytes.
55 * - FAILED if random device could not be opened
57 status_t (*get_bytes_from_device
) (private_randomizer_t
*this,bool pseudo_random
, size_t bytes
, u_int8_t
*buffer
);
62 char *random_dev_name
;
65 * Pseudo random device name.
67 char *pseudo_random_dev_name
;
72 * Implementation of private_randomizer_t.get_bytes_from_device.
74 static status_t
get_bytes_from_device(private_randomizer_t
*this,bool pseudo_random
, size_t bytes
, u_int8_t
*buffer
)
76 /* number of bytes already done */
78 /* device file descriptor */
83 device_name
= (pseudo_random
) ?
this->pseudo_random_dev_name
: this->random_dev_name
;
86 device
= open(device_name
, 0);
92 // read until nbytes are read
95 got
= read(device
, buffer
+ ndone
, bytes
- ndone
);
110 * Implementation of randomizer_t.get_random_bytes.
112 static status_t
get_random_bytes(private_randomizer_t
*this,size_t bytes
, u_int8_t
*buffer
)
114 return (this->get_bytes_from_device(this, FALSE
, bytes
, buffer
));
118 * Implementation of randomizer_t.allocate_random_bytes.
120 static status_t
allocate_random_bytes(private_randomizer_t
*this, size_t bytes
, chunk_t
*chunk
)
123 chunk
->ptr
= allocator_alloc(bytes
);
124 if (chunk
->ptr
== NULL
)
128 return (this->get_bytes_from_device(this, FALSE
, bytes
, chunk
->ptr
));
132 * Implementation of randomizer_t.get_pseudo_random_bytes.
134 static status_t
get_pseudo_random_bytes(private_randomizer_t
*this,size_t bytes
, u_int8_t
*buffer
)
136 return (this->get_bytes_from_device(this, TRUE
, bytes
, buffer
));
141 * Implementation of randomizer_t.allocate_pseudo_random_bytes.
143 static status_t
allocate_pseudo_random_bytes(private_randomizer_t
*this, size_t bytes
, chunk_t
*chunk
)
146 chunk
->ptr
= allocator_alloc(bytes
);
147 if (chunk
->ptr
== NULL
)
151 return (this->get_bytes_from_device(this, TRUE
, bytes
, chunk
->ptr
));
156 * Implementation of randomizer_t.destroy.
158 static status_t
destroy(private_randomizer_t
*this)
160 allocator_free(this->random_dev_name
);
161 allocator_free(this->pseudo_random_dev_name
);
162 allocator_free(this);
168 * Described in header.
170 randomizer_t
*randomizer_create(void)
172 return randomizer_create_on_devices(DEFAULT_RANDOM_DEVICE
,DEFAULT_PSEUDO_RANDOM_DEVICE
);
176 * Described in header.
178 randomizer_t
*randomizer_create_on_devices(char * random_dev_name
,char * prandom_dev_name
)
180 private_randomizer_t
*this = allocator_alloc_thing(private_randomizer_t
);
185 if ((random_dev_name
== NULL
) || (prandom_dev_name
== NULL
))
190 /* public functions */
191 this->public.get_random_bytes
= (status_t (*) (randomizer_t
*,size_t, u_int8_t
*)) get_random_bytes
;
192 this->public.allocate_random_bytes
= (status_t (*) (randomizer_t
*,size_t, chunk_t
*)) allocate_random_bytes
;
193 this->public.get_pseudo_random_bytes
= (status_t (*) (randomizer_t
*,size_t, u_int8_t
*)) get_pseudo_random_bytes
;
194 this->public.allocate_pseudo_random_bytes
= (status_t (*) (randomizer_t
*,size_t, chunk_t
*)) allocate_pseudo_random_bytes
;
195 this->public.destroy
= (status_t (*) (randomizer_t
*))destroy
;
197 /* private functions */
198 this->get_bytes_from_device
= get_bytes_from_device
;
201 this->random_dev_name
= allocator_alloc(strlen(random_dev_name
) + 1);
202 if (this->random_dev_name
== NULL
)
204 allocator_free(this);
207 strcpy(this->random_dev_name
,random_dev_name
);
209 this->pseudo_random_dev_name
= allocator_alloc(strlen(prandom_dev_name
) + 1);
210 if (this->pseudo_random_dev_name
== NULL
)
212 allocator_free(this->random_dev_name
);
213 allocator_free(this);
216 strcpy(this->pseudo_random_dev_name
,prandom_dev_name
);
218 return &(this->public);