0 | |
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
|
1 | |
*
|
2 | |
* LibTomCrypt is a library that provides various cryptographic
|
3 | |
* algorithms in a highly modular and flexible manner.
|
4 | |
*
|
5 | |
* The library is free for all purposes without any express
|
6 | |
* guarantee it works.
|
7 | |
*
|
8 | |
* Tom St Denis, tomstdenis@gmail.com, http://libtom.org
|
9 | |
*/
|
10 | |
#include "tomcrypt.h"
|
11 | |
|
12 | |
/**
|
13 | |
@file crypt_sizes.c
|
14 | |
|
15 | |
Make various struct sizes available to dynamic languages
|
16 | |
like Python - Larry Bugbee, February 2013
|
17 | |
|
18 | |
LB - Dec 2013 - revised to include compiler define options
|
19 | |
*/
|
20 | |
|
21 | |
|
22 | |
typedef struct {
|
23 | |
const char *name;
|
24 | |
const unsigned int size;
|
25 | |
} crypt_size;
|
26 | |
|
27 | |
#define _SZ_STRINGIFY_S(s) { #s, sizeof(struct s) }
|
28 | |
#define _SZ_STRINGIFY_T(s) { #s, sizeof(s) }
|
29 | |
|
30 | |
static const crypt_size _crypt_sizes[] = {
|
31 | |
// hash state sizes
|
32 | |
_SZ_STRINGIFY_S(ltc_hash_descriptor),
|
33 | |
_SZ_STRINGIFY_T(hash_state),
|
34 | |
#ifdef LTC_SHA256
|
35 | |
_SZ_STRINGIFY_S(sha256_state),
|
36 | |
#endif
|
37 | |
#ifdef LTC_SHA512
|
38 | |
_SZ_STRINGIFY_S(sha512_state),
|
39 | |
#endif
|
40 | |
#ifdef LTC_WHIRLPOOL
|
41 | |
_SZ_STRINGIFY_S(whirlpool_state),
|
42 | |
#endif
|
43 | |
#ifdef LTC_MD2
|
44 | |
_SZ_STRINGIFY_S(md2_state),
|
45 | |
#endif
|
46 | |
#ifdef LTC_MD4
|
47 | |
_SZ_STRINGIFY_S(md4_state),
|
48 | |
#endif
|
49 | |
#ifdef LTC_MD5
|
50 | |
_SZ_STRINGIFY_S(md5_state),
|
51 | |
#endif
|
52 | |
#ifdef LTC_RIPEMD128
|
53 | |
_SZ_STRINGIFY_S(rmd128_state),
|
54 | |
#endif
|
55 | |
#ifdef LTC_RIPEMD160
|
56 | |
_SZ_STRINGIFY_S(rmd160_state),
|
57 | |
#endif
|
58 | |
#ifdef LTC_RIPEMD256
|
59 | |
_SZ_STRINGIFY_S(rmd256_state),
|
60 | |
#endif
|
61 | |
#ifdef LTC_RIPEMD320
|
62 | |
_SZ_STRINGIFY_S(rmd320_state),
|
63 | |
#endif
|
64 | |
#ifdef LTC_SHA1
|
65 | |
_SZ_STRINGIFY_S(sha1_state),
|
66 | |
#endif
|
67 | |
#ifdef LTC_TIGER
|
68 | |
_SZ_STRINGIFY_S(tiger_state),
|
69 | |
#endif
|
70 | |
#ifdef LTC_CHC_HASH
|
71 | |
_SZ_STRINGIFY_S(chc_state),
|
72 | |
#endif
|
73 | |
|
74 | |
// block cipher key sizes
|
75 | |
_SZ_STRINGIFY_S(ltc_cipher_descriptor),
|
76 | |
_SZ_STRINGIFY_T(symmetric_key),
|
77 | |
#ifdef LTC_ANUBIS
|
78 | |
_SZ_STRINGIFY_S(anubis_key),
|
79 | |
#endif
|
80 | |
#ifdef LTC_CAMELLIA
|
81 | |
_SZ_STRINGIFY_S(camellia_key),
|
82 | |
#endif
|
83 | |
#ifdef LTC_BLOWFISH
|
84 | |
_SZ_STRINGIFY_S(blowfish_key),
|
85 | |
#endif
|
86 | |
#ifdef LTC_CAST5
|
87 | |
_SZ_STRINGIFY_S(cast5_key),
|
88 | |
#endif
|
89 | |
#ifdef LTC_DES
|
90 | |
_SZ_STRINGIFY_S(des_key),
|
91 | |
_SZ_STRINGIFY_S(des3_key),
|
92 | |
#endif
|
93 | |
#ifdef LTC_KASUMI
|
94 | |
_SZ_STRINGIFY_S(kasumi_key),
|
95 | |
#endif
|
96 | |
#ifdef LTC_KHAZAD
|
97 | |
_SZ_STRINGIFY_S(khazad_key),
|
98 | |
#endif
|
99 | |
#ifdef LTC_KSEED
|
100 | |
_SZ_STRINGIFY_S(kseed_key),
|
101 | |
#endif
|
102 | |
#ifdef LTC_MULTI2
|
103 | |
_SZ_STRINGIFY_S(multi2_key),
|
104 | |
#endif
|
105 | |
#ifdef LTC_NOEKEON
|
106 | |
_SZ_STRINGIFY_S(noekeon_key),
|
107 | |
#endif
|
108 | |
#ifdef LTC_RC2
|
109 | |
_SZ_STRINGIFY_S(rc2_key),
|
110 | |
#endif
|
111 | |
#ifdef LTC_RC5
|
112 | |
_SZ_STRINGIFY_S(rc5_key),
|
113 | |
#endif
|
114 | |
#ifdef LTC_RC6
|
115 | |
_SZ_STRINGIFY_S(rc6_key),
|
116 | |
#endif
|
117 | |
#ifdef LTC_SKIPJACK
|
118 | |
_SZ_STRINGIFY_S(skipjack_key),
|
119 | |
#endif
|
120 | |
#ifdef LTC_XTEA
|
121 | |
_SZ_STRINGIFY_S(xtea_key),
|
122 | |
#endif
|
123 | |
#ifdef LTC_RIJNDAEL
|
124 | |
_SZ_STRINGIFY_S(rijndael_key),
|
125 | |
#endif
|
126 | |
#ifdef LTC_SAFER
|
127 | |
_SZ_STRINGIFY_S(safer_key),
|
128 | |
#endif
|
129 | |
#ifdef LTC_SAFERP
|
130 | |
_SZ_STRINGIFY_S(saferp_key),
|
131 | |
#endif
|
132 | |
#ifdef LTC_TWOFISH
|
133 | |
_SZ_STRINGIFY_S(twofish_key),
|
134 | |
#endif
|
135 | |
|
136 | |
// mode sizes
|
137 | |
#ifdef LTC_CBC_MODE
|
138 | |
_SZ_STRINGIFY_T(symmetric_CBC),
|
139 | |
#endif
|
140 | |
#ifdef LTC_CFB_MODE
|
141 | |
_SZ_STRINGIFY_T(symmetric_CFB),
|
142 | |
#endif
|
143 | |
#ifdef LTC_CTR_MODE
|
144 | |
_SZ_STRINGIFY_T(symmetric_CTR),
|
145 | |
#endif
|
146 | |
#ifdef LTC_ECB_MODE
|
147 | |
_SZ_STRINGIFY_T(symmetric_ECB),
|
148 | |
#endif
|
149 | |
#ifdef LTC_F8_MODE
|
150 | |
_SZ_STRINGIFY_T(symmetric_F8),
|
151 | |
#endif
|
152 | |
#ifdef LTC_LRW_MODE
|
153 | |
_SZ_STRINGIFY_T(symmetric_LRW),
|
154 | |
#endif
|
155 | |
#ifdef LTC_OFB_MODE
|
156 | |
_SZ_STRINGIFY_T(symmetric_OFB),
|
157 | |
#endif
|
158 | |
|
159 | |
// MAC sizes -- no states for ccm, lrw
|
160 | |
#ifdef LTC_F9_MODE
|
161 | |
_SZ_STRINGIFY_T(f9_state),
|
162 | |
#endif
|
163 | |
#ifdef LTC_HMAC
|
164 | |
_SZ_STRINGIFY_T(hmac_state),
|
165 | |
#endif
|
166 | |
#ifdef LTC_OMAC
|
167 | |
_SZ_STRINGIFY_T(omac_state),
|
168 | |
#endif
|
169 | |
#ifdef LTC_PELICAN
|
170 | |
_SZ_STRINGIFY_T(pelican_state),
|
171 | |
#endif
|
172 | |
#ifdef LTC_PMAC
|
173 | |
_SZ_STRINGIFY_T(pmac_state),
|
174 | |
#endif
|
175 | |
#ifdef LTC_XCBC
|
176 | |
_SZ_STRINGIFY_T(xcbc_state),
|
177 | |
#endif
|
178 | |
#ifdef LTC_OCB_MODE
|
179 | |
_SZ_STRINGIFY_T(ocb_state),
|
180 | |
#endif
|
181 | |
#ifdef LTC_OCB3_MODE
|
182 | |
_SZ_STRINGIFY_T(ocb3_state),
|
183 | |
#endif
|
184 | |
#ifdef LTC_GCM_MODE
|
185 | |
_SZ_STRINGIFY_T(gcm_state),
|
186 | |
#endif
|
187 | |
#ifdef LTC_EAX_MODE
|
188 | |
_SZ_STRINGIFY_T(eax_state),
|
189 | |
#endif
|
190 | |
#ifdef LTC_CCM_MODE
|
191 | |
// not defined
|
192 | |
#endif
|
193 | |
#ifdef LRW_MODE
|
194 | |
// not defined
|
195 | |
#endif
|
196 | |
|
197 | |
// asymmetric keys
|
198 | |
#ifdef LTC_MRSA
|
199 | |
_SZ_STRINGIFY_T(rsa_key),
|
200 | |
#endif
|
201 | |
#ifdef LTC_MDSA
|
202 | |
_SZ_STRINGIFY_T(dsa_key),
|
203 | |
#endif
|
204 | |
#ifdef LTC_MDH
|
205 | |
_SZ_STRINGIFY_T(dh_key),
|
206 | |
#endif
|
207 | |
#ifdef LTC_MECC
|
208 | |
_SZ_STRINGIFY_T(ltc_ecc_set_type),
|
209 | |
_SZ_STRINGIFY_T(ecc_key),
|
210 | |
_SZ_STRINGIFY_T(ecc_point),
|
211 | |
#endif
|
212 | |
#ifdef LTC_MKAT
|
213 | |
_SZ_STRINGIFY_T(katja_key),
|
214 | |
#endif
|
215 | |
|
216 | |
// prng state sizes
|
217 | |
_SZ_STRINGIFY_S(ltc_prng_descriptor),
|
218 | |
_SZ_STRINGIFY_T(prng_state),
|
219 | |
#ifdef LTC_FORTUNA
|
220 | |
_SZ_STRINGIFY_S(fortuna_prng),
|
221 | |
#endif
|
222 | |
#ifdef LTC_RC4
|
223 | |
_SZ_STRINGIFY_S(rc4_prng),
|
224 | |
#endif
|
225 | |
#ifdef LTC_SOBER128
|
226 | |
_SZ_STRINGIFY_S(sober128_prng),
|
227 | |
#endif
|
228 | |
#ifdef LTC_YARROW
|
229 | |
_SZ_STRINGIFY_S(yarrow_prng),
|
230 | |
#endif
|
231 | |
// sprng has no state as it uses other potentially available sources
|
232 | |
// like /dev/random. See Developers Guide for more info.
|
233 | |
};
|
234 | |
|
235 | |
/* crypt_get_size()
|
236 | |
* sizeout will be the size (bytes) of the named struct or union
|
237 | |
* return -1 if named item not found
|
238 | |
*/
|
239 | |
int crypt_get_size(const char* namein, unsigned int *sizeout) {
|
240 | |
int i;
|
241 | |
int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
|
242 | |
for (i=0; i<count; i++) {
|
243 | |
if (strcmp(_crypt_sizes[i].name, namein) == 0) {
|
244 | |
*sizeout = _crypt_sizes[i].size;
|
245 | |
return 0;
|
246 | |
}
|
247 | |
}
|
248 | |
return -1;
|
249 | |
}
|
250 | |
|
251 | |
/* crypt_list_all_sizes()
|
252 | |
* if names_list is NULL, names_list_size will be the minimum
|
253 | |
* size needed to receive the complete names_list
|
254 | |
* if names_list is NOT NULL, names_list must be the addr with
|
255 | |
* sufficient memory allocated into which the names_list
|
256 | |
* is to be written. Also, the value in names_list_size
|
257 | |
* sets the upper bound of the number of characters to be
|
258 | |
* written.
|
259 | |
* a -1 return value signifies insufficient space made available
|
260 | |
*/
|
261 | |
int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
|
262 | |
int i;
|
263 | |
unsigned int total_len = 0;
|
264 | |
char number[32];
|
265 | |
int number_len;
|
266 | |
int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
|
267 | |
|
268 | |
/* calculate amount of memory required for the list */
|
269 | |
for (i=0; i<count; i++) {
|
270 | |
total_len += strlen(_crypt_sizes[i].name) + 1;
|
271 | |
/* the above +1 is for the commas */
|
272 | |
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
|
273 | |
if ((number_len < 0) ||
|
274 | |
((unsigned int)number_len >= sizeof(number)))
|
275 | |
return -1;
|
276 | |
total_len += strlen(number) + 1;
|
277 | |
/* this last +1 is for newlines (and ending NULL) */
|
278 | |
}
|
279 | |
|
280 | |
if (names_list == NULL) {
|
281 | |
*names_list_size = total_len;
|
282 | |
} else {
|
283 | |
if (total_len > *names_list_size) {
|
284 | |
return -1;
|
285 | |
}
|
286 | |
/* build the names list */
|
287 | |
char *ptr = names_list;
|
288 | |
for (i=0; i<count; i++) {
|
289 | |
strcpy(ptr, _crypt_sizes[i].name);
|
290 | |
ptr += strlen(_crypt_sizes[i].name);
|
291 | |
strcpy(ptr, ",");
|
292 | |
ptr += 1;
|
293 | |
|
294 | |
number_len = snprintf(number, sizeof(number), "%u", _crypt_sizes[i].size);
|
295 | |
strcpy(ptr, number);
|
296 | |
ptr += number_len;
|
297 | |
strcpy(ptr, "\n");
|
298 | |
ptr += 1;
|
299 | |
}
|
300 | |
/* to remove the trailing new-line */
|
301 | |
ptr -= 1;
|
302 | |
*ptr = 0;
|
303 | |
}
|
304 | |
return 0;
|
305 | |
}
|
306 | |
|
307 | |
|
308 | |
/* $Source$ */
|
309 | |
/* $Revision$ */
|
310 | |
/* $Date$ */
|