]> git.cryptolib.org Git - avr-crypto-lib.git/blob - jh/jh_simple_small_core.c
c3c97aed8cea63c1dd143f9540db0f27dc721357
[avr-crypto-lib.git] / jh / jh_simple_small_core.c
1 /* jh_simple_speed.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2010 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 #include <stdint.h>
21 #include <avr/pgmspace.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "memxor.h"
25 #include "jh_simple.h"
26
27 #define DEBUG 0
28
29 #if DEBUG
30 #include "cli.h"
31 #endif
32
33 static uint8_t sbox0[] PROGMEM =
34         {  9,  0,  4, 11, 13, 12,  3, 15,  1, 10,  2,  6,  7,  5,  8, 14 };
35 static uint8_t sbox1[] PROGMEM =
36         {  3, 12,  6, 13,  5,  7,  1,  9, 15,  2,  0,  4, 11, 10, 14,  8 };
37
38 static uint8_t round_const_0[] PROGMEM = {
39   0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08,
40   0xb2, 0xfb, 0x13, 0x66, 0xea, 0x95, 0x7d, 0x3e,
41   0x3a, 0xde, 0xc1, 0x75, 0x12, 0x77, 0x50, 0x99,
42   0xda, 0x2f, 0x59, 0x0b, 0x06, 0x67, 0x32, 0x2a,
43 };
44
45 static
46 uint8_t jh_l(uint8_t v, uint8_t w){
47         v ^= ((w<<1)^(w>>3)^((w>>2)&2))&0xf;
48         w ^= ((v<<1)^(v>>3)^((v>>2)&2))&0xf;
49         return v|(w<<4);
50 }
51
52 static
53 void jh_round(uint8_t* a, uint8_t* rc){
54         uint8_t b[128];
55         uint8_t i,r,x,y;
56         for(i=0; i<128; ++i){
57                 if(i%4==0){
58                         r = rc[i/4];
59                 }
60                 x = pgm_read_byte(((r&0x80)?sbox1:sbox0)+(a[i]>>4));
61                 y = pgm_read_byte(((r&0x40)?sbox1:sbox0)+(a[i]&0xf));
62                 a[i]=jh_l(y,x);
63                 r<<=2;
64         }
65         /* pi permutation */
66         for(i=1; i<128; i+=2){
67                 a[i] = (a[i]<<4)|(a[i]>>4);
68         }
69         /* P' permutation */
70         for(i=0; i<64; ++i){
71                 b[i] = (a[i*2]&0xF0) | (a[i*2+1]>>4);
72                 b[64+i] = (a[i*2]<<4) | (a[i*2+1]&0x0F);
73         }
74         memcpy(a,b,64);
75         /* phi permutation */
76         for(i=64; i<128; i+=1){
77                 a[i] = (b[i]<<4)|(b[i]>>4);
78         }
79 }
80
81 static
82 void jh_next_round_const(uint8_t* a){
83         uint8_t b[32];
84         uint8_t i,x,y;
85         for(i=0; i<32; ++i){
86                 x = pgm_read_byte(sbox0+(a[i]>>4));
87                 y = pgm_read_byte(sbox0+(a[i]&0xf));
88                 a[i]=jh_l(y,x);
89         }
90         /* pi permutation */
91         for(i=1; i<32; i+=2){
92                 a[i] = (a[i]<<4)|(a[i]>>4);
93         }
94         /* P' permutation */
95         for(i=0; i<16; ++i){
96                 b[i] = (a[i*2]&0xF0) | (a[i*2+1]>>4);
97                 b[16+i] = (a[i*2]<<4) | (a[i*2+1]&0x0F);
98         }
99         memcpy(a,b,16);
100         /* phi permutation */
101         for(i=16; i<32; i+=1){
102                 a[i] = (b[i]<<4)|(b[i]>>4);
103         }
104 }
105
106
107
108 static inline
109 void group(uint8_t *a){
110         uint8_t b[128];
111         uint8_t i,x,y;
112         for(i=0; i<128; ++i){
113                 x =   (((a[i/8+  0])>>4)&0x8)
114                         | (((a[i/8+ 32])>>5)&0x4)
115                         | (((a[i/8+ 64])>>6)&0x2)
116                         | (((a[i/8+ 96])>>7)&0x1);
117                 a[i/8] <<= 1; a[i/8+32]<<=1; a[i/8+64]<<=1; a[i/8+96]<<=1;
118                 y =   (((a[i/8+ 16])>>4)&0x8)
119                     | (((a[i/8+ 48])>>5)&0x4)
120                     | (((a[i/8+ 80])>>6)&0x2)
121                     | (((a[i/8+112])>>7)&0x1);
122                 a[i/8+16] <<= 1; a[i/8+48]<<=1; a[i/8+80]<<=1; a[i/8+112]<<=1;
123                 b[i]= (x<<4)|y;
124         }
125         memcpy(a,b,128);
126 }
127
128 static inline
129 void degroup(uint8_t *a){
130         uint8_t b[128];
131         static uint8_t idx[]={112,80,48,16,96,64,32,0};
132         uint8_t i,j,k,t;
133         for(i=0;i<128;++i){
134                 j=i/8;
135                 t = a[i];
136                 for(k=0; k<8; ++k){
137                         b[j+idx[k]]<<=1; b[j+idx[k]] |= t&1; t>>=1;
138                 }
139         }
140         memcpy(a,b,128);
141 }
142
143 void jh_encrypt(uint8_t* a){
144         uint8_t i;
145         uint8_t rc[32];
146         /* grouping */
147 #if DEBUG
148         cli_putstr_P(PSTR("\r\n== pre group ==\r\n"));
149         cli_hexdump_block(a, 128, 4, 16);
150 #endif
151         group(a);
152         for(i=0;i<32;++i){
153                 rc[i] = pgm_read_byte(&(round_const_0[i]));
154         }
155         for(i=0;i<35;++i){
156                 jh_round(a, rc);
157                 jh_next_round_const(rc);
158         }
159         uint8_t r,x,y;
160
161         for(i=0; i<128; ++i){
162                 if(i%4==0){
163                         r = rc[i/4];
164                 }
165                 x = pgm_read_byte(((r&0x80)?sbox1:sbox0)+(a[i]>>4));
166                 y = pgm_read_byte(((r&0x40)?sbox1:sbox0)+(a[i]&0xf));
167                 a[i]=(x<<4)|y;
168                 r<<=2;
169         }
170         /* degrouping */
171 #if DEBUG
172         cli_putstr_P(PSTR("\r\n== pre degroup ==\r\n"));
173         cli_hexdump_block(a, 128, 4, 16);
174 #endif
175         degroup(a);
176 #if DEBUG
177         cli_putstr_P(PSTR("\r\n== post degroup ==\r\n"));
178         cli_hexdump_block(a, 128, 4, 16);
179 #endif
180 }
181
182