1 /* keysize_descriptor.c */
3 This file is part of the AVR-Crypto-Lib.
4 Copyright (C) 2006-2015 Daniel Otte (bg@nerilex.org)
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 bg@nerilex.org
24 * \license GPLv3 or later
29 #include <avr/pgmspace.h>
30 #include "keysize_descriptor.h"
32 uint8_t is_valid_keysize_P(PGM_VOID_P ks_desc, uint16_t keysize){
34 type = pgm_read_byte(ks_desc++);
35 if (type == KS_TYPE_TERMINATOR) {
38 if (type == KS_TYPE_LIST) {
41 items = pgm_read_byte(ks_desc++);
43 item = pgm_read_word(ks_desc);
44 ks_desc = (uint8_t*)ks_desc + 2;
45 if (item == keysize) {
49 ks_desc = (uint8_t*)ks_desc - 2;
51 if (type == KS_TYPE_RANGE) {
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) {
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)) {
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;