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