579ef56d9f24bb2e183a8e064d40acf4fb8f45b5
[arm-crypto-lib.git] / bcal / keysize_descriptor.c
1 /* keysize_descriptor.c */
2 /*
3     This file is part of the ARM-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 "keysize_descriptor.h"
30
31 uint8_t is_valid_keysize_P(const void* ks_desc, uint16_t keysize){
32         uint8_t type;
33         type = *((uint8_t*)ks_desc);
34         ks_desc = (uint8_t*)ks_desc + 1;
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 = *((uint8_t*)ks_desc);
41                 ks_desc = (uint8_t*)ks_desc + 1;
42                 while(items--){
43                         item = *((uint16_t*)ks_desc);
44                         ks_desc = (uint8_t*)ks_desc + 2;
45                         if(item==keysize)
46                                 return 1;
47                 }
48                 ks_desc = (uint8_t*)ks_desc - 2;
49         }
50         if(type==KS_TYPE_RANGE){
51                 uint16_t max, min;
52                 min = *((uint16_t*)ks_desc);
53                 ks_desc = (uint8_t*)ks_desc + 2;
54                 max = *((uint16_t*)ks_desc);
55                 if(min<=keysize && keysize<=max)
56                         return 1;
57         }
58         if(type==KS_TYPE_ARG_RANGE){
59                 uint16_t max, min, dist, offset;
60                 min = *((uint16_t*)ks_desc);
61                 ks_desc = (uint8_t*)ks_desc + 2;
62                 max = *((uint16_t*)ks_desc);
63                 ks_desc = (uint8_t*)ks_desc + 2;
64                 dist = *((uint16_t*)ks_desc);
65                 ks_desc = (uint8_t*)ks_desc + 2;
66                 offset = *((uint16_t*)ks_desc);
67                 if(min<=keysize && keysize<=max && (keysize%dist==offset))
68                         return 1;
69         }
70         if(type>KS_TYPE_ARG_RANGE){
71                 /* bad error, you may insert a big warning message here */
72                 return 0;
73         }
74         return is_valid_keysize_P((uint8_t*)ks_desc+1, keysize); /* search the next record */
75 }
76
77 uint16_t get_keysize(const void* ks_desc){
78         uint8_t type;
79         uint16_t keysize;
80         type = *((uint8_t*)ks_desc);
81         if(type==KS_TYPE_LIST){
82                 ks_desc = (uint8_t*)ks_desc + 1;
83         }
84         ks_desc = (uint8_t*)ks_desc + 1;
85         keysize = *((uint8_t*)ks_desc);
86         return keysize;
87 }
88
89 uint16_t get_keysizes(const void* ks_desc, uint16_t** list){
90         uint8_t type;
91         uint16_t items;
92         uint8_t i;
93         type = *((uint8_t*)ks_desc);
94         ks_desc = (uint8_t*)ks_desc + 1;
95         if(type==KS_TYPE_LIST){
96                 items = *((uint8_t*)ks_desc);
97                 ks_desc = (uint8_t*)ks_desc + 1;
98                 if(!*list){
99                         *list = malloc(items*2);
100                         if(!*list){
101                                 return 0;
102                         }
103                 }
104                 for(i=0; i<items; ++i){
105                         ((uint16_t*)(*list))[i] = *((uint16_t*)ks_desc);
106                         ks_desc = (uint8_t*)ks_desc + 2;
107                 }
108                 return items;
109         }
110         if(type==KS_TYPE_ARG_RANGE){
111                 uint16_t min, max, distance, offset;
112                 min = *((uint16_t*)ks_desc);
113                 ks_desc = (uint8_t*)ks_desc + 2;
114                 max = *((uint16_t*)ks_desc);
115                 ks_desc = (uint8_t*)ks_desc + 2;
116                 distance = *((uint16_t*)ks_desc);
117                 ks_desc = (uint8_t*)ks_desc + 2;
118                 offset = *((uint16_t*)ks_desc);
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 = *((uint16_t*)ks_desc);
140                 ks_desc = (uint8_t*)ks_desc + 2;
141                 max = *((uint16_t*)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 }