4 * @brief Implementation of randomizer_t.
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #include <sys/types.h>
30 #include "randomizer.h"
33 typedef struct private_randomizer_t private_randomizer_t
;
36 * Private data of an randomizer_t object.
38 struct private_randomizer_t
{
41 * Public randomizer_t interface.
46 * @brief Reads a specific number of bytes from random or pseudo random device.
48 * @param this calling object
49 * @param pseudo_random TRUE, if from pseudo random bytes should be read,
50 * FALSE for true random bytes
51 * @param bytes number of bytes to read
52 * @param[out] buffer pointer to buffer where to write the data in.
53 * Size of buffer has to be at least bytes.
55 status_t (*get_bytes_from_device
) (private_randomizer_t
*this,bool pseudo_random
, size_t bytes
, u_int8_t
*buffer
);
60 * Implementation of private_randomizer_t.get_bytes_from_device.
62 static status_t
get_bytes_from_device(private_randomizer_t
*this,bool pseudo_random
, size_t bytes
, u_int8_t
*buffer
)
69 device_name
= pseudo_random ? DEV_URANDOM
: DEV_RANDOM
;
71 device
= open(device_name
, 0);
77 /* read until nbytes are read */
80 got
= read(device
, buffer
+ ndone
, bytes
- ndone
);
92 * Implementation of randomizer_t.get_random_bytes.
94 static status_t
get_random_bytes(private_randomizer_t
*this,size_t bytes
, u_int8_t
*buffer
)
96 return this->get_bytes_from_device(this, FALSE
, bytes
, buffer
);
100 * Implementation of randomizer_t.allocate_random_bytes.
102 static status_t
allocate_random_bytes(private_randomizer_t
*this, size_t bytes
, chunk_t
*chunk
)
106 chunk
->ptr
= malloc(bytes
);
107 status
= this->get_bytes_from_device(this, FALSE
, bytes
, chunk
->ptr
);
108 if (status
!= SUCCESS
)
116 * Implementation of randomizer_t.get_pseudo_random_bytes.
118 static status_t
get_pseudo_random_bytes(private_randomizer_t
*this,size_t bytes
, u_int8_t
*buffer
)
120 return (this->get_bytes_from_device(this, TRUE
, bytes
, buffer
));
124 * Implementation of randomizer_t.allocate_pseudo_random_bytes.
126 static status_t
allocate_pseudo_random_bytes(private_randomizer_t
*this, size_t bytes
, chunk_t
*chunk
)
130 chunk
->ptr
= malloc(bytes
);
131 status
= this->get_bytes_from_device(this, TRUE
, bytes
, chunk
->ptr
);
132 if (status
!= SUCCESS
)
140 * Implementation of randomizer_t.destroy.
142 static void destroy(private_randomizer_t
*this)
148 * Described in header.
150 randomizer_t
*randomizer_create(void)
152 private_randomizer_t
*this = malloc_thing(private_randomizer_t
);
154 /* public functions */
155 this->public.get_random_bytes
= (status_t (*) (randomizer_t
*,size_t, u_int8_t
*)) get_random_bytes
;
156 this->public.allocate_random_bytes
= (status_t (*) (randomizer_t
*,size_t, chunk_t
*)) allocate_random_bytes
;
157 this->public.get_pseudo_random_bytes
= (status_t (*) (randomizer_t
*,size_t, u_int8_t
*)) get_pseudo_random_bytes
;
158 this->public.allocate_pseudo_random_bytes
= (status_t (*) (randomizer_t
*,size_t, chunk_t
*)) allocate_pseudo_random_bytes
;
159 this->public.destroy
= (void (*) (randomizer_t
*))destroy
;
161 /* private functions */
162 this->get_bytes_from_device
= get_bytes_from_device
;
164 return &(this->public);