1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
14 Make various struct sizes available to dynamic languages
15 like Python - Larry Bugbee, February 2013
17 LB - Dec 2013 - revised to include compiler define options
23 const unsigned int size;
26 #define _SZ_STRINGIFY_S(s) { #s, sizeof(struct s) }
27 #define _SZ_STRINGIFY_T(s) { #s, sizeof(s) }
29 static const crypt_size _crypt_sizes[] = {
30 /* hash state sizes */
31 _SZ_STRINGIFY_S(ltc_hash_descriptor),
32 _SZ_STRINGIFY_T(hash_state),
34 _SZ_STRINGIFY_S(chc_state),
37 _SZ_STRINGIFY_S(whirlpool_state),
40 _SZ_STRINGIFY_S(sha3_state),
43 _SZ_STRINGIFY_S(sha512_state),
46 _SZ_STRINGIFY_S(sha256_state),
49 _SZ_STRINGIFY_S(sha1_state),
52 _SZ_STRINGIFY_S(md5_state),
55 _SZ_STRINGIFY_S(md4_state),
58 _SZ_STRINGIFY_S(md2_state),
61 _SZ_STRINGIFY_S(tiger_state),
64 _SZ_STRINGIFY_S(rmd128_state),
67 _SZ_STRINGIFY_S(rmd160_state),
70 _SZ_STRINGIFY_S(rmd256_state),
73 _SZ_STRINGIFY_S(rmd320_state),
76 _SZ_STRINGIFY_S(blake2s_state),
79 _SZ_STRINGIFY_S(blake2b_state),
82 /* block cipher key sizes */
83 _SZ_STRINGIFY_S(ltc_cipher_descriptor),
84 _SZ_STRINGIFY_T(symmetric_key),
86 _SZ_STRINGIFY_S(anubis_key),
89 _SZ_STRINGIFY_S(camellia_key),
92 _SZ_STRINGIFY_S(blowfish_key),
95 _SZ_STRINGIFY_S(cast5_key),
98 _SZ_STRINGIFY_S(des_key),
99 _SZ_STRINGIFY_S(des3_key),
102 _SZ_STRINGIFY_S(kasumi_key),
105 _SZ_STRINGIFY_S(khazad_key),
108 _SZ_STRINGIFY_S(kseed_key),
111 _SZ_STRINGIFY_S(multi2_key),
114 _SZ_STRINGIFY_S(noekeon_key),
117 _SZ_STRINGIFY_S(rc2_key),
120 _SZ_STRINGIFY_S(rc5_key),
123 _SZ_STRINGIFY_S(rc6_key),
126 _SZ_STRINGIFY_S(skipjack_key),
129 _SZ_STRINGIFY_S(xtea_key),
132 _SZ_STRINGIFY_S(rijndael_key),
135 _SZ_STRINGIFY_S(safer_key),
138 _SZ_STRINGIFY_S(saferp_key),
141 _SZ_STRINGIFY_S(twofish_key),
146 _SZ_STRINGIFY_T(symmetric_ECB),
149 _SZ_STRINGIFY_T(symmetric_CFB),
152 _SZ_STRINGIFY_T(symmetric_OFB),
155 _SZ_STRINGIFY_T(symmetric_CBC),
158 _SZ_STRINGIFY_T(symmetric_CTR),
161 _SZ_STRINGIFY_T(symmetric_LRW),
164 _SZ_STRINGIFY_T(symmetric_F8),
167 _SZ_STRINGIFY_T(symmetric_xts),
170 /* stream cipher sizes */
172 _SZ_STRINGIFY_T(chacha_state),
174 #ifdef LTC_RC4_STREAM
175 _SZ_STRINGIFY_T(rc4_state),
177 #ifdef LTC_SOBER128_STREAM
178 _SZ_STRINGIFY_T(sober128_state),
181 /* MAC sizes -- no states for ccm, lrw */
183 _SZ_STRINGIFY_T(hmac_state),
186 _SZ_STRINGIFY_T(omac_state),
189 _SZ_STRINGIFY_T(pmac_state),
192 _SZ_STRINGIFY_T(poly1305_state),
195 _SZ_STRINGIFY_T(eax_state),
198 _SZ_STRINGIFY_T(ocb_state),
201 _SZ_STRINGIFY_T(ocb3_state),
204 _SZ_STRINGIFY_T(ccm_state),
207 _SZ_STRINGIFY_T(gcm_state),
210 _SZ_STRINGIFY_T(pelican_state),
213 _SZ_STRINGIFY_T(xcbc_state),
216 _SZ_STRINGIFY_T(f9_state),
218 #ifdef LTC_CHACHA20POLY1305_MODE
219 _SZ_STRINGIFY_T(chacha20poly1305_state),
222 /* asymmetric keys */
224 _SZ_STRINGIFY_T(rsa_key),
227 _SZ_STRINGIFY_T(dsa_key),
230 _SZ_STRINGIFY_T(dh_key),
233 _SZ_STRINGIFY_T(ltc_ecc_set_type),
234 _SZ_STRINGIFY_T(ecc_point),
235 _SZ_STRINGIFY_T(ecc_key),
238 _SZ_STRINGIFY_T(katja_key),
243 _SZ_STRINGIFY_T(ltc_asn1_list), /* a list entry */
244 _SZ_STRINGIFY_T(ltc_utctime),
245 _SZ_STRINGIFY_T(ltc_generalizedtime),
248 /* prng state sizes */
249 _SZ_STRINGIFY_S(ltc_prng_descriptor),
250 _SZ_STRINGIFY_T(prng_state),
252 _SZ_STRINGIFY_S(fortuna_prng),
254 #ifdef LTC_CHACHA20_PRNG
255 _SZ_STRINGIFY_S(chacha20_prng),
258 _SZ_STRINGIFY_S(rc4_prng),
261 _SZ_STRINGIFY_S(sober128_prng),
264 _SZ_STRINGIFY_S(yarrow_prng),
266 /* sprng has no state as it uses other potentially available sources */
267 /* like /dev/random. See Developers Guide for more info. */
270 _SZ_STRINGIFY_T(adler32_state),
273 _SZ_STRINGIFY_T(crc32_state),
276 _SZ_STRINGIFY_T(ltc_mp_digit),
277 _SZ_STRINGIFY_T(ltc_math_descriptor)
282 * sizeout will be the size (bytes) of the named struct or union
283 * return -1 if named item not found
285 int crypt_get_size(const char* namein, unsigned int *sizeout) {
287 int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
288 for (i=0; i<count; i++) {
289 if (XSTRCMP(_crypt_sizes[i].name, namein) == 0) {
290 *sizeout = _crypt_sizes[i].size;
297 /* crypt_list_all_sizes()
298 * if names_list is NULL, names_list_size will be the minimum
299 * size needed to receive the complete names_list
300 * if names_list is NOT NULL, names_list must be the addr with
301 * sufficient memory allocated into which the names_list
302 * is to be written. Also, the value in names_list_size
303 * sets the upper bound of the number of characters to be
305 * a -1 return value signifies insufficient space made available
307 int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
309 unsigned int total_len = 0;
312 int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
314 /* calculate amount of memory required for the list */
315 for (i=0; i<count; i++) {
316 number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
319 total_len += number_len;
320 /* this last +1 is for newlines (and ending NULL) */
323 if (names_list == NULL) {
324 *names_list_size = total_len;
326 if (total_len > *names_list_size) {
329 /* build the names list */
331 for (i=0; i<count; i++) {
332 number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
333 if (number_len < 0) return -1;
334 if ((unsigned int)number_len > total_len) return -1;
335 total_len -= number_len;
338 /* to remove the trailing new-line */
346 /* ref: $Format:%D$ */
347 /* git commit: $Format:%H$ */
348 /* commit time: $Format:%ai$ */