]> git.cryptolib.org Git - avr-crypto-lib.git/blob - keysize_descriptor.c
turning on software flow control for uart
[avr-crypto-lib.git] / keysize_descriptor.c
1 /* keysize_descriptor.c */
2 /*
3     This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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   bg@nerilex.org
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         }
38         if (type == KS_TYPE_LIST) {
39                 uint8_t items;
40                 uint16_t item;
41                 items = pgm_read_byte(ks_desc++);
42                 while (items--) {
43                         item = pgm_read_word(ks_desc);
44                         ks_desc = (uint8_t*)ks_desc + 2;
45                         if (item == keysize) {
46                                 return 1;
47                         }
48                 }
49                 ks_desc = (uint8_t*)ks_desc - 2;
50         }
51         if (type == KS_TYPE_RANGE) {
52                 uint16_t max, min;
53                 min = pgm_read_word(ks_desc);
54                 ks_desc = (uint8_t*)ks_desc + 2;
55                 max = pgm_read_word(ks_desc);
56                 if (min <= keysize && keysize <= max) {
57                         return 1;
58                 }
59         }
60         if (type == KS_TYPE_ARG_RANGE) {
61                 uint16_t max, min, dist, offset;
62                 min = pgm_read_word(ks_desc);
63                 ks_desc = (uint8_t*)ks_desc + 2;
64                 max = pgm_read_word(ks_desc);
65                 ks_desc = (uint8_t*)ks_desc + 2;
66                 dist = pgm_read_word(ks_desc);
67                 ks_desc = (uint8_t*)ks_desc + 2;
68                 offset = pgm_read_word(ks_desc);
69                 if (min <= keysize && keysize <= max && (keysize % dist == offset)) {
70                         return 1;
71                 }
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 }