]> git.cryptolib.org Git - avr-crypto-lib.git/blob - jh/jh_simple_speed_core.c
removed warnings and bugs
[avr-crypto-lib.git] / jh / jh_simple_speed_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 #include "jh_tables.h"
27
28 #define DEBUG 0
29
30 #if DEBUG
31 #include "cli.h"
32 #endif
33
34 static
35 void jh_round(uint8_t* a, uint8_t roundno){
36         uint8_t b[128];
37         uint8_t i,r,u,v,x,y;
38         uint8_t *pr;
39         pr = jh_round_const + 32*roundno;
40         for(i=0; i<128; ++i){
41                 if(i%4==0){
42                         r = pgm_read_byte(pr++);
43                 }
44                 b[i]=pgm_read_byte(&(jh_lutbox[((r&0xC0)<<2)|a[i]]));
45                 r<<=2;
46         }
47         for(i=0;i<128;++i){
48                 u = pgm_read_byte(jh_permutation_table+2*i);
49                 v = pgm_read_byte(jh_permutation_table+2*i+1);
50                 x = b[u>>1];
51                 y = b[v>>1];
52                 if(u&1){
53                         x <<= 4;
54                 }else{
55                         x &= 0xf0;
56                 }
57                 if(v&1){
58                         y &= 0x0f;
59                 }else{
60                         y >>= 4;
61                 }
62                 a[i] = x|y;
63         }
64 }
65
66 static
67 uint8_t jh_l_inv(uint8_t a){
68         uint8_t v,w;
69         v = a>>4;
70         w = a&0xf;
71         v ^= ((w<<1)^(w>>3)^((w>>2)&2))&0xf;
72         w ^= ((v<<1)^(v>>3)^((v>>2)&2))&0xf;
73         return w|(v<<4);
74 }
75
76 static inline
77 void group(uint8_t *a){
78         uint8_t b[128];
79         uint8_t i,x,y;
80         for(i=0; i<128; ++i){
81                 x =   (((a[i/8+  0])>>4)&0x8)
82                         | (((a[i/8+ 32])>>5)&0x4)
83                         | (((a[i/8+ 64])>>6)&0x2)
84                         | (((a[i/8+ 96])>>7)&0x1);
85                 a[i/8] <<= 1; a[i/8+32]<<=1; a[i/8+64]<<=1; a[i/8+96]<<=1;
86                 y =   (((a[i/8+ 16])>>4)&0x8)
87                     | (((a[i/8+ 48])>>5)&0x4)
88                     | (((a[i/8+ 80])>>6)&0x2)
89                     | (((a[i/8+112])>>7)&0x1);
90                 a[i/8+16] <<= 1; a[i/8+48]<<=1; a[i/8+80]<<=1; a[i/8+112]<<=1;
91                 b[i]= (x<<4)|y;
92         }
93         memcpy(a,b,128);
94 }
95
96 static inline
97 void degroup(uint8_t *a){
98         uint8_t b[128];
99         uint8_t i,j;
100         for(i=0;i<128;++i){
101                 j=i/8;
102             b[j+  0]<<=1; b[j+  0] |= ((a[i])>>7)&1;
103             b[j+ 32]<<=1; b[j+ 32] |= ((a[i])>>6)&1;
104             b[j+ 64]<<=1; b[j+ 64] |= ((a[i])>>5)&1;
105             b[j+ 96]<<=1; b[j+ 96] |= ((a[i])>>4)&1;
106             b[j+ 16]<<=1; b[j+ 16] |= ((a[i])>>3)&1;
107             b[j+ 48]<<=1; b[j+ 48] |= ((a[i])>>2)&1;
108             b[j+ 80]<<=1; b[j+ 80] |= ((a[i])>>1)&1;
109             b[j+112]<<=1; b[j+112] |= ((a[i])>>0)&1;
110         }
111         memcpy(a,b,128);
112 }
113
114 void jh_encrypt(uint8_t* a){
115         uint8_t i;
116         /* grouping */
117 #if DEBUG
118         cli_putstr_P(PSTR("\r\n== pre group ==\r\n"));
119         cli_hexdump_block(a, 128, 4, 16);
120 #endif
121         group(a);
122         for(i=0;i<35;++i){
123                 jh_round(a, i);
124         }
125         uint8_t r=0;
126         uint8_t *pr;
127
128         pr = jh_round_const + 32*35;
129         for(i=0; i<128; ++i){
130                 if(i%4==0){
131                         r = pgm_read_byte(pr++);
132                 }
133                 a[i]=jh_l_inv(pgm_read_byte(&(jh_lutbox[((r&0xC0)<<2)|a[i]])));
134                 r<<=2;
135         }
136         /* degrouping */
137 #if DEBUG
138         cli_putstr_P(PSTR("\r\n== pre degroup ==\r\n"));
139         cli_hexdump_block(a, 128, 4, 16);
140 #endif
141         degroup(a);
142 #if DEBUG
143         cli_putstr_P(PSTR("\r\n== post degroup ==\r\n"));
144         cli_hexdump_block(a, 128, 4, 16);
145 #endif
146 }
147
148