adding CS-Cipher

This commit is contained in:
bg 2010-12-18 23:47:24 +00:00
parent d7838aec00
commit 0542221a3e
20 changed files with 7404 additions and 4 deletions

View File

@ -94,7 +94,7 @@ $(foreach a, $(ALGORITHMS), \
$(BIN_DIR)$(call lc, $(a))/$(b), \
$(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
$($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
$($(a)_DEFINES), \
$($(a)_DEF), \
)) \
) \
)
@ -105,7 +105,7 @@ $(foreach a, $(ALGORITHMS), \
$(BIN_DIR)$(call lc, $(a))/$(TEST_DIR)$(b), \
$(call find_source_file, $(b), $($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR) ),\
$($(a)_DIR) $($(a)_INCDIR) $(GLOBAL_INCDIR), \
$($(a)_DEFINES) \
$($(a)_DEF) \
)) \
) \
)

View File

@ -14,7 +14,7 @@ TESTSRC_DIR = test_src/
#uisp -dprog=bsd -dlpt=/dev/parport1 --upload if=$(PRG).hex
ERASECMD =
TESTPORT = /dev/ttyUSB1
TESTPORTBAUDR = 38400
TESTPORTBAUDR = 115200
TESTLOG_DIR = testlog/#
TESTPREFIX = nessie-
SPEEDTOOL = host/get_performance.rb

53
bcal/bcal_cscipher.c Normal file
View File

@ -0,0 +1,53 @@
/* bcal_cscipher.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file bcal_cscipher.c
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2010-12-17
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include <stdlib.h>
#include "blockcipher_descriptor.h"
#include "cscipher.h"
#include "keysize_descriptor.h"
const char cscipher_str[] PROGMEM = "CS-Cipher";
const uint8_t cscipher_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(128),
KS_TYPE_TERMINATOR };
const bcdesc_t cscipher_desc PROGMEM = {
BCDESC_TYPE_BLOCKCIPHER,
BC_INIT_TYPE_1,
cscipher_str,
sizeof(cscipher_ctx_t),
64,
{(void_fpt)cscipher_init},
{(void_fpt)cscipher_enc},
{(void_fpt)cscipher_dec},
(bc_free_fpt)NULL,
cscipher_keysize_desc
};

33
bcal/bcal_cscipher.h Normal file
View File

@ -0,0 +1,33 @@
/* bcal_cscipher.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file bcal_cscipher.h
* \email daniel.otte@rub.de
* \author Daniel Otte
* \date 2010-12-17
* \license GPLv3 or later
*
*/
#include <avr/pgmspace.h>
#include "blockcipher_descriptor.h"
#include "cscipher.h"
#include "keysize_descriptor.h"
extern const bcdesc_t cscipher_desc;

32
cscipher/cscipher.h Normal file
View File

@ -0,0 +1,32 @@
/* cscipher.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CSCIPHER_H_
#define CSCIPHER_H_
typedef struct {
uint8_t keys[9][8];
} cscipher_ctx_t;
void cscipher_enc(void* buffer, const cscipher_ctx_t* ctx);
void cscipher_dec(void* buffer, const cscipher_ctx_t* ctx);
void cscipher_init(void* key, cscipher_ctx_t* ctx);
#endif /* CSCIPHER_H_ */

40
cscipher/cscipher_sbox.c Normal file
View File

