1 /* keysize_descriptor.c */
3 This file is part of the ARM-Crypto-Lib.
4 Copyright (C) 2009 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
20 * \file keysize_descriptor.c
22 * \email daniel.otte@rub.de
24 * \license GPLv3 or later
29 #include "keysize_descriptor.h"
31 #define KS8 ((uint8_t*)ks_desc)
32 uint8_t is_valid_keysize_P(const void* ks_desc, uint16_t keysize){
36 if(type==KS_TYPE_TERMINATOR)
38 if(type==KS_TYPE_LIST){
44 item = KS8[1] * 256 + KS8[0];
51 if(type==KS_TYPE_RANGE){
53 min = KS8[1] * 256 + KS8[0];
54 ks_desc = (uint8_t*)ks_desc + 2;
55 max = KS8[1] * 256 + KS8[0];
56 if(min <= keysize && keysize <= max)
59 if(type==KS_TYPE_ARG_RANGE){
60 uint16_t max, min, dist, offset;
61 min = KS8[1] * 256 + KS8[0];
62 max = KS8[3] * 256 + KS8[2];
63 dist = KS8[5] * 256 + KS8[4];
64 offset = KS8[7] * 256 + KS8[6];
66 if(min <= keysize && keysize <= max && (keysize % dist == offset))
69 if(type>KS_TYPE_ARG_RANGE){
70 /* bad error, you may insert a big warning message here */
73 return is_valid_keysize_P(KS8+1, keysize); /* search the next record */
76 uint16_t get_keysize(const void* ks_desc){
79 type = *((uint8_t*)ks_desc);
80 if(type==KS_TYPE_LIST){
81 ks_desc = (uint8_t*)ks_desc + 1;
83 ks_desc = (uint8_t*)ks_desc + 1;
84 keysize = *((uint8_t*)ks_desc);
88 uint16_t get_keysizes(const void* ks_desc, uint16_t** list){
92 type = *((uint8_t*)ks_desc);
93 ks_desc = (uint8_t*)ks_desc + 1;
94 if(type==KS_TYPE_LIST){
95 items = *((uint8_t*)ks_desc);
96 ks_desc = (uint8_t*)ks_desc + 1;
98 *list = malloc(items*2);
103 for(i=0; i<items; ++i){
104 ((uint16_t*)(*list))[i] = *((uint16_t*)ks_desc);
105 ks_desc = (uint8_t*)ks_desc + 2;
109 if(type==KS_TYPE_ARG_RANGE){
110 uint16_t min, max, distance, offset;
111 min = *((uint16_t*)ks_desc);
112 ks_desc = (uint8_t*)ks_desc + 2;
113 max = *((uint16_t*)ks_desc);
114 ks_desc = (uint8_t*)ks_desc + 2;
115 distance = *((uint16_t*)ks_desc);
116 ks_desc = (uint8_t*)ks_desc + 2;
117 offset = *((uint16_t*)ks_desc);
118 items = (max-min)/distance+1;
119 if(min%distance!=offset){
121 min += (distance-(min%distance-offset))%distance;
124 *list = malloc(items*2);
131 ((uint16_t*)*list)[i++] = min;
136 if(type==KS_TYPE_RANGE){
137 uint16_t min, max, distance=8, offset=0;
138 min = *((uint16_t*)ks_desc);
139 ks_desc = (uint8_t*)ks_desc + 2;
140 max = *((uint16_t*)ks_desc);
141 items = (max-min)/distance+1;
142 if(min%distance!=offset){
144 min += (distance-(min%distance-offset))%distance;
147 *list = malloc(items*2);
154 ((uint16_t*)*list)[i++] = min;