]> git.cryptolib.org Git - avr-crypto-lib.git/blob - trivium.c
insereated GPLv3 stub
[avr-crypto-lib.git] / trivium.c
1 /* trivium.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-lib.
4     Copyright (C) 2008  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
24  * 
25  */
26
27  
28 #include <stdint.h>
29 #include <string.h>
30 #include "trivium.h"
31
32 #define S(i) ((((*ctx)[(i)/8])>>((i)%8))&1)
33 uint8_t trivium_enc(trivium_ctx_t* ctx){
34         uint8_t t1,t2,t3,z;
35         
36         t1 = S(65)  ^ S(92);
37         t2 = S(161) ^ S(176);
38         t3 = S(242) ^ S(287);
39         z  = t1^t2^t3;
40         t1 ^= (S(90)  & S(91))  ^ S(170);
41         t2 ^= (S(174) & S(175)) ^ S(263);
42         t3 ^= (S(285) & S(286)) ^ S(68);
43         
44         /* shift whole state and insert ts later */
45         uint8_t i,c1=0,c2;
46         for(i=0; i<36; ++i){
47                 c2=(((*ctx)[i])>>7);
48                 (*ctx)[i] = (((*ctx)[i])<<1)|c1;
49                 c1=c2;
50         }
51         /* insert ts */
52         (*ctx)[0] = (((*ctx)[0])&0xFE)| t3; /* s0*/
53         (*ctx)[93/8] = (((*ctx)[93/8])& (~(1<<(93%8)))) | (t1<<(93%8)); /* s93 */
54         (*ctx)[177/8] = (((*ctx)[177/8])& (~(1<<(177%8)))) | (t2<<(177%8));/* s177 */
55         
56         return z;
57 }
58
59 #define KEYSIZE_B ((keysize_b+7)/8)
60 #define IVSIZE_B  ((ivsize_b +7)/8)
61
62 void trivium_init(const void* key, uint8_t keysize_b, 
63                   const void* iv,  uint8_t ivsize_b,
64                   trivium_ctx_t* ctx){
65         uint16_t i;
66         uint8_t c1=0,c2;
67
68         memset((*ctx)+KEYSIZE_B, 0, 35-KEYSIZE_B);
69         memcpy((*ctx), key, KEYSIZE_B);
70         memcpy((*ctx)+12, iv, IVSIZE_B); /* iv0 is at s96, must shift to s93 */
71         
72         for(i=12+IVSIZE_B; i>10; --i){
73                 c2=(((*ctx)[i])<<5);
74                 (*ctx)[i] = (((*ctx)[i])>>3)|c1;
75                 c1=c2;
76         }
77         (*ctx)[35]=0xE0;
78         
79         for(i=0; i<4*288; ++i){
80                 trivium_enc(ctx);
81         }
82 }
83
84