#include "cli.h"
#endif
-static uint8_t sbox0[] PROGMEM =
+const static uint8_t sbox0[] PROGMEM =
{ 9, 0, 4, 11, 13, 12, 3, 15, 1, 10, 2, 6, 7, 5, 8, 14 };
-static uint8_t sbox1[] PROGMEM =
+const static uint8_t sbox1[] PROGMEM =
{ 3, 12, 6, 13, 5, 7, 1, 9, 15, 2, 0, 4, 11, 10, 14, 8 };
-static uint8_t round_const_0[] PROGMEM = {
+const static uint8_t round_const_0[] PROGMEM = {
0x6a, 0x09, 0xe6, 0x67, 0xf3, 0xbc, 0xc9, 0x08,
0xb2, 0xfb, 0x13, 0x66, 0xea, 0x95, 0x7d, 0x3e,
0x3a, 0xde, 0xc1, 0x75, 0x12, 0x77, 0x50, 0x99,
}
static
-void jh_round(uint8_t* a, uint8_t* rc){
+void jh_round(uint8_t *a, const uint8_t *rc){
uint8_t b[128];
- uint8_t i,r,x,y;
+ uint8_t i,r=0,x,y;
for(i=0; i<128; ++i){
if(i%4==0){
r = rc[i/4];
}
static
-void jh_next_round_const(uint8_t* a){
+void jh_next_round_const(uint8_t *a){
uint8_t b[32];
uint8_t i,x,y;
for(i=0; i<32; ++i){
}
}
+static const uint8_t idx[]={112,80,48,16,96,64,32,0};
static inline
void group(uint8_t *a){
uint8_t b[128];
- uint8_t i,x,y;
+ uint8_t i,j,k,x=0;
for(i=0; i<128; ++i){
- x = (((a[i/8+ 0])>>4)&0x8)
- | (((a[i/8+ 32])>>5)&0x4)
- | (((a[i/8+ 64])>>6)&0x2)
- | (((a[i/8+ 96])>>7)&0x1);
- a[i/8] <<= 1; a[i/8+32]<<=1; a[i/8+64]<<=1; a[i/8+96]<<=1;
- y = (((a[i/8+ 16])>>4)&0x8)
- | (((a[i/8+ 48])>>5)&0x4)
- | (((a[i/8+ 80])>>6)&0x2)
- | (((a[i/8+112])>>7)&0x1);
- a[i/8+16] <<= 1; a[i/8+48]<<=1; a[i/8+80]<<=1; a[i/8+112]<<=1;
- b[i]= (x<<4)|y;
+ j=i/8;
+ for(k=0;k<8;++k){
+ x>>=1;
+ x |= a[j+idx[k]]&0x80;
+ a[j+idx[k]] <<= 1;
+ }
+ b[i]= x;
}
memcpy(a,b,128);
}
static inline
void degroup(uint8_t *a){
uint8_t b[128];
- static uint8_t idx[]={112,80,48,16,96,64,32,0};
uint8_t i,j,k,t;
for(i=0;i<128;++i){
j=i/8;
t = a[i];
for(k=0; k<8; ++k){
- b[j+idx[k]]<<=1; b[j+idx[k]] |= t&1; t>>=1;
+ b[j+idx[k]]<<=1;
+ b[j+idx[k]] |= t&1;
+ t>>=1;
}
}
memcpy(a,b,128);
}
-void jh_encrypt(uint8_t* a){
+void jh_encrypt(uint8_t *a){
uint8_t i;
uint8_t rc[32];
/* grouping */
for(i=0;i<32;++i){
rc[i] = pgm_read_byte(&(round_const_0[i]));
}
- for(i=0;i<35;++i){
+ for(i=0;i<42;++i){
jh_round(a, rc);
jh_next_round_const(rc);
}
- uint8_t r,x,y;
- for(i=0; i<128; ++i){
- if(i%4==0){
- r = rc[i/4];
- }
- x = pgm_read_byte(((r&0x80)?sbox1:sbox0)+(a[i]>>4));
- y = pgm_read_byte(((r&0x40)?sbox1:sbox0)+(a[i]&0xf));
- a[i]=(x<<4)|y;
- r<<=2;
- }
/* degrouping */
#if DEBUG
cli_putstr_P(PSTR("\r\n== pre degroup ==\r\n"));