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