]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/gf256_table_gen.c
fixing E-Mail-Address & Copyright
[avr-crypto-lib.git] / host / gf256_table_gen.c
1 /* gf256_table_gen.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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    bg@nerilex.org
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                      
64 int main(int argc, char** argv){
65         int i,j;
66         int c;
67         int option_index=0;
68         char *fmt=NULL;
69         static struct option long_options[] =
70              {
71                /* These options set a flag. */
72                {"print-header",    no_argument, &print_header, 1},
73                {"no-print-header", no_argument, &print_header, 0},
74                {"print-braces",    no_argument, &print_braces, 1},
75                {"no-print-braces", no_argument, &print_braces, 0},
76                {"print-dec",       no_argument, &print_base, 0},
77                {"print-hex",       no_argument, &print_base, 1},
78                {"print-octal",     no_argument, &print_base, 2},
79                {"align",           no_argument, &print_align, 1},
80                {"no-align",        no_argument, &print_align, 0},
81                {"print-prefix",    no_argument, &print_prefix, 1},
82                {"no-print-prefix", no_argument, &print_prefix, 0},
83                
84                /* These options don't set a flag.
85                   We distinguish them by their indices. */
86                {"help",    no_argument      , 0, '?'},     
87                {"reducer", required_argument, 0, 'r'},
88                {"min",     required_argument, 0, 'l'},
89                {"max",     required_argument, 0, 'u'},
90                {"format",  required_argument, 0, 'f'},
91                {0, 0, 0, 0}
92              };
93
94         
95         
96         unsigned long ul_a;
97         int columns=8;
98         unsigned long ul_reducer=0x00, max=0xff, min=0x00;
99         uint8_t reducer, a;
100         char** eptr;
101         FILE *of = stdout;
102         while(c!=-1){
103                 c = getopt_long(argc, argv,"cCbBdh8aApPr:l:u:f:", 
104                                 long_options, &option_index);
105                 switch(c){
106                         case 'c': print_header=1; break;
107                         case 'C': print_header=0; break;
108                         case 'b': print_braces=1; break;
109                         case 'B': print_braces=0; break;
110                         case 'h': print_base = 1; break;
111                         case 'd': print_base = 0; break;
112                         case '8': print_base = 2; break;
113                         case 'a': print_align =1; break;
114                         case 'A': print_align =0; break;
115                         case 'p': print_prefix=1; break;
116                         case 'P': print_prefix=0; break;
117                         case 'r': ul_reducer = strtoul(optarg, eptr, 0);
118                                   if((**eptr)!='\0'){
119                                              fprintf(stderr, "Error, invalid reducer value \"%s\"!\n",
120                                                      optarg);
121                                              return -1;
122                                           }
123                                           break;
124                         case 'l': min = strtoul(optarg, eptr, 0);
125                                   if(((**eptr)!='\0')||min>0xff){
126                                              fprintf(stderr, "Error, invalid minimum value \"%s\"!\n",
127                                                      optarg);
128                                              return -1;
129                                           }
130                                           break;
131                         case 'u': max = strtoul(optarg, eptr, 0);
132                                   if(((**eptr)!='\0')||max>0xff){
133                                              fprintf(stderr, "Error, invalid maximum value \"%s\"!\n",
134                                                      optarg);
135                                              return -1;
136                                           }
137                                           break;
138                         case 'f': fmt = optarg; break;
139                         case  -1: break;
140                         case '?': 
141                         default: 
142                                 printf ("unknown option '%c' (%2.2X) !\n\t", c, c);
143                                 print_help();
144                                 return 0;
145                                 break;
146                 }
147         }
148         if(!(reducer = ul_reducer&0xff)){
149                         fprintf(stderr, "You must specify a reductionpolynomial!\n", argv[i]);
150                         return -1;
151         }
152         if(!fmt)
153                 fmt = fmt_strings[print_prefix*6+print_base*2+print_align];
154         
155         for(i=optind; i<argc; ++i){
156                 ul_a=strtoul(argv[i], eptr, 0)&0xff;
157                 if(((**eptr)!='\0')|| ul_a>0xff){
158                         fprintf(stderr, "Error, invalid number \"%s\"!\n", argv[i]);
159                         return -1;
160                 }
161                 a = ul_a;
162                 if(print_header)
163                         fprintf(of,"\nuint8_t lut_gfmul_%s_%2.2x",argv[i],reducer);
164                 if(print_braces)
165                         fprintf(of," = {\n\t");
166                 
167                 for(j=min; j<=max-1; ++j){
168                         fprintf(of,fmt,gf256mul(a, j, reducer));
169                         if(j%columns==columns-1){
170                                 fprintf(of, ",\n\t");
171                         }else{
172                                 fprintf(of, ", ");
173                         }
174                 }
175                 fprintf(of, fmt, gf256mul(a,max, reducer));
176                 if(print_braces)
177                         fprintf(of, " }");
178                 fprintf(of, "\n\n");    
179         }
180         return 0;
181 }