]> pd.if.org Git - zpackage/blob - libtomcrypt/src/misc/crypt/crypt_sizes.c
commit files needed for zpm-fetchurl
[zpackage] / libtomcrypt / src / misc / crypt / crypt_sizes.c
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
2  *
3  * LibTomCrypt is a library that provides various cryptographic
4  * algorithms in a highly modular and flexible manner.
5  *
6  * The library is free for all purposes without any express
7  * guarantee it works.
8  */
9 #include "tomcrypt.h"
10
11 /**
12   @file crypt_sizes.c
13
14   Make various struct sizes available to dynamic languages
15   like Python - Larry Bugbee, February 2013
16
17   LB - Dec 2013 - revised to include compiler define options
18 */
19
20
21 typedef struct {
22     const char *name;
23     const unsigned int size;
24 } crypt_size;
25
26 #define _SZ_STRINGIFY_S(s) { #s, sizeof(struct s) }
27 #define _SZ_STRINGIFY_T(s) { #s, sizeof(s) }
28
29 static const crypt_size _crypt_sizes[] = {
30     /* hash state sizes */
31     _SZ_STRINGIFY_S(ltc_hash_descriptor),
32     _SZ_STRINGIFY_T(hash_state),
33 #ifdef LTC_CHC_HASH
34     _SZ_STRINGIFY_S(chc_state),
35 #endif
36 #ifdef LTC_WHIRLPOOL
37     _SZ_STRINGIFY_S(whirlpool_state),
38 #endif
39 #ifdef LTC_SHA3
40     _SZ_STRINGIFY_S(sha3_state),
41 #endif
42 #ifdef LTC_SHA512
43     _SZ_STRINGIFY_S(sha512_state),
44 #endif
45 #ifdef LTC_SHA256
46     _SZ_STRINGIFY_S(sha256_state),
47 #endif
48 #ifdef LTC_SHA1
49     _SZ_STRINGIFY_S(sha1_state),
50 #endif
51 #ifdef LTC_MD5
52     _SZ_STRINGIFY_S(md5_state),
53 #endif
54 #ifdef LTC_MD4
55     _SZ_STRINGIFY_S(md4_state),
56 #endif
57 #ifdef LTC_MD2
58     _SZ_STRINGIFY_S(md2_state),
59 #endif
60 #ifdef LTC_TIGER
61     _SZ_STRINGIFY_S(tiger_state),
62 #endif
63 #ifdef LTC_RIPEMD128
64     _SZ_STRINGIFY_S(rmd128_state),
65 #endif
66 #ifdef LTC_RIPEMD160
67     _SZ_STRINGIFY_S(rmd160_state),
68 #endif
69 #ifdef LTC_RIPEMD256
70     _SZ_STRINGIFY_S(rmd256_state),
71 #endif
72 #ifdef LTC_RIPEMD320
73     _SZ_STRINGIFY_S(rmd320_state),
74 #endif
75 #ifdef LTC_BLAKE2S
76     _SZ_STRINGIFY_S(blake2s_state),
77 #endif
78 #ifdef LTC_BLAKE2B
79     _SZ_STRINGIFY_S(blake2b_state),
80 #endif
81
82     /* block cipher key sizes */
83     _SZ_STRINGIFY_S(ltc_cipher_descriptor),
84     _SZ_STRINGIFY_T(symmetric_key),
85 #ifdef LTC_ANUBIS
86     _SZ_STRINGIFY_S(anubis_key),
87 #endif
88 #ifdef LTC_CAMELLIA
89     _SZ_STRINGIFY_S(camellia_key),
90 #endif
91 #ifdef LTC_BLOWFISH
92     _SZ_STRINGIFY_S(blowfish_key),
93 #endif
94 #ifdef LTC_CAST5
95     _SZ_STRINGIFY_S(cast5_key),
96 #endif
97 #ifdef LTC_DES
98     _SZ_STRINGIFY_S(des_key),
99     _SZ_STRINGIFY_S(des3_key),
100 #endif
101 #ifdef LTC_KASUMI
102     _SZ_STRINGIFY_S(kasumi_key),
103 #endif
104 #ifdef LTC_KHAZAD
105     _SZ_STRINGIFY_S(khazad_key),
106 #endif
107 #ifdef LTC_KSEED
108     _SZ_STRINGIFY_S(kseed_key),
109 #endif
110 #ifdef LTC_MULTI2
111     _SZ_STRINGIFY_S(multi2_key),
112 #endif
113 #ifdef LTC_NOEKEON
114     _SZ_STRINGIFY_S(noekeon_key),
115 #endif
116 #ifdef LTC_RC2
117     _SZ_STRINGIFY_S(rc2_key),
118 #endif
119 #ifdef LTC_RC5
120     _SZ_STRINGIFY_S(rc5_key),
121 #endif
122 #ifdef LTC_RC6
123     _SZ_STRINGIFY_S(rc6_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_RC4_STREAM
175     _SZ_STRINGIFY_T(rc4_state),
176 #endif
177 #ifdef LTC_SOBER128_STREAM
178     _SZ_STRINGIFY_T(sober128_state),
179 #endif
180
181     /* MAC sizes            -- no states for ccm, lrw */
182 #ifdef LTC_HMAC
183     _SZ_STRINGIFY_T(hmac_state),
184 #endif
185 #ifdef LTC_OMAC
186     _SZ_STRINGIFY_T(omac_state),
187 #endif
188 #ifdef LTC_PMAC
189     _SZ_STRINGIFY_T(pmac_state),
190 #endif
191 #ifdef LTC_POLY1305
192     _SZ_STRINGIFY_T(poly1305_state),
193 #endif
194 #ifdef LTC_EAX_MODE
195     _SZ_STRINGIFY_T(eax_state),
196 #endif
197 #ifdef LTC_OCB_MODE
198     _SZ_STRINGIFY_T(ocb_state),
199 #endif
200 #ifdef LTC_OCB3_MODE
201     _SZ_STRINGIFY_T(ocb3_state),
202 #endif
203 #ifdef LTC_CCM_MODE
204     _SZ_STRINGIFY_T(ccm_state),
205 #endif
206 #ifdef LTC_GCM_MODE
207     _SZ_STRINGIFY_T(gcm_state),
208 #endif
209 #ifdef LTC_PELICAN
210     _SZ_STRINGIFY_T(pelican_state),
211 #endif
212 #ifdef LTC_XCBC
213     _SZ_STRINGIFY_T(xcbc_state),
214 #endif
215 #ifdef LTC_F9_MODE
216     _SZ_STRINGIFY_T(f9_state),
217 #endif
218 #ifdef LTC_CHACHA20POLY1305_MODE
219     _SZ_STRINGIFY_T(chacha20poly1305_state),
220 #endif
221
222     /* asymmetric keys */
223 #ifdef LTC_MRSA
224     _SZ_STRINGIFY_T(rsa_key),
225 #endif
226 #ifdef LTC_MDSA
227     _SZ_STRINGIFY_T(dsa_key),
228 #endif
229 #ifdef LTC_MDH
230     _SZ_STRINGIFY_T(dh_key),
231 #endif
232 #ifdef LTC_MECC
233     _SZ_STRINGIFY_T(ltc_ecc_set_type),
234     _SZ_STRINGIFY_T(ecc_point),
235     _SZ_STRINGIFY_T(ecc_key),
236 #endif
237 #ifdef LTC_MKAT
238     _SZ_STRINGIFY_T(katja_key),
239 #endif
240
241     /* DER handling */
242 #ifdef LTC_DER
243     _SZ_STRINGIFY_T(ltc_asn1_list),  /* a list entry */
244     _SZ_STRINGIFY_T(ltc_utctime),
245     _SZ_STRINGIFY_T(ltc_generalizedtime),
246 #endif
247
248     /* prng state sizes */
249     _SZ_STRINGIFY_S(ltc_prng_descriptor),
250     _SZ_STRINGIFY_T(prng_state),
251 #ifdef LTC_FORTUNA
252     _SZ_STRINGIFY_S(fortuna_prng),
253 #endif
254 #ifdef LTC_CHACHA20_PRNG
255     _SZ_STRINGIFY_S(chacha20_prng),
256 #endif
257 #ifdef LTC_RC4
258     _SZ_STRINGIFY_S(rc4_prng),
259 #endif
260 #ifdef LTC_SOBER128
261     _SZ_STRINGIFY_S(sober128_prng),
262 #endif
263 #ifdef LTC_YARROW
264     _SZ_STRINGIFY_S(yarrow_prng),
265 #endif
266     /* sprng has no state as it uses other potentially available sources */
267     /* like /dev/random.  See Developers Guide for more info. */
268
269 #ifdef LTC_ADLER32
270     _SZ_STRINGIFY_T(adler32_state),
271 #endif
272 #ifdef LTC_CRC32
273     _SZ_STRINGIFY_T(crc32_state),
274 #endif
275
276     _SZ_STRINGIFY_T(ltc_mp_digit),
277     _SZ_STRINGIFY_T(ltc_math_descriptor)
278
279 };
280
281 /* crypt_get_size()
282  * sizeout will be the size (bytes) of the named struct or union
283  * return -1 if named item not found
284  */
285 int crypt_get_size(const char* namein, unsigned int *sizeout) {
286     int i;
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;
291             return 0;
292         }
293     }
294     return -1;
295 }
296
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
304  *     written.
305  * a -1 return value signifies insufficient space made available
306  */
307 int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
308     int i;
309     unsigned int total_len = 0;
310     char *ptr;
311     int number_len;
312     int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
313
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);
317         if (number_len < 0)
318           return -1;
319         total_len += number_len;
320         /* this last +1 is for newlines (and ending NULL) */
321     }
322
323     if (names_list == NULL) {
324         *names_list_size = total_len;
325     } else {
326         if (total_len > *names_list_size) {
327             return -1;
328         }
329         /* build the names list */
330         ptr = 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;
336             ptr += number_len;
337         }
338         /* to remove the trailing new-line */
339         ptr -= 1;
340         *ptr = 0;
341     }
342     return 0;
343 }
344
345
346 /* ref:         $Format:%D$ */
347 /* git commit:  $Format:%H$ */
348 /* commit time: $Format:%ai$ */