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
12 @file crypt_constants.c
14 Make various constants available to dynamic languages
15 like Python - Larry Bugbee, February 2013
17 LB - Dec 2013 - revised to include compiler define options
18 LB - Mar 2014 - added endianness and word size
26 #define _C_STRINGIFY(s) { #s, s }
28 static const crypt_constant _crypt_constants[] = {
30 _C_STRINGIFY(CRYPT_OK),
31 _C_STRINGIFY(CRYPT_ERROR),
32 _C_STRINGIFY(CRYPT_NOP),
33 _C_STRINGIFY(CRYPT_INVALID_KEYSIZE),
34 _C_STRINGIFY(CRYPT_INVALID_ROUNDS),
35 _C_STRINGIFY(CRYPT_FAIL_TESTVECTOR),
36 _C_STRINGIFY(CRYPT_BUFFER_OVERFLOW),
37 _C_STRINGIFY(CRYPT_INVALID_PACKET),
38 _C_STRINGIFY(CRYPT_INVALID_PRNGSIZE),
39 _C_STRINGIFY(CRYPT_ERROR_READPRNG),
40 _C_STRINGIFY(CRYPT_INVALID_CIPHER),
41 _C_STRINGIFY(CRYPT_INVALID_HASH),
42 _C_STRINGIFY(CRYPT_INVALID_PRNG),
43 _C_STRINGIFY(CRYPT_MEM),
44 _C_STRINGIFY(CRYPT_PK_TYPE_MISMATCH),
45 _C_STRINGIFY(CRYPT_PK_NOT_PRIVATE),
46 _C_STRINGIFY(CRYPT_INVALID_ARG),
47 _C_STRINGIFY(CRYPT_FILE_NOTFOUND),
48 _C_STRINGIFY(CRYPT_PK_INVALID_TYPE),
49 _C_STRINGIFY(CRYPT_OVERFLOW),
50 _C_STRINGIFY(CRYPT_UNUSED1),
51 _C_STRINGIFY(CRYPT_INPUT_TOO_LONG),
52 _C_STRINGIFY(CRYPT_PK_INVALID_SIZE),
53 _C_STRINGIFY(CRYPT_INVALID_PRIME_SIZE),
54 _C_STRINGIFY(CRYPT_PK_INVALID_PADDING),
55 _C_STRINGIFY(CRYPT_HASH_OVERFLOW),
57 _C_STRINGIFY(PK_PUBLIC),
58 _C_STRINGIFY(PK_PRIVATE),
60 _C_STRINGIFY(LTC_ENCRYPT),
61 _C_STRINGIFY(LTC_DECRYPT),
66 _C_STRINGIFY(LTC_PKCS_1_EMSA),
67 _C_STRINGIFY(LTC_PKCS_1_EME),
70 _C_STRINGIFY(LTC_PKCS_1_V1_5),
71 _C_STRINGIFY(LTC_PKCS_1_OAEP),
72 _C_STRINGIFY(LTC_PKCS_1_PSS),
73 _C_STRINGIFY(LTC_PKCS_1_V1_5_NA1),
86 _C_STRINGIFY(MIN_KAT_SIZE),
87 _C_STRINGIFY(MAX_KAT_SIZE),
94 _C_STRINGIFY(ECC_BUF_SIZE),
95 _C_STRINGIFY(ECC_MAXSIZE),
102 _C_STRINGIFY(LTC_MDSA_DELTA),
103 _C_STRINGIFY(LTC_MDSA_MAX_GROUP),
108 #ifdef LTC_MILLER_RABIN_REPS
109 _C_STRINGIFY(LTC_MILLER_RABIN_REPS),
115 _C_STRINGIFY(LTC_ASN1_EOL),
116 _C_STRINGIFY(LTC_ASN1_BOOLEAN),
117 _C_STRINGIFY(LTC_ASN1_INTEGER),
118 _C_STRINGIFY(LTC_ASN1_SHORT_INTEGER),
119 _C_STRINGIFY(LTC_ASN1_BIT_STRING),
120 _C_STRINGIFY(LTC_ASN1_OCTET_STRING),
121 _C_STRINGIFY(LTC_ASN1_NULL),
122 _C_STRINGIFY(LTC_ASN1_OBJECT_IDENTIFIER),
123 _C_STRINGIFY(LTC_ASN1_IA5_STRING),
124 _C_STRINGIFY(LTC_ASN1_PRINTABLE_STRING),
125 _C_STRINGIFY(LTC_ASN1_UTF8_STRING),
126 _C_STRINGIFY(LTC_ASN1_UTCTIME),
127 _C_STRINGIFY(LTC_ASN1_CHOICE),
128 _C_STRINGIFY(LTC_ASN1_SEQUENCE),
129 _C_STRINGIFY(LTC_ASN1_SET),
130 _C_STRINGIFY(LTC_ASN1_SETOF),
131 _C_STRINGIFY(LTC_ASN1_RAW_BIT_STRING),
132 _C_STRINGIFY(LTC_ASN1_TELETEX_STRING),
133 _C_STRINGIFY(LTC_ASN1_CONSTRUCTED),
134 _C_STRINGIFY(LTC_ASN1_CONTEXT_SPECIFIC),
135 _C_STRINGIFY(LTC_ASN1_GENERALIZEDTIME),
136 _C_STRINGIFY(LTC_DER_MAX_RECURSION),
143 _C_STRINGIFY(CTR_COUNTER_LITTLE_ENDIAN),
144 _C_STRINGIFY(CTR_COUNTER_BIG_ENDIAN),
145 _C_STRINGIFY(LTC_CTR_RFC3686),
150 _C_STRINGIFY(LTC_GCM_MODE_IV),
151 _C_STRINGIFY(LTC_GCM_MODE_AAD),
152 _C_STRINGIFY(LTC_GCM_MODE_TEXT),
155 _C_STRINGIFY(LTC_MP_LT),
156 _C_STRINGIFY(LTC_MP_EQ),
157 _C_STRINGIFY(LTC_MP_GT),
159 _C_STRINGIFY(LTC_MP_NO),
160 _C_STRINGIFY(LTC_MP_YES),
162 _C_STRINGIFY(MAXBLOCKSIZE),
163 _C_STRINGIFY(TAB_SIZE),
164 _C_STRINGIFY(ARGTYPE),
195 {"ENDIAN_LITTLE", 1},
197 {"ENDIAN_LITTLE", 0},
206 #ifdef ENDIAN_32BITWORD
207 {"ENDIAN_32BITWORD", 1},
209 {"ENDIAN_32BITWORD", 0},
212 #ifdef ENDIAN_64BITWORD
213 {"ENDIAN_64BITWORD", 1},
215 {"ENDIAN_64BITWORD", 0},
218 #ifdef ENDIAN_NEUTRAL
219 {"ENDIAN_NEUTRAL", 1},
221 {"ENDIAN_NEUTRAL", 0},
226 /* crypt_get_constant()
227 * valueout will be the value of the named constant
228 * return -1 if named item not found
230 int crypt_get_constant(const char* namein, int *valueout) {
232 int _crypt_constants_len = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
233 for (i=0; i<_crypt_constants_len; i++) {
234 if (XSTRCMP(_crypt_constants[i].name, namein) == 0) {
235 *valueout = _crypt_constants[i].value;
242 /* crypt_list_all_constants()
243 * if names_list is NULL, names_list_size will be the minimum
244 * number of bytes needed to receive the complete names_list
245 * if names_list is NOT NULL, names_list must be the addr of
246 * sufficient memory allocated into which the names_list
247 * is to be written. Also, the value in names_list_size
248 * sets the upper bound of the number of characters to be
250 * a -1 return value signifies insufficient space made available
252 int crypt_list_all_constants(char *names_list, unsigned int *names_list_size) {
254 unsigned int total_len = 0;
257 int count = sizeof(_crypt_constants) / sizeof(_crypt_constants[0]);
259 /* calculate amount of memory required for the list */
260 for (i=0; i<count; i++) {
261 number_len = snprintf(NULL, 0, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
264 total_len += number_len;
267 if (names_list == NULL) {
268 *names_list_size = total_len;
270 if (total_len > *names_list_size) {
273 /* build the names list */
275 for (i=0; i<count; i++) {
276 number_len = snprintf(ptr, total_len, "%s,%d\n", _crypt_constants[i].name, _crypt_constants[i].value);
277 if (number_len < 0) return -1;
278 if ((unsigned int)number_len > total_len) return -1;
279 total_len -= number_len;
282 /* to remove the trailing new-line */
290 /* ref: $Format:%D$ */
291 /* git commit: $Format:%H$ */
292 /* commit time: $Format:%ai$ */