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