]> AND Private Git Repository - Cipher_code.git/blob - Arduino/libraries/AESLib-master/keysize_descriptor.c~
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
new tests for eor
[Cipher_code.git] / Arduino / libraries / AESLib-master / keysize_descriptor.c~
1 /* keysize_descriptor.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  * \file    keysize_descriptor.c 
21  * \author  Daniel Otte
22  * \email   daniel.otte@rub.de
23  * \date    2009-01-07
24  * \license GPLv3 or later
25  */
26
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <avr/pgmspace.h>
30 #include "keysize_descriptor.h"
31
32 uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize){
33         uint8_t type;
34         type = pgm_read_byte(ks_desc++);
35         if(type==KS_TYPE_TERMINATOR)
36                 return 0;
37         if(type==KS_TYPE_LIST){
38                 uint8_t items;
39                 uint16_t item;
40                 items = pgm_read_byte(ks_desc++);
41                 while(items--){
42                         item = pgm_read_word(ks_desc);
43                         ks_desc = (uint8_t*)ks_desc + 2;
44                         if(item==keysize)
45                                 return 1;
46                 }
47                 ks_desc = (uint8_t*)ks_desc - 2;
48         }
49         if(type==KS_TYPE_RANGE){
50                 uint16_t max, min;
51                 min = pgm_read_word(ks_desc);
52                 ks_desc = (uint8_t*)ks_desc + 2;
53                 max = pgm_read_word(ks_desc);
54                 if(min<=keysize && keysize<=max)
55                         return 1;
56         }
57         if(type==KS_TYPE_ARG_RANGE){
58                 uint16_t max, min, dist, offset;
59                 min = pgm_read_word(ks_desc);
60                 ks_desc = (uint8_t*)ks_desc + 2;
61                 max = pgm_read_word(ks_desc);
62                 ks_desc = (uint8_t*)ks_desc + 2;
63                 dist = pgm_read_word(ks_desc);
64                 ks_desc = (uint8_t*)ks_desc + 2;
65                 offset = pgm_read_word(ks_desc);
66                 if(min<=keysize && keysize<=max && (keysize%dist==offset))
67                         return 1;
68         }
69         if(type>KS_TYPE_ARG_RANGE){
70                 /* bad error, you may insert a big warning message here */
71                 return 0;
72         }
73         return is_valid_keysize_P((uint8_t*)ks_desc+1, keysize); /* search the next record */
74 }
75
76 uint16_t get_keysize(PGM_VOID_P ks_desc){
77         uint8_t type;
78         uint16_t keysize;
79         type = pgm_read_byte(ks_desc);
80         if(type==KS_TYPE_LIST){
81                 ks_desc = (uint8_t*)ks_desc + 1;
82         }
83         ks_desc = (uint8_t*)ks_desc + 1;
84         keysize = pgm_read_word(ks_desc);
85         return keysize;
86 }
87
88 uint16_t get_keysizes(PGM_VOID_P ks_desc, uint16_t** list){
89         uint8_t type;
90         uint16_t items;
91         uint8_t i;
92         type = pgm_read_byte(ks_desc);
93         ks_desc = (uint8_t*)ks_desc + 1;
94         if(type==KS_TYPE_LIST){
95                 items = pgm_read_byte(ks_desc);
96                 ks_desc = (uint8_t*)ks_desc + 1;
97                 if(!*list){
98                         *list = malloc(items*2);
99                         if(!*list){
100                                 return 0;
101                         }
102                 }
103                 for(i=0; i<items; ++i){
104                         ((uint16_t*)(*list))[i] = pgm_read_word(ks_desc);
105                         ks_desc = (uint8_t*)ks_desc + 2;
106                 }
107                 return items;
108         }
109         if(type==KS_TYPE_ARG_RANGE){
110                 uint16_t min, max, distance, offset;
111                 min = pgm_read_word(ks_desc);
112                 ks_desc = (uint8_t*)ks_desc + 2;
113                 max = pgm_read_word(ks_desc);
114                 ks_desc = (uint8_t*)ks_desc + 2;
115                 distance = pgm_read_word(ks_desc);
116                 ks_desc = (uint8_t*)ks_desc + 2;
117                 offset = pgm_read_word(ks_desc);
118                 ks_desc = (uint8_t*)ks_desc + 2;
119                 items = (max-min)/distance+1;
120                 if(min%distance!=offset){
121                         --items;
122                         min += (distance-(min%distance-offset))%distance;
123                 }
124                 if(!*list){
125                         *list = malloc(items*2);
126                         if(!*list){
127                                 return 0;
128                         }
129                 }
130                 i=0;
131                 while(min<max){
132                         ((uint16_t*)*list)[i++] = min;
133                         min += distance;
134                 }
135                 return i;
136         }
137         if(type==KS_TYPE_RANGE){
138                 uint16_t min, max, distance=8, offset=0;
139                 min = pgm_read_word(ks_desc);
140                 ks_desc = (uint8_t*)ks_desc + 2;
141                 max = pgm_read_word(ks_desc);
142                 items = (max-min)/distance+1;
143                 if(min%distance!=offset){
144                         --items;
145                         min += (distance-(min%distance-offset))%distance;
146                 }
147                 if(!*list){
148                         *list = malloc(items*2);
149                         if(!*list){
150                                 return 0;
151                         }
152                 }
153                 i=0;
154                 while(min<max){
155                         ((uint16_t*)*list)[i++] = min;
156                         min += distance;
157                 }
158                 return i;
159         }
160         return 0;
161 }