]> git.cryptolib.org Git - avr-crypto-lib.git/blob - grain.c
+Grain +corrected orthograpic some errors
[avr-crypto-lib.git] / grain.c
1 /**
2  * 
3  * author: Daniel Otte
4  * email:  daniel.otte@rub.de
5  * license: GPLv3
6  * 
7  */
8
9
10 #include <stdint.h>
11 #include <string.h>
12 #include <avr/pgmspace.h>
13 #include "grain.h"
14
15
16 #define GRAIN_REVERSEKEY
17
18 /* s0, s1, s2, ..., s78, s79 */
19 #define S(i) ((ctx->lfsr[9-((i)/8)])>>(7-((i)%8)))
20 /* b0, b1, b2, ..., b78, b79 */
21 #define B(i) ((ctx->nfsr[9-((i)/8)])>>(7-((i)%8)))
22
23
24 uint8_t h_lut[4] PROGMEM = {0x4C, 0xB6, 0xD3, 0x26};
25
26 uint8_t grain_enc(grain_ctx_t* ctx){
27         uint8_t s80, s0, c1, c2;
28         uint8_t i;
29         /* clock the LFSR */
30         s0=S(0);
31         s80 =S(62) ^ S(51) ^ S(38) ^ S(23) ^ S(13) ^ s0;
32         s80 &= 1;
33         c1 = s80;
34         for(i=0; i<10; ++i){
35                 c2 = (ctx->lfsr[i])>>7;
36                 ctx->lfsr[i] = ((ctx->lfsr[i])<<1) | c1;
37                 c1 = c2;
38         }
39         /* clock the NFSR */
40         uint8_t b80, a,b,d,e;
41         b80 = B(62) ^ B(60) ^ B(52) ^ B(45) ^ 
42               B(37) ^ B(33) ^ B(28) ^ B(21) ^ 
43               B(14) ^ B( 9) ^ B( 0) ^ s0;
44         b80 ^= (a = B(63) & B(60));
45         b80 ^= (b = B(37) & B(33));
46         b80 ^= B(15) & B( 9); /* c */
47         b80 ^= (d = B(60) & B(52) & B(45));
48         b80 ^= (e = B(33) & B(28) & B(21));
49         b80 ^= B(63) & B(45) & B(28) & B(9); /* f */
50         /* -- */
51         b80 ^= b & B(60) & B(52); /* g */
52         b80 ^= a & B(21) & B(15); /* h */
53         b80 ^= d & B(63) & B(37); /* i */
54         b80 ^= e & B(15) & B( 9); /* j */
55         b80 ^= e & B(52) & B(45) & B(37); /* k */
56         c1 = b80 & 1;
57         for(i=0; i<10; ++i){
58                 c2 = (ctx->nfsr[i])>>7;
59                 ctx->nfsr[i] = ((ctx->nfsr[i])<<1) | c1;
60                 c1 = c2;
61         }
62         /* now the h function */
63         uint8_t h;
64         i = (S(2)&1) | 
65             ((S(24)&1) << 1) |
66             ((S(45)&1) << 2) |
67             ((S(63)&1) << 3) |
68             ((B(62)&1) << 4);
69         
70         h = (pgm_read_byte(h_lut+(i/8)))>>(i%8);
71         
72         h ^= B(0) ^ B(1) ^ B(3) ^ B(9) ^ B(30) ^ B(42) ^ B(55);
73         return h&1;
74 }
75
76 #ifdef GRAIN_REVERSEKEY
77
78 static
79 uint8_t reverse_bits(uint8_t a){
80         uint8_t lut[16] = {
81                 0x0, 0x8, 0x4, 0xC,   /* 0000 1000 0100 1100 */
82                 0x2, 0xA, 0x6, 0xE,   /* 0010 1010 0110 1110 */
83                 0x1, 0x9, 0x5, 0xD,   /* 0001 1001 0101 1101 */
84                 0x3, 0xB, 0x7, 0xF }; /* 0011 1011 0111 1111 */
85         uint8_t x;
86         x = ((lut[a&0xf]) << 4) | lut[a>>4];
87         return x;
88 }
89 #else
90
91 #define reverse_bits(a) (a)
92
93 #endif
94
95 void grain_init(const void* key, const void* iv, grain_ctx_t* ctx){
96         uint8_t i,t;
97         
98         /* load the 80bit key */
99         for(i=0; i<10; ++i){
100                 ctx->nfsr[9-i] = reverse_bits(((uint8_t*)key)[i]);
101         }
102         /* load the 64bit iv */
103         for(i=0; i<8; ++i){
104                 ctx->lfsr[9-i] = reverse_bits(((uint8_t*)iv)[i]);
105         }
106         /* set the other bits of iv to 1 */
107         ctx->lfsr[0] = ctx->lfsr[1] = 0xFF;
108         
109         /* run it 160 times */
110         for(i=0; i<160; ++i){
111                 t = grain_enc(ctx);
112                 (ctx->lfsr[0]) ^= t;
113                 (ctx->nfsr[0]) ^= t;
114         }
115 }
116
117
118
119
120
121