]> git.cryptolib.org Git - avr-crypto-lib.git/blob - trivium/trivium.c
new and more compact aes
[avr-crypto-lib.git] / trivium / trivium.c
1 /* trivium.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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:  bg@nerilex.org
23  * license: GPLv3
24  * 
25  */
26
27  
28 #include <stdint.h>
29 #include <string.h>
30 #include "trivium.h"
31 #include <avr/pgmspace.h>
32
33 #define G(i) ((((*ctx)[(i)/8])>>(((i)%8)))&1)
34 #define S(i,v) ((*ctx)[(i)/8] = (((*ctx)[(i)/8]) & (uint8_t)~(1<<((i)%8))) | ((v)<<((i)%8)))
35 uint8_t trivium_enc(trivium_ctx_t *ctx){
36         uint8_t t1,t2,t3,z;
37         
38         t1 = G(65)  ^ G(92);
39         t2 = G(161) ^ G(176);
40         t3 = G(242) ^ G(287);
41         z  = t1^t2^t3;
42         t1 ^= (G(90)  & G(91))  ^ G(170);
43         t2 ^= (G(174) & G(175)) ^ G(263);
44         t3 ^= (G(285) & G(286)) ^ G(68);
45         
46         /* shift whole state and insert ts later */
47         uint8_t i,c1=0,c2;
48         for(i=0; i<36; ++i){
49                 c2=(((*ctx)[i])>>7);
50                 (*ctx)[i] = (((*ctx)[i])<<1)|c1;
51                 c1=c2;
52         }
53         /* insert ts */
54         S(0, t3);
55         S(93, t1);
56         S(177, t2);
57         
58         return z?0x080:0x00;
59 }
60
61 uint8_t trivium_getbyte(trivium_ctx_t *ctx){
62         uint8_t r=0, i=0;
63         do{
64                 r>>=1;
65                 r |= trivium_enc(ctx);
66         }while(++i<8);
67         return r;
68 }
69
70 #define KEYSIZE_B ((keysize_b+7)/8)
71 #define IVSIZE_B  ((ivsize_b +7)/8)
72
73 static const uint8_t rev_table[16] PROGMEM = {
74         0x00, 0x08, 0x04, 0x0C,   /* 0000 1000 0100 1100 */
75         0x02, 0x0A, 0x06, 0x0E,   /* 0010 1010 0110 1110 */
76         0x01, 0x09, 0x05, 0x0D,   /* 0001 1001 0101 1101 */
77         0x03, 0x0B, 0x07, 0x0F    /* 0011 1011 0111 1111 */
78 };
79
80 void trivium_init(const void *key, uint16_t keysize_b,
81                   const void *iv,  uint16_t ivsize_b,
82                   trivium_ctx_t *ctx){
83         uint16_t i;
84         uint8_t c1,c2;
85         uint8_t t1,t2;
86         memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
87         c2=0;
88         c1=KEYSIZE_B;
89         do{
90                 t1 = ((uint8_t*)key)[--c1];
91                 t2 = (pgm_read_byte(&(rev_table[t1&0x0f]))<<4)|(pgm_read_byte(&(rev_table[t1>>4])));
92                 (*ctx)[c2++] = t2;
93         }while(c1!=0);
94
95         c2=12;
96         c1=IVSIZE_B;
97         do{
98                 t1 = ((uint8_t*)iv)[--c1];
99                 t2 = (pgm_read_byte(&(rev_table[t1&0x0f]))<<4)|(pgm_read_byte(&(rev_table[t1>>4])));
100                 (*ctx)[c2++] = t2;
101         }while(c1!=0);
102
103         for(i=12+IVSIZE_B; i>10; --i){
104                 c2=(((*ctx)[i])<<5);
105                 (*ctx)[i] = (((*ctx)[i])>>3)|c1;
106                 c1=c2;
107         }
108
109         (*ctx)[35] = 0xE0;
110         
111         for(i=0; i<4*288; ++i){
112                 trivium_enc(ctx);
113         }
114 }
115
116