X-Git-Url: https://git.cryptolib.org/?p=avr-crypto-lib.git;a=blobdiff_plain;f=bcal%2Fbcal-cbc.c;fp=bcal%2Fbcal-cbc.c;h=1135e7b7e19ddce36ca710c782850d2321b60c8b;hp=718838762fe4a24d63a7490edd69c762dea59d48;hb=8d1970350aa1d7cdcb59cf79f7f60e385e2816e5;hpb=a30df60ac814350db243b270034ef188c8d02d5e diff --git a/bcal/bcal-cbc.c b/bcal/bcal-cbc.c index 7188387..1135e7b 100644 --- a/bcal/bcal-cbc.c +++ b/bcal/bcal-cbc.c @@ -1,21 +1,21 @@ /* bcal-cbc.c */ /* - This file is part of the AVR-Crypto-Lib. - Copyright (C) 2010 Daniel Otte (daniel.otte@rub.de) + 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 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. + 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 . -*/ + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ #include #include @@ -23,62 +23,72 @@ #include "bcal-basic.h" #include "memxor.h" -uint8_t bcal_cbc_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b, bcal_cbc_ctx_t *ctx){ - ctx->desc = (bcdesc_t*)desc; - ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc)+7)/8; - ctx->prev_block = malloc(ctx->blocksize_B); +uint8_t bcal_cbc_init(const bcdesc_t *desc, const void *key, uint16_t keysize_b, + bcal_cbc_ctx_t *ctx) +{ + ctx->desc = (bcdesc_t*) desc; + ctx->blocksize_B = (bcal_cipher_getBlocksize_b(desc) + 7) / 8; + ctx->prev_block = malloc(ctx->blocksize_B); - if(ctx->prev_block == NULL){ - return 0x11; - } - return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx)); + if (ctx->prev_block == NULL) { + return 0x11; + } + return bcal_cipher_init(desc, key, keysize_b, &(ctx->cctx)); } -void bcal_cbc_free(bcal_cbc_ctx_t *ctx){ - bcal_cipher_free(&(ctx->cctx)); - free(ctx->prev_block); +void bcal_cbc_free(bcal_cbc_ctx_t *ctx) +{ + bcal_cipher_free(&(ctx->cctx)); + free(ctx->prev_block); } - -void bcal_cbc_loadIV(const void *iv, bcal_cbc_ctx_t *ctx){ - if(iv){ - memcpy(ctx->prev_block, iv, ctx->blocksize_B); - } +void bcal_cbc_loadIV(const void *iv, bcal_cbc_ctx_t *ctx) +{ + if (iv) { + memcpy(ctx->prev_block, iv, ctx->blocksize_B); + } } -void bcal_cbc_encNext(void *block, bcal_cbc_ctx_t *ctx){ - memxor(block, ctx->prev_block, ctx->blocksize_B); - bcal_cipher_enc(block, &(ctx->cctx)); - memcpy(ctx->prev_block, block, ctx->blocksize_B); +void bcal_cbc_encNext(void *block, bcal_cbc_ctx_t *ctx) +{ + memxor(block, ctx->prev_block, ctx->blocksize_B); + bcal_cipher_enc(block, &(ctx->cctx)); + memcpy(ctx->prev_block, block, ctx->blocksize_B); } -void bcal_cbc_decNext(void *block, bcal_cbc_ctx_t *ctx){ - uint8_t tmp[ctx->blocksize_B]; - memcpy(tmp, block, ctx->blocksize_B); - bcal_cipher_dec(block, &(ctx->cctx)); - memxor(block, ctx->prev_block, ctx->blocksize_B); - memcpy(ctx->prev_block, tmp, ctx->blocksize_B); +void bcal_cbc_decNext(void *block, bcal_cbc_ctx_t *ctx) +{ + uint8_t tmp[ctx->blocksize_B]; + memcpy(tmp, block, ctx->blocksize_B); + bcal_cipher_dec(block, &(ctx->cctx)); + memxor(block, ctx->prev_block, ctx->blocksize_B); + memcpy(ctx->prev_block, tmp, ctx->blocksize_B); } -void bcal_cbc_decRand(void *block, const void *prev_block, bcal_cbc_ctx_t *ctx){ - bcal_cipher_dec(block, &(ctx->cctx)); - memxor(block, prev_block, ctx->blocksize_B); +void bcal_cbc_decRand(void *block, const void *prev_block, bcal_cbc_ctx_t *ctx) +{ + bcal_cipher_dec(block, &(ctx->cctx)); + memxor(block, prev_block, ctx->blocksize_B); } -void bcal_cbc_encMsg(const void *iv, void *msg, uint16_t msg_blocks, bcal_cbc_ctx_t *ctx){ - bcal_cbc_loadIV(iv, ctx); - while(msg_blocks--){ - bcal_cbc_encNext(msg, ctx); - msg = (uint8_t*)msg + ctx->blocksize_B; - } +void bcal_cbc_encMsg(const void *iv, void *msg, uint16_t msg_blocks, + bcal_cbc_ctx_t *ctx) +{ + bcal_cbc_loadIV(iv, ctx); + while (msg_blocks--) { + bcal_cbc_encNext(msg, ctx); + msg = (uint8_t*) msg + ctx->blocksize_B; + } } -void bcal_cbc_decMsg(const void *iv, void *msg, uint16_t msg_blocks, bcal_cbc_ctx_t *ctx){ - msg=(uint8_t*)msg + (msg_blocks-1)*ctx->blocksize_B; - while(msg_blocks > 1){ - bcal_cbc_decRand(msg, (uint8_t*)msg-ctx->blocksize_B, ctx); - msg_blocks -= 1; - msg=(uint8_t*)msg-ctx->blocksize_B; - } - bcal_cbc_decRand(msg, iv, ctx); +void bcal_cbc_decMsg(const void *iv, void *msg, uint16_t msg_blocks, + bcal_cbc_ctx_t *ctx) +{ + msg = (uint8_t*) msg + (msg_blocks - 1) * ctx->blocksize_B; + while (msg_blocks > 1) { + bcal_cbc_decRand(msg, (uint8_t*) msg - ctx->blocksize_B, ctx); + msg_blocks -= 1; + msg = (uint8_t*) msg - ctx->blocksize_B; + } + bcal_cbc_decRand(msg, iv, ctx); }