17041c480d1063c5cbd337a3a7631fd3f9f63511
[bertos.git] / app / randpool / randpool_demo.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Test function for randpool API.
9  *
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:
13  *      - /dev/random
14  *      - /dev/urandom
15  *      - /dev/input/mouse0
16  *      
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.
21  * 
22  * \version $Id$
23  * \author Daniele Basile <asterix@develer.com>
24  */
25
26 /*#*
27  *#* $Log$
28  *#* Revision 1.1  2007/02/15 18:17:51  asterix
29  *#* Add randpool test program.
30  *#*
31  *#*/
32
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <cfg/compiler.h>
37 #include <drv/timer.h>
38 #include <algos/md2.h> 
39 #include <algos/randpool.h>
40 #include <unistd.h>
41
42 #define LEN 256      //Size of buffer containing a random number.
43 #define SAMPLE 1000  //Defoult number of byte that put in entropy pool.
44
45 int main (int argc, char *argv[])
46 {
47         EntropyPool pool;
48         uint8_t ch;
49         uint8_t buff[LEN];
50         FILE *pdev;
51         int opt = getopt (argc, argv, "murh");
52         int samp = SAMPLE;
53         int round = 10;
54         int mode = 0;
55         int  pass = 0;
56
57         timer_init();
58
59         randpool_init(&pool, NULL, 0); //Init a entropy pool.
60
61         /*
62          * Chose a source of entropy.
63          */
64         switch(opt)
65         {
66                 case 'm':
67                 {
68                         pdev = fopen("/dev/input/mouse0", "r");
69                         break;
70                 }
71                 case 'u':
72                 {
73                         pdev = fopen("/dev/urandom", "r");
74                         break;
75                 }
76                 case 'r':
77                 {
78                         pdev = fopen("/dev/random", "r");
79                         break;
80                 }
81                 case 'h':
82                 {
83                 }
84                 default:
85                 {
86                         printf("\n");   
87                         printf("\n");   
88                         printf("randpool_demo [OPTION] [SAMPLE] [ROUND] [MODE]\n");     
89                         printf("\n");   
90                         printf("OPTION:\n");    
91                         printf("  -r  /dev/random\n");  
92                         printf("  -u  /dev/urandom\n"); 
93                         printf("  -m  /dev/input/mouse0\n");    
94                         printf("\n");   
95                         printf("SAMPLE:\n");    
96                         printf("  num  number of entropy byte to put in etropy pool.\n");       
97                         printf("\n");   
98                         printf("ROUND:\n");     
99                         printf("  num  number call of randpool_get function.\n");       
100                         printf("\n");
101                         printf("MODE:\n");      
102                         printf("  0  binmode\n");       
103                         printf("  1  vector mode\n");   
104                         printf("  2  matrix mode\n");   
105                         printf("\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"); 
112                         printf("\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");      
121                         printf("\n");
122                         exit(1);
123                 }
124
125         }
126
127         /*
128          *  
129          */
130         if(argc > 2)
131         {
132                 if(argv[2])
133                         samp = atoi(argv[2]);  //Number of byte take from entropy source
134                 if(argv[3])
135                         round = atoi(argv[3]); //Number of time we call randpool_get.
136                 if(argv[4])
137                 {       
138                         switch(atoi(argv[4]))
139                         {
140                                 case 1:
141                                 {
142                                         mode = 1;
143                                         printf("# Created by Asterix.\n");
144                                         printf("# name: __nargin__\n");
145                                         printf("# type: scalar\n");
146                                         printf("0\n");
147                                         break;
148                                 }
149                                 case 2:
150                                 {
151                                         mode = 2;
152                                         printf("# Created by Asterix.\n");
153                                         printf("# name: __nargin__\n");
154                                         printf("# type: scalar\n");
155                                         printf("0\n");
156                                         break;
157                                 }
158                                 default:
159                                 {
160                                         break;
161                                 }
162
163                         }
164
165                 }
166
167         }
168
169         /*
170          * Add byte to entropy pool from a source of entropy.
171          */
172         for(int i = 0; i < samp; i++)
173         {
174
175                 ch = fgetc(pdev);
176                 randpool_add(&pool, &ch, sizeof(ch));
177                 
178         }
179
180         fclose(pdev);
181
182         
183         for(int k = 0; k < round; k++)
184         {       
185                 switch(mode)
186                 {
187                         case 1:
188                         {
189                                 printf("\n");
190                                 printf("\n# name: vet%d",k);
191                                 printf("\n# type: matrix");
192                                 printf("\n# rows: 1");
193                                 printf("\n# columns: %d\n", LEN);
194                                 pass = 1;
195                                 break;
196                         }
197                         case 2:
198                         {
199                                 printf("\n");
200                                 printf("\n# name: randMatrix");
201                                 printf("\n# type: matrix");
202                                 printf("\n# rows: %d",round);
203                                 printf("\n# columns: %d\n", LEN);
204                                 pass = 1;
205                                 mode = 0;
206                                 break;
207                         }
208                 }
209
210                 randpool_get(&pool, buff, LEN);
211
212                 if(pass)
213                 {
214                         for(int j = 0; j < LEN; j++)
215                         {
216                                 printf("%d ", buff[j]);
217                         }
218                         printf("\n");
219                 }
220                 else
221                         fwrite(buff, sizeof(uint8_t), LEN, stdout);
222         }
223         
224
225 }
226