]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal-cfb_bit.c
adding CFB-mode for biwise operation
[avr-crypto-lib.git] / bcal-cfb_bit.c
1 /* bcal-cfb_bit.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de)
5
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.
10
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.
15
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/>.
18 */
19
20
21 #include <stdint.h>
22 #include <string.h>
23 #include "bcal-cfb_bit.h"
24 #include "bcal-basic.h"
25
26 static uint8_t read_bit(void* block, uint32_t index){
27         uint8_t r;
28         r=((uint8_t*)block)[index/8];
29         r=(r&(0x80>>(index&7)))?0xff:0x00;
30         return r;
31 }
32
33 static void write_bit(void* block, uint32_t index, uint8_t value){
34         if(value){
35                 /* set bit */
36                 ((uint8_t*)block)[index/8] |= 0x80>>(index&7);
37         }else{
38                 /* clear bit */
39                 ((uint8_t*)block)[index/8] &= ~(0x80>>(index&7));
40         }
41 }
42
43 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){
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){
48                 return 0x11;
49         }
50         if(size_b>bcal_cipher_getBlocksize_b(desc)){
51                 return 0x12;
52         }
53         ctx->size_b = size_b;
54         return bcal_cipher_init(desc, key, keysize, &(ctx->cctx));
55 }
56
57 void bcal_cfb_b_free(bcal_cfb_b_ctx_t* ctx){
58         free(ctx->in_block);
59         bcal_cipher_free(&(ctx->cctx));
60 }
61
62 void bcal_cfb_b_loadIV(const void* iv, bcal_cfb_b_ctx_t* ctx){
63         memcpy(ctx->in_block, iv, ctx->blocksize_B);
64 }
65
66 void bcal_cfb_b_encNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx){
67         uint8_t tmp[ctx->blocksize_B];
68         offset &= 7;
69         memcpy(tmp, ctx->in_block, ctx->blocksize_B);
70         bcal_cipher_enc(tmp, &(ctx->cctx));
71         uint16_t i,j;
72         uint8_t a;
73         for(i=0; i<ctx->blocksize_B*8-ctx->size_b; ++i){
74                 a = read_bit(ctx->in_block, i+ctx->size_b);
75                 write_bit(ctx->in_block, i, a);
76         }
77         for(j=offset,i=0; i<ctx->size_b; ++i, ++j){
78                 a = read_bit(tmp, i) ^ read_bit(block, j);
79                 write_bit(ctx->in_block, ctx->blocksize_B*8-ctx->size_b+i, a);
80                 write_bit(block, j, a);
81         }
82 }
83
84 void bcal_cfb_b_decNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx){
85         uint8_t tmp[ctx->blocksize_B];
86         offset &= 7;
87         memcpy(tmp, ctx->in_block, ctx->blocksize_B);
88         bcal_cipher_enc(tmp, &(ctx->cctx));
89         uint16_t i,j;
90         uint8_t a,b;
91         for(i=0; i<ctx->blocksize_B*8-ctx->size_b; ++i){
92                 a = read_bit(ctx->in_block, i+ctx->size_b);
93                 write_bit(ctx->in_block, i, a);
94         }
95         for(j=offset,i=0; i<ctx->size_b; ++i, ++j){
96                 a = read_bit(tmp, i);
97                 b = read_bit(block, j);
98                 a ^= b;
99                 write_bit(ctx->in_block, ctx->blocksize_B*8-ctx->size_b+i, b);
100                 write_bit(block, j, a);
101         }
102 }
103
104 void bcal_cfb_b_encMsg(const void* iv, void* msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t* ctx){
105         bcal_cfb_b_loadIV(iv, ctx);
106         uint32_t addr;
107         addr = ((uint16_t)msg)*8+offset;
108         while(msg_blocks--){
109                 msg = (void*)((uint16_t)(addr/8));
110                 offset = addr&7;
111                 bcal_cfb_b_encNext(msg, offset, ctx);
112                 addr += ctx->size_b;
113         }
114 }
115
116 void bcal_cfb_b_decMsg(const void* iv, void* msg, uint8_t offset, uint32_t msg_blocks, bcal_cfb_b_ctx_t* ctx){
117         bcal_cfb_b_loadIV(iv, ctx);
118         uint32_t addr;
119         addr = ((uint16_t)msg)*8+offset;
120         while(msg_blocks--){
121                 msg = (void*)((uint16_t)(addr/8));
122                 offset = addr&7;
123                 bcal_cfb_b_decNext(msg, offset, ctx);
124                 addr += ctx->size_b;
125         }
126 }