@ -0,0 +1,40 @@
/* cscipher_sbox.c */
/*
This file is part of the AVM-Crypto-Lib.
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <avr/pgmspace.h>
uint8_t cscipher_sbox[] PROGMEM = {
0x29, 0x0d, 0x61, 0x40, 0x9c, 0xeb, 0x9e, 0x8f, 0x1f, 0x85, 0x5f, 0x58, 0x5b, 0x01, 0x39, 0x86,
0x97, 0x2e, 0xd7, 0xd6, 0x35, 0xae, 0x17, 0x16, 0x21, 0xb6, 0x69, 0x4e, 0xa5, 0x72, 0x87, 0x08,
0x3c, 0x18, 0xe6, 0xe7, 0xfa, 0xad, 0xb8, 0x89, 0xb7, 0x00, 0xf7, 0x6f, 0x73, 0x84, 0x11, 0x63,
0x3f, 0x96, 0x7f, 0x6e, 0xbf, 0x14, 0x9d, 0xac, 0xa4, 0x0e, 0x7e, 0xf6, 0x20, 0x4a, 0x62, 0x30,
0x03, 0xc5, 0x4b, 0x5a, 0x46, 0xa3, 0x44, 0x65, 0x7d, 0x4d, 0x3d, 0x42, 0x79, 0x49, 0x1b, 0x5c,
0xf5, 0x6c, 0xb5, 0x94, 0x54, 0xff, 0x56, 0x57, 0x0b, 0xf4, 0x43, 0x0c, 0x4f, 0x70, 0x6d, 0x0a,
0xe4, 0x02, 0x3e, 0x2f, 0xa2, 0x47, 0xe0, 0xc1, 0xd5, 0x1a, 0x95, 0xa7, 0x51, 0x5e, 0x33, 0x2b,
0x5d, 0xd4, 0x1d, 0x2c, 0xee, 0x75, 0xec, 0xdd, 0x7c, 0x4c, 0xa6, 0xb4, 0x78, 0x48, 0x3a, 0x32,
0x98, 0xaf, 0xc0, 0xe1, 0x2d, 0x09, 0x0f, 0x1e, 0xb9, 0x27, 0x8a, 0xe9, 0xbd, 0xe3, 0x9f, 0x07,
0xb1, 0xea, 0x92, 0x93, 0x53, 0x6a, 0x31, 0x10, 0x80, 0xf2, 0xd8, 0x9b, 0x04, 0x36, 0x06, 0x8e,
0xbe, 0xa9, 0x64, 0x45, 0x38, 0x1c, 0x7a, 0x6b, 0xf3, 0xa1, 0xf0, 0xcd, 0x37, 0x25, 0x15, 0x81,
0xfb, 0x90, 0xe8, 0xd9, 0x7b, 0x52, 0x19, 0x28, 0x26, 0x88, 0xfc, 0xd1, 0xe2, 0x8c, 0xa0, 0x34,
0x82, 0x67, 0xda, 0xcb, 0xc7, 0x41, 0xe5, 0xc4, 0xc8, 0xef, 0xdb, 0xc3, 0xcc, 0xab, 0xce, 0xed,
0xd0, 0xbb, 0xd3, 0xd2, 0x71, 0x68, 0x13, 0x12, 0x9a, 0xb3, 0xc2, 0xca, 0xde, 0x77, 0xdc, 0xdf,
0x66, 0x83, 0xbc, 0x8d, 0x60, 0xc6, 0x22, 0x23, 0xb2, 0x8b, 0x91, 0x05, 0x76, 0xcf, 0x74, 0xc9,
0xaa, 0xf1, 0x99, 0xa8, 0x59, 0x50, 0x3b, 0x2a, 0xfe, 0xf9, 0x24, 0xb0, 0xba, 0xfd, 0xf8, 0x55
};

29
cscipher/cscipher_sbox.h Normal file
View File

@ -0,0 +1,29 @@
/* cscipher_sbox.h */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CSCIPHER_SBOX_H_
#define CSCIPHER_SBOX_H_
#include <stdint.h>
#include <avr/pgmspace.h>
extern uint8_t cscipher_sbox[];
#endif /* CSCIPHER_SBOX_H_ */

175
cscipher/cscipher_small.c Normal file
View File

@ -0,0 +1,175 @@
/* cscipher_small_core.c */
/*
This file is part of the ARM-Crypto-Lib.
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <avr/pgmspace.h>
#include <stdlib.h>
#include <string.h>
#include "memxor.h"
#include "cscipher.h"
#define DEBUG 0
#if DEBUG
#include "cli.h"
#endif
#define ROTL(a) (((a)<<1)|((a)>>7))
#ifndef SBOX_PROG
#define SBOX_PROG 0
#endif
#if SBOX_PROG
static const uint8_t fg_table[] PROGMEM = {
0xfa, 0xd6, 0xb0, 0xb2, 0x7b, 0x5e, 0x71, 0x78,
0xed, 0xd4, 0xa5, 0xb3, 0xef, 0xdc, 0xe7, 0xf9
};
static
uint8_t p(uint8_t a){
a ^= pgm_read_byte(fg_table+(a&0xf))&0xf0;
a ^= pgm_read_byte(fg_table+(a>>4)) &0x0f;
a ^= pgm_read_byte(fg_table+(a&0xf))&0xf0;
return a;
}
#define P(a) p(a)
#else
#include "cscipher_sbox.h"
#define P(a) pgm_read_byte(cscipher_sbox+(a))
#endif
static uint8_t round_const[] PROGMEM = {
0xb7, 0xe1, 0x51, 0x62, 0x8a, 0xed, 0x2a, 0x6a,
0xbf, 0x71, 0x58, 0x80, 0x9c, 0xf4, 0xf3, 0xc7 };
static uint8_t ks_const[] PROGMEM = {
0x29,0x0d,0x61,0x40,0x9c,0xeb,0x9e,0x8f,
0x1f,0x85,0x5f,0x58,0x5b,0x01,0x39,0x86,
0x97,0x2e,0xd7,0xd6,0x35,0xae,0x17,0x16,
0x21,0xb6,0x69,0x4e,0xa5,0x72,0x87,0x08,
0x3c,0x18,0xe6,0xe7,0xfa,0xad,0xb8,0x89,
0xb7,0x00,0xf7,0x6f,0x73,0x84,0x11,0x63,
0x3f,0x96,0x7f,0x6e,0xbf,0x14,0x9d,0xac,
0xa4,0x0e,0x7e,0xf6,0x20,0x4a,0x62,0x30,
0x03,0xc5,0x4b,0x5a,0x46,0xa3,0x44,0x65
};
static uint16_t m(uint16_t a){
uint8_t xl, xr, yl, yr;
uint16_t ret;
xr = a>>8;
xl = a&0xff;
yl = (ROTL(xl)&0x55)^xl^xr;
yr = ROTL(xl)^xr;
ret = (P(yr)<<8)|P(yl);
return ret;
}
static uint16_t m_inv(uint16_t a){
uint8_t xl, xr;
xr = P(a>>8);
xl = P(a&0xff);
xl ^= xr;
xl ^= (ROTL(xl)&0xaa);
xr ^= ROTL(xl);
return (xr<<8)|xl;
}
void cscipher_enc(void* buffer, const cscipher_ctx_t* ctx){
uint8_t i,j,k;
uint8_t tmp[8];
for(i=0; i<8; ++i){
#if DEBUG
cli_putstr_P(PSTR("\r\nDBG: round "));
cli_hexdump(&i, 1);
cli_putstr_P(PSTR(" buffer:"));
cli_hexdump(buffer, 8);
#endif
for(j=0; j<3; ++j){
if(j==0){
memxor(buffer, ctx->keys[i], 8);
}else{
memxor_P(buffer, round_const+((j==1)?0:8), 8);
}
for(k=0; k<4; ++k){
((uint16_t*)tmp)[k] = m(((uint16_t*)buffer)[k]);
}
for(k=0; k<4; ++k){
((uint8_t*)buffer)[k] = tmp[2*k];
((uint8_t*)buffer)[k+4] = tmp[2*k+1];
}
}
}
memxor(buffer, ctx->keys[8], 8);
}
void cscipher_dec(void* buffer, const cscipher_ctx_t* ctx){
uint8_t i=7,j,k;
uint8_t tmp[8];
memxor(buffer, ctx->keys[8], 8);
do{
for(j=0; j<3; ++j){
for(k=0; k<4; ++k){
tmp[2*k] = ((uint8_t*)buffer)[k];
tmp[2*k+1] = ((uint8_t*)buffer)[4+k];
}
for(k=0; k<4; ++k){
((uint16_t*)buffer)[k] = m_inv(((uint16_t*)tmp)[k]);
}
if(j==2){
memxor(buffer, ctx->keys[i], 8);
}else{
memxor_P(buffer, round_const+((j==1)?0:8), 8);
}
}
}while(i--);
}
void cscipher_init(void* key, cscipher_ctx_t* ctx){
uint8_t tmp_key[16], tmp[8];
uint8_t i,j,k,t;
memcpy(tmp_key, key, 16);
for(i=0; i<9; ++i){
#if DEBUG
cli_putstr_P(PSTR("\r\nDBG: round "));
cli_hexdump(&i, 1);
cli_putstr_P(PSTR(" key state:"));
cli_hexdump(tmp_key, 16);
#endif
memcpy(tmp, tmp_key+(((i&1)==0)?0:8), 8);
memxor_P(tmp, ks_const+8*i, 8);
for(j=0; j<8; ++j){
tmp[j] = P(tmp[j]);
}
for(j=0; j<8; ++j){
for(k=0; k<8; ++k){
t<<=1;
t |= tmp[k]>>7;
tmp[k]<<=1;
}
tmp_key[j+(((i&1)==0)?8:0)] ^= t;
}
memcpy(ctx->keys[i], tmp_key+(((i&1)==0)?8:0), 8);
}
}

View File

@ -0,0 +1,399 @@
/* cscipher_tiny_asm.S */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "avr-asm-macros.S"
/*
uint8_t p(uint8_t a){
a ^= pgm_read_byte(fg_table+(a&0xf))&0xf0;
a ^= pgm_read_byte(fg_table+(a>>4)) &0x0f;
a ^= pgm_read_byte(fg_table+(a&0xf))&0xf0;
return a;
}
*/
fg_table:
.byte 0xfa, 0xd6, 0xb0, 0xb2, 0x7b, 0x5e, 0x71, 0x78
.byte 0xed, 0xd4, 0xa5, 0xb3, 0xef, 0xdc, 0xe7, 0xf9
.global p
p:
ldi r30, lo8(fg_table)
ldi r31, hi8(fg_table)
movw r26, r30
mov r25, r24
andi r25, 0x0F
add r30, r25
adc r31, r1
lpm r25, Z
andi r25, 0xF0
eor r24, r25
movw r30, r26
mov r25, r24
swap r25
andi r25, 0x0F
add r30, r25
adc r31, r1
lpm r25, Z
andi r25, 0x0F
eor r24, r25
movw r30, r26
mov r25, r24
andi r25, 0x0F
add r30, r25
adc r31, r1
lpm r25, Z
andi r25, 0xF0
eor r24, r25
clr r25
ret
ks_const:
.byte 0x29,0x0d,0x61,0x40,0x9c,0xeb,0x9e,0x8f
.byte 0x1f,0x85,0x5f,0x58,0x5b,0x01,0x39,0x86
.byte 0x97,0x2e,0xd7,0xd6,0x35,0xae,0x17,0x16
.byte 0x21,0xb6,0x69,0x4e,0xa5,0x72,0x87,0x08
.byte 0x3c,0x18,0xe6,0xe7,0xfa,0xad,0xb8,0x89
.byte 0xb7,0x00,0xf7,0x6f,0x73,0x84,0x11,0x63
.byte 0x3f,0x96,0x7f,0x6e,0xbf,0x14,0x9d,0xac
.byte 0xa4,0x0e,0x7e,0xf6,0x20,0x4a,0x62,0x30
.byte 0x03,0xc5,0x4b,0x5a,0x46,0xa3,0x44,0x65
CTX_0 = 18
CTX_1 = 19
CNT = 17
.global cscipher_init
cscipher_init:
push CNT
push_range 28, 29
stack_alloc 24, 28, 29
adiw r28, 1
movw r30, r24
movw CTX_0, r22
/* copy key to local tmp_key */
ldi r22, 16
10: ld r23, Z+
st Y+, r23
dec r22
brne 10b
sbiw r28, 16
ldi CNT, 0xff
10: /* main loop */
inc CNT
/* copy part of tmp_key to tmp */
ldi r23, 8
11: ldd r22, Y+0
sbrc CNT, 0
ldd r22, Y+8
std Y+16, r22
adiw r28, 1
dec r23
brne 11b
adiw r28, 8 /* Y points at tmp */
/* xor ks constant into tmp */
movw r24, r28
ldi r22, lo8(ks_const)
ldi r23, hi8(ks_const)
mov r21, CNT
swap r21
lsr r21
add r22, r21
adc r23, r1
clr r21
ldi r20, 8
call memxor_P
/* do P transformation */
ldi r22, 8
20: ld r24, Y
rcall p
st Y+, r24
dec r22
brne 20b
sbiw r28, 8 /* Y points at tmp */
movw r26, r28
sbiw r26, 8
sbrc CNT, 0
sbiw r26, 8
/* do T transformation */
movw r30, CTX_0
ldi r22, 8
30: ldi r23, 8
35: ld r24, Y
rol r24
rol r21
st Y+, r24
dec r23
brne 35b
sbiw r28, 8 /* Y points at tmp */
ld r24, X
eor r21, r24
st X+, r21
st Z+, r21
dec r22
brne 30b
sbiw r28, 16 /* Y points at tmp_key (again) */
movw CTX_0, r30
sbrs CNT, 3
rjmp 10b
stack_free 24
pop_range 28, 29
pop CNT
ret
round_const:
.byte 0xb7, 0xe1, 0x51, 0x62, 0x8a, 0xed, 0x2a, 0x6a
.byte 0xbf, 0x71, 0x58, 0x80, 0x9c, 0xf4, 0xf3, 0xc7
/*
void cscipher_enc(void* buffer, const cscipher_ctx_t* ctx){
uint8_t i,j,k;
uint8_t tmp[8];
for(i=0; i<8; ++i){
for(j=0; j<3; ++j){
if(j==0){
memxor(buffer, ctx->keys[i], 8);
}else{
memxor_P(buffer, round_const+((j==1)?0:8), 8);
}
for(k=0; k<4; ++k){
((uint16_t*)tmp)[k] = m(((uint16_t*)buffer)[k]);
}
for(k=0; k<4; ++k){
((uint8_t*)buffer)[k] = tmp[2*k];
((uint8_t*)buffer)[k+4] = tmp[2*k+1];
}
}
}
memxor(buffer, ctx->keys[8], 8);
}
*/
TMP_0 = 2
TMP_1 = 3
TMP_2 = 4
TMP_3 = 5
TMP_4 = 6
TMP_5 = 7
TMP_6 = 8
TMP_7 = 9
CTX_0 = 10
CTX_1 = 11
CNT_0 = 16
CNT_1 = 17
DST_0 = 12
DST_1 = 13
SRC_0 = 14
SRC_1 = 15
.global cscipher_enc
cscipher_enc:
push_range 2, 17
push_range 28, 29
movw r28, r24
movw CTX_0, r22
ldi CNT_0, 8
/* main loop */
10: ldi CNT_1, 2
clt
/* sub loop */
20: ldi r27, 0
ldi r26, TMP_0
movw DST_0, r26
ldi r30, lo8(round_const)
ldi r31, hi8(round_const)
sbrs CNT_1, 0
adiw r30, 8
sbrc CNT_1, 1
movw r30, CTX_0
movw SRC_0, r30
ldi r21, 4
/* xor and m transformation */
25: ld r24, Y+
ld r25, Y+
movw r30, SRC_0
brts 30f
ld r22, Z+
ld r23, Z+
rjmp 35f
30: lpm r22, Z+
lpm r23, Z+
35:
movw SRC_0, r30
eor r24, r22
eor r25, r23
movw r22, r24
mov r25, r22
rol r25
adc r25, r1
mov r22, r25
andi r22, 0x55
eor r22, r24
eor r22, r23
eor r23, r25
mov r24, r23
rcall p
mov r23, r24
mov r24, r22
rcall p
movw r26, DST_0
st X+, r24
st X+, r23
movw DST_0, r26
dec r21
brne 25b
sbrc CNT_1, 1
movw CTX_0, SRC_0
sbiw r28, 8
std Y+0, TMP_0
std Y+4, TMP_1
std Y+1, TMP_2
std Y+5, TMP_3
std Y+2, TMP_4
std Y+6, TMP_5
std Y+3, TMP_6
std Y+7, TMP_7
set
dec CNT_1
brpl 20b
dec CNT_0
brne 10b
movw r24, r28
movw r22, CTX_0
clr r21
ldi r20, 8
pop_range 28, 29
pop_range 2, 17
rjmp memxor
/*
void cscipher_dec(void* buffer, const cscipher_ctx_t* ctx){
uint8_t i=7,j,k;
uint8_t tmp[8];
memxor(buffer, ctx->keys[8], 8);
do{
for(j=0; j<3; ++j){
for(k=0; k<4; ++k){
tmp[2*k] = ((uint8_t*)buffer)[k];
tmp[2*k+1] = ((uint8_t*)buffer)[4+k];
}
for(k=0; k<4; ++k){
((uint16_t*)buffer)[k] = m_inv(((uint16_t*)tmp)[k]);
}
if(j==2){
memxor(buffer, ctx->keys[i], 8);
}else{
memxor_P(buffer, round_const+((j==1)?0:8), 8);
}
}
}while(i--);
}
*/
.global cscipher_dec
cscipher_dec:
push_range 2, 17
push_range 28, 29
movw r28, r24
movw r26, r22
adiw r26, 7*8
adiw r26, 8
movw CTX_0, r26
movw r22, r26
clr r21
ldi r20, 8
call memxor
ldi CNT_0, 7
10:
ldi CNT_1, 3
20:
clr r27
ldi r26, TMP_0
movw DST_0, r26
ldi r21, 4
30:
ldd r23, Y+4
ld r24, Y+
/* m_inv transformation */
; mov r23, r25
rcall p
mov r22, r24
mov r24, r23
rcall p
eor r22, r24
mov r25, r24
mov r24, r22
rol r24
adc r24, r1
andi r24, 0xaa
eor r24, r22
mov r22, r24
rol r22
adc r22, r1
eor r25, r22
movw r26, DST_0
st X+, r24
st X+, r25
movw DST_0, r26
dec r21
brne 30b
sbiw r28, 4
std Y+0, TMP_0
std Y+1, TMP_1
std Y+2, TMP_2
std Y+3, TMP_3
std Y+4, TMP_4
std Y+5, TMP_5
std Y+6, TMP_6
std Y+7, TMP_7
movw r24, r28
clr r21
ldi r20, 8
sbrc CNT_1, 1
rjmp 40f
movw r26, CTX_0
sbiw r26, 8
movw CTX_0, r26
movw r22, r26
call memxor
rjmp 45f
40:
ldi r26, lo8(round_const)
ldi r27, hi8(round_const)
sbrc CNT_1, 0
adiw r26, 8
movw r22, r26
call memxor_P
45:
dec CNT_1
brne 20b
dec CNT_0
brpl 10b
90:
pop_range 28, 29
pop_range 2, 17
ret

35
cscipher/sbox_check.rb Normal file
View File

@ -0,0 +1,35 @@
$sbox = [
0x29, 0x0d, 0x61, 0x40, 0x9c, 0xeb, 0x9e, 0x8f, 0x1f, 0x85, 0x5f, 0x58, 0x5b, 0x01, 0x39, 0x86,
0x97, 0x2e, 0xd7, 0xd6, 0x35, 0xae, 0x17, 0x16, 0x21, 0xb6, 0x69, 0x4e, 0xa5, 0x72, 0x87, 0x08,
0x3c, 0x18, 0xe6, 0xe7, 0xfa, 0xad, 0xb8, 0x89, 0xb7, 0x00, 0xf7, 0x6f, 0x73, 0x84, 0x11, 0x63,
0x3f, 0x96, 0x7f, 0x6e, 0xbf, 0x14, 0x9d, 0xac, 0xa4, 0x0e, 0x7e, 0xf6, 0x20, 0x4a, 0x62, 0x30,
0x03, 0xc5, 0x4b, 0x5a, 0x46, 0xa3, 0x44, 0x65, 0x7d, 0x4d, 0x3d, 0x42, 0x79, 0x49, 0x1b, 0x5c,
0xf5, 0x6c, 0xb5, 0x94, 0x54, 0xff, 0x56, 0x57, 0x0b, 0xf4, 0x43, 0x0c, 0x4f, 0x70, 0x6d, 0x0a,
0xe4, 0x02, 0x3e, 0x2f, 0xa2, 0x47, 0xe0, 0xc1, 0xd5, 0x1a, 0x95, 0xa7, 0x51, 0x5e, 0x33, 0x2b,
0x5d, 0xd4, 0x1d, 0x2c, 0xee, 0x75, 0xec, 0xdd, 0x7c, 0x4c, 0xa6, 0xb4, 0x78, 0x48, 0x3a, 0x32,
0x98, 0xaf, 0xc0, 0xe1, 0x2d, 0x09, 0x0f, 0x1e, 0xb9, 0x27, 0x8a, 0xe9, 0xbd, 0xe3, 0x9f, 0x07,
0xb1, 0xea, 0x92, 0x93, 0x53, 0x6a, 0x31, 0x10, 0x80, 0xf2, 0xd8, 0x9b, 0x04, 0x36, 0x06, 0x8e,
0xbe, 0xa9, 0x64, 0x45, 0x38, 0x1c, 0x7a, 0x6b, 0xf3, 0xa1, 0xf0, 0xcd, 0x37, 0x25, 0x15, 0x81,
0xfb, 0x90, 0xe8, 0xd9, 0x7b, 0x52, 0x19, 0x28, 0x26, 0x88, 0xfc, 0xd1, 0xe2, 0x8c, 0xa0, 0x34,
0x82, 0x67, 0xda, 0xcb, 0xc7, 0x41, 0xe5, 0xc4, 0xc8, 0xef, 0xdb, 0xc3, 0xcc, 0xab, 0xce, 0xed,
0xd0, 0xbb, 0xd3, 0xd2, 0x71, 0x68, 0x13, 0x12, 0x9a, 0xb3, 0xc2, 0xca, 0xde, 0x77, 0xdc, 0xdf,
0x66, 0x83, 0xbc, 0x8d, 0x60, 0xc6, 0x22, 0x23, 0xb2, 0x8b, 0x91, 0x05, 0x76, 0xcf, 0x74, 0xc9,
0xaa, 0xf1, 0x99, 0xa8, 0x59, 0x50, 0x3b, 0x2a, 0xfe, 0xf9, 0x24, 0xb0, 0xba, 0xfd, 0xf8, 0x55
]
$error_list = Array.new
256.times do |i|
if $sbox[$sbox[i]]==i
putc('*')
else
putc('!')
$error_list << i;
end
puts("") if(i%16==15)
end
if $error_list.length!=0
print "Errors: "
$error_list.each {|x| printf("%02X ", x)}
puts("")
end

View File

@ -18,6 +18,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
=end
require 'rubygems'
require 'serialport'
def read_line(error_msg=true)

View File

@ -30,6 +30,7 @@
#include "cli.h"
#endif
void jh_encrypt(uint8_t *a);
void jh_init(uint16_t hashlen_b, jh_ctx_t* ctx){
memset(ctx->a, 0, 128);

View File

@ -50,7 +50,7 @@ uint8_t jh_l(uint8_t v, uint8_t w){
}
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;
for(i=0; i<128; ++i){

View File

@ -3,5 +3,7 @@
#include <stdint.h>
void memxor(void* dest, const void* src, uint16_t n);
void memxor_P(void* dest, const void* src, uint16_t n);
#endif

52
memxor/memxor_p.S Normal file
View File

@ -0,0 +1,52 @@
/* memxor_p.s */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2006-2010 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* File: memxor.S
* Author: Daniel Otte
* Date: 2008-08-07
* License: GPLv3 or later
* Description: memxor, XORing one block into another
*
*/
/*
* void memxor(void* dest, const void* src, uint16_t n);
*/
/*
* param dest is passed in r24:r25
* param src is passed in r22:r23
* param n is passed in r20:r21
*/
.global memxor_P
memxor_P:
movw r26, r24
movw r30, r22
movw r24, r20
adiw r24, 0
breq 2f
1:
lpm r20, Z+
ld r21, X
eor r20, r21
st X+, r20
sbiw r24, 1
brne 1b
2:
ret

View File

@ -0,0 +1,13 @@
# Makefile for CS-Cipher
ALGO_NAME := CSCIPHER_SMALL_C
# comment out the following line for removement of CS-Cipher from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := cscipher/
$(ALGO_NAME)_INCDIR := bcal/ memxor/
$(ALGO_NAME)_OBJ := cscipher_small.o cscipher_sbox.o memxor.o memxor_p.o
$(ALGO_NAME)_TEST_BIN := main-cscipher-test.o bcal_cscipher.o $(CLI_STD) $(BCAL_STD)
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance
$(ALGO_NAME)_DEF := SBOX_PROG=0

14
mkfiles/cscipher_tiny.mk Normal file
View File

@ -0,0 +1,14 @@
# Makefile for CS-Cipher
ALGO_NAME := CSCIPHER_TINY
# comment out the following line for removement of CS-Cipher from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := cscipher/
$(ALGO_NAME)_INCDIR := bcal/ memxor/
$(ALGO_NAME)_OBJ := cscipher_tiny_asm.o cscipher_tiny_stub.o memxor.o memxor_p.o
$(ALGO_NAME)_TEST_BIN := main-cscipher-test.o bcal_cscipher.o $(CLI_STD) $(BCAL_STD)
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance
$(ALGO_NAME)_DEF := SBOX_PROG=1

View File

@ -0,0 +1,14 @@
# Makefile for CS-Cipher
ALGO_NAME := CSCIPHER_TINY_C
# comment out the following line for removement of CS-Cipher from the build process
BLOCK_CIPHERS += $(ALGO_NAME)
$(ALGO_NAME)_DIR := cscipher/
$(ALGO_NAME)_INCDIR := bcal/ memxor/
$(ALGO_NAME)_OBJ := cscipher_small.o memxor.o memxor_p.o
$(ALGO_NAME)_TEST_BIN := main-cscipher-test.o bcal_cscipher.o $(CLI_STD) $(BCAL_STD)
$(ALGO_NAME)_NESSIE_TEST := test nessie
$(ALGO_NAME)_PERFORMANCE_TEST := performance
$(ALGO_NAME)_DEF := SBOX_PROG=1

View File

@ -0,0 +1,171 @@
/* main-cscipher-test.c */
/*
This file is part of the AVR-Crypto-Lib.
Copyright (C) 2008 Daniel Otte (daniel.otte@rub.de)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* cscipher test-suit
*
*/
#include "config.h"
#include "uart_i.h"
#include "debug.h"
#include "cscipher.h"
#include "nessie_bc_test.h"
#include "cli.h"
#include "performance_test.h"
#include "bcal-performance.h"
#include "bcal_cscipher.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
char* algo_name = "CS-Cipher";
const bcdesc_t* algolist[] PROGMEM = {
(bcdesc_t*)&cscipher_desc,
NULL
};
/*****************************************************************************
* additional validation-functions *
*****************************************************************************/
void cscipher_init_dummy(const uint8_t* key, uint16_t keysize_b, void* ctx){
cscipher_init(key, ctx);
}
void testrun_nessie_cscipher(void){
nessie_bc_init();
nessie_bc_ctx.blocksize_B = 8;
nessie_bc_ctx.keysize_b = 128;
nessie_bc_ctx.name = algo_name;
nessie_bc_ctx.ctx_size_B = sizeof(cscipher_ctx_t);
nessie_bc_ctx.cipher_enc = (nessie_bc_enc_fpt)cscipher_enc;
nessie_bc_ctx.cipher_dec = (nessie_bc_dec_fpt)cscipher_dec;
nessie_bc_ctx.cipher_free = (nessie_bc_free_fpt)NULL;
nessie_bc_ctx.cipher_genctx = (nessie_bc_gen_fpt)cscipher_init_dummy;
nessie_bc_run();
}
void testrun_cscipher(void){
uint8_t data[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
uint8_t key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
cscipher_ctx_t ctx;
cli_putstr_P(PSTR("\r\n== CS-Cipher test==\r\nkey: "));
cli_hexdump(key, 16);
memset(&ctx, 0, 8*9);
cscipher_init(key, &ctx);
cli_putstr_P(PSTR("\r\nround keys:\r\n"));
cli_hexdump_block(&ctx, 8*9, 4, 8);
cli_putstr_P(PSTR("\r\nplain: "));
cli_hexdump(data, 8);
cscipher_enc(data, &ctx);
cli_putstr_P(PSTR("\r\ncipher: "));
cli_hexdump(data, 8);
cscipher_dec(data, &ctx);
cli_putstr_P(PSTR("\r\nplain: "));
cli_hexdump(data, 8);
}
void testrun_long_cscipher(void){
uint8_t data[8];
char str[10];
uint16_t i;
uint8_t j;
uint8_t key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
cscipher_ctx_t ctx;
cli_putstr_P(PSTR("\r\n== CS-Cipher test==\r\nkey: "));
cli_hexdump(key, 16);
cscipher_init(key, &ctx);
memset(data, 0, 8);
cli_putstr_P(PSTR("\r\nplain: "));
cli_hexdump(data, 8);
cli_putstr_P(PSTR("\r\nencrypting 1,000,000 times:\r\n"));
for(i=0; i<10000;++i){
for(j=0;j<100;++j){
cscipher_enc(data, &ctx);
}
if(i%128==0){
itoa(i, str, 10);
cli_putstr_P(PSTR("\r\n"));
cli_putstr(str);
cli_putstr_P(PSTR(": "));
}
cli_putc('*');
}
cli_putstr_P(PSTR("\r\ncipher: "));
cli_hexdump(data, 8);
cli_putstr_P(PSTR("\r\ndecrypting 1,000,000 times:"));
for(i=0; i<10000;++i){
for(j=0;j<100;++j){
cscipher_dec(data, &ctx);
}
if(i%128==0){
itoa(i, str, 10);
cli_putstr_P(PSTR("\r\n"));
cli_putstr(str);
cli_putstr_P(PSTR(": "));
}
cli_putc('*');
}
cli_putstr_P(PSTR("\r\nplain: "));
cli_hexdump(data, 8);
}
void testrun_performance_cscipher(void){
bcal_performance_multiple(algolist);
}
/*****************************************************************************
* main *
*****************************************************************************/
const char nessie_str[] PROGMEM = "nessie";
const char test_str[] PROGMEM = "test";
const char longtest_str[] PROGMEM = "longtest";
const char performance_str[] PROGMEM = "performance";
const char echo_str[] PROGMEM = "echo";
cmdlist_entry_t cmdlist[] PROGMEM = {
{ nessie_str, NULL, testrun_nessie_cscipher },
{ test_str, NULL, testrun_cscipher},
{ longtest_str, NULL, testrun_long_cscipher},
{ performance_str, NULL, testrun_performance_cscipher},
{ echo_str, (void*)1, (void_fpt)echo_ctrl},
{ NULL, NULL, NULL}
};
int main (void){
DEBUG_INIT();
cli_rx = (cli_rx_fpt)uart0_getc;
cli_tx = (cli_tx_fpt)uart0_putc;
for(;;){
cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
cli_putstr(algo_name);
cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
cmd_interface(cmdlist);
}
}

File diff suppressed because it is too large Load Diff