]> git.cryptolib.org Git - arm-crypto-lib.git/blob - keysize_descriptor.c
AES added
[arm-crypto-lib.git] / keysize_descriptor.c
1 /* keysize_descriptor.c */
2 /*
3     This file is part of the ARM-Crypto-Lib.
4     Copyright (C) 2006-2010  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 "keysize_descriptor.h"
29
30 uint8_t is_valid_keysize_P(const void* ks_desc, uint16_t keysize){
31         uint8_t type;
32         type = *((uint8_t*)ks_desc);
33         ks_desc = (uint8_t*)ks_desc + 1;
34         if(type==KS_TYPE_TERMINATOR)
35                 return 0;
36         if(type==KS_TYPE_LIST){
37                 uint8_t items;
38                 uint16_t item;
39                 items = *((uint8_t*)ks_desc);
40                 ks_desc = (uint8_t*)ks_desc + 1;
41                 while(items--){
42                         item = *((uint16_t*)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 = *((uint16_t*)ks_desc);
52                 ks_desc = (uint8_t*)ks_desc + 2;
53                 max = *((uint16_t*)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 = *((uint16_t*)ks_desc);
60                 ks_desc = (uint8_t*)ks_desc + 2;
61                 max = *((uint16_t*)ks_desc);
62                 ks_desc = (uint8_t*)ks_desc + 2;
63                 dist = *((uint16_t*)ks_desc);
64                 ks_desc = (uint8_t*)ks_desc + 2;
65                 offset = *((uint16_t*)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(const void* ks_desc){
77         uint8_t type;
78         uint16_t keysize;
79         type = *((uint8_t*)ks_desc);
80         if(type==KS_TYPE_LIST)
81                 ks_desc = (uint8_t*)ks_desc + 1;
82         ks_desc = (uint8_t*)ks_desc + 1;
83         keysize = *((uint16_t*)ks_desc);
84         return keysize;
85 }
86
87