]> git.cryptolib.org Git - arm-crypto-lib.git/blob - grain/grain.c
fixing some header comments
[arm-crypto-lib.git] / grain / grain.c
1 /* grain.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2011  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  * 
21  * author: Daniel Otte
22  * email:  daniel.otte@rub.de
23  * license: GPLv3 or later
24  * 
25  */
26
27
28 #include <stdint.h>
29 #include <string.h>
30 #include "grain.h"
31
32
33 #define GRAIN_REVERSEKEY
34
35 /* s0, s1, s2, ..., s78, s79 */
36 #define S(i) ((ctx->lfsr[9-((i)/8)])>>(7-((i)%8)))
37 /* b0, b1, b2, ..., b78, b79 */
38 #define B(i) ((ctx->nfsr[9-((i)/8)])>>(7-((i)%8)))
39 #define _B(i) (((ctx->nfsr[9-((i)/8)])>>(7-((i)%8)))&1)
40
41
42 uint8_t h_lut[4] = {0x4C, 0xB6, 0xD3, 0x26};
43
44 #ifdef GRAIN_BADOPTIMISATION
45 uint8_t g_lut[128] = {
46         0xF0, 0xA5, 0x0F, 0x5A, 0x0F, 0x5A, 0xF0, 0xA5, 0x0F, 0x5A, 0xF0, 0xA5, 0xF0, 0x5A, 0x0F, 0x0F, 
47         0xC3, 0x96, 0x3C, 0x69, 0x3C, 0x69, 0xC3, 0x96, 0x9C, 0xC9, 0x63, 0x36, 0x63, 0xC9, 0x9C, 0x9C, 
48         0x0F, 0x5A, 0x0F, 0x5A, 0xF0, 0xA5, 0xF0, 0x5A, 0xF0, 0xA5, 0xF0, 0xA5, 0x0F, 0xA5, 0x0F, 0xF0, 
49         0x3C, 0x69, 0x3C, 0x69, 0xC3, 0x96, 0xC3, 0x69, 0x63, 0x36, 0x63, 0x36, 0x9C, 0x36, 0x9C, 0x63, 
50         0x0F, 0xD2, 0xF0, 0x2D, 0xF0, 0x2D, 0x0F, 0xD2, 0xF0, 0x2D, 0x0F, 0xD2, 0x0F, 0x2D, 0xF0, 0x78, 
51         0x3C, 0xE1, 0xC3, 0x1E, 0xC3, 0x1E, 0x3C, 0xE1, 0x63, 0xBE, 0x9C, 0x41, 0x9C, 0xBE, 0x63, 0xEB, 
52         0x00, 0xDD, 0x00, 0xDD, 0xFF, 0x22, 0xFF, 0xDD, 0xFF, 0x22, 0xFF, 0x22, 0x00, 0x22, 0xF0, 0x87, 
53         0xF3, 0x2E, 0xF3, 0x2E, 0x0C, 0xD1, 0x0C, 0x2E, 0xAC, 0x71, 0xAC, 0x71, 0x53, 0x71, 0xA3, 0xD4  };
54 #endif
55
56 uint8_t grain_enc(grain_ctx_t* ctx){
57         uint8_t s80, s0, c1, c2;
58         uint8_t i;
59         /* clock the LFSR */
60         s0=S(0);
61         s80 =S(62) ^ S(51) ^ S(38) ^ S(23) ^ S(13) ^ s0;
62         s80 &= 1;
63         c1 = s80;
64         for(i=0; i<10; ++i){
65                 c2 = (ctx->lfsr[i])>>7;
66                 ctx->lfsr[i] = ((ctx->lfsr[i])<<1) | c1;
67                 c1 = c2;
68         }
69         /* clock the NFSR */
70         uint8_t b80;
71 /*      778 Byte in this variant / 617 clks enc_time */
72 #ifndef GRAIN_BADOPTIMISATION
73     uint8_t a,b,d,e;
74         b80 = B(62) ^ B(60) ^ B(52) ^ B(45) ^ 
75               B(37) ^ B(33) ^ B(28) ^ B(21) ^ 
76               B(14) ^ B( 9) ^ B( 0) ^ s0;
77         b80 ^= (a = B(63) & B(60));
78         b80 ^= (b = B(37) & B(33));
79         b80 ^= B(15) & B( 9); // c 
80         b80 ^= (d = B(60) & B(52) & B(45));
81         b80 ^= (e = B(33) & B(28) & B(21));
82         b80 ^= B(63) & B(45) & B(28) & B(9); // f 
83         /* -- */
84         b80 ^= b & B(60) & B(52); // g 
85         b80 ^= a & B(21) & B(15); // h 
86         b80 ^= d & B(63) & B(37); // i 
87         b80 ^= e & B(15) & B( 9); // j 
88         b80 ^= e & B(52) & B(45) & B(37); // k
89 #else
90         /* let's reorder the bits */
91         uint16_t x; 
92
93 /*
94         x  = _B(21); x<<=1;
95         x |= _B(33); x<<=1;
96         x |= _B(9) ; x<<=1;
97         x |= _B(45); x<<=1;
98         x |= _B(52); x<<=1;
99         x |= _B(37); x<<=1;
100         x |= _B(60); x<<=1;
101         x |= _B(28); x<<=1;
102         x |= _B(15); x<<=1;
103         x |= _B(63);
104 */
105         x  = ((ctx->nfsr[8])&0x41)<<1; // B15 & B09
106         x |= ((ctx->nfsr[2])&0x09);    // B63 & B60 
107 //      x |= ((ctx->nfsr[4])&0x04)<<4; // B45
108         x |= (((ctx->nfsr[5])&0x44) | 
109               ((ctx->nfsr[3])&0x08) | 
110               (((((ctx->nfsr[7])&0x04)<<3) |((ctx->nfsr[4])&0x04))<<2) )<<2; // B37 & B33
111 //      x |= ((ctx->nfsr[3])&0x08)<<2; // B52
112         x |= ((ctx->nfsr[6])&0x08)>>1; // B28
113 //      x |= ((ctx->nfsr[7])&0x04)<<7; // B21 
114
115
116         b80 = (g_lut[x/8])>>(x%8);
117         b80 ^= s0 ^ B(62) ^ B(14) ^ B(0);
118 #endif
119         c1 = b80 & 1;
120         for(i=0; i<10; ++i){
121                 c2 = (ctx->nfsr[i])>>7;
122                 ctx->nfsr[i] = ((ctx->nfsr[i])<<1) | c1;
123                 c1 = c2;
124         }
125         /* now the h function */
126         uint8_t h;
127         i = (S(2)&1) | 
128             ((S(24)&1) << 1) |
129             ((S(45)&1) << 2) |
130             ((S(63)&1) << 3) |
131             ((B(62)&1) << 4);
132         
133         h = (h_lut[i/8])>>(i%8);
134         
135         h ^= B(0) ^ B(1) ^ B(3) ^ B(9) ^ B(30) ^ B(42) ^ B(55);
136         return (h&1);
137 }
138
139 uint8_t grain_getbyte(grain_ctx_t* ctx){
140         uint8_t i=0;
141         uint8_t r=0;
142         do{
143                 r >>= 1;
144                 r |= grain_enc(ctx)?0x80:0x00;
145         }while(++i<8);
146         return r;
147 }
148
149 #ifdef GRAIN_REVERSEKEY
150
151 static
152 uint8_t reverse_bits(uint8_t a){
153         uint8_t lut[16] = {
154                 0x0, 0x8, 0x4, 0xC,   /* 0000 1000 0100 1100 */
155                 0x2, 0xA, 0x6, 0xE,   /* 0010 1010 0110 1110 */
156                 0x1, 0x9, 0x5, 0xD,   /* 0001 1001 0101 1101 */
157                 0x3, 0xB, 0x7, 0xF }; /* 0011 1011 0111 1111 */
158         uint8_t x;
159         x = ((lut[a&0xf]) << 4) | lut[a>>4];
160         return x;
161 }
162 #else
163
164 #define reverse_bits(a) (a)
165
166 #endif
167
168 void grain_init(const void* key, const void* iv, grain_ctx_t* ctx){
169         uint8_t i,t;
170         
171         /* load the 80bit key */
172         for(i=0; i<10; ++i){
173                 ctx->nfsr[9-i] = reverse_bits(((uint8_t*)key)[i]);
174         }
175         /* load the 64bit iv */
176         for(i=0; i<8; ++i){
177                 ctx->lfsr[9-i] = reverse_bits(((uint8_t*)iv)[i]);
178         }
179         /* set the other bits of iv to 1 */
180         ctx->lfsr[0] = ctx->lfsr[1] = 0xFF;
181         
182         /* run it 160 times */
183         for(i=0; i<160; ++i){
184                 t = grain_enc(ctx);
185                 (ctx->lfsr[0]) ^= t;
186                 (ctx->nfsr[0]) ^= t;
187         }
188 }
189
190
191
192
193
194