3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2011 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 <avr/pgmspace.h>
24 #include "streamcipher_descriptor.h"
25 #include "keysize_descriptor.h"
27 uint8_t scal_cipher_init(const scdesc_t *cipher_descriptor,
28 const void *key, uint16_t keysize_b,
29 const void *iv, uint16_t ivsize_b, scgen_ctx_t *ctx){
33 if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_keysize_desc)), keysize_b)){
36 if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_ivsize_desc)), ivsize_b)){
41 flags = pgm_read_byte(&(cipher_descriptor->flags));
42 ctx->desc_ptr = cipher_descriptor;
43 ctx->keysize = keysize_b;
44 ctx->ivsize = ivsize_b;
45 ctx->ctx = malloc(pgm_read_word(&(cipher_descriptor->ctxsize_B)));
49 init_fpt = (void_fpt)pgm_read_word(&(cipher_descriptor->init));
50 switch(flags&SC_INIT_TYPE){
52 ((sc_init1_fpt)init_fpt)(key, ctx->ctx);
55 ((sc_init2_fpt)init_fpt)(key, iv, ctx->ctx);
58 ((sc_init3_fpt)init_fpt)(key, keysize_b, ctx->ctx);
61 ((sc_init4_fpt)init_fpt)(key, keysize_b, iv, ctx->ctx);
64 ((sc_init5_fpt)init_fpt)(key, keysize_b, iv, ivsize_b, ctx->ctx);
70 blocksize_b = pgm_read_word(&(cipher_descriptor->gensize_b));
72 ctx->buffer=malloc((blocksize_b+7)/8);
73 if(ctx->buffer==NULL){
82 void scal_cipher_free(scgen_ctx_t *ctx){
91 uint8_t scal_cipher_gen_byte(scgen_ctx_t *ctx){
95 flags = pgm_read_byte(&(ctx->desc_ptr->flags));
96 blocksize_b = pgm_read_word(&(ctx->desc_ptr->gensize_b));
97 gen_fpt = (void_fpt)(pgm_read_word(&(ctx->desc_ptr->gen.genvoid)));
100 if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
101 return ((sc_gen1_fpt)gen_fpt)(ctx->ctx);
104 ((sc_gen2_fpt)gen_fpt)(&r, ctx->ctx);
112 r |= ((((sc_gen1_fpt)gen_fpt)(ctx->ctx))&(0xff<<(8-blocksize_b)))>>fill;
119 ((sc_gen2_fpt)gen_fpt)(ctx->buffer, ctx->ctx);
120 ctx->index = blocksize_b;
122 r=ctx->buffer[(blocksize_b-ctx->index)/8];
128 void scal_cipher_gen_block(void *block, scgen_ctx_t *ctx){
131 flags = pgm_read_byte(&(ctx->desc_ptr->flags));
132 gen_fpt = (void_fpt)pgm_read_word(&(ctx->desc_ptr->gen));
133 if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
134 *((uint8_t*)block) = ((sc_gen1_fpt)gen_fpt)(ctx->ctx);
136 ((sc_gen2_fpt)gen_fpt)(block, ctx->ctx);
140 void scal_cipher_gen_fillblock(void *block, uint16_t blocksize_B, scgen_ctx_t *ctx){
142 *((uint8_t*)block) = scal_cipher_gen_byte(ctx);
143 block = (uint8_t*)block + 1;
148 uint16_t scal_cipher_getBlocksize_b(const scdesc_t *desc){
149 uint16_t blocksize_b;
150 blocksize_b = pgm_read_word(&(desc->gensize_b));
154 PGM_VOID_P scal_cipher_getKeysizeDesc(const scdesc_t *desc){
155 return (PGM_VOID_P)pgm_read_word(&(desc->valid_keysize_desc));
158 PGM_VOID_P scal_cipher_getIVsizeDesc(const scdesc_t *desc){
159 return (PGM_VOID_P)pgm_read_word(&(desc->valid_ivsize_desc));