From 3790ef3b40efb6649a517144f3089aba564ca541 Mon Sep 17 00:00:00 2001 From: bg Date: Sun, 31 Jan 2010 19:58:53 +0000 Subject: [PATCH] adding CFB-mode for biwise operation --- bcal-cfb_bit.c | 126 +++++++++++++++++++++++++++++++++++++++ bcal-cfb_bit.h | 41 +++++++++++++ mkfiles/aes.mk | 3 +- mkfiles/aes_c.mk | 3 +- test_src/main-aes-test.c | 66 ++++++++++++++++++++ 5 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 bcal-cfb_bit.c create mode 100644 bcal-cfb_bit.h diff --git a/bcal-cfb_bit.c b/bcal-cfb_bit.c new file mode 100644 index 0000000..6f373c9 --- /dev/null +++ b/bcal-cfb_bit.c @@ -0,0 +1,126 @@ +/* bcal-cfb_bit.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-cfb_bit.h" +#include "bcal-basic.h" + +static uint8_t read_bit(void* block, uint32_t index){ + uint8_t r; + r=((uint8_t*)block)[index/8]; + r=(r&(0x80>>(index&7)))?0xff:0x00; + return r; +} + +static void write_bit(void* block, uint32_t index, uint8_t value){ + if(value){ + /* set bit */ + ((uint8_t*)block)[index/8] |= 0x80>>(index&7); + }else{ + /* clear bit */ + ((uint8_t*)block)[index/8] &= ~(0x80>>(index&7)); + } +} + +uint8_t bcal_cfb_b_init(const bcdesc_t* desc, const void* key, uint16_t keysize, uint16_t size_b, bcal_cfb_b_ctx_t* ctx){ + ctx->desc = (bcdesc_t*)desc; + ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8; + ctx->in_block=malloc(ctx->blocksize_B); + if(ctx->in_block==NULL){ + return 0x11; + } + if(size_b>bcal_cipher_getBlocksize_b(desc)){ + return 0x12; + } + ctx->size_b = size_b; + return bcal_cipher_init(desc, key, keysize, &(ctx->cctx)); +} + +void bcal_cfb_b_free(bcal_cfb_b_ctx_t* ctx){ + free(ctx->in_block); + bcal_cipher_free(&(ctx->cctx)); +} + +void bcal_cfb_b_loadIV(const void* iv, bcal_cfb_b_ctx_t* ctx){ + memcpy(ctx->in_block, iv, ctx->blocksize_B); +} + +void bcal_cfb_b_encNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx){ + uint8_t tmp[ctx->blocksize_B]; + offset &= 7; + memcpy(tmp, ctx->in_block, ctx->blocksize_B); + bcal_cipher_enc(tmp, &(ctx->cctx)); + uint16_t i,j; + uint8_t a; + for(i=0; iblocksize_B*8-ctx->size_b; ++i){ + a = read_bit(ctx->in_block, i+ctx->size_b); + write_bit(ctx->in_block, i, a); + } + for(j=offset,i=0; isize_b; ++i, ++j){ + a = read_bit(tmp, i) ^ read_bit(block, j); + write_bit(ctx->in_block, ctx->blocksize_B*8-ctx->size_b+i, a); + write_bit(block, j, a); + } +} + +void bcal_cfb_b_decNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx){ + uint8_t tmp[ctx->blocksize_B]; + offset &= 7; + memcpy(tmp, ctx->in_block, ctx->blocksize_B); + bcal_cipher_enc(tmp, &(ctx->cctx)); + uint16_t i,j; + uint8_t a,b; + for(i=0; iblocksize_B*8-ctx->size_b; ++i){ + a = read_bit(ctx->in_block, i+ctx->size_b); + write_bit(ctx->in_block, i, a); + } + for(j=offset,i=0; isize_b; ++i, ++j){ + a = read_bit(tmp, i); + b = read_bit(block, j); + a ^= b; + write_bit(ctx->in_block, ctx->blocksize_B*8-ctx->size_b+i, b); + write_bit(block, j, a); + } +} + +void bcal_cfb_b_encMsg(const void* iv, void* msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t* ctx){ + bcal_cfb_b_loadIV(iv, ctx); + uint32_t addr; + addr = ((uint16_t)msg)*8+offset; + while(msg_blocks--){ + msg = (void*)((uint16_t)(addr/8)); + offset = addr&7; + bcal_cfb_b_encNext(msg, offset, ctx); + addr += ctx->size_b; + } +} + +void bcal_cfb_b_decMsg(const void* iv, void* msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t* ctx){ + bcal_cfb_b_loadIV(iv, ctx); + uint32_t addr; + addr = ((uint16_t)msg)*8+offset; + while(msg_blocks--){ + msg = (void*)((uint16_t)(addr/8)); + offset = addr&7; + bcal_cfb_b_decNext(msg, offset, ctx); + addr += ctx->size_b; + } +} diff --git a/bcal-cfb_bit.h b/bcal-cfb_bit.h new file mode 100644 index 0000000..01cc69f --- /dev/null +++ b/bcal-cfb_bit.h @@ -0,0 +1,41 @@ +/* bcal-cfb_bit.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 . +*/ + +#include +#include "bcal-basic.h" +#include "blockcipher_descriptor.h" + + +typedef struct{ + bcdesc_t* desc; + bcgen_ctx_t cctx; + uint8_t* in_block; + uint8_t blocksize_B; + uint16_t size_b; +} bcal_cfb_b_ctx_t; + + +uint8_t bcal_cfb_b_init(const bcdesc_t* desc, const void* key, uint16_t keysize, uint16_t size_b, bcal_cfb_b_ctx_t* ctx); +void bcal_cfb_b_free(bcal_cfb_b_ctx_t* ctx); +void bcal_cfb_b_loadIV(const void* iv, bcal_cfb_b_ctx_t* ctx); +void bcal_cfb_b_encNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx); +void bcal_cfb_b_decNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx); +void bcal_cfb_b_encMsg(const void* iv, void* msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t* ctx); +void bcal_cfb_b_decMsg(const void* iv, void* msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t* ctx); + diff --git a/mkfiles/aes.mk b/mkfiles/aes.mk index af5601a..a0cf5fd 100644 --- a/mkfiles/aes.mk +++ b/mkfiles/aes.mk @@ -10,7 +10,8 @@ $(ALGO_NAME)_OBJ := aes_enc-asm.o aes_dec-asm.o aes_sbox-asm.o aes_invsbox- $(ALGO_NAME)_TEST_BIN := main-aes-test.o $(CLI_STD) \ 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 bcal-cfb_byte.o + keysize_descriptor.o dump-asm.o dump-decl.o bcal-cfb_byte.o \ + bcal-cfb_bit.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 734db13..9d5d980 100644 --- a/mkfiles/aes_c.mk +++ b/mkfiles/aes_c.mk @@ -13,7 +13,8 @@ $(ALGO_NAME)_OBJ := aes_enc.o aes_dec.o aes_sbox.o aes_invsbox.o \ $(ALGO_NAME)_TEST_BIN := main-aes-test.o $(CLI_STD) \ 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 bcal-cfb_byte.o + keysize_descriptor.o dump-asm.o dump-decl.o bcal-cfb_byte.o \ + bcal-cfb_bit.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 7d1fa9a..d50c739 100644 --- a/test_src/main-aes-test.c +++ b/test_src/main-aes-test.c @@ -38,6 +38,7 @@ #include "bcal_aes256.h" #include "bcal-cbc.h" #include "bcal-cfb_byte.h" +#include "bcal-cfb_bit.h" #include #include @@ -270,6 +271,69 @@ void testrun_aes128_cfb8(void){ } +void testrun_aes128_cfb1(void){ + uint8_t key[16]; + uint8_t iv[16]; + uint8_t plain[64]; + + bcal_cfb_b_ctx_t ctx; + uint8_t r; + + memcpy_P(key, modes_key, 16); + memcpy_P(iv, modes_iv, 16); + memcpy_P(plain, modes_plain, 64); + + cli_putstr_P(PSTR("\r\n** AES128-CFB1-TEST **")); + r = bcal_cfb_b_init(&aes128_desc, key, 128, 1, &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, 2, 4, 8); + if(r) + return; + uint8_t i, bit_offset, byte_offset; + bcal_cfb_b_loadIV(iv, &ctx); + for(i=0; i<16; ++i){ + byte_offset = i/8; + bit_offset = i&7; + cli_putstr_P(PSTR("\r\n plain bit: ")); + cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); + bcal_cfb_b_encNext(plain+byte_offset, bit_offset, &ctx); + cli_putstr_P(PSTR("\r\n cipher bit: ")); + cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); + } + cli_putstr_P(PSTR("\r\n ciphertext: ")); + cli_hexdump_block(plain, 2, 4, 8); + + bcal_cfb_b_loadIV(iv, &ctx); + for(i=0; i<16; ++i){ + byte_offset = i/8; + bit_offset = i&7; + cli_putstr_P(PSTR("\r\n plain bit: ")); + cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); + bcal_cfb_b_decNext(plain+byte_offset, bit_offset, &ctx); + cli_putstr_P(PSTR("\r\n cipher bit: ")); + cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); + } + cli_putstr_P(PSTR("\r\n plaintext: ")); + cli_hexdump_block(plain, 2, 4, 8); + + + bcal_cfb_b_encMsg(iv, plain, 0, 64*8, &ctx); + cli_putstr_P(PSTR("\r\n ciphertext: ")); + cli_hexdump_block(plain, 64, 4, 8); + + bcal_cfb_b_decMsg(iv, plain, 0, 64*8, &ctx); + cli_putstr_P(PSTR("\r\n plaintext: ")); + cli_hexdump_block(plain, 64, 4, 8); + + bcal_cfb_b_free(&ctx); + +} /*****************************************************************************/ void testrun_performance_aes128(void){ @@ -406,6 +470,7 @@ const char test_str[] PROGMEM = "test"; const char testkey_str[] PROGMEM = "testkey"; const char testcbc_str[] PROGMEM = "testcbc"; const char testcfb8_str[] PROGMEM = "testcfb8"; +const char testcfb1_str[] PROGMEM = "testcfb1"; const char performance_str[] PROGMEM = "performance"; const char dump_str[] PROGMEM = "dump"; const char echo_str[] PROGMEM = "echo"; @@ -416,6 +481,7 @@ cmdlist_entry_t cmdlist[] PROGMEM = { { testkey_str, NULL, testrun_testkey_aes}, { testcbc_str, NULL, testrun_aes128_cbc}, { testcfb8_str, NULL, testrun_aes128_cfb8}, + { testcfb1_str, NULL, testrun_aes128_cfb1}, { performance_str, NULL, testrun_performance_aes}, { dump_str, (void*)1, (void_fpt)dump}, { echo_str, (void*)1, (void_fpt)echo_ctrl}, -- 2.39.2