]> git.cryptolib.org Git - avr-crypto-lib.git/blob - jh/jh_simple_small_core.c
fixing E-Mail-Address & Copyright
[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-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 #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 const static uint8_t sbox0[] PROGMEM =
34         {  9,  0,  4, 11, 13, 12,  3, 15,  1, 10,  2,  6,  7,  5,  8, 14 };
35 const static uint8_t sbox1[] PROGMEM =
36         {  3, 12,  6, 13,  5,  7,  1,  9, 15,  2,  0,  4, 11, 10, 14,  8 };
37
38 const 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, const uint8_t *rc){
54         uint8_t b[128];
55         uint8_t i,r=0,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 static const uint8_t idx[]={112,80,48,16,96,64,32,0};
107
108
109 static inline
110 void group(uint8_t *a){
111         uint8_t b[128];
112         uint8_t i,j,k,x=0;
113         for(i=0; i<128; ++i){
114                 j=i/8;
115                 for(k=0;k<8;++k){
116                         x>>=1;
117                         x |= a[j+idx[k]]&0x80;
118                         a[j+idx[k]] <<= 1;
119                 }
120                 b[i]= x;
121         }
122         memcpy(a,b,128);
123 }
124
125 static inline
126 void degroup(uint8_t *a){
127         uint8_t b[128];
128         uint8_t i,j,k,t;
129         for(i=0;i<128;++i){
130                 j=i/8;
131                 t = a[i];
132                 for(k=0; k<8; ++k){
133                         b[j+idx[k]]<<=1;
134                         b[j+idx[k]] |= t&1;
135                         t>>=1;
136                 }
137         }
138         memcpy(a,b,128);
139 }
140
141 void jh_encrypt(uint8_t *a){
142         uint8_t i;
143         uint8_t rc[32];
144         /* grouping */
145 #if DEBUG
146         cli_putstr_P(PSTR("\r\n== pre group ==\r\n"));
147         cli_hexdump_block(a, 128, 4, 16);
148 #endif
149         group(a);
150         for(i=0;i<32;++i){
151                 rc[i] = pgm_read_byte(&(round_const_0[i]));
152         }
153         for(i=0;i<42;++i){
154                 jh_round(a, rc);
155                 jh_next_round_const(rc);
156         }
157
158         /* degrouping */
159 #if DEBUG
160         cli_putstr_P(PSTR("\r\n== pre degroup ==\r\n"));
161         cli_hexdump_block(a, 128, 4, 16);
162 #endif
163         degroup(a);
164 #if DEBUG
165         cli_putstr_P(PSTR("\r\n== post degroup ==\r\n"));
166         cli_hexdump_block(a, 128, 4, 16);
167 #endif
168 }
169
170