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