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.
48 * \author Daniele Basile <asterix@develer.com>
53 *#* Revision 1.1 2007/02/15 18:17:51 asterix
54 *#* Add randpool test program.
61 #include <cfg/compiler.h>
62 #include <drv/timer.h>
63 #include <algos/md2.h>
64 #include <algos/randpool.h>
67 #define LEN 256 //Size of buffer containing a random number.
68 #define SAMPLE 1000 //Defoult number of byte that put in entropy pool.
70 int main (int argc, char *argv[])
76 int opt = getopt (argc, argv, "murh");
84 randpool_init(&pool, NULL, 0); //Init a entropy pool.
87 * Chose a source of entropy.
93 pdev = fopen("/dev/input/mouse0", "r");
98 pdev = fopen("/dev/urandom", "r");
103 pdev = fopen("/dev/random", "r");
113 printf("randpool_demo [OPTION] [SAMPLE] [ROUND] [MODE]\n");
116 printf(" -r /dev/random\n");
117 printf(" -u /dev/urandom\n");
118 printf(" -m /dev/input/mouse0\n");
121 printf(" num number of entropy byte to put in etropy pool.\n");
124 printf(" num number call of randpool_get function.\n");
127 printf(" 0 binmode\n");
128 printf(" 1 vector mode\n");
129 printf(" 2 matrix mode\n");
131 printf("Test program of randpool API.\n");
132 printf("This program create an entropy pool of 256byte, and fill it\n");
133 printf("with entropy from a exsternal source. This source can be:\n");
134 printf(" - /dev/random (option: -r)\n");
135 printf(" - /dev/urandom (option: -u)\n");
136 printf(" - /dev/input/mouse0 (option: -m)\n");
138 printf("Once the pool is full, program call a randpool_get ROUND time,\n");
139 printf("printing on STDOUT random value, in accord with a select mode.\n");
140 printf("The mode are:\n");
141 printf(" - binmode: program print on STDOUT random byte. (option: 0 (defaul mode)\n");
142 printf(" - vector mode: program print on STDOUT a ROUND vector of random value.\n");
143 printf(" The output is format for octave program. (option: 1)\n");
144 printf(" - matrix mode: program print on STDOUT a matrix of random value.\n");
145 printf(" The output is format for octave program. (option: 2)\n");
158 samp = atoi(argv[2]); //Number of byte take from entropy source
160 round = atoi(argv[3]); //Number of time we call randpool_get.
163 switch(atoi(argv[4]))
168 printf("# Created by Asterix.\n");
169 printf("# name: __nargin__\n");
170 printf("# type: scalar\n");
177 printf("# Created by Asterix.\n");
178 printf("# name: __nargin__\n");
179 printf("# type: scalar\n");
195 * Add byte to entropy pool from a source of entropy.
197 for(int i = 0; i < samp; i++)
201 randpool_add(&pool, &ch, sizeof(ch));
208 for(int k = 0; k < round; k++)
215 printf("\n# name: vet%d",k);
216 printf("\n# type: matrix");
217 printf("\n# rows: 1");
218 printf("\n# columns: %d\n", LEN);
225 printf("\n# name: randMatrix");
226 printf("\n# type: matrix");
227 printf("\n# rows: %d",round);
228 printf("\n# columns: %d\n", LEN);
235 randpool_get(&pool, buff, LEN);
239 for(int j = 0; j < LEN; j++)
241 printf("%d ", buff[j]);
246 fwrite(buff, sizeof(uint8_t), LEN, stdout);