4 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief Test function for randpool API.
10 * This program return a rand number generate from randpool function.
11 * For use randpool function you need a souce of entropy. In this
12 * program you can choose from:
17 * There are 3 mode of output:
18 * - binmode: program generate a sequenze of random byte.
19 * - Matrix of random number for octave program.
20 * - Vector of random number for octave program.
23 * \author Daniele Basile <asterix@develer.com>
28 *#* Revision 1.1 2007/02/15 18:17:51 asterix
29 *#* Add randpool test program.
36 #include <cfg/compiler.h>
37 #include <drv/timer.h>
38 #include <algos/md2.h>
39 #include <algos/randpool.h>
42 #define LEN 256 //Size of buffer containing a random number.
43 #define SAMPLE 1000 //Defoult number of byte that put in entropy pool.
45 int main (int argc, char *argv[])
51 int opt = getopt (argc, argv, "murh");
59 randpool_init(&pool, NULL, 0); //Init a entropy pool.
62 * Chose a source of entropy.
68 pdev = fopen("/dev/input/mouse0", "r");
73 pdev = fopen("/dev/urandom", "r");
78 pdev = fopen("/dev/random", "r");
88 printf("randpool_demo [OPTION] [SAMPLE] [ROUND] [MODE]\n");
91 printf(" -r /dev/random\n");
92 printf(" -u /dev/urandom\n");
93 printf(" -m /dev/input/mouse0\n");
96 printf(" num number of entropy byte to put in etropy pool.\n");
99 printf(" num number call of randpool_get function.\n");
102 printf(" 0 binmode\n");
103 printf(" 1 vector mode\n");
104 printf(" 2 matrix mode\n");
106 printf("Test program of randpool API.\n");
107 printf("This program create an entropy pool of 256byte, and fill it\n");
108 printf("with entropy from a exsternal source. This source can be:\n");
109 printf(" - /dev/random (option: -r)\n");
110 printf(" - /dev/urandom (option: -u)\n");
111 printf(" - /dev/input/mouse0 (option: -m)\n");
113 printf("Once the pool is full, program call a randpool_get ROUND time,\n");
114 printf("printing on STDOUT random value, in accord with a select mode.\n");
115 printf("The mode are:\n");
116 printf(" - binmode: program print on STDOUT random byte. (option: 0 (defaul mode)\n");
117 printf(" - vector mode: program print on STDOUT a ROUND vector of random value.\n");
118 printf(" The output is format for octave program. (option: 1)\n");
119 printf(" - matrix mode: program print on STDOUT a matrix of random value.\n");
120 printf(" The output is format for octave program. (option: 2)\n");
133 samp = atoi(argv[2]); //Number of byte take from entropy source
135 round = atoi(argv[3]); //Number of time we call randpool_get.
138 switch(atoi(argv[4]))
143 printf("# Created by Asterix.\n");
144 printf("# name: __nargin__\n");
145 printf("# type: scalar\n");
152 printf("# Created by Asterix.\n");
153 printf("# name: __nargin__\n");
154 printf("# type: scalar\n");
170 * Add byte to entropy pool from a source of entropy.
172 for(int i = 0; i < samp; i++)
176 randpool_add(&pool, &ch, sizeof(ch));
183 for(int k = 0; k < round; k++)
190 printf("\n# name: vet%d",k);
191 printf("\n# type: matrix");
192 printf("\n# rows: 1");
193 printf("\n# columns: %d\n", LEN);
200 printf("\n# name: randMatrix");
201 printf("\n# type: matrix");
202 printf("\n# rows: %d",round);
203 printf("\n# columns: %d\n", LEN);
210 randpool_get(&pool, buff, LEN);
214 for(int j = 0; j < LEN; j++)
216 printf("%d ", buff[j]);
221 fwrite(buff, sizeof(uint8_t), LEN, stdout);