X-Git-Url: https://git.cryptolib.org/?a=blobdiff_plain;f=bcal-cbc.c;fp=bcal-cbc.c;h=16d27a1f73bb9f17317392fdd0e65dda5dd3b9a8;hb=3f2f2071c47e2e424af71cc85697f3eb26c24277;hp=0000000000000000000000000000000000000000;hpb=33a28395461ecd0c1c7820f59e180f8f2ca9216f;p=arm-crypto-lib.git diff --git a/bcal-cbc.c b/bcal-cbc.c new file mode 100644 index 0000000..16d27a1 --- /dev/null +++ b/bcal-cbc.c @@ -0,0 +1,84 @@ +/* bcal-cbc.c */ +/* + This file is part of the ARM-Crypto-Lib. + Copyright (C) 2006-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 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 . +*/ + +#include +#include +#include "bcal-cbc.h" +#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); + + 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_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_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_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); +} +