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/>.
22 #include "bcal-cfb_bit.h"
23 #include "bcal-basic.h"
25 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)
37 ((uint8_t*) block)[index / 8] |= 0x80 >> (index & 7);
40 ((uint8_t*) block)[index / 8] &= ~(0x80 >> (index & 7));
44 uint8_t bcal_cfb_b_init(const bcdesc_t *desc, const void *key,
45 uint16_t keysize_b, uint16_t size_b, bcal_cfb_b_ctx_t *ctx)
47 ctx->desc = (bcdesc_t*) desc;
48 ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc) + 7) / 8;
49 ctx->in_block = malloc(ctx->blocksize_B);
50 if (ctx->in_block == NULL) {
53 if (size_b > bcal_cipher_getBlocksize_b(desc)) {
57 return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx));
60 void bcal_cfb_b_free(bcal_cfb_b_ctx_t *ctx)
63 bcal_cipher_free(&(ctx->cctx));
66 void bcal_cfb_b_loadIV(const void *iv, bcal_cfb_b_ctx_t *ctx)
69 memcpy(ctx->in_block, iv, ctx->blocksize_B);
73 void bcal_cfb_b_encNext(void *block, uint8_t offset, bcal_cfb_b_ctx_t *ctx)
75 uint8_t tmp[ctx->blocksize_B];
77 memcpy(tmp, ctx->in_block, ctx->blocksize_B);
78 bcal_cipher_enc(tmp, &(ctx->cctx));
81 for (i = 0; i < ctx->blocksize_B * 8 - ctx->size_b; ++i) {
82 a = read_bit(ctx->in_block, i + ctx->size_b);
83 write_bit(ctx->in_block, i, a);
85 for (j = offset, i = 0; i < ctx->size_b; ++i, ++j) {
86 a = read_bit(tmp, i) ^ read_bit(block, j);
87 write_bit(ctx->in_block, ctx->blocksize_B * 8 - ctx->size_b + i, a);
88 write_bit(block, j, a);
92 void bcal_cfb_b_decNext(void *block, uint8_t offset, bcal_cfb_b_ctx_t *ctx)
94 uint8_t tmp[ctx->blocksize_B];
96 memcpy(tmp, ctx->in_block, ctx->blocksize_B);
97 bcal_cipher_enc(tmp, &(ctx->cctx));
100 for (i = 0; i < ctx->blocksize_B * 8 - ctx->size_b; ++i) {
101 a = read_bit(ctx->in_block, i + ctx->size_b);
102 write_bit(ctx->in_block, i, a);
104 for (j = offset, i = 0; i < ctx->size_b; ++i, ++j) {
105 a = read_bit(tmp, i);
106 b = read_bit(block, j);
108 write_bit(ctx->in_block, ctx->blocksize_B * 8 - ctx->size_b + i, b);
109 write_bit(block, j, a);
113 void bcal_cfb_b_encMsg(const void *iv, void *msg, uint8_t offset,
114 uint32_t msg_blocks, bcal_cfb_b_ctx_t *ctx)
116 bcal_cfb_b_loadIV(iv, ctx);
118 addr = ((uint16_t) msg) * 8 + offset;
119 while (msg_blocks--) {
120 msg = (void*) ((uint16_t) (addr / 8));
122 bcal_cfb_b_encNext(msg, offset, ctx);
127 void bcal_cfb_b_decMsg(const void *iv, void *msg, uint8_t offset,
128 uint32_t msg_blocks, bcal_cfb_b_ctx_t *ctx)
130 bcal_cfb_b_loadIV(iv, ctx);
132 addr = ((uint16_t) msg) * 8 + offset;
133 while (msg_blocks--) {
134 msg = (void*) ((uint16_t) (addr / 8));
136 bcal_cfb_b_decNext(msg, offset, ctx);