From 9e69044519495dad3e2f76fc7795e72390b79a45 Mon Sep 17 00:00:00 2001 From: bg Date: Sat, 30 Jan 2010 00:49:31 +0000 Subject: [PATCH] "new cbc-mode for bcal; docu comming soon --- bcal-basic.c | 39 ++++++++++------- bcal-basic.h | 7 ++-- bcal-cbc.c | 83 +++++++++++++++++++++++++++++++++++++ bcal-cbc.h | 44 ++++++++++++++++++++ bcal_aes128.c | 4 +- bcal_aes128.h | 2 +- bcal_aes192.h | 2 +- bcal_aes256.h | 2 +- bcal_camellia128.h | 2 +- bcal_cast5.h | 2 +- bcal_cast6.h | 2 +- bcal_des.h | 2 +- bcal_noekeon.h | 2 +- bcal_present.h | 2 +- bcal_rc5.h | 2 +- bcal_rc6.h | 2 +- bcal_seed.h | 2 +- bcal_serpent.h | 2 +- bcal_skipjack.h | 2 +- bcal_tdes.h | 2 +- bcal_xtea.h | 2 +- keysize_descriptor.c | 14 +++---- mkfiles/aes.mk | 6 ++- mkfiles/aes_c.mk | 4 +- test_src/main-aes-test.c | 67 +++++++++++++++++++++++++++++- test_src/main-aes128-test.c | 3 +- test_src/main-sha256-test.c | 32 +++++++------- test_src/main-shabal-test.c | 44 +++++++++++--------- 28 files changed, 293 insertions(+), 86 deletions(-) create mode 100644 bcal-cbc.c create mode 100644 bcal-cbc.h diff --git a/bcal-basic.c b/bcal-basic.c index d37f40a..07ec2da 100644 --- a/bcal-basic.c +++ b/bcal-basic.c @@ -20,34 +20,34 @@ #include #include #include +#include #include "blockcipher_descriptor.h" #include "keysize_descriptor.h" - uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor, const void* key, uint16_t keysize, bcgen_ctx_t* ctx){ - if(!is_valid_keysize_P((PGM_VOID_P)(pgm_read_word(cipher_descriptor->valid_keysize_desc)), - keysize)) + if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_keysize_desc)), + keysize)){ return 1; - + } uint8_t flags; bc_init_fpt init_fpt; ctx->desc_ptr = (bcdesc_t*)cipher_descriptor; ctx->keysize = keysize; flags = pgm_read_byte(cipher_descriptor->flags); - init_fpt.initvoid = (void_fpt)(pgm_read_word(cipher_descriptor->init.initvoid)); + init_fpt.initvoid = (void_fpt)(pgm_read_word(&(cipher_descriptor->init.initvoid))); if(init_fpt.initvoid == NULL){ - if(!(ctx->ctx = malloc(keysize/8))) + if(!(ctx->ctx = malloc((keysize+7)/8))) return 2; - memcpy(ctx->ctx, key, keysize/8); + memcpy(ctx->ctx, key, (keysize+7)/8); return 0; } - if(!(ctx->ctx = malloc(pgm_read_word(cipher_descriptor->ctxsize_B)))) + if(!(ctx->ctx = malloc(pgm_read_word(&(cipher_descriptor->ctxsize_B))))) return 3; if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){ - init_fpt.init1((void*)key, ctx->ctx); + init_fpt.init1((void*)key, (ctx->ctx)); }else{ - init_fpt.init2((void*)key, keysize, ctx->ctx); + init_fpt.init2((void*)key, keysize, (ctx->ctx)); } return 0; } @@ -56,32 +56,39 @@ void bcal_cipher_free(bcgen_ctx_t* ctx){ if(!ctx) return; bc_free_fpt free_fpt; - free_fpt = (bc_free_fpt)(pgm_read_word(ctx->desc_ptr->free)); + free_fpt = (bc_free_fpt)(pgm_read_word(&(ctx->desc_ptr->free))); if(free_fpt) - free_fpt(ctx->ctx); + free_fpt((ctx->ctx)); free(ctx->ctx); } void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx){ bc_enc_fpt enc_fpt; - enc_fpt.encvoid = (void_fpt)pgm_read_word(ctx->desc_ptr->enc.encvoid); + enc_fpt.encvoid = (void_fpt)pgm_read_word(&(ctx->desc_ptr->enc.encvoid)); if(!enc_fpt.encvoid){ /* very bad error, no enciphering function specified */ return; } - enc_fpt.enc1(block, ctx->ctx); + enc_fpt.enc1(block, (ctx->ctx)); } void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx){ bc_dec_fpt dec_fpt; - dec_fpt.decvoid = (void_fpt)pgm_read_word(ctx->desc_ptr->dec.decvoid); + dec_fpt.decvoid = (void_fpt)pgm_read_word(&(ctx->desc_ptr->dec.decvoid)); if(!dec_fpt.decvoid){ /* very bad error, no deciphering function specified */ return; } - dec_fpt.dec1(block, ctx->ctx); + dec_fpt.dec1(block, (ctx->ctx)); } +uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t* desc){ + return pgm_read_word(&(desc->blocksize_b)); +} + +PGM_VOID_P bcal_cipher_getKeysizeDesc(const bcdesc_t* desc){ + return (PGM_VOID_P)pgm_read_word(&(desc->valid_keysize_desc)); +} diff --git a/bcal-basic.h b/bcal-basic.h index 4d79ef8..e9c8331 100644 --- a/bcal-basic.h +++ b/bcal-basic.h @@ -22,14 +22,15 @@ #include #include -#include "blockciper_descriptor.h" +#include "blockcipher_descriptor.h" #include "keysize_descriptor.h" - +#include uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor, const void* key, uint16_t keysize, bcgen_ctx_t* ctx); void bcal_cipher_free(bcgen_ctx_t* ctx); void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx); void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx); - +uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t* desc); +PGM_VOID_P bcal_cipher_getKeysizeDesc(const bcdesc_t* desc); #endif /* BCAL_BASIC_H_ */ diff --git a/bcal-cbc.c b/bcal-cbc.c new file mode 100644 index 0000000..7ae892e --- /dev/null +++ b/bcal-cbc.c @@ -0,0 +1,83 @@ +/* bcal-cbc.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 . +*/ + +#include +#include +#include "bcal-cbc.h" +#include "bcal-basic.h" +#include "memxor.h" + +uint8_t bcal_cbc_init(const bcdesc_t* desc, const void* key, uint16_t keysize, bcal_cbc_ctx_t* ctx){ + ctx->desc = (bcdesc_t*)desc; + ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8; + ctx->prev_block = malloc(ctx->blocksize_B); + + if(!(ctx->prev_block)){ + return 0x11; + } + return bcal_cipher_init(desc, key, keysize, &(ctx->cctx)); +} + +void bcal_cbc_free(bcal_cbc_ctx_t* ctx){ + bcal_cipher_free(&(ctx->cctx)); + free(ctx->prev_block); +} + + +void bcal_cbc_loadIV(const void* iv, bcal_cbc_ctx_t* ctx){ + memcpy(ctx->prev_block, iv, ctx->blocksize_B); +} + +void bcal_cbc_encNext(void* block, bcal_cbc_ctx_t* ctx){ + memxor(block, ctx->prev_block, ctx->blocksize_B); + bcal_cipher_enc(block, &(ctx->cctx)); + memcpy(ctx->prev_block, block, ctx->blocksize_B); +} + +void bcal_cbc_decNext(void* block, bcal_cbc_ctx_t* ctx){ + uint8_t tmp[ctx->blocksize_B]; + memcpy(tmp, block, ctx->blocksize_B); + bcal_cipher_dec(block, &(ctx->cctx)); + memxor(block, ctx->prev_block, ctx->blocksize_B); + memcpy(ctx->prev_block, tmp, ctx->blocksize_B); +} +void bcal_cbc_decRand(void* block, const void* prev_block, bcal_cbc_ctx_t* ctx){ + bcal_cipher_dec(block, &(ctx->cctx)); + memxor(block, prev_block, ctx->blocksize_B); +} + +void bcal_cbc_encMsg(const void* iv, void* msg, uint16_t msg_blocks, bcal_cbc_ctx_t* ctx){ + bcal_cbc_loadIV(iv, ctx); + while(msg_blocks){ + bcal_cbc_encNext(msg, ctx); + msg_blocks -= 1; + msg = (uint8_t*)msg + ctx->blocksize_B; + } +} + +void bcal_cbc_decMsg(const void* iv, void* msg, uint16_t msg_blocks, bcal_cbc_ctx_t* ctx){ + msg=(uint8_t*)msg + (msg_blocks-1)*ctx->blocksize_B; + while(msg_blocks>1){ + bcal_cbc_decRand(msg, (uint8_t*)msg-ctx->blocksize_B, ctx); + msg_blocks -= 1; + msg=(uint8_t*)msg-ctx->blocksize_B; + } + bcal_cbc_decRand(msg, iv, ctx); +} + diff --git a/bcal-cbc.h b/bcal-cbc.h new file mode 100644 index 0000000..fc1ffa1 --- /dev/null +++ b/bcal-cbc.h @@ -0,0 +1,44 @@ +/* bcal-cbc.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 . +*/ + +#ifndef BCALCBC_H_ +#define BCALCBC_H_ + +#include +#include "blockcipher_descriptor.h" +#include "bcal-basic.h" + +typedef struct{ + bcdesc_t* desc; + bcgen_ctx_t cctx; + uint8_t* prev_block; + uint8_t blocksize_B; +} bcal_cbc_ctx_t; + +uint8_t bcal_cbc_init(const bcdesc_t* desc, const void* key, uint16_t keysize, bcal_cbc_ctx_t* ctx); +void bcal_cbc_free(bcal_cbc_ctx_t* ctx); +void bcal_cbc_loadIV(const void* iv, bcal_cbc_ctx_t* ctx); +void bcal_cbc_encNext(void* block, bcal_cbc_ctx_t* ctx); +void bcal_cbc_decNext(void* block, bcal_cbc_ctx_t* ctx); +void bcal_cbc_decRand(void* block, const void* prev_block, bcal_cbc_ctx_t* ctx); +void bcal_cbc_encMsg(const void* iv, void* msg, uint16_t msg_blocks, bcal_cbc_ctx_t* ctx); +void bcal_cbc_decMsg(const void* iv, void* msg, uint16_t msg_blocks, bcal_cbc_ctx_t* ctx); + + +#endif /* BCALCBC_H_ */ diff --git a/bcal_aes128.c b/bcal_aes128.c index 8bdde25..946d7a9 100644 --- a/bcal_aes128.c +++ b/bcal_aes128.c @@ -41,11 +41,11 @@ const uint8_t aes128_keysize_desc[] PROGMEM = { KS_TYPE_LIST, 1, KS_INT(128), const bcdesc_t aes128_desc PROGMEM = { BCDESC_TYPE_BLOCKCIPHER, - BC_INIT_TYPE_2, + BC_INIT_TYPE_1, aes128_str, sizeof(aes128_ctx_t), 128, - {(void_fpt)aes_init}, + {(void_fpt)aes128_init}, {(void_fpt)aes128_enc}, {(void_fpt)aes128_dec}, (bc_free_fpt)NULL, diff --git a/bcal_aes128.h b/bcal_aes128.h index ff65689..7257425 100644 --- a/bcal_aes128.h +++ b/bcal_aes128.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "aes.h" #include "aes128_enc.h" #include "aes128_dec.h" diff --git a/bcal_aes192.h b/bcal_aes192.h index 7abc44e..cff8371 100644 --- a/bcal_aes192.h +++ b/bcal_aes192.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "aes.h" #include "aes192_enc.h" #include "aes192_dec.h" diff --git a/bcal_aes256.h b/bcal_aes256.h index 216342b..19dbb50 100644 --- a/bcal_aes256.h +++ b/bcal_aes256.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "aes.h" #include "aes256_enc.h" #include "aes256_dec.h" diff --git a/bcal_camellia128.h b/bcal_camellia128.h index fcd6970..bbe0199 100644 --- a/bcal_camellia128.h +++ b/bcal_camellia128.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "camellia.h" #include "keysize_descriptor.h" diff --git a/bcal_cast5.h b/bcal_cast5.h index b5ade13..2b620ea 100644 --- a/bcal_cast5.h +++ b/bcal_cast5.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "cast5.h" #include "keysize_descriptor.h" diff --git a/bcal_cast6.h b/bcal_cast6.h index eb527cd..c327cf6 100644 --- a/bcal_cast6.h +++ b/bcal_cast6.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "cast6.h" #include "keysize_descriptor.h" diff --git a/bcal_des.h b/bcal_des.h index 571c6c6..84f76d2 100644 --- a/bcal_des.h +++ b/bcal_des.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "des.h" #include "keysize_descriptor.h" diff --git a/bcal_noekeon.h b/bcal_noekeon.h index c74b1af..e8ea544 100644 --- a/bcal_noekeon.h +++ b/bcal_noekeon.h @@ -1,7 +1,7 @@ /* bcal_noekeon.h */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "noekeon.h" #include "keysize_descriptor.h" diff --git a/bcal_present.h b/bcal_present.h index 52cabe7..1de8365 100644 --- a/bcal_present.h +++ b/bcal_present.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "present.h" #include "keysize_descriptor.h" diff --git a/bcal_rc5.h b/bcal_rc5.h index 6c33993..30d2100 100644 --- a/bcal_rc5.h +++ b/bcal_rc5.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "rc5.h" #include "keysize_descriptor.h" diff --git a/bcal_rc6.h b/bcal_rc6.h index 9c5745c..f6d0d1b 100644 --- a/bcal_rc6.h +++ b/bcal_rc6.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "rc6.h" #include "keysize_descriptor.h" diff --git a/bcal_seed.h b/bcal_seed.h index 9bd1f18..a8bdf2c 100644 --- a/bcal_seed.h +++ b/bcal_seed.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "seed.h" #include "keysize_descriptor.h" diff --git a/bcal_serpent.h b/bcal_serpent.h index dc3674a..60fc8cc 100644 --- a/bcal_serpent.h +++ b/bcal_serpent.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "serpent.h" #include "keysize_descriptor.h" diff --git a/bcal_skipjack.h b/bcal_skipjack.h index ba15424..e238141 100644 --- a/bcal_skipjack.h +++ b/bcal_skipjack.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "skipjack.h" #include "keysize_descriptor.h" diff --git a/bcal_tdes.h b/bcal_tdes.h index 43ee27f..e2f2b31 100644 --- a/bcal_tdes.h +++ b/bcal_tdes.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "des.h" #include "keysize_descriptor.h" diff --git a/bcal_xtea.h b/bcal_xtea.h index 4142386..66e6d26 100644 --- a/bcal_xtea.h +++ b/bcal_xtea.h @@ -26,7 +26,7 @@ */ #include -#include "blopckcipher_descriptor.h" +#include "blockcipher_descriptor.h" #include "xtea.h" #include "keysize_descriptor.h" diff --git a/keysize_descriptor.c b/keysize_descriptor.c index 193c07a..5dcdcc0 100644 --- a/keysize_descriptor.c +++ b/keysize_descriptor.c @@ -39,16 +39,16 @@ uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize){ items = pgm_read_byte(ks_desc++); while(items--){ item = pgm_read_word(ks_desc); - ks_desc+=2; + ks_desc = (uint8_t*)ks_desc + 2; if(item==keysize) return 1; } - ks_desc -= 2; + ks_desc = (uint8_t*)ks_desc - 2; } if(type==KS_TYPE_RANGE){ uint16_t max, min; min = pgm_read_word(ks_desc); - ks_desc+=2; + ks_desc = (uint8_t*)ks_desc + 2; max = pgm_read_word(ks_desc); if(min<=keysize && keysize<=max) return 1; @@ -56,11 +56,11 @@ uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize){ if(type==KS_TYPE_ARG_RANGE){ uint16_t max, min, dist, offset; min = pgm_read_word(ks_desc); - ks_desc+=2; + ks_desc = (uint8_t*)ks_desc + 2; max = pgm_read_word(ks_desc); - ks_desc+=2; + ks_desc = (uint8_t*)ks_desc + 2; dist = pgm_read_word(ks_desc); - ks_desc+=2; + ks_desc = (uint8_t*)ks_desc + 2; offset = pgm_read_word(ks_desc); if(min<=keysize && keysize<=max && (keysize%dist==offset)) return 1; @@ -69,7 +69,7 @@ uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize){ /* bad error, you may insert a big warning message here */ return 0; } - return is_valid_keysize(ks_desc+1, keysize) /* search the next record */ + return is_valid_keysize_P((uint8_t*)ks_desc+1, keysize); /* search the next record */ } diff --git a/mkfiles/aes.mk b/mkfiles/aes.mk index d687c74..bc52d94 100644 --- a/mkfiles/aes.mk +++ b/mkfiles/aes.mk @@ -6,9 +6,11 @@ BLOCK_CIPHERS += $(ALGO_NAME) $(ALGO_NAME)_DIR := aes/ $(ALGO_NAME)_OBJ := aes_enc-asm.o aes_dec-asm.o aes_sbox-asm.o aes_invsbox-asm.o \ - aes_keyschedule-asm.o + aes_keyschedule-asm.o $(ALGO_NAME)_TEST_BIN := main-aes-test.o $(CLI_STD) \ - nessie_bc_test.o nessie_common.o performance_test.o + nessie_bc_test.o nessie_common.o performance_test.o memxor.o \ + bcal_aes128.o bcal_aes192.o bcal_aes256.o bcal-basic.o bcal-cbc.o \ + keysize_descriptor.o dump-asm.o dump-decl.o $(ALGO_NAME)_NESSIE_TEST := test nessie $(ALGO_NAME)_PERFORMANCE_TEST := performance diff --git a/mkfiles/aes_c.mk b/mkfiles/aes_c.mk index f030136..69ddd5a 100644 --- a/mkfiles/aes_c.mk +++ b/mkfiles/aes_c.mk @@ -11,7 +11,9 @@ $(ALGO_NAME)_OBJ := aes_enc.o aes_dec.o aes_sbox.o aes_invsbox.o \ aes128_enc.o aes128_dec.o aes192_enc.o aes192_dec.o \ aes256_enc.o aes256_dec.o $(ALGO_NAME)_TEST_BIN := main-aes-test.o $(CLI_STD) \ - nessie_bc_test.o nessie_common.o performance_test.o + nessie_bc_test.o nessie_common.o performance_test.o memxor.o \ + bcal_aes128.o bcal_aes192.o bcal_aes256.o bcal-basic.o bcal-cbc.o \ + keysize_descriptor.o dump-asm.o dump-decl.o $(ALGO_NAME)_NESSIE_TEST := test nessie $(ALGO_NAME)_PERFORMANCE_TEST := performance diff --git a/test_src/main-aes-test.c b/test_src/main-aes-test.c index 71b6600..ac80d80 100644 --- a/test_src/main-aes-test.c +++ b/test_src/main-aes-test.c @@ -31,6 +31,12 @@ #include "nessie_bc_test.h" #include "cli.h" #include "performance_test.h" +#include "dump.h" + +#include "bcal_aes128.h" +#include "bcal_aes192.h" +#include "bcal_aes256.h" +#include "bcal-cbc.h" #include #include @@ -87,8 +93,19 @@ void testrun_test_aes(void){ aes128_dec(data, &ctx); cli_putstr_P(PSTR("\r\n plaintext: ")); cli_hexdump(data, 16); - - + cli_putstr(PSTR("\r\n testing bcal:")); + bcgen_ctx_t bcal_ctx; + uint8_t r; + r = bcal_cipher_init(&aes128_desc, key, 128, &bcal_ctx); + cli_putstr_P(PSTR("\r\n init = 0x")); + cli_hexdump(&r, 1); + + bcal_cipher_enc(data, &bcal_ctx); + cli_putstr_P(PSTR("\r\n ciphertext: ")); + cli_hexdump(data, 16); + bcal_cipher_dec(data, &bcal_ctx); + cli_putstr_P(PSTR("\r\n plaintext: ")); + cli_hexdump(data, 16); } void testrun_testkey_aes128(void){ @@ -162,6 +179,48 @@ void testrun_testkey_aes(void){ testrun_testkey_aes192(); testrun_testkey_aes256(); } + + +void testrun_aes128_cbc(void){ + uint8_t key[] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c }; + uint8_t iv[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; + uint8_t plain[] = { 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, + /* --- */ + 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, + 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51, + /* --- */ + 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, + 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef, + /* --- */ + 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, + 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 }; + bcal_cbc_ctx_t ctx; + uint8_t r; + cli_putstr_P(PSTR("\r\n** AES128-CBC-TEST **")); + r = bcal_cbc_init(&aes128_desc, key, 128, &ctx); + cli_putstr_P(PSTR("\r\n init = 0x")); + cli_hexdump(&r, 1); + cli_putstr_P(PSTR("\r\n key: ")); + cli_hexdump(key, 128/8); + cli_putstr_P(PSTR("\r\n IV: ")); + cli_hexdump(iv, 128/8); + cli_putstr_P(PSTR("\r\n plaintext:")); + cli_hexdump_block(plain, 4*128/8, 4, 8); + if(r) + return; + bcal_cbc_encMsg(iv, plain, 4, &ctx); + cli_putstr_P(PSTR("\r\n ciphertext: ")); + cli_hexdump_block(plain, 4*128/8, 4, 8); + bcal_cbc_decMsg(iv, plain, 4, &ctx); + cli_putstr_P(PSTR("\r\n plaintext: ")); + cli_hexdump_block(plain, 4*128/8, 4, 8); + bcal_cbc_free(&ctx); +} + + /*****************************************************************************/ void testrun_performance_aes128(void){ @@ -296,14 +355,18 @@ void testrun_performance_aes(void){ const char nessie_str[] PROGMEM = "nessie"; const char test_str[] PROGMEM = "test"; const char testkey_str[] PROGMEM = "testkey"; +const char testcbc128_str[] PROGMEM = "testcbc128"; const char performance_str[] PROGMEM = "performance"; +const char dump_str[] PROGMEM = "dump"; const char echo_str[] PROGMEM = "echo"; cmdlist_entry_t cmdlist[] PROGMEM = { { nessie_str, NULL, testrun_nessie_aes }, { test_str, NULL, testrun_test_aes}, { testkey_str, NULL, testrun_testkey_aes}, + { testcbc128_str, NULL, testrun_aes128_cbc}, { performance_str, NULL, testrun_performance_aes}, + { dump_str, (void*)1, (void_fpt)dump}, { echo_str, (void*)1, (void_fpt)echo_ctrl}, { NULL, NULL, NULL} }; diff --git a/test_src/main-aes128-test.c b/test_src/main-aes128-test.c index 01cf583..72e06f6 100644 --- a/test_src/main-aes128-test.c +++ b/test_src/main-aes128-test.c @@ -30,10 +30,10 @@ #include "nessie_bc_test.h" #include "cli.h" #include "performance_test.h" - #include #include #include +#include char* algo_name = "AES-128"; @@ -99,6 +99,7 @@ void testrun_testkey_aes128(void){ void testrun_testkey_aes(void){ testrun_testkey_aes128(); } + /*****************************************************************************/ void testrun_performance_aes128(void){ diff --git a/test_src/main-sha256-test.c b/test_src/main-sha256-test.c index f316567..1907390 100644 --- a/test_src/main-sha256-test.c +++ b/test_src/main-sha256-test.c @@ -88,10 +88,10 @@ void test_monte(void){ 0x38, 0xF0, 0xDF, 0x70, 0x1D, 0xA9, 0x3C, 0x3B, 0xF2, 0xC9, 0xC8, 0x68, 0x96, 0xE7, 0xE6, 0xC7 }; uint8_t hash[SHA256_HASH_BYTES]; - sha256(hash, data1, 3*32*8); + sha256((sha256_hash_t*)hash, data1, 3*32*8); cli_putstr_P(PSTR("\r\n hash(data1) = ")); cli_hexdump(hash, 32); - sha256(hash, data2, 3*32*8); + sha256((sha256_hash_t*)hash, data2, 3*32*8); cli_putstr_P(PSTR("\r\n hash(data2) = ")); cli_hexdump(hash, 32); } @@ -116,7 +116,7 @@ void test_monte2(void){ 0x39, 0xd8, 0x35, 0xa7, 0x24, 0xe2, 0xfa, 0xe7 }; uint8_t hash[SHA256_HASH_BYTES]; - sha256(hash, data, 1024); + sha256((sha256_hash_t*)hash, data, 1024); cli_putstr_P(PSTR("\r\n hash(data) = ")); cli_hexdump(hash, 32); } @@ -139,19 +139,19 @@ const char shavs_test3_str[] PROGMEM = "shavs_test3"; const char dump_str[] PROGMEM = "dump"; cmdlist_entry_t cmdlist[] PROGMEM = { - { nessie_str, NULL, testrun_nessie_sha256}, - { test_str, NULL, testrun_nessie_sha256}, - { monte_str, NULL, test_monte}, - { monte2_str, NULL, test_monte2}, - { performance_str, NULL, testrun_performance_sha256}, - { echo_str, (void*)1, (void_fpt)echo_ctrl}, - { shavs_list_str, NULL, shavs_listalgos}, - { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo}, - { shavs_test1_str, NULL, shavs_test1}, - { shavs_test2_str, NULL, shavs_test2}, - { shavs_test3_str, NULL, shavs_test3}, - { dump_str, (void*)1, (void_fpt)dump}, - { NULL, NULL, NULL} + { nessie_str, NULL, testrun_nessie_sha256 }, + { test_str, NULL, testrun_nessie_sha256 }, + { monte_str, NULL, test_monte }, + { monte2_str, NULL, test_monte2 }, + { performance_str, NULL, testrun_performance_sha256 }, + { echo_str, (void*)1, (void_fpt)echo_ctrl }, + { shavs_list_str, NULL, shavs_listalgos }, + { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo }, + { shavs_test1_str, NULL, shavs_test1 }, + { shavs_test2_str, NULL, shavs_test2 }, + { shavs_test3_str, NULL, shavs_test3 }, + { dump_str, (void*)1, (void_fpt)dump }, + { NULL, NULL, NULL } }; int main (void){ diff --git a/test_src/main-shabal-test.c b/test_src/main-shabal-test.c index 17f303f..bcdc57a 100644 --- a/test_src/main-shabal-test.c +++ b/test_src/main-shabal-test.c @@ -18,7 +18,7 @@ */ /* * shabal test-suit - * + * */ #include "config.h" @@ -48,7 +48,7 @@ const hfdesc_t* algolist[] PROGMEM = { (hfdesc_t*)&shabal224_desc, (hfdesc_t*)&shabal256_desc, (hfdesc_t*)&shabal384_desc, - (hfdesc_t*)&shabal512_desc, + (hfdesc_t*)&shabal512_desc, NULL }; @@ -111,7 +111,7 @@ void shabal_ctx_dump(shabal_ctx_t* ctx){ cli_putc('0'+i/100); if(i>=10) cli_putc('0'+(i/10)%10); - cli_putc('0'+i%10); + cli_putc('0'+i%10); cli_putstr_P(PSTR("\r\n a = ")); cli_hexdump_block(ctx->a, 12*4, 5, 4*8); cli_putstr_P(PSTR("\r\n b_buffer = ")); @@ -121,7 +121,7 @@ void shabal_ctx_dump(shabal_ctx_t* ctx){ if(ctx->b == &(ctx->b_buffer[0])) cli_putstr_P(PSTR("\r\nb --> b_buffer")); if(ctx->b == &(ctx->c_buffer[0])) - cli_putstr_P(PSTR("\r\nb --> c_buffer")); + cli_putstr_P(PSTR("\r\nb --> c_buffer")); if(ctx->c == &(ctx->b_buffer[0])) cli_putstr_P(PSTR("\r\nc --> b_buffer")); if(ctx->c == &(ctx->c_buffer[0])) @@ -132,7 +132,7 @@ void shabal_ctx_dump(shabal_ctx_t* ctx){ cli_putstr_P(PSTR("\r\n b (should) = ")); cli_hexdump(&p, 2); cli_putstr_P(PSTR("\r\n c = ")); - cli_hexdump(&(ctx->c), 2); + cli_hexdump(&(ctx->c), 2); p = ctx->c_buffer; cli_putstr_P(PSTR("\r\n c (should) = ")); cli_hexdump(&p, 2); @@ -200,26 +200,30 @@ const char echo_str[] PROGMEM = "echo"; const char shavs_list_str[] PROGMEM = "shavs_list"; const char shavs_set_str[] PROGMEM = "shavs_set"; const char shavs_test1_str[] PROGMEM = "shavs_test1"; +const char shavs_test2_str[] PROGMEM = "shavs_test2"; +const char shavs_test3_str[] PROGMEM = "shavs_test3"; cmdlist_entry_t cmdlist[] PROGMEM = { - { nessie_str, NULL, testrun_nessie_shabal}, - { test_str, NULL, testrun_stdtest_shabal}, - { testinit192_str, NULL, testinit_192}, - { testinit_str, NULL, testinit}, - { testshort_str, NULL, testshort}, - { performance_str, NULL, performance_shabal}, - { shavs_list_str, NULL, shavs_listalgos}, - { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo}, - { shavs_test1_str, NULL, shavs_test1}, - { echo_str, (void*)1, (void_fpt)echo_ctrl}, - { NULL, NULL, NULL} + { nessie_str, NULL, testrun_nessie_shabal }, + { test_str, NULL, testrun_stdtest_shabal }, + { testinit192_str, NULL, testinit_192 }, + { testinit_str, NULL, testinit }, + { testshort_str, NULL, testshort }, + { performance_str, NULL, performance_shabal }, + { shavs_list_str, NULL, shavs_listalgos }, + { shavs_set_str, (void*)1, (void_fpt)shavs_setalgo }, + { shavs_test1_str, NULL, shavs_test1 }, + { shavs_test2_str, NULL, shavs_test2 }, + { shavs_test3_str, NULL, shavs_test3 }, + { 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; + cli_tx = (cli_tx_fpt)uart0_putc; shavs_algolist=(hfdesc_t**)algolist; shavs_algo=(hfdesc_t*)&shabal256_desc; for(;;){ @@ -230,7 +234,7 @@ int main (void){ cli_putstr_P(PSTR(" ")); cli_putstr(__TIME__); cli_putstr_P(PSTR(")\r\nloaded and running\r\n")); - + cmd_interface(cmdlist); } -} +} -- 2.39.5