summaryrefslogtreecommitdiffhomepage
path: root/libtomcrypt/src/misc/crypt/crypt_sizes.c
blob: dd857ea76b689faae3229e453c715e712bd59452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* LibTomCrypt, modular cryptographic library -- Tom St Denis
 *
 * LibTomCrypt is a library that provides various cryptographic
 * algorithms in a highly modular and flexible manner.
 *
 * The library is free for all purposes without any express
 * guarantee it works.
 */
#include "tomcrypt.h"

/**
  @file crypt_sizes.c

  Make various struct sizes available to dynamic languages
  like Python - Larry Bugbee, February 2013

  LB - Dec 2013 - revised to include compiler define options
*/


typedef struct {
    const char *name;
    const unsigned int size;
} crypt_size;

#define _SZ_STRINGIFY_S(s) { #s, sizeof(struct s) }
#define _SZ_STRINGIFY_T(s) { #s, sizeof(s) }

static const crypt_size _crypt_sizes[] = {
    /* hash state sizes */
    _SZ_STRINGIFY_S(ltc_hash_descriptor),
    _SZ_STRINGIFY_T(hash_state),
#ifdef LTC_CHC_HASH
    _SZ_STRINGIFY_S(chc_state),
#endif
#ifdef LTC_WHIRLPOOL
    _SZ_STRINGIFY_S(whirlpool_state),
#endif
#ifdef LTC_SHA3
    _SZ_STRINGIFY_S(sha3_state),
#endif
#ifdef LTC_SHA512
    _SZ_STRINGIFY_S(sha512_state),
#endif
#ifdef LTC_SHA256
    _SZ_STRINGIFY_S(sha256_state),
#endif
#ifdef LTC_SHA1
    _SZ_STRINGIFY_S(sha1_state),
#endif
#ifdef LTC_MD5
    _SZ_STRINGIFY_S(md5_state),
#endif
#ifdef LTC_MD4
    _SZ_STRINGIFY_S(md4_state),
#endif
#ifdef LTC_MD2
    _SZ_STRINGIFY_S(md2_state),
#endif
#ifdef LTC_TIGER
    _SZ_STRINGIFY_S(tiger_state),
#endif
#ifdef LTC_RIPEMD128
    _SZ_STRINGIFY_S(rmd128_state),
#endif
#ifdef LTC_RIPEMD160
    _SZ_STRINGIFY_S(rmd160_state),
#endif
#ifdef LTC_RIPEMD256
    _SZ_STRINGIFY_S(rmd256_state),
#endif
#ifdef LTC_RIPEMD320
    _SZ_STRINGIFY_S(rmd320_state),
#endif
#ifdef LTC_BLAKE2S
    _SZ_STRINGIFY_S(blake2s_state),
#endif
#ifdef LTC_BLAKE2B
    _SZ_STRINGIFY_S(blake2b_state),
#endif

    /* block cipher key sizes */
    _SZ_STRINGIFY_S(ltc_cipher_descriptor),
    _SZ_STRINGIFY_T(symmetric_key),
#ifdef LTC_ANUBIS
    _SZ_STRINGIFY_S(anubis_key),
#endif
#ifdef LTC_CAMELLIA
    _SZ_STRINGIFY_S(camellia_key),
#endif
#ifdef LTC_BLOWFISH
    _SZ_STRINGIFY_S(blowfish_key),
#endif
#ifdef LTC_CAST5
    _SZ_STRINGIFY_S(cast5_key),
#endif
#ifdef LTC_DES
    _SZ_STRINGIFY_S(des_key),
    _SZ_STRINGIFY_S(des3_key),
#endif
#ifdef LTC_KASUMI
    _SZ_STRINGIFY_S(kasumi_key),
#endif
#ifdef LTC_KHAZAD
    _SZ_STRINGIFY_S(khazad_key),
#endif
#ifdef LTC_KSEED
    _SZ_STRINGIFY_S(kseed_key),
#endif
#ifdef LTC_MULTI2
    _SZ_STRINGIFY_S(multi2_key),
#endif
#ifdef LTC_NOEKEON
    _SZ_STRINGIFY_S(noekeon_key),
#endif
#ifdef LTC_RC2
    _SZ_STRINGIFY_S(rc2_key),
#endif
#ifdef LTC_RC5
    _SZ_STRINGIFY_S(rc5_key),
#endif
#ifdef LTC_RC6
    _SZ_STRINGIFY_S(rc6_key),
#endif
#ifdef LTC_SKIPJACK
    _SZ_STRINGIFY_S(skipjack_key),
#endif
#ifdef LTC_XTEA
    _SZ_STRINGIFY_S(xtea_key),
#endif
#ifdef LTC_RIJNDAEL
    _SZ_STRINGIFY_S(rijndael_key),
#endif
#ifdef LTC_SAFER
    _SZ_STRINGIFY_S(safer_key),
#endif
#ifdef LTC_SAFERP
    _SZ_STRINGIFY_S(saferp_key),
#endif
#ifdef LTC_TWOFISH
    _SZ_STRINGIFY_S(twofish_key),
#endif

    /* mode sizes */
#ifdef LTC_ECB_MODE
    _SZ_STRINGIFY_T(symmetric_ECB),
#endif
#ifdef LTC_CFB_MODE
    _SZ_STRINGIFY_T(symmetric_CFB),
#endif
#ifdef LTC_OFB_MODE
    _SZ_STRINGIFY_T(symmetric_OFB),
#endif
#ifdef LTC_CBC_MODE
    _SZ_STRINGIFY_T(symmetric_CBC),
#endif
#ifdef LTC_CTR_MODE
    _SZ_STRINGIFY_T(symmetric_CTR),
#endif
#ifdef LTC_LRW_MODE
    _SZ_STRINGIFY_T(symmetric_LRW),
#endif
#ifdef LTC_F8_MODE
    _SZ_STRINGIFY_T(symmetric_F8),
#endif
#ifdef LTC_XTS_MODE
    _SZ_STRINGIFY_T(symmetric_xts),
#endif

    /* stream cipher sizes */
#ifdef LTC_CHACHA
    _SZ_STRINGIFY_T(chacha_state),
#endif
#ifdef LTC_RC4_STREAM
    _SZ_STRINGIFY_T(rc4_state),
#endif
#ifdef LTC_SOBER128_STREAM
    _SZ_STRINGIFY_T(sober128_state),
#endif

    /* MAC sizes            -- no states for ccm, lrw */
#ifdef LTC_HMAC
    _SZ_STRINGIFY_T(hmac_state),
#endif
#ifdef LTC_OMAC
    _SZ_STRINGIFY_T(omac_state),
#endif
#ifdef LTC_PMAC
    _SZ_STRINGIFY_T(pmac_state),
#endif
#ifdef LTC_POLY1305
    _SZ_STRINGIFY_T(poly1305_state),
#endif
#ifdef LTC_EAX_MODE
    _SZ_STRINGIFY_T(eax_state),
#endif
#ifdef LTC_OCB_MODE
    _SZ_STRINGIFY_T(ocb_state),
#endif
#ifdef LTC_OCB3_MODE
    _SZ_STRINGIFY_T(ocb3_state),
#endif
#ifdef LTC_CCM_MODE
    _SZ_STRINGIFY_T(ccm_state),
#endif
#ifdef LTC_GCM_MODE
    _SZ_STRINGIFY_T(gcm_state),
#endif
#ifdef LTC_PELICAN
    _SZ_STRINGIFY_T(pelican_state),
#endif
#ifdef LTC_XCBC
    _SZ_STRINGIFY_T(xcbc_state),
#endif
#ifdef LTC_F9_MODE
    _SZ_STRINGIFY_T(f9_state),
#endif
#ifdef LTC_CHACHA20POLY1305_MODE
    _SZ_STRINGIFY_T(chacha20poly1305_state),
#endif

    /* asymmetric keys */
#ifdef LTC_MRSA
    _SZ_STRINGIFY_T(rsa_key),
#endif
#ifdef LTC_MDSA
    _SZ_STRINGIFY_T(dsa_key),
#endif
#ifdef LTC_MDH
    _SZ_STRINGIFY_T(dh_key),
#endif
#ifdef LTC_MECC
    _SZ_STRINGIFY_T(ltc_ecc_set_type),
    _SZ_STRINGIFY_T(ecc_point),
    _SZ_STRINGIFY_T(ecc_key),
#endif
#ifdef LTC_MKAT
    _SZ_STRINGIFY_T(katja_key),
#endif

    /* DER handling */
#ifdef LTC_DER
    _SZ_STRINGIFY_T(ltc_asn1_list),  /* a list entry */
    _SZ_STRINGIFY_T(ltc_utctime),
    _SZ_STRINGIFY_T(ltc_generalizedtime),
#endif

    /* prng state sizes */
    _SZ_STRINGIFY_S(ltc_prng_descriptor),
    _SZ_STRINGIFY_T(prng_state),
#ifdef LTC_FORTUNA
    _SZ_STRINGIFY_S(fortuna_prng),
#endif
#ifdef LTC_CHACHA20_PRNG
    _SZ_STRINGIFY_S(chacha20_prng),
#endif
#ifdef LTC_RC4
    _SZ_STRINGIFY_S(rc4_prng),
#endif
#ifdef LTC_SOBER128
    _SZ_STRINGIFY_S(sober128_prng),
#endif
#ifdef LTC_YARROW
    _SZ_STRINGIFY_S(yarrow_prng),
#endif
    /* sprng has no state as it uses other potentially available sources */
    /* like /dev/random.  See Developers Guide for more info. */

#ifdef LTC_ADLER32
    _SZ_STRINGIFY_T(adler32_state),
#endif
#ifdef LTC_CRC32
    _SZ_STRINGIFY_T(crc32_state),
#endif

    _SZ_STRINGIFY_T(ltc_mp_digit),
    _SZ_STRINGIFY_T(ltc_math_descriptor)

};

/* crypt_get_size()
 * sizeout will be the size (bytes) of the named struct or union
 * return -1 if named item not found
 */
int crypt_get_size(const char* namein, unsigned int *sizeout) {
    int i;
    int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);
    for (i=0; i<count; i++) {
        if (XSTRCMP(_crypt_sizes[i].name, namein) == 0) {
            *sizeout = _crypt_sizes[i].size;
            return 0;
        }
    }
    return -1;
}

/* crypt_list_all_sizes()
 * if names_list is NULL, names_list_size will be the minimum
 *     size needed to receive the complete names_list
 * if names_list is NOT NULL, names_list must be the addr with
 *     sufficient memory allocated into which the names_list
 *     is to be written.  Also, the value in names_list_size
 *     sets the upper bound of the number of characters to be
 *     written.
 * a -1 return value signifies insufficient space made available
 */
int crypt_list_all_sizes(char *names_list, unsigned int *names_list_size) {
    int i;
    unsigned int total_len = 0;
    char *ptr;
    int number_len;
    int count = sizeof(_crypt_sizes) / sizeof(_crypt_sizes[0]);

    /* calculate amount of memory required for the list */
    for (i=0; i<count; i++) {
        number_len = snprintf(NULL, 0, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
        if (number_len < 0)
          return -1;
        total_len += number_len;
        /* this last +1 is for newlines (and ending NULL) */
    }

    if (names_list == NULL) {
        *names_list_size = total_len;
    } else {
        if (total_len > *names_list_size) {
            return -1;
        }
        /* build the names list */
        ptr = names_list;
        for (i=0; i<count; i++) {
            number_len = snprintf(ptr, total_len, "%s,%u\n", _crypt_sizes[i].name, _crypt_sizes[i].size);
            if (number_len < 0) return -1;
            if ((unsigned int)number_len > total_len) return -1;
            total_len -= number_len;
            ptr += number_len;
        }
        /* to remove the trailing new-line */
        ptr -= 1;
        *ptr = 0;
    }
    return 0;
}


/* ref:         $Format:%D$ */
/* git commit:  $Format:%H$ */
/* commit time: $Format:%ai$ */