3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2006-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/>.
23 #include "blockcipher_descriptor.h"
24 #include "keysize_descriptor.h"
26 uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
27 const void* key, uint16_t keysize_b, bcgen_ctx_t* ctx){
28 if(!is_valid_keysize_P(cipher_descriptor->valid_keysize_desc, keysize_b)){
33 ctx->desc_ptr = (bcdesc_t*)cipher_descriptor;
34 ctx->keysize = keysize_b;
35 flags = cipher_descriptor->flags;
36 init_fpt.initvoid = (void_fpt)(cipher_descriptor->init.initvoid);
37 if(init_fpt.initvoid == NULL){
38 if(!(ctx->ctx = malloc((keysize_b+7)/8)))
40 memcpy(ctx->ctx, key, (keysize_b+7)/8);
43 if(!(ctx->ctx = malloc(cipher_descriptor->ctxsize_B)))
45 if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
46 init_fpt.init1((void*)key, (ctx->ctx));
48 init_fpt.init2((void*)key, keysize_b, (ctx->ctx));
53 void bcal_cipher_free(bcgen_ctx_t* ctx){
57 free_fpt = (bc_free_fpt)(ctx->desc_ptr->free);
63 void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx){
65 enc_fpt.encvoid = (void_fpt)(ctx->desc_ptr->enc.encvoid);
67 /* very bad error, no enciphering function specified */
70 enc_fpt.enc1(block, (ctx->ctx));
74 void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx){
76 dec_fpt.decvoid = (void_fpt)(ctx->desc_ptr->dec.decvoid);
78 /* very bad error, no deciphering function specified */
81 dec_fpt.dec1(block, (ctx->ctx));
84 uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t* desc){
85 return (desc->blocksize_b);
88 const void* bcal_cipher_getKeysizeDesc(const bcdesc_t* desc){
89 return (desc->valid_keysize_desc);