]> git.cryptolib.org Git - arm-crypto-lib.git/blob - bcal-basic.c
AES added
[arm-crypto-lib.git] / bcal-basic.c
1 /* bcal-basic.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 #include <stdlib.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include "blockcipher_descriptor.h"
24 #include "keysize_descriptor.h"
25
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)){
29                 return 1;
30         }
31         uint8_t flags;
32         bc_init_fpt init_fpt;
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)))
39                         return 2;
40                 memcpy(ctx->ctx, key, (keysize_b+7)/8);
41                 return 0;
42         }
43         if(!(ctx->ctx = malloc(cipher_descriptor->ctxsize_B)))
44                 return 3;
45         if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
46                 init_fpt.init1((void*)key, (ctx->ctx));
47         }else{
48                 init_fpt.init2((void*)key, keysize_b, (ctx->ctx));
49         }
50         return 0;
51 }
52
53 void bcal_cipher_free(bcgen_ctx_t* ctx){
54         if(!ctx)
55                 return;
56         bc_free_fpt free_fpt;
57         free_fpt = (bc_free_fpt)(ctx->desc_ptr->free);
58         if(free_fpt)
59                 free_fpt((ctx->ctx));
60         free(ctx->ctx);
61 }
62
63 void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx){
64         bc_enc_fpt enc_fpt;
65         enc_fpt.encvoid = (void_fpt)(ctx->desc_ptr->enc.encvoid);
66         if(!enc_fpt.encvoid){
67                 /* very bad error, no enciphering function specified */
68                 return;
69         }
70         enc_fpt.enc1(block, (ctx->ctx));
71         
72 }
73
74 void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx){
75         bc_dec_fpt dec_fpt;
76         dec_fpt.decvoid = (void_fpt)(ctx->desc_ptr->dec.decvoid);
77         if(!dec_fpt.decvoid){
78                 /* very bad error, no deciphering function specified */
79                 return;
80         }
81         dec_fpt.dec1(block, (ctx->ctx));
82 }
83
84 uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t* desc){
85         return (desc->blocksize_b);
86 }
87
88 const void* bcal_cipher_getKeysizeDesc(const bcdesc_t* desc){
89         return (desc->valid_keysize_desc);
90 }
91
92