]> git.cryptolib.org Git - avr-crypto-lib.git/blob - scal/scal-basic.c
trivium fixed; further migrating to SCAL
[avr-crypto-lib.git] / scal / scal-basic.c
1 /* scal-basic.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2011  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 "streamcipher_descriptor.h"
25 #include "keysize_descriptor.h"
26
27 #include "cli.h"
28
29 uint8_t scal_cipher_init(const scdesc_t* cipher_descriptor,
30                          const void* key, uint16_t keysize_b,
31                          const void* iv,  uint16_t ivsize_b, scgen_ctx_t* ctx){
32         ctx->buffer = NULL;
33         ctx->ctx = NULL;
34
35         if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_keysize_desc)), keysize_b)){
36                 return 1;
37         }
38         if(!is_valid_keysize_P((PGM_VOID_P)pgm_read_word(&(cipher_descriptor->valid_ivsize_desc)), ivsize_b)){
39                 return 2;
40         }
41         uint8_t flags;
42         void_fpt init_fpt;
43         flags = pgm_read_byte(&(cipher_descriptor->flags));
44         ctx->desc_ptr = cipher_descriptor;
45         ctx->keysize = keysize_b;
46         ctx->ivsize  = ivsize_b;
47         ctx->ctx = malloc(pgm_read_word(&(cipher_descriptor->ctxsize_B)));
48         if(ctx->ctx==NULL){
49                 return 3;
50         }
51         init_fpt = (void_fpt)pgm_read_word(&(cipher_descriptor->init));
52         switch(flags&SC_INIT_TYPE){
53                 case SC_INIT_TYPE_1:
54                         ((sc_init1_fpt)init_fpt)(key, ctx->ctx);
55                         break;
56                 case SC_INIT_TYPE_2:
57                         ((sc_init2_fpt)init_fpt)(key, iv, ctx->ctx);
58                         break;
59                 case SC_INIT_TYPE_3:
60                         ((sc_init3_fpt)init_fpt)(key, keysize_b, ctx->ctx);
61                         break;
62                 case SC_INIT_TYPE_4:
63                         ((sc_init4_fpt)init_fpt)(key, keysize_b, iv, ctx->ctx);
64                         break;
65                 case SC_INIT_TYPE_5:
66                         ((sc_init5_fpt)init_fpt)(key, keysize_b, iv, ivsize_b, ctx->ctx);
67                         break;
68                 default:
69                         return 4;
70         }
71         uint16_t blocksize_b;
72         blocksize_b = pgm_read_word(&(cipher_descriptor->gensize_b));
73         if(blocksize_b>8){
74                 ctx->buffer=malloc((blocksize_b+7)/8);
75                 if(ctx->buffer==NULL){
76                         return 5;
77                 }
78                 ctx->index=0;
79         }
80         return 0;
81 }
82
83
84 void scal_cipher_free(scgen_ctx_t* ctx){
85         if(ctx->buffer){
86                 free(ctx->buffer);
87         }
88         if(ctx->ctx){
89                 free(ctx->ctx);
90         }
91 }
92
93 uint8_t scal_cipher_gen_byte(scgen_ctx_t* ctx){
94         uint8_t flags;
95         uint16_t blocksize_b;
96         void_fpt gen_fpt;
97         flags = pgm_read_byte(&(ctx->desc_ptr->flags));
98         blocksize_b = pgm_read_word(&(ctx->desc_ptr->gensize_b));
99         gen_fpt = (void_fpt)(pgm_read_word(&(ctx->desc_ptr->gen.genvoid)));
100
101         if(blocksize_b==8){
102                 if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
103                         return ((sc_gen1_fpt)gen_fpt)(ctx->ctx);
104                 }else{
105                         uint8_t r;
106                         ((sc_gen2_fpt)gen_fpt)(&r, ctx->ctx);
107                         return r;
108                 }
109         }
110         if(blocksize_b<8){
111                 uint8_t r=0;
112                 uint8_t fill=0;
113                 do{
114                         r |= ((((sc_gen1_fpt)gen_fpt)(ctx->ctx))&(0xff<<(8-blocksize_b)))>>fill;
115                         fill += blocksize_b;
116                 }while(fill<8);
117 //              cli_putstr_P(PSTR("\r\nDBG: "));
118 //              cli_hexdump_byte(r);
119                 return r;
120         }else{
121                 uint8_t r;
122                 if(ctx->index==0){
123                         ((sc_gen2_fpt)gen_fpt)(ctx->buffer, ctx->ctx);
124                         ctx->index = blocksize_b;
125                 }
126                 r=ctx->buffer[(blocksize_b-ctx->index)/8];
127                 ctx->index -= 8;
128                 return r;
129         }
130 }
131
132 void scal_cipher_gen_block(void* block, scgen_ctx_t* ctx){
133         uint8_t flags;
134         uint16_t blocksize_b;
135         void_fpt gen_fpt;
136         flags = pgm_read_byte(&(ctx->desc_ptr->flags));
137         blocksize_b = pgm_read_word(&(ctx->desc_ptr->gensize_b));
138         gen_fpt = (void_fpt)pgm_read_word(&(ctx->desc_ptr->gen));
139         if((flags&SC_GEN_TYPE)==SC_GEN_TYPE_1){
140                 *((uint8_t*)block) = ((sc_gen1_fpt)gen_fpt)(ctx->ctx);
141         }else{
142                 ((sc_gen2_fpt)gen_fpt)(block, ctx->ctx);
143         }
144 }
145
146 void scal_cipher_gen_fillblock(void* block, uint16_t blocksize_B, scgen_ctx_t* ctx){
147         while(blocksize_B){
148                 *((uint8_t*)block) = scal_cipher_gen_byte(ctx);
149                 block = (uint8_t*)block + 1;
150                 blocksize_B -= 1;
151         }
152 }
153
154 uint16_t scal_cipher_getBlocksize_b(const scdesc_t* desc){
155         uint16_t blocksize_b;
156         blocksize_b = pgm_read_word(&(desc->gensize_b));
157         return blocksize_b;
158 }
159
160 PGM_VOID_P scal_cipher_getKeysizeDesc(const scdesc_t* desc){
161         return (PGM_VOID_P)pgm_read_word(&(desc->valid_keysize_desc));
162 }
163
164 PGM_VOID_P scal_cipher_getIVsizeDesc(const scdesc_t* desc){
165         return (PGM_VOID_P)pgm_read_word(&(desc->valid_ivsize_desc));
166 }
167
168