]> git.cryptolib.org Git - avr-crypto-lib.git/blob - noekeon.c
+ noekeon
[avr-crypto-lib.git] / noekeon.c
1 /*
2  * author: Daniel Otte
3  * email:  daniel.otte@rub.de
4  * license: GPLv3
5  * 
6  * 
7  * 
8  */
9
10 #include <stdint.h>
11 #include <string.h>
12 #include <avr/pgmspace.h>
13 #include "noekeon.h"
14
15 #define ROUND_NR 16
16
17 #define RC_POS 0
18
19 static
20 void gamma(uint32_t* a){
21         uint32_t tmp;
22         
23         a[1] ^= ~((a[3]) | (a[2]));
24         a[0] ^=   a[2] & a[1];  
25         
26         tmp=a[3]; a[3]=a[0]; a[0]=tmp;
27         a[2] ^= a[0] ^ a[1] ^ a[3];
28         
29         a[1] ^= ~((a[3]) | (a[2]));
30         a[0] ^=   a[2] & a[1];  
31 }       
32
33 #define ROTL32(a,n) (((a)<<n)|((a)>>(32-n)))
34 #define ROTR32(a,n) (((a)>>n)|((a)<<(32-n)))
35
36 static
37 void pi1(uint32_t* a){
38         a[1] = ROTL32(a[1], 1);
39         a[2] = ROTL32(a[2], 5);
40         a[3] = ROTL32(a[3], 2);
41 }
42
43 static
44 void pi2(uint32_t* a){
45         a[1] = ROTR32(a[1], 1);
46         a[2] = ROTR32(a[2], 5);
47         a[3] = ROTR32(a[3], 2);
48 }
49
50 static
51 void theta(uint32_t* k, uint32_t* a){
52         uint32_t temp;
53         temp = a[0] ^ a[2]; temp ^= ROTR32(temp, 8) ^ ROTL32(temp, 8);
54         a[1] ^= temp;
55         a[3] ^= temp;
56         
57         a[0] ^= k[0];
58         a[1] ^= k[1];
59         a[2] ^= k[2];
60         a[3] ^= k[3];
61
62         temp = a[1] ^ a[3]; temp ^= ROTR32(temp, 8) ^ ROTL32(temp, 8);
63         a[0] ^= temp;
64         a[2] ^= temp;   
65 }
66
67 static 
68 void noekeon_round(uint32_t* key, uint32_t* state, uint8_t const1, uint8_t const2){
69         ((uint8_t*)state)[RC_POS] ^= const1;
70         theta(key, state);
71         ((uint8_t*)state)[RC_POS] ^= const2;
72         pi1(state);
73         gamma(state);
74         pi2(state);
75 }
76
77 uint8_t rc_tab[] PROGMEM = {
78 /*      0x80, */
79               0x1B, 0x36, 0x6C, 0xD8, 0xAB, 0x4D, 0x9A,
80         0x2F, 0x5E, 0xBC, 0x63, 0xC6, 0x97, 0x35, 0x6A,
81         0xD4
82 };
83 /* for more rounds
84  0xD4, 0xB3, 0x7D, 0xFA, 0xEF, 0xC5, 0x91, 0x39,
85  0x72, 0xE4, 0xD3, 0xBD, 0x61, 0xC2, 0x9F, 0x25,
86 */
87
88 static
89 void changendian32(void* a){
90         ((uint8_t*)a)[0] ^= ((uint8_t*)a)[3];
91         ((uint8_t*)a)[3] ^= ((uint8_t*)a)[0];
92         ((uint8_t*)a)[0] ^= ((uint8_t*)a)[3];
93         
94         ((uint8_t*)a)[1] ^= ((uint8_t*)a)[2];
95         ((uint8_t*)a)[2] ^= ((uint8_t*)a)[1];
96         ((uint8_t*)a)[1] ^= ((uint8_t*)a)[2];
97 }
98
99 static
100 void changendian(void* a){
101         changendian32((uint32_t*)(&(((uint32_t*)a)[0])));
102         changendian32((uint32_t*)(&(((uint32_t*)a)[1])));
103         changendian32((uint32_t*)(&(((uint32_t*)a)[2])));
104         changendian32((uint32_t*)(&(((uint32_t*)a)[3])));
105 }
106
107 /******************************************************************************/
108
109 void noekeon_enc(void* buffer, void* key){
110         uint8_t rc=0x80;
111         int8_t i;
112         
113         changendian(buffer);
114         changendian(key);
115
116         for(i=0; i<ROUND_NR; ++i){
117                 noekeon_round((uint32_t*)key, (uint32_t*)buffer, rc, 0);
118                 rc = pgm_read_byte(rc_tab+i);
119         }
120         ((uint8_t*)buffer)[RC_POS] ^= rc;
121         theta((uint32_t*)key, (uint32_t*)buffer);
122
123         changendian(buffer);
124         changendian(key);
125 }
126
127
128 void noekeon_dec(void* buffer, void* key){
129         uint8_t rc;
130         int8_t i;
131         uint8_t nullv[16];
132         uint8_t dkey[16];
133         
134
135         changendian(buffer);
136         changendian(key);
137         
138         memset(nullv, 0, 16);
139         memcpy(dkey, key, 16);
140         
141         theta((uint32_t*)nullv, (uint32_t*)dkey);
142         for(i=ROUND_NR-1; i>=0; --i){
143                 rc = pgm_read_byte(rc_tab+i);
144                 noekeon_round((uint32_t*)dkey, (uint32_t*)buffer, 0, rc);
145         }
146         theta((uint32_t*)dkey, (uint32_t*)buffer);
147         ((uint8_t*)buffer)[RC_POS] ^= 0x80;
148
149         changendian(buffer);
150         changendian(key);
151 }
152
153 void noekeon_init(void* key, noekeon_ctx_t* ctx){
154         uint8_t nullv[16];
155         
156         memset(nullv, 0, 16);
157         memcpy(ctx, key, 16);
158         noekeon_enc(ctx, nullv);
159 }
160