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