Attachment 'mix.c'

Download

   1 /*
   2  *  cc -o mix mix.c ; ./mix File1 File2 ...
   3  *
   4  *  set DESTROY_SEEN_ENTROPY==0 for testing!
   5  *  else this will clobber the argument files...
   6  *  (C) 2008 CAcert Inc.
   7  */
   8 
   9 #define BLOCK 512
  10 #define DESTROY_SEEN_ENTROPY 1
  11 static char mix[BLOCK];
  12 
  13 void splat(int fd, char * name, long total_to_splat);
  14 
  15 main(int argc, char **argv)
  16 {
  17   int fds[argc];      /* fds[0] is wasted, to align with argv */
  18   int i;
  19   int more_data = 1;
  20   long total = 0;
  21   const int START = 1;   /* for both argv[] and fds[] */
  22   const int END = argc;
  23 
  24   if (argc <= 1)
  25   {
  26     char *p = "Usage: mix files...\n";
  27     write(2, p, strlen(p));
  28     exit(2);
  29   }
  30 
  31   for (i = START; i < END; i++)   /* open all the input files */
  32   {
  33     fds[i] = open(argv[i], 2);    /* 2 == O_RDWR */
  34     if (fds[i] < 0)
  35     {
  36       char *p = "bad file: ";
  37       write(2, p, strlen(p));     /* 2 == stderr */
  38       write(2, argv[i], strlen(argv[i]));
  39       write(2, "\n", 1);
  40       exit(2);
  41     }
  42   }
  43 
  44   /*
  45    *  Read each Nth block, in parallel, across the files.
  46    *  Stop at the first end-of-file.
  47    *  Any entropy longer than the shortest file is wasted.
  48    */
  49   while (more_data)
  50   {
  51     for (i = START; i < END; i++)   /* for each file */
  52     {
  53       int j;
  54       char next[BLOCK];
  55       int got;
  56 
  57       got = read(fds[i], next, BLOCK);  /* next block */
  58 
  59       if (got != BLOCK)
  60       {
  61         more_data = 0;
  62         break;
  63       }
  64   
  65       for (j = 0; j < BLOCK; j++)   /* mix in one block */
  66       {
  67         mix[j] ^= next[j];
  68       }
  69   
  70     }
  71 
  72     if (!more_data) /* yuch, need a 2-level break */
  73       break;
  74 
  75     total += BLOCK;
  76     write(1, mix, BLOCK);      /* 1 == stdout */
  77   }
  78 
  79 
  80   if (DESTROY_SEEN_ENTROPY)
  81   {
  82     for (i = START; i < END; i++)
  83     {
  84       splat(fds[i], argv[i], total);
  85     }
  86   }
  87 
  88 }
  89 
  90 
  91 /*
  92  *  Yes, the #includes are here, below, deliberately.
  93  *  That's so that the above code that does the mixing
  94  *  is totally clean of alien influences.
  95  */
  96 #include <sys/types.h>
  97 #include <unistd.h>
  98 char zeros[BLOCK];   /* default initialise to zeros */
  99 
 100 /*
 101  *  It is the user's responsibility to really clean the drive,
 102  *  using e.g., 33 pass or 35 pass.
 103  *  This just prevents inadvertant re-use of the entropy.
 104  */
 105 void splat(int fd, char *name, long total)
 106 {
 107    off_t offset;
 108    /* rewind to start and write nulls */
 109    lseek(fd, (off_t) 0, SEEK_SET);
 110 
 111    for (offset = 0; offset < (off_t)total; offset += BLOCK)
 112      write(fd, zeros, BLOCK);
 113 
 114    /* unlink(name); */
 115 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-09-15 14:26:33, 2.4 KB) [[attachment:mix.c]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.