]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/gf256_table_gen.c
squeezing out a little more speed
[avr-crypto-lib.git] / host / gf256_table_gen.c
1 /* gf256_table_gen.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program 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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * \file     gf256_table_gen.c
21  * \email    daniel.otte@rub.de
22  * \author   Daniel Otte 
23  * \date     2009-01-13
24  * \license  GPLv3 or later
25  * 
26  */
27 #include <stdint.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <getopt.h>
31 #include "gf256mul.h"
32
33 int print_header =  1,
34     print_braces =  1,
35     print_base   =  1,
36     print_align  =  1,
37     print_prefix =  1;
38
39 char* fmt_strings[]={"%d", "%3d",
40                      "%X", "%2.2X",
41                      "%o", "%3o",
42                      "%d", "%3d",
43                      "0x%X", "0x%2.2X",
44                      "0%o", "0%3o"};
45                      
46 void print_help(void){
47         puts(
48 "gf256_table_gen [-cCbBdhoaApP] [-l min] [-u max]\n"
49 "                [-f formatstring] [-o file] -r reducer value {values} \n"
50 "\n"
51 " -c/-C turn C-header on/off (default: on)\n"
52 " -b/-B turn braces on/off (defaul: on)\n"
53 " -d/-h/-8 print values in decimal/hexadecimal/octal (default: hexadecimal)\n"
54 " -a/-a  turn alignment on/off (default: on)\n"
55 " -p/-P  turn base prefix on/off (default: on)\n"
56 " -r reducer reducer is the reduction polynomial represented as numeric value\n"
57 " -l min starting value for multiplication table (default: 0)\n"
58 " -u max upper limit value for multiplication table (default: 255)\n"           
59 " -f formatstring string for formating the output of a singel multiplicxation\n"
60 " -o file outputfile\n"
61 }
62                      
63 int main(int argc, char** argv){
64         int i,j;
65         int c;
66         int option_index=0;
67         char* fmt=NULL;
68         static struct option long_options[] =
69              {
70                /* These options set a flag. */
71                {"print-header",    no_argument, &print_header, 1},
72                {"no-print-header", no_argument, &print_header, 0},
73                {"print-braces",    no_argument, &print_braces, 1},
74                {"no-print-braces", no_argument, &print_braces, 0},
75                {"print-dec",       no_argument, &print_base, 0},
76                {"print-hex",       no_argument, &print_base, 1},
77                {"print-octal",     no_argument, &print_base, 2},
78                {"align",           no_argument, &print_align, 1},
79                {"no-align",        no_argument, &print_align, 0},
80                {"print-prefix",    no_argument, &print_prefix, 1},
81                {"no-print-prefix", no_argument, &print_prefix, 0},
82                
83                /* These options don't set a flag.
84                   We distinguish them by their indices. */
85                {"help",    no_argument      , 0, '?'},     
86                {"reducer", required_argument, 0, 'r'},
87                {"min",     required_argument, 0, 'l'},
88                {"max",     required_argument, 0, 'u'},
89                {"format",  required_argument, 0, 'f'},
90                {0, 0, 0, 0}
91              };
92
93         
94         
95         unsigned long ul_a;
96         int columns=8;
97         unsigned long ul_reducer=0x00, max=0xff, min=0x00;
98         uint8_t reducer, a;
99         char** eptr;
100         FILE* of = stdout;
101         while(c!=-1){
102                 c = getopt_long(argc, argv,"cCbBdhoaApPr:l:u:f:", 
103                                 long_options, &option_index);
104                 switch(c){
105                         case 'c': print_header=1; break;
106                         case 'C': print_header=0; break;
107                         case 'b': print_braces=1; break;
108                         case 'B': print_braces=0; break;
109                         case 'h': print_base = 1; break;
110                         case 'd': print_base = 0; break;
111                         case '8': print_base = 2; break;
112                         case 'a': print_align =1; break;
113                         case 'A': print_align =0; break;
114                         case 'p': print_prefix=1; break;
115                         case 'P': print_prefix=0; break;
116                         case 'r': ul_reducer = strtoul(optarg, eptr, 0);
117                                   if((**eptr)!='\0'){
118                                              fprintf(stderr, "Error, invalid reducer value \"%s\"!\n",
119                                                      optarg);
120                                              return -1;
121                                           }
122                                           break;
123                         case 'l': min = strtoul(optarg, eptr, 0);
124                                   if(((**eptr)!='\0')||min>0xff){
125                                              fprintf(stderr, "Error, invalid minimum value \"%s\"!\n",
126                                                      optarg);
127                                              return -1;
128                                           }
129                                           break;
130                         case 'u': max = strtoul(optarg, eptr, 0);
131                                   if(((**eptr)!='\0')||max>0xff){
132                                              fprintf(stderr, "Error, invalid maximum value \"%s\"!\n",
133                                                      optarg);
134                                              return -1;
135                                           }
136                                           break;
137                         case 'f': fmt = optarg; break;
138                         case '?': 
139                         default: 
140                                 print_help();
141                                 break;
142                 }
143         }
144         if(!reducer = ul_reducer&0xff){
145                         fprintf(stderr, "You must specify a reductionpolynomial!\n", argv[i]);
146                         return -1;
147         }
148         if(!fmt)
149                 fmt = fmt_strings[print_prefix*6+print_base*2+print_align];
150         
151         for(i=optind; i<argc; ++i){
152                 ul_a=strtoul(argv[i], eptr, 0)&0xff;
153                 if(((**eptr)!='\0')|| ul_a>0xff){
154                         fprintf(stderr, "Error, invalid number \"%s\"!\n", argv[i]);
155                         return -1;
156                 }
157                 a = ul_a;
158                 if(print_header)
159                         fprintf(of,"\nuint8_t lut_gfmul_%s_%2.2x",argv[i],reducer);
160                 if(print_braces)
161                         fprintf(of," = {\n\t");
162                 
163                 for(j=min; j<=max-1; ++j){
164                         fprintf(of,fmt,gf256mul(a, j, reducer));
165                         if(j%columns==columns-1){
166                                 fprintf(of, ",\n\t");
167                         }else{
168                                 fprintf(of, ", ");
169                         }
170                 }
171                 fprintf(of, fmt, gf256mul(a,max, reducer));
172                 if(print_braces)
173                         fprintf(of, " }");
174                 fprintf(of, "\n\n");    
175         }
176         return 0;
177 }