randpool_demo.c

Go to the documentation of this file.
00001 
00051 /*#*
00052  *#* $Log$
00053  *#* Revision 1.1  2007/02/15 18:17:51  asterix
00054  *#* Add randpool test program.
00055  *#*
00056  *#*/
00057 
00058 #include <stdlib.h>
00059 #include <stdio.h>
00060 #include <string.h>
00061 #include <cfg/compiler.h>
00062 #include <drv/timer.h>
00063 #include <algo/md2.h> 
00064 #include <algo/randpool.h>
00065 #include <unistd.h>
00066 
00067 #define LEN 256      //Size of buffer containing a random number.
00068 #define SAMPLE 1000  //Defoult number of byte that put in entropy pool.
00069 
00070 int main (int argc, char *argv[])
00071 {
00072     EntropyPool pool;
00073     uint8_t ch;
00074     uint8_t buff[LEN];
00075     FILE *pdev;
00076     int opt = getopt (argc, argv, "murh");
00077     int samp = SAMPLE;
00078     int round = 10;
00079     int mode = 0;
00080     int  pass = 0;
00081 
00082     timer_init();
00083 
00084     randpool_init(&pool, NULL, 0); //Init a entropy pool.
00085 
00086     /*
00087      * Chose a source of entropy.
00088      */
00089     switch(opt)
00090     {
00091         case 'm':
00092         {
00093             pdev = fopen("/dev/input/mouse0", "r");
00094             break;
00095         }
00096         case 'u':
00097         {
00098             pdev = fopen("/dev/urandom", "r");
00099             break;
00100         }
00101         case 'r':
00102         {
00103             pdev = fopen("/dev/random", "r");
00104             break;
00105         }
00106         case 'h':
00107         {
00108         }
00109         default:
00110         {
00111             printf("\n");   
00112             printf("\n");   
00113             printf("randpool_demo [OPTION] [SAMPLE] [ROUND] [MODE]\n"); 
00114             printf("\n");   
00115             printf("OPTION:\n");    
00116             printf("  -r  /dev/random\n");  
00117             printf("  -u  /dev/urandom\n"); 
00118             printf("  -m  /dev/input/mouse0\n");    
00119             printf("\n");   
00120             printf("SAMPLE:\n");    
00121             printf("  num  number of entropy byte to put in etropy pool.\n");   
00122             printf("\n");   
00123             printf("ROUND:\n"); 
00124             printf("  num  number call of randpool_get function.\n");   
00125             printf("\n");
00126             printf("MODE:\n");  
00127             printf("  0  binmode\n");   
00128             printf("  1  vector mode\n");   
00129             printf("  2  matrix mode\n");   
00130             printf("\n");
00131             printf("Test program of randpool API.\n");  
00132             printf("This program create an entropy pool of 256byte, and fill it\n");    
00133             printf("with entropy from a exsternal source. This source can be:\n");  
00134             printf("  - /dev/random (option: -r)\n");   
00135             printf("  - /dev/urandom (option: -u)\n");  
00136             printf("  - /dev/input/mouse0 (option: -m)\n"); 
00137             printf("\n");   
00138             printf("Once the pool is full, program call a randpool_get ROUND time,\n"); 
00139             printf("printing on STDOUT random value, in accord with a select mode.\n"); 
00140             printf("The mode are:\n");  
00141             printf("  - binmode: program print on STDOUT random byte. (option: 0 (defaul mode)\n"); 
00142             printf("  - vector mode: program print on STDOUT a ROUND vector of random value.\n");   
00143             printf("                 The output is format for octave program. (option: 1)\n");  
00144             printf("  - matrix mode: program print on STDOUT a matrix of random value.\n"); 
00145             printf("                 The output is format for octave program. (option: 2)\n");  
00146             printf("\n");
00147             exit(1);
00148         }
00149 
00150     }
00151 
00152     /*
00153      *  
00154      */
00155     if(argc > 2)
00156     {
00157         if(argv[2])
00158             samp = atoi(argv[2]);  //Number of byte take from entropy source
00159         if(argv[3])
00160             round = atoi(argv[3]); //Number of time we call randpool_get.
00161         if(argv[4])
00162         {   
00163             switch(atoi(argv[4]))
00164             {
00165                 case 1:
00166                 {
00167                     mode = 1;
00168                     printf("# Created by Asterix.\n");
00169                     printf("# name: __nargin__\n");
00170                     printf("# type: scalar\n");
00171                     printf("0\n");
00172                     break;
00173                 }
00174                 case 2:
00175                 {
00176                     mode = 2;
00177                     printf("# Created by Asterix.\n");
00178                     printf("# name: __nargin__\n");
00179                     printf("# type: scalar\n");
00180                     printf("0\n");
00181                     break;
00182                 }
00183                 default:
00184                 {
00185                     break;
00186                 }
00187 
00188             }
00189 
00190         }
00191 
00192     }
00193 
00194     /*
00195      * Add byte to entropy pool from a source of entropy.
00196      */
00197     for(int i = 0; i < samp; i++)
00198     {
00199 
00200         ch = fgetc(pdev);
00201         randpool_add(&pool, &ch, sizeof(ch));
00202         
00203     }
00204 
00205     fclose(pdev);
00206 
00207     
00208     for(int k = 0; k < round; k++)
00209     {   
00210         switch(mode)
00211         {
00212             case 1:
00213             {
00214                 printf("\n");
00215                 printf("\n# name: vet%d",k);
00216                 printf("\n# type: matrix");
00217                 printf("\n# rows: 1");
00218                 printf("\n# columns: %d\n", LEN);
00219                 pass = 1;
00220                 break;
00221             }
00222             case 2:
00223             {
00224                 printf("\n");
00225                 printf("\n# name: randMatrix");
00226                 printf("\n# type: matrix");
00227                 printf("\n# rows: %d",round);
00228                 printf("\n# columns: %d\n", LEN);
00229                 pass = 1;
00230                 mode = 0;
00231                 break;
00232             }
00233         }
00234 
00235         randpool_get(&pool, buff, LEN);
00236 
00237         if(pass)
00238         {
00239             for(int j = 0; j < LEN; j++)
00240             {
00241                 printf("%d ", buff[j]);
00242             }
00243             printf("\n");
00244         }
00245         else
00246             fwrite(buff, sizeof(uint8_t), LEN, stdout);
00247     }
00248     
00249 
00250 }
00251