1 /* keysize_descriptor.c */
3 This file is part of the AVR-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 #if (defined(__AVR__))
30 #include <avr\pgmspace.h>
34 #include "keysize_descriptor.h"
36 uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize){
38 type = pgm_read_byte(ks_desc++);
39 if(type==KS_TYPE_TERMINATOR)
41 if(type==KS_TYPE_LIST){
44 items = pgm_read_byte(ks_desc++);
46 item = pgm_read_word(ks_desc);
47 ks_desc = (uint8_t*)ks_desc + 2;
51 ks_desc = (uint8_t*)ks_desc - 2;
53 if(type==KS_TYPE_RANGE){
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)
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))
73 if(type>KS_TYPE_ARG_RANGE){
74 /* bad error, you may insert a big warning message here */
77 return is_valid_keysize_P((uint8_t*)ks_desc+1, keysize); /* search the next record */
80 uint16_t get_keysize(PGM_VOID_P ks_desc){
83 type = pgm_read_byte(ks_desc);
84 if(type==KS_TYPE_LIST){
85 ks_desc = (uint8_t*)ks_desc + 1;
87 ks_desc = (uint8_t*)ks_desc + 1;
88 keysize = pgm_read_word(ks_desc);
92 uint16_t get_keysizes(PGM_VOID_P ks_desc, uint16_t** list){
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;
102 *list = malloc(items*2);
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;
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){
126 min += (distance-(min%distance-offset))%distance;
129 *list = malloc(items*2);
136 ((uint16_t*)*list)[i++] = min;
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){
149 min += (distance-(min%distance-offset))%distance;
152 *list = malloc(items*2);
159 ((uint16_t*)*list)[i++] = min;