3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 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/>.
23 #include "bcal-cfb_bit.h"
24 #include "bcal-basic.h"
26 static uint8_t read_bit(void *block, uint32_t index){
28 r=((uint8_t*)block)[index/8];
29 r=(r&(0x80>>(index&7)))?0xff:0x00;
33 static void write_bit(void *block, uint32_t index, uint8_t value){
36 ((uint8_t*)block)[index/8] |= 0x80>>(index&7);
39 ((uint8_t*)block)[index/8] &= ~(0x80>>(index&7));
43 uint8_t bcal_cfb_b_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b, uint16_t size_b, bcal_cfb_b_ctx_t *ctx){
44 ctx->desc = (bcdesc_t*)desc;
45 ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8;
46 ctx->in_block=malloc(ctx->blocksize_B);
47 if(ctx->in_block==NULL){
50 if(size_b>bcal_cipher_getBlocksize_b(desc)){
54 return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
57 void bcal_cfb_b_free(bcal_cfb_b_ctx_t *ctx){
59 bcal_cipher_free(&(ctx->cctx));
62 void bcal_cfb_b_loadIV(const void *iv, bcal_cfb_b_ctx_t *ctx){
64 memcpy(ctx->in_block, iv, ctx->blocksize_B);
68 void bcal_cfb_b_encNext(void *block, uint8_t offset, bcal_cfb_b_ctx_t *ctx){
69 uint8_t tmp[ctx->blocksize_B];
71 memcpy(tmp, ctx->in_block, ctx->blocksize_B);
72 bcal_cipher_enc(tmp, &(ctx->cctx));
75 for(i=0; i<ctx->blocksize_B*8-ctx->size_b; ++i){
76 a = read_bit(ctx->in_block, i+ctx->size_b);
77 write_bit(ctx->in_block, i, a);
79 for(j=offset,i=0; i<ctx->size_b; ++i, ++j){
80 a = read_bit(tmp, i) ^ read_bit(block, j);
81 write_bit(ctx->in_block, ctx->blocksize_B*8-ctx->size_b+i, a);
82 write_bit(block, j, a);
86 void bcal_cfb_b_decNext(void *block, uint8_t offset, bcal_cfb_b_ctx_t *ctx){
87 uint8_t tmp[ctx->blocksize_B];
89 memcpy(tmp, ctx->in_block, ctx->blocksize_B);
90 bcal_cipher_enc(tmp, &(ctx->cctx));
93 for(i=0; i<ctx->blocksize_B*8-ctx->size_b; ++i){
94 a = read_bit(ctx->in_block, i+ctx->size_b);
95 write_bit(ctx->in_block, i, a);
97 for(j=offset,i=0; i<ctx->size_b; ++i, ++j){
99 b = read_bit(block, j);
101 write_bit(ctx->in_block, ctx->blocksize_B*8-ctx->size_b+i, b);
102 write_bit(block, j, a);
106 void bcal_cfb_b_encMsg(const void *iv, void *msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t *ctx){
107 bcal_cfb_b_loadIV(iv, ctx);
109 addr = ((uint16_t)msg)*8+offset;
111 msg = (void*)((uint16_t)(addr/8));
113 bcal_cfb_b_encNext(msg, offset, ctx);
118 void bcal_cfb_b_decMsg(const void *iv, void *msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t *ctx){
119 bcal_cfb_b_loadIV(iv, ctx);
121 addr = ((uint16_t)msg)*8+offset;
123 msg = (void*)((uint16_t)(addr/8));
125 bcal_cfb_b_decNext(msg, offset, ctx);