1 /* jh_simple_speed.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
21 #include <avr/pgmspace.h>
25 #include "jh_simple.h"
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 };
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,
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;
53 void jh_round(uint8_t* a, const uint8_t* rc){
60 x = pgm_read_byte(((r&0x80)?sbox1:sbox0)+(a[i]>>4));
61 y = pgm_read_byte(((r&0x40)?sbox1:sbox0)+(a[i]&0xf));
66 for(i=1; i<128; i+=2){
67 a[i] = (a[i]<<4)|(a[i]>>4);
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);
76 for(i=64; i<128; i+=1){
77 a[i] = (b[i]<<4)|(b[i]>>4);
82 void jh_next_round_const(uint8_t* a){
86 x = pgm_read_byte(sbox0+(a[i]>>4));
87 y = pgm_read_byte(sbox0+(a[i]&0xf));
92 a[i] = (a[i]<<4)|(a[i]>>4);
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);
100 /* phi permutation */
101 for(i=16; i<32; i+=1){
102 a[i] = (b[i]<<4)|(b[i]>>4);
106 static const uint8_t idx[]={112,80,48,16,96,64,32,0};
110 void group(uint8_t *a){
113 for(i=0; i<128; ++i){
117 x |= a[j+idx[k]]&0x80;
126 void degroup(uint8_t *a){
141 void jh_encrypt(uint8_t* a){
146 cli_putstr_P(PSTR("\r\n== pre group ==\r\n"));
147 cli_hexdump_block(a, 128, 4, 16);
151 rc[i] = pgm_read_byte(&(round_const_0[i]));
155 jh_next_round_const(rc);
160 cli_putstr_P(PSTR("\r\n== pre degroup ==\r\n"));
161 cli_hexdump_block(a, 128, 4, 16);
165 cli_putstr_P(PSTR("\r\n== post degroup ==\r\n"));
166 cli_hexdump_block(a, 128, 4, 16);