]> git.cryptolib.org Git - avr-crypto-lib.git/blob - bcal-basic.c
3d7a65c41b8f30c490b0e0ce606756ad0cb7ec0c
[avr-crypto-lib.git] / bcal-basic.c
1 /* bcal-basic.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2009  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 <avr/pgmspace.h>
24 #include "blockcipher_descriptor.h"
25 #include "keysize_descriptor.h"
26
27 uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
28                          const void* key, uint16_t keysize_b, bcgen_ctx_t* ctx){
29         if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_keysize_desc)),
30                                keysize_b)){
31                 return 1;
32         }
33         uint8_t flags;
34         bc_init_fpt init_fpt;
35         ctx->desc_ptr = (bcdesc_t*)cipher_descriptor;
36         ctx->keysize  = keysize_b;
37         flags = pgm_read_byte(cipher_descriptor->flags);
38         init_fpt.initvoid = (void_fpt)(pgm_read_word(&(cipher_descriptor->init.initvoid)));
39         if(init_fpt.initvoid == NULL){
40                 if(!(ctx->ctx = malloc((keysize_b+7)/8)))
41                         return 2;
42                 memcpy(ctx->ctx, key, (keysize_b+7)/8);
43                 return 0;
44         }
45         if(!(ctx->ctx = malloc(pgm_read_word(&(cipher_descriptor->ctxsize_B)))))
46                 return 3;
47         if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
48                 init_fpt.init1((void*)key, (ctx->ctx));
49         }else{
50                 init_fpt.init2((void*)key, keysize_b, (ctx->ctx));
51         }
52         return 0;
53 }
54
55 void bcal_cipher_free(bcgen_ctx_t* ctx){
56         if(!ctx)
57                 return;
58         bc_free_fpt free_fpt;
59         free_fpt = (bc_free_fpt)(pgm_read_word(&(ctx->desc_ptr->free)));
60         if(free_fpt)
61                 free_fpt((ctx->ctx));
62         free(ctx->ctx);
63 }
64
65 void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx){
66         bc_enc_fpt enc_fpt;
67         enc_fpt.encvoid = (void_fpt)pgm_read_word(&(ctx->desc_ptr->enc.encvoid));
68         if(!enc_fpt.encvoid){
69                 /* very bad error, no enciphering function specified */
70                 return;
71         }
72         enc_fpt.enc1(block, (ctx->ctx));
73         
74 }
75
76 void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx){
77         bc_dec_fpt dec_fpt;
78         dec_fpt.decvoid = (void_fpt)pgm_read_word(&(ctx->desc_ptr->dec.decvoid));
79         if(!dec_fpt.decvoid){
80                 /* very bad error, no deciphering function specified */
81                 return;
82         }
83         dec_fpt.dec1(block, (ctx->ctx));
84 }
85
86 uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t* desc){
87         return pgm_read_word(&(desc->blocksize_b));
88 }
89
90 PGM_VOID_P bcal_cipher_getKeysizeDesc(const bcdesc_t* desc){
91         return (PGM_VOID_P)pgm_read_word(&(desc->valid_keysize_desc));
92 }
93
94