]> git.cryptolib.org Git - avr-crypto-lib.git/blob - grain_nfsr_lutgen.c
+bad optimisation (doesn't improve anything)
[avr-crypto-lib.git] / grain_nfsr_lutgen.c
1 /**
2  * 
3  * author: Daniel Otte
4  * email:  daniel.otte@rub.de
5  * license: GPLv3
6  *
7  * this program generate a lookuptable for the nfsr-feedback-function in grain 
8  */
9
10 #include <stdint.h>
11 #include <stdio.h>
12
13 #define X(i) ((x)>>((i)))
14 #define B63 X(0)
15 #define B60 X(3)
16 #define B52 X(5)
17 #define B45 X(6)
18 #define B37 X(4)
19 #define B33 X(8)
20 #define B28 X(2)
21 #define B21 X(9)
22 #define B15 X(1)
23 #define B09 X(7)
24
25 uint8_t g(uint16_t x){
26         uint8_t a,b,d,e;
27         uint8_t ret;
28
29         ret = B60 ^ B52 ^ B45 ^ B37 ^ B33 ^ B28 ^ B21 ^ B09;
30         ret ^= (a = B63 & B60);
31         ret ^= (b = B37 & B33);
32         ret ^= B15 & B09;
33         ret ^= (d = B60 & B52 & B45);
34         ret ^= (e = B33 & B28 & B21);
35         ret ^= B63 & B45 & B28 & B09;
36         ret ^= b & B60 & B52;
37         ret ^= a & B21 & B15;
38         ret ^= d & B63 & B37;
39         ret ^= e & B15 & B09;
40         ret ^= e & B52 & B45 & B37;
41         
42         return ret&1;
43 }
44
45 int main(void){
46         uint16_t i; 
47         uint8_t t, lut[128]={0}; /* 2**10 / 8 == 2**(10-3) == 2**7 == 128 */
48         puts(
49         "/* \n"
50         " * author: Daniel Otte \n"
51         " * email:  daniel.otte@rub.de  \n"
52         " * license: GPLv3 \n"
53         " *  \n"
54         " * this program generate a lookuptable for the h-function in grain  \n"
55         " *  \n"
56         " */ \n");
57         puts("/* \n"
58              " * b63 b15 b28 b60 b37 b52 b45 b09 b33 b21 - g");
59         
60         for(i=0; i<0x0400; ++i){
61                 t = g(i);
62                 printf(" *  %c   %c   %c   %c   %c   %c   %c   %c   %c   %c  - %c\n",
63                         (i&0x01)?'1':'0',
64                         (i&0x02)?'1':'0',
65                         (i&0x04)?'1':'0',
66                         (i&0x08)?'1':'0',
67                         (i&0x10)?'1':'0',
68                         (i&0x20)?'1':'0',
69                         (i&0x40)?'1':'0',
70                         (i&0x80)?'1':'0',
71                         (i&0x0100)?'1':'0',
72                         (i&0x0200)?'1':'0',
73                         t?'1':'0' );
74                 lut[i/8] |= t<<(i%8);
75 //              if(i%4==3){     
76 //                      puts(" * --");
77 //              }
78         }
79         puts(" */\n");
80         
81         printf(" uint8_t g_lut[128]= {");
82         for(i=0; i<128; ++i){
83                 if(i%16==0){
84                         printf("\n\t");
85                 }
86                 printf("0x%2.2X%c ", lut[i], (i!=127)?',':' ');
87         }
88         printf("};\n\n");
89         return 0; 
90 }
91