keysize_descriptor.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /**
  17. * \file keysize_descriptor.c
  18. * \author Daniel Otte
  19. * \email daniel.otte@rub.de
  20. * \date 2009-01-07
  21. * \license GPLv3 or later
  22. */
  23. #include <stdint.h>
  24. #include <stdlib.h>
  25. #include <crypto/keysize_descriptor.h>
  26. #define KS8 ((uint8_t*)ks_desc)
  27. uint8_t is_valid_keysize_P(const void* ks_desc, uint16_t keysize){
  28. uint8_t type;
  29. type = KS8[0];
  30. ks_desc = KS8 + 1;
  31. if(type==KS_TYPE_TERMINATOR)
  32. return 0;
  33. if(type==KS_TYPE_LIST){
  34. uint8_t items;
  35. uint16_t item;
  36. items = KS8[0];
  37. ks_desc = KS8 + 1;
  38. while(items--){
  39. item = KS8[1] * 256 + KS8[0];
  40. ks_desc = KS8 + 2;
  41. if(item==keysize)
  42. return 1;
  43. }
  44. ks_desc = KS8 - 2;
  45. }
  46. if(type==KS_TYPE_RANGE){
  47. uint16_t max, min;
  48. min = KS8[1] * 256 + KS8[0];
  49. ks_desc = (uint8_t*)ks_desc + 2;
  50. max = KS8[1] * 256 + KS8[0];
  51. if(min <= keysize && keysize <= max)
  52. return 1;
  53. }
  54. if(type==KS_TYPE_ARG_RANGE){
  55. uint16_t max, min, dist, offset;
  56. min = KS8[1] * 256 + KS8[0];
  57. max = KS8[3] * 256 + KS8[2];
  58. dist = KS8[5] * 256 + KS8[4];
  59. offset = KS8[7] * 256 + KS8[6];
  60. ks_desc = KS8 + 6;
  61. if(min <= keysize && keysize <= max && (keysize % dist == offset))
  62. return 1;
  63. }
  64. if(type>KS_TYPE_ARG_RANGE){
  65. /* bad error, you may insert a big warning message here */
  66. return 0;
  67. }
  68. return is_valid_keysize_P(KS8+1, keysize); /* search the next record */
  69. }
  70. uint16_t get_keysize(const void* ks_desc){
  71. uint8_t type;
  72. uint16_t keysize;
  73. type = *((uint8_t*)ks_desc);
  74. if(type==KS_TYPE_LIST){
  75. ks_desc = (uint8_t*)ks_desc + 1;
  76. }
  77. ks_desc = (uint8_t*)ks_desc + 1;
  78. keysize = *((uint8_t*)ks_desc);
  79. return keysize;
  80. }
  81. uint16_t get_keysizes(const void* ks_desc, uint16_t** list){
  82. uint8_t type;
  83. uint16_t items;
  84. uint8_t i;
  85. type = *((uint8_t*)ks_desc);
  86. ks_desc = (uint8_t*)ks_desc + 1;
  87. if(type==KS_TYPE_LIST){
  88. items = *((uint8_t*)ks_desc);
  89. ks_desc = (uint8_t*)ks_desc + 1;
  90. if(!*list){
  91. *list = malloc(items*2);
  92. if(!*list){
  93. return 0;
  94. }
  95. }
  96. for(i=0; i<items; ++i){
  97. ((uint16_t*)(*list))[i] = *((uint16_t*)ks_desc);
  98. ks_desc = (uint8_t*)ks_desc + 2;
  99. }
  100. return items;
  101. }
  102. if(type==KS_TYPE_ARG_RANGE){
  103. uint16_t min, max, distance, offset;
  104. min = *((uint16_t*)ks_desc);
  105. ks_desc = (uint8_t*)ks_desc + 2;
  106. max = *((uint16_t*)ks_desc);
  107. ks_desc = (uint8_t*)ks_desc + 2;
  108. distance = *((uint16_t*)ks_desc);
  109. ks_desc = (uint8_t*)ks_desc + 2;
  110. offset = *((uint16_t*)ks_desc);
  111. items = (max-min)/distance+1;
  112. if(min%distance!=offset){
  113. --items;
  114. min += (distance-(min%distance-offset))%distance;
  115. }
  116. if(!*list){
  117. *list = malloc(items*2);
  118. if(!*list){
  119. return 0;
  120. }
  121. }
  122. i=0;
  123. while(min<max){
  124. ((uint16_t*)*list)[i++] = min;
  125. min += distance;
  126. }
  127. return i;
  128. }
  129. if(type==KS_TYPE_RANGE){
  130. uint16_t min, max, distance=8, offset=0;
  131. min = *((uint16_t*)ks_desc);
  132. ks_desc = (uint8_t*)ks_desc + 2;
  133. max = *((uint16_t*)ks_desc);
  134. items = (max-min)/distance+1;
  135. if(min%distance!=offset){
  136. --items;
  137. min += (distance-(min%distance-offset))%distance;
  138. }
  139. if(!*list){
  140. *list = malloc(items*2);
  141. if(!*list){
  142. return 0;
  143. }
  144. }
  145. i=0;
  146. while(min<max){
  147. ((uint16_t*)*list)[i++] = min;
  148. min += distance;
  149. }
  150. return i;
  151. }
  152. return 0;
  153. }