]> git.cryptolib.org Git - arm-crypto-lib.git/blob - bcal-cfb_bit.c
AES added
[arm-crypto-lib.git] / bcal-cfb_bit.c
1 /* bcal-cfb_bit.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-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_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){
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_b, &(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         if(iv){
64                 memcpy(ctx->in_block, iv, ctx->blocksize_B);
65         }
66 }
67
68 void bcal_cfb_b_encNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx){
69         uint8_t tmp[ctx->blocksize_B];
70         offset &= 7;
71         memcpy(tmp, ctx->in_block, ctx->blocksize_B);
72         bcal_cipher_enc(tmp, &(ctx->cctx));
73         uint16_t i,j;
74         uint8_t a;
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);
78         }
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);
83         }
84 }
85
86 void bcal_cfb_b_decNext(void* block, uint8_t offset, bcal_cfb_b_ctx_t* ctx){
87         uint8_t tmp[ctx->blocksize_B];
88         offset &= 7;
89         memcpy(tmp, ctx->in_block, ctx->blocksize_B);
90         bcal_cipher_enc(tmp, &(ctx->cctx));
91         uint16_t i,j;
92         uint8_t a,b;
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);
96         }
97         for(j=offset,i=0; i<ctx->size_b; ++i, ++j){
98                 a = read_bit(tmp, i);
99                 b = read_bit(block, j);
100                 a ^= b;
101                 write_bit(ctx->in_block, ctx->blocksize_B*8-ctx->size_b+i, b);
102                 write_bit(block, j, a);
103         }
104 }
105
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);
108         uint64_t addr;
109         addr = ((uint32_t)msg)*8+offset;
110         while(msg_blocks--){
111                 msg = (void*)((uint32_t)(addr/8));
112                 offset = addr&7;
113                 bcal_cfb_b_encNext(msg, offset, ctx);
114                 addr += ctx->size_b;
115         }
116 }
117
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);
120         uint64_t addr;
121         addr = ((uint32_t)msg)*8+offset;
122         while(msg_blocks--){
123                 msg = (void*)((uint32_t)(addr/8));
124                 offset = addr&7;
125                 bcal_cfb_b_decNext(msg, offset, ctx);
126                 addr += ctx->size_b;
127         }
128 }