]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/gf256_table_gen.c
little helper for generating multiplication tables for multiplication in GF(2**8)
[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 int main(int argc, char** argv){
47         int i,j;
48         int c;
49         int option_index=0;
50         char* fmt=NULL;
51         static struct option long_options[] =
52              {
53                /* These options set a flag. */
54                {"print-header",    no_argument, &print_header, 1},
55                {"no-print-header", no_argument, &print_header, 0},
56                {"print-braces",    no_argument, &print_braces, 1},
57                {"no-print-braces", no_argument, &print_braces, 0},
58                {"print-dec",       no_argument, &print_base, 0},
59                {"print-hex",       no_argument, &print_base, 1},
60                {"print-octal",     no_argument, &print_base, 2},
61                {"align",           no_argument, &print_align, 1},
62                {"no-align",        no_argument, &print_align, 0},
63                {"print-prefix",    no_argument, &print_prefix, 1},
64                {"no-print-prefix", no_argument, &print_prefix, 0},
65                
66                /* These options don't set a flag.
67                   We distinguish them by their indices. */
68                {"reducer", required_argument, 0, 'r'},
69                {"min",     required_argument, 0, 'l'},
70                {"max",     required_argument, 0, 'u'},
71                {"format",  required_argument, 0, 'f'},
72                {0, 0, 0, 0}
73              };
74
75         
76         
77         unsigned long ul_a;
78         int columns=8;
79         unsigned long ul_reducer=0x1b, max=0xff, min=0x00;
80         uint8_t reducer, a;
81         char** eptr;
82         FILE* of = stdout;
83         while(c!=-1){
84                 c = getopt_long(argc, argv,"cCbBdhoaApPr:l:u:f:", 
85                                 long_options, &option_index);
86                 switch(c){
87                         case 'c': print_header=1; break;
88                         case 'C': print_header=0; break;
89                         case 'b': print_braces=1; break;
90                         case 'B': print_braces=0; break;
91                         case 'h': print_base = 1; break;
92                         case 'd': print_base = 0; break;
93                         case 'o': print_base = 2; break;
94                         case 'a': print_align =1; break;
95                         case 'A': print_align =0; break;
96                         case 'p': print_prefix=1; break;
97                         case 'P': print_prefix=0; break;
98                         case 'r': ul_reducer = strtoul(optarg, eptr, 0);
99                                   if((**eptr)!='\0'){
100                                              fprintf(stderr, "Error, invalid reducer value \"%s\"!\n",
101                                                      optarg);
102                                              return -1;
103                                           }
104                                           break;
105                         case 'l': min = strtoul(optarg, eptr, 0);
106                                   if(((**eptr)!='\0')||min>0xff){
107                                              fprintf(stderr, "Error, invalid minimum value \"%s\"!\n",
108                                                      optarg);
109                                              return -1;
110                                           }
111                                           break;
112                         case 'u': max = strtoul(optarg, eptr, 0);
113                                   if(((**eptr)!='\0')||max>0xff){
114                                              fprintf(stderr, "Error, invalid maximum value \"%s\"!\n",
115                                                      optarg);
116                                              return -1;
117                                           }
118                                           break;
119                         case 'f': fmt = optarg; break;
120                         default: break;
121                 }
122         }
123         reducer = ul_reducer&0xff;
124         if(!fmt)
125                 fmt = fmt_strings[print_prefix*6+print_base*2+print_align];
126         
127         for(i=optind; i<argc; ++i){
128                 ul_a=strtoul(argv[i], eptr, 0)&0xff;
129                 if(((**eptr)!='\0')|| ul_a>0xff){
130                         fprintf(stderr, "Error, invalid number \"%s\"!\n", argv[i]);
131                         return -1;
132                 }
133                 a = ul_a;
134                 if(print_header)
135                         fprintf(of,"\nuint8_t lut_gfmul_%s_%2.2x",argv[i],reducer);
136                 if(print_braces)
137                         fprintf(of," = {\n\t");
138                 
139                 for(j=min; j<=max-1; ++j){
140                         fprintf(of,fmt,gf256mul(a, j, reducer));
141                         if(j%columns==columns-1){
142                                 fprintf(of, ",\n\t");
143                         }else{
144                                 fprintf(of, ", ");
145                         }
146                 }
147                 fprintf(of, fmt, gf256mul(a,max, reducer));
148                 if(print_braces)
149                         fprintf(of, " }");
150                 fprintf(of, "\n\n");    
151         }
152         return 0;
153 }