3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2009 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/>.
24 #if (defined(__AVR__))
25 #include <avr\pgmspace.h>
29 #include "blockcipher_descriptor.h"
30 #include "keysize_descriptor.h"
32 uint8_t bcal_cipher_init(const bcdesc_t* cipher_descriptor,
33 const void* key, uint16_t keysize_b, bcgen_ctx_t* ctx){
34 if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_keysize_desc)),
40 ctx->desc_ptr = (bcdesc_t*)cipher_descriptor;
41 ctx->keysize = keysize_b;
42 flags = pgm_read_byte(cipher_descriptor->flags);
43 init_fpt.initvoid = (void_fpt)(pgm_read_word(&(cipher_descriptor->init.initvoid)));
44 if(init_fpt.initvoid == NULL){
45 if(!(ctx->ctx = malloc((keysize_b+7)/8)))
47 memcpy(ctx->ctx, key, (keysize_b+7)/8);
50 if(!(ctx->ctx = malloc(pgm_read_word(&(cipher_descriptor->ctxsize_B)))))
52 if((flags&BC_INIT_TYPE)==BC_INIT_TYPE_1){
53 init_fpt.init1((void*)key, (ctx->ctx));
55 init_fpt.init2((void*)key, keysize_b, (ctx->ctx));
60 void bcal_cipher_free(bcgen_ctx_t* ctx){
64 free_fpt = (bc_free_fpt)(pgm_read_word(&(ctx->desc_ptr->free)));
70 void bcal_cipher_enc(void* block, const bcgen_ctx_t* ctx){
72 enc_fpt.encvoid = (void_fpt)pgm_read_word(&(ctx->desc_ptr->enc.encvoid));
74 /* very bad error, no enciphering function specified */
77 enc_fpt.enc1(block, (ctx->ctx));
81 void bcal_cipher_dec(void* block, const bcgen_ctx_t* ctx){
83 dec_fpt.decvoid = (void_fpt)pgm_read_word(&(ctx->desc_ptr->dec.decvoid));
85 /* very bad error, no deciphering function specified */
88 dec_fpt.dec1(block, (ctx->ctx));
91 uint16_t bcal_cipher_getBlocksize_b(const bcdesc_t* desc){
92 return pgm_read_word(&(desc->blocksize_b));
95 PGM_VOID_P bcal_cipher_getKeysizeDesc(const bcdesc_t* desc){
96 return (PGM_VOID_P)pgm_read_word(&(desc->valid_keysize_desc));