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