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"
32 typedef struct private_randomizer_t private_randomizer_t
;
35 * Private data of an randomizer_t object.
37 struct private_randomizer_t
{
40 * Public randomizer_t interface.
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 from 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.
54 status_t (*get_bytes_from_device
) (private_randomizer_t
*this,bool pseudo_random
, size_t bytes
, u_int8_t
*buffer
);
59 * Implementation of private_randomizer_t.get_bytes_from_device.
61 static status_t
get_bytes_from_device(private_randomizer_t
*this,bool pseudo_random
, size_t bytes
, u_int8_t
*buffer
)
68 device_name
= pseudo_random
? PSEUDO_RANDOM_DEVICE
: RANDOM_DEVICE
;
70 device
= open(device_name
, 0);
76 /* read until nbytes are read */
79 got
= read(device
, buffer
+ ndone
, bytes
- ndone
);
91 * Implementation of randomizer_t.get_random_bytes.
93 static status_t
get_random_bytes(private_randomizer_t
*this,size_t bytes
, u_int8_t
*buffer
)
95 return this->get_bytes_from_device(this, FALSE
, bytes
, buffer
);
99 * Implementation of randomizer_t.allocate_random_bytes.
101 static status_t
allocate_random_bytes(private_randomizer_t
*this, size_t bytes
, chunk_t
*chunk
)
105 chunk
->ptr
= malloc(bytes
);
106 status
= this->get_bytes_from_device(this, FALSE
, bytes
, chunk
->ptr
);
107 if (status
!= SUCCESS
)
115 * Implementation of randomizer_t.get_pseudo_random_bytes.
117 static status_t
get_pseudo_random_bytes(private_randomizer_t
*this,size_t bytes
, u_int8_t
*buffer
)
119 return (this->get_bytes_from_device(this, TRUE
, bytes
, buffer
));
123 * Implementation of randomizer_t.allocate_pseudo_random_bytes.
125 static status_t
allocate_pseudo_random_bytes(private_randomizer_t
*this, size_t bytes
, chunk_t
*chunk
)
129 chunk
->ptr
= malloc(bytes
);
130 status
= this->get_bytes_from_device(this, TRUE
, bytes
, chunk
->ptr
);
131 if (status
!= SUCCESS
)
139 * Implementation of randomizer_t.destroy.
141 static void destroy(private_randomizer_t
*this)
147 * Described in header.
149 randomizer_t
*randomizer_create(void)
151 private_randomizer_t
*this = malloc_thing(private_randomizer_t
);
153 /* public functions */
154 this->public.get_random_bytes
= (status_t (*) (randomizer_t
*,size_t, u_int8_t
*)) get_random_bytes
;
155 this->public.allocate_random_bytes
= (status_t (*) (randomizer_t
*,size_t, chunk_t
*)) allocate_random_bytes
;
156 this->public.get_pseudo_random_bytes
= (status_t (*) (randomizer_t
*,size_t, u_int8_t
*)) get_pseudo_random_bytes
;
157 this->public.allocate_pseudo_random_bytes
= (status_t (*) (randomizer_t
*,size_t, chunk_t
*)) allocate_pseudo_random_bytes
;
158 this->public.destroy
= (void (*) (randomizer_t
*))destroy
;
160 /* private functions */
161 this->get_bytes_from_device
= get_bytes_from_device
;
163 return &(this->public);