4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
33 * \brief Test function for randpool API.
35 * This program return a rand number generate from randpool function.
36 * For use randpool function you need a souce of entropy. In this
37 * program you can choose from:
42 * There are 3 mode of output:
43 * - binmode: program generate a sequenze of random byte.
44 * - Matrix of random number for octave program.
45 * - Vector of random number for octave program.
47 * \author Daniele Basile <asterix@develer.com>
52 *#* Revision 1.1 2007/02/15 18:17:51 asterix
53 *#* Add randpool test program.
60 #include <cfg/compiler.h>
61 #include <drv/timer.h>
63 #include <algo/randpool.h>
66 #define LEN 256 //Size of buffer containing a random number.
67 #define SAMPLE 1000 //Defoult number of byte that put in entropy pool.
69 int main (int argc, char *argv[])
75 int opt = getopt (argc, argv, "murh");
83 randpool_init(&pool, NULL, 0); //Init a entropy pool.
86 * Chose a source of entropy.
92 pdev = fopen("/dev/input/mouse0", "r");
97 pdev = fopen("/dev/urandom", "r");
102 pdev = fopen("/dev/random", "r");
112 printf("randpool_demo [OPTION] [SAMPLE] [ROUND] [MODE]\n");
115 printf(" -r /dev/random\n");
116 printf(" -u /dev/urandom\n");
117 printf(" -m /dev/input/mouse0\n");
120 printf(" num number of entropy byte to put in etropy pool.\n");
123 printf(" num number call of randpool_get function.\n");
126 printf(" 0 binmode\n");
127 printf(" 1 vector mode\n");
128 printf(" 2 matrix mode\n");
130 printf("Test program of randpool API.\n");
131 printf("This program create an entropy pool of 256byte, and fill it\n");
132 printf("with entropy from a exsternal source. This source can be:\n");
133 printf(" - /dev/random (option: -r)\n");
134 printf(" - /dev/urandom (option: -u)\n");
135 printf(" - /dev/input/mouse0 (option: -m)\n");
137 printf("Once the pool is full, program call a randpool_get ROUND time,\n");
138 printf("printing on STDOUT random value, in accord with a select mode.\n");
139 printf("The mode are:\n");
140 printf(" - binmode: program print on STDOUT random byte. (option: 0 (defaul mode)\n");
141 printf(" - vector mode: program print on STDOUT a ROUND vector of random value.\n");
142 printf(" The output is format for octave program. (option: 1)\n");
143 printf(" - matrix mode: program print on STDOUT a matrix of random value.\n");
144 printf(" The output is format for octave program. (option: 2)\n");
157 samp = atoi(argv[2]); //Number of byte take from entropy source
159 round = atoi(argv[3]); //Number of time we call randpool_get.
162 switch(atoi(argv[4]))
167 printf("# Created by Asterix.\n");
168 printf("# name: __nargin__\n");
169 printf("# type: scalar\n");
176 printf("# Created by Asterix.\n");
177 printf("# name: __nargin__\n");
178 printf("# type: scalar\n");
194 * Add byte to entropy pool from a source of entropy.
196 for(int i = 0; i < samp; i++)
200 randpool_add(&pool, &ch, sizeof(ch));
207 for(int k = 0; k < round; k++)
214 printf("\n# name: vet%d",k);
215 printf("\n# type: matrix");
216 printf("\n# rows: 1");
217 printf("\n# columns: %d\n", LEN);
224 printf("\n# name: randMatrix");
225 printf("\n# type: matrix");
226 printf("\n# rows: %d",round);
227 printf("\n# columns: %d\n", LEN);
234 randpool_get(&pool, buff, LEN);
238 for(int j = 0; j < LEN; j++)
240 printf("%d ", buff[j]);
245 fwrite(buff, sizeof(uint8_t), LEN, stdout);