1 /* gf256_table_gen.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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.
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.
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/>.
20 * \file gf256_table_gen.c
21 * \email bg@nerilex.org
24 * \license GPLv3 or later
39 char *fmt_strings[]={"%d", "%3d",
46 void print_help(void){
48 "gf256_table_gen [-cCbBdhoaApP] [-l min] [-u max]\n"
49 " [-f formatstring] [-o file] -r reducer value {values} \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"
64 int main(int argc, char** argv){
69 static struct option long_options[] =
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},
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'},
98 unsigned long ul_reducer=0x00, max=0xff, min=0x00;
103 c = getopt_long(argc, argv,"cCbBdh8aApPr:l:u:f:",
104 long_options, &option_index);
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);
119 fprintf(stderr, "Error, invalid reducer value \"%s\"!\n",
124 case 'l': min = strtoul(optarg, eptr, 0);
125 if(((**eptr)!='\0')||min>0xff){
126 fprintf(stderr, "Error, invalid minimum value \"%s\"!\n",
131 case 'u': max = strtoul(optarg, eptr, 0);
132 if(((**eptr)!='\0')||max>0xff){
133 fprintf(stderr, "Error, invalid maximum value \"%s\"!\n",
138 case 'f': fmt = optarg; break;
142 printf ("unknown option '%c' (%2.2X) !\n\t", c, c);
148 if(!(reducer = ul_reducer&0xff)){
149 fprintf(stderr, "You must specify a reductionpolynomial!\n", argv[i]);
153 fmt = fmt_strings[print_prefix*6+print_base*2+print_align];
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]);
163 fprintf(of,"\nuint8_t lut_gfmul_%s_%2.2x",argv[i],reducer);
165 fprintf(of," = {\n\t");
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");
175 fprintf(of, fmt, gf256mul(a,max, reducer